source: trunk/src/Outputs/VOTableCatalogueWriter.cc @ 1061

Last change on this file since 1061 was 1061, checked in by MatthewWhiting, 12 years ago

Ticket #162 - A large refactoring of the Column-related code. Columns have moved to Outputs, and in a new namespace Catalogues. The interface has changed, using strings to record the type rather
than the enum. Also included is a new class CatalogueSpecification?, that is designed to hold a collection of Columns. This is not yet implemented - everything still uses the full & log column
vectors, and the code still passes the verification script.

File size: 7.7 KB
Line 
1#include <duchamp/Outputs/VOTableCatalogueWriter.hh>
2#include <duchamp/Outputs/CatalogueWriter.hh>
3#include <duchamp/Outputs/columns.hh>
4#include <duchamp/Detection/detection.hh>
5#include <duchamp/Utils/utils.hh>
6#include <duchamp/Utils/VOField.hh>
7#include <duchamp/Utils/VOParam.hh>
8#include <ios>
9#include <iostream>
10#include <fstream>
11
12namespace duchamp {
13
14   VOTableCatalogueWriter::VOTableCatalogueWriter():
15    CatalogueWriter()
16  {
17    this->itsTableName="";
18    this->itsTableDescription="";
19  }
20
21  VOTableCatalogueWriter::VOTableCatalogueWriter(std::string name):
22    CatalogueWriter(name)
23  {
24    this->itsTableName="";
25    this->itsTableDescription="";
26  }
27
28  VOTableCatalogueWriter::VOTableCatalogueWriter(const VOTableCatalogueWriter& other)
29  {
30    this->operator=(other);
31  }
32
33  VOTableCatalogueWriter& VOTableCatalogueWriter::operator= (const VOTableCatalogueWriter& other)
34  {
35    if(this==&other) return *this;
36    ((CatalogueWriter &) *this) = other;
37    this->itsOpenFlag=false;
38    this->itsTableName=other.itsTableName;
39    this->itsTableDescription=other.itsTableDescription;
40    return *this;
41  }
42 
43  bool VOTableCatalogueWriter::openCatalogue(std::ios_base::openmode mode)
44  {
45    if(this->itsName == ""){
46      DUCHAMPERROR("VOTableCatalogueWriter","No catalogue name provided");
47      this->itsOpenFlag = false;
48    }
49    else {
50      this->itsFileStream.open(this->itsName.c_str(),mode);
51      this->itsOpenFlag = !this->itsFileStream.fail();
52    }
53    if(!this->itsOpenFlag)
54      DUCHAMPERROR("VOTableCatalogueWriter","Could not open file \""<<this->itsName<<"\"");
55    return this->itsOpenFlag;
56
57  }
58
59  void VOTableCatalogueWriter::writeHeader()
60  {
61    if(this->itsOpenFlag){
62
63      this->itsFileStream<<"<?xml version=\"1.0\"?>\n";
64      this->itsFileStream<<"<VOTABLE version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
65      this->itsFileStream<<" xsi:noNamespaceSchemaLocation=\"http://www.ivoa.net/xml/VOTable/VOTable/v1.1\">\n";
66     
67      std::string ID,equinox,system;
68      if(std::string(this->itsHead->WCS().lngtyp)=="RA") { //J2000 or B1950
69        if(this->itsHead->WCS().equinox==2000.){
70          ID=equinox= "J2000";
71          system="eq_FK5";
72        }
73        else{
74          ID=equinox="B1950";
75          system="eq_FK4";
76        }
77        this->itsFileStream <<"  <COOSYS ID=\""<<ID<<"\" equinox=\""<<equinox<<"\" system=\""<<system<<"\"/>\n";
78      }
79      else{
80        ID=system="galactic";
81        this->itsFileStream <<"  <COOSYS ID=\""<<ID<<"\" system=\""<<system<<"\"/>\n";
82      }
83      this->itsFileStream<<"  <RESOURCE name=\"Duchamp Output\">\n";
84      this->itsFileStream<<"    <TABLE name=\""<<this->itsTableName<<"\">\n";
85      this->itsFileStream<<"      <DESCRIPTION>"<<this->itsTableDescription<<"</DESCRIPTION>\n";
86
87     
88    }
89  }
90
91  void VOTableCatalogueWriter::writeParameters()
92  {
93    if(this->itsOpenFlag){
94      std::vector<VOParam> paramList = this->itsParam->getVOParams();
95      for(std::vector<VOParam>::iterator param=paramList.begin();param<paramList.end();param++){
96        this->itsFileStream << "      ";
97        param->printParam(this->itsFileStream);
98      }   
99    }
100  }
101
102  void VOTableCatalogueWriter::writeStats()
103  {
104    if(this->itsOpenFlag){
105     
106      VOParam threshParam("thresholdActual","","float",this->itsStats->getThreshold(),0,this->itsHead->getFluxUnits());
107      this->itsFileStream << "      ";
108      threshParam.printParam(this->itsFileStream);
109      if(!this->itsParam->getFlagUserThreshold()){
110        VOParam middleParam("noiseMeanActual","","float",this->itsStats->getMiddle(),0,this->itsHead->getFluxUnits());
111        this->itsFileStream << "      ";
112        middleParam.printParam(this->itsFileStream);
113        VOParam spreadParam("noiseSpreadActual","","float",this->itsStats->getSpread(),0,this->itsHead->getFluxUnits());
114        this->itsFileStream << "      ";
115        spreadParam.printParam(this->itsFileStream);
116      }
117    }
118  }
119
120  void VOTableCatalogueWriter::writeTableHeader()
121  {
122    std::map<std::string,std::string> posUCDmap;
123    posUCDmap.insert(std::pair<std::string,std::string>("ra","pos.eq.ra"));
124    posUCDmap.insert(std::pair<std::string,std::string>("dec","pos.eq.dec"));
125    posUCDmap.insert(std::pair<std::string,std::string>("glon","pos.galactic.lng"));
126    posUCDmap.insert(std::pair<std::string,std::string>("glat","pos.galactic.lat"));
127    Catalogues::Column raCol=this->itsColumnSpecification->at(Catalogues::RAJD);
128    std::string lngUCDbase = posUCDmap[makelower(raCol.getName())];
129    Catalogues::Column decCol=this->itsColumnSpecification->at(Catalogues::DECJD);
130    std::string latUCDbase = posUCDmap[makelower(decCol.getName())];
131
132    std::map<std::string,std::string> specUCDmap;
133    specUCDmap.insert(std::pair<std::string,std::string>("VELO","phys.veloc;spect.dopplerVeloc"));
134    specUCDmap.insert(std::pair<std::string,std::string>("VOPT","phys.veloc;spect.dopplerVeloc.opt"));
135    specUCDmap.insert(std::pair<std::string,std::string>("VRAD","phys.veloc;spect.dopplerVeloc.rad"));
136    specUCDmap.insert(std::pair<std::string,std::string>("FREQ","em.freq"));
137    specUCDmap.insert(std::pair<std::string,std::string>("ENER","em.energy"));
138    specUCDmap.insert(std::pair<std::string,std::string>("WAVN","em.wavenumber"));
139    specUCDmap.insert(std::pair<std::string,std::string>("WAVE","em.wl"));
140    specUCDmap.insert(std::pair<std::string,std::string>("AWAV","em.wl"));
141    specUCDmap.insert(std::pair<std::string,std::string>("ZOPT","src.redshift"));
142    specUCDmap.insert(std::pair<std::string,std::string>("BETA","src.redshift; spect.dopplerVeloc"));
143    std::string specUCDbase = specUCDmap[this->itsColumnSpecification->at(Catalogues::VEL).getName()];
144
145    std::vector<Catalogues::Column>::iterator col;
146    for(col=this->itsColumnSpecification->begin();col<this->itsColumnSpecification->end();col++){
147
148      if(col->doCol(Catalogues::VOTABLE,this->itsHead->isSpecOK())){
149        VOField field(*col);
150        if(col->type()=="RAJD")  field.setUCD(lngUCDbase+";meta.main");
151        if(col->type()=="WRA")   field.setUCD("phys.angSize;"+lngUCDbase);
152        if(col->type()=="DECJD") field.setUCD(latUCDbase+";meta.main");
153        if(col->type()=="WDEC")  field.setUCD("phys.angSize;"+latUCDbase);     
154        if(col->type()=="VEL")   field.setUCD(specUCDbase+";meta.main");
155        if(col->type()=="W20")   field.setUCD("spect.line.width;"+specUCDbase);
156        if(col->type()=="W50")   field.setUCD("spect.line.width;"+specUCDbase);
157        if(col->type()=="WVEL")  field.setUCD("spect.line.width;"+specUCDbase);
158        this->itsFileStream << "      ";
159        field.printField(this->itsFileStream);
160      }
161
162    }
163
164    this->itsFileStream<<"      <DATA>\n"
165                        <<"        <TABLEDATA>\n";
166
167  }
168
169  void VOTableCatalogueWriter::writeEntries()
170  {
171    this->itsFileStream.setf(std::ios::fixed); 
172
173    for(std::vector<Detection>::iterator obj=this->itsObjectList->begin();obj<this->itsObjectList->end();obj++)
174      this->writeEntry(&*obj);
175  }
176
177  void VOTableCatalogueWriter::writeEntry(Detection *object)
178  {
179
180    this->itsFileStream<<"        <TR>\n";
181    this->itsFileStream<<"          ";
182    std::vector<Catalogues::Column>::iterator col;
183    for(col=this->itsColumnSpecification->begin();col<this->itsColumnSpecification->end();col++){
184      if(col->doCol(Catalogues::VOTABLE,this->itsHead->isSpecOK())){
185        this->itsFileStream<<"<TD>";
186        object->printTableEntry(this->itsFileStream, *col);
187        this->itsFileStream<<"</TD>";
188      }
189    }
190    this->itsFileStream<<"\n";
191    this->itsFileStream<<"        </TR>\n";
192
193  }
194
195  void VOTableCatalogueWriter::writeFooter()
196  {
197    this->itsFileStream<<"        </TABLEDATA>\n";
198    this->itsFileStream<<"      </DATA>\n";
199    this->itsFileStream<<"    </TABLE>\n";
200    this->itsFileStream<<"  </RESOURCE>\n";
201    this->itsFileStream<<"</VOTABLE>\n";
202    resetiosflags(std::ios::fixed);
203  }
204
205  bool VOTableCatalogueWriter::closeCatalogue()
206  {
207    this->itsFileStream.close();
208    return !this->itsFileStream.fail();
209  }
210
211
212
213
214}
Note: See TracBrowser for help on using the repository browser.