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

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

Adding the licensing text to files that didn't have it.

File size: 9.0 KB
Line 
1// -----------------------------------------------------------------------
2// VOTableCatalogueWriter.cc: Writing output catalogues to VOTable files
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <duchamp/Outputs/VOTableCatalogueWriter.hh>
29#include <duchamp/Outputs/CatalogueWriter.hh>
30#include <duchamp/Outputs/columns.hh>
31#include <duchamp/Detection/detection.hh>
32#include <duchamp/Utils/utils.hh>
33#include <duchamp/Utils/VOField.hh>
34#include <duchamp/Utils/VOParam.hh>
35#include <ios>
36#include <iostream>
37#include <fstream>
38
39namespace duchamp {
40
41   VOTableCatalogueWriter::VOTableCatalogueWriter():
42    CatalogueWriter()
43  {
44    this->itsTableName="";
45    this->itsTableDescription="";
46  }
47
48  VOTableCatalogueWriter::VOTableCatalogueWriter(std::string name):
49    CatalogueWriter(name)
50  {
51    this->itsTableName="";
52    this->itsTableDescription="";
53  }
54
55  VOTableCatalogueWriter::VOTableCatalogueWriter(const VOTableCatalogueWriter& other)
56  {
57    this->operator=(other);
58  }
59
60  VOTableCatalogueWriter& VOTableCatalogueWriter::operator= (const VOTableCatalogueWriter& other)
61  {
62    if(this==&other) return *this;
63    ((CatalogueWriter &) *this) = other;
64    this->itsOpenFlag=false;
65    this->itsTableName=other.itsTableName;
66    this->itsTableDescription=other.itsTableDescription;
67    return *this;
68  }
69 
70  bool VOTableCatalogueWriter::openCatalogue(std::ios_base::openmode mode)
71  {
72    if(this->itsName == ""){
73      DUCHAMPERROR("VOTableCatalogueWriter","No catalogue name provided");
74      this->itsOpenFlag = false;
75    }
76    else {
77      this->itsFileStream.open(this->itsName.c_str(),mode);
78      this->itsOpenFlag = !this->itsFileStream.fail();
79    }
80    if(!this->itsOpenFlag)
81      DUCHAMPERROR("VOTableCatalogueWriter","Could not open file \""<<this->itsName<<"\"");
82    return this->itsOpenFlag;
83
84  }
85
86  void VOTableCatalogueWriter::writeHeader()
87  {
88    if(this->itsOpenFlag){
89
90      this->itsFileStream<<"<?xml version=\"1.0\"?>\n";
91      this->itsFileStream<<"<VOTABLE version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
92      this->itsFileStream<<" xsi:noNamespaceSchemaLocation=\"http://www.ivoa.net/xml/VOTable/VOTable/v1.1\">\n";
93     
94      std::string ID,equinox,system;
95      if(std::string(this->itsHead->WCS().lngtyp)=="RA") { //J2000 or B1950
96        if(this->itsHead->WCS().equinox==2000.){
97          ID=equinox= "J2000";
98          system="eq_FK5";
99        }
100        else{
101          ID=equinox="B1950";
102          system="eq_FK4";
103        }
104        this->itsFileStream <<"  <COOSYS ID=\""<<ID<<"\" equinox=\""<<equinox<<"\" system=\""<<system<<"\"/>\n";
105      }
106      else{
107        ID=system="galactic";
108        this->itsFileStream <<"  <COOSYS ID=\""<<ID<<"\" system=\""<<system<<"\"/>\n";
109      }
110      this->itsFileStream<<"  <RESOURCE name=\"Duchamp Output\">\n";
111      this->itsFileStream<<"    <TABLE name=\""<<this->itsTableName<<"\">\n";
112      this->itsFileStream<<"      <DESCRIPTION>"<<this->itsTableDescription<<"</DESCRIPTION>\n";
113
114     
115    }
116  }
117
118  void VOTableCatalogueWriter::writeParameters()
119  {
120    if(this->itsOpenFlag){
121      std::vector<VOParam> paramList = this->itsParam->getVOParams();
122      for(std::vector<VOParam>::iterator param=paramList.begin();param<paramList.end();param++){
123        this->itsFileStream << "      ";
124        param->printParam(this->itsFileStream);
125      }   
126    }
127  }
128
129  void VOTableCatalogueWriter::writeStats()
130  {
131    if(this->itsOpenFlag){
132     
133      VOParam threshParam("thresholdActual","","float",this->itsStats->getThreshold(),0,this->itsHead->getFluxUnits());
134      this->itsFileStream << "      ";
135      threshParam.printParam(this->itsFileStream);
136      if(!this->itsParam->getFlagUserThreshold()){
137        VOParam middleParam("noiseMeanActual","","float",this->itsStats->getMiddle(),0,this->itsHead->getFluxUnits());
138        this->itsFileStream << "      ";
139        middleParam.printParam(this->itsFileStream);
140        VOParam spreadParam("noiseSpreadActual","","float",this->itsStats->getSpread(),0,this->itsHead->getFluxUnits());
141        this->itsFileStream << "      ";
142        spreadParam.printParam(this->itsFileStream);
143      }
144    }
145  }
146
147  void VOTableCatalogueWriter::writeTableHeader()
148  {
149    std::map<std::string,std::string> posUCDmap;
150    posUCDmap.insert(std::pair<std::string,std::string>("ra","pos.eq.ra"));
151    posUCDmap.insert(std::pair<std::string,std::string>("dec","pos.eq.dec"));
152    posUCDmap.insert(std::pair<std::string,std::string>("glon","pos.galactic.lng"));
153    posUCDmap.insert(std::pair<std::string,std::string>("glat","pos.galactic.lat"));
154    Catalogues::Column &raCol=this->itsColumnSpecification->column("RAJD");
155    std::string lngUCDbase = posUCDmap[makelower(raCol.getName())];
156    Catalogues::Column &decCol=this->itsColumnSpecification->column("DECJD");
157    std::string latUCDbase = posUCDmap[makelower(decCol.getName())];
158
159    std::map<std::string,std::string> specUCDmap;
160    specUCDmap.insert(std::pair<std::string,std::string>("VELO","phys.veloc;spect.dopplerVeloc"));
161    specUCDmap.insert(std::pair<std::string,std::string>("VOPT","phys.veloc;spect.dopplerVeloc.opt"));
162    specUCDmap.insert(std::pair<std::string,std::string>("VRAD","phys.veloc;spect.dopplerVeloc.rad"));
163    specUCDmap.insert(std::pair<std::string,std::string>("FREQ","em.freq"));
164    specUCDmap.insert(std::pair<std::string,std::string>("ENER","em.energy"));
165    specUCDmap.insert(std::pair<std::string,std::string>("WAVN","em.wavenumber"));
166    specUCDmap.insert(std::pair<std::string,std::string>("WAVE","em.wl"));
167    specUCDmap.insert(std::pair<std::string,std::string>("AWAV","em.wl"));
168    specUCDmap.insert(std::pair<std::string,std::string>("ZOPT","src.redshift"));
169    specUCDmap.insert(std::pair<std::string,std::string>("BETA","src.redshift; spect.dopplerVeloc"));
170    std::string specUCDbase = specUCDmap[this->itsColumnSpecification->column("VEL").getName()];
171
172    for(size_t i=0;i<this->itsColumnSpecification->size();i++){
173     
174      Catalogues::Column *col = this->itsColumnSpecification->pCol(i);
175      if(col->doCol(Catalogues::VOTABLE,this->itsHead->isSpecOK())){
176        VOField field(*col);
177        if(col->type()=="RAJD")  field.setUCD(lngUCDbase+";meta.main");
178        if(col->type()=="WRA")   field.setUCD("phys.angSize;"+lngUCDbase);
179        if(col->type()=="DECJD") field.setUCD(latUCDbase+";meta.main");
180        if(col->type()=="WDEC")  field.setUCD("phys.angSize;"+latUCDbase);     
181        if(col->type()=="VEL")   field.setUCD(specUCDbase+";meta.main");
182        if(col->type()=="W20")   field.setUCD("spect.line.width;"+specUCDbase);
183        if(col->type()=="W50")   field.setUCD("spect.line.width;"+specUCDbase);
184        if(col->type()=="WVEL")  field.setUCD("spect.line.width;"+specUCDbase);
185        this->itsFileStream << "      ";
186        field.printField(this->itsFileStream);
187      }
188
189    }
190
191    this->itsFileStream<<"      <DATA>\n"
192                        <<"        <TABLEDATA>\n";
193
194  }
195
196  void VOTableCatalogueWriter::writeEntries()
197  {
198    this->itsFileStream.setf(std::ios::fixed); 
199
200    for(std::vector<Detection>::iterator obj=this->itsObjectList->begin();obj<this->itsObjectList->end();obj++)
201      this->writeEntry(&*obj);
202  }
203
204  void VOTableCatalogueWriter::writeEntry(Detection *object)
205  {
206
207    this->itsFileStream<<"        <TR>\n";
208    this->itsFileStream<<"          ";
209    for(size_t i=0;i<this->itsColumnSpecification->size();i++){
210      Catalogues::Column *col = this->itsColumnSpecification->pCol(i);
211      if(col->doCol(Catalogues::VOTABLE,this->itsHead->isSpecOK())){
212        this->itsFileStream<<"<TD>";
213        object->printTableEntry(this->itsFileStream, *col);
214        this->itsFileStream<<"</TD>";
215      }
216    }
217    this->itsFileStream<<"\n";
218    this->itsFileStream<<"        </TR>\n";
219
220  }
221
222  void VOTableCatalogueWriter::writeFooter()
223  {
224    this->itsFileStream<<"        </TABLEDATA>\n";
225    this->itsFileStream<<"      </DATA>\n";
226    this->itsFileStream<<"    </TABLE>\n";
227    this->itsFileStream<<"  </RESOURCE>\n";
228    this->itsFileStream<<"</VOTABLE>\n";
229    resetiosflags(std::ios::fixed);
230  }
231
232  bool VOTableCatalogueWriter::closeCatalogue()
233  {
234    this->itsFileStream.close();
235    return !this->itsFileStream.fail();
236  }
237
238
239
240
241}
Note: See TracBrowser for help on using the repository browser.