source: tags/release-1.6.1/src/Outputs/VOTableCatalogueWriter.cc

Last change on this file was 1356, checked in by MatthewWhiting, 10 years ago

Fixing #216 - was missing a semi-colon joining the new parts of the UCD to the old.

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/FileCatalogueWriter.hh>
31#include <duchamp/Outputs/columns.hh>
32#include <duchamp/Detection/detection.hh>
33#include <duchamp/Utils/utils.hh>
34#include <duchamp/Utils/VOField.hh>
35#include <duchamp/Utils/VOParam.hh>
36#include <ios>
37#include <iostream>
38#include <fstream>
39
40namespace duchamp {
41
42    VOTableCatalogueWriter::VOTableCatalogueWriter():
43        FileCatalogueWriter()
44    {
45        this->itsTableName="";
46        this->itsTableDescription="";
47    }
48
49    VOTableCatalogueWriter::VOTableCatalogueWriter(std::string name):
50        FileCatalogueWriter(name)
51    {
52        this->itsTableName="";
53        this->itsTableDescription="";
54    }
55
56    VOTableCatalogueWriter::VOTableCatalogueWriter(const VOTableCatalogueWriter& other)
57    {
58        this->operator=(other);
59    }
60
61    VOTableCatalogueWriter& VOTableCatalogueWriter::operator= (const VOTableCatalogueWriter& other)
62    {
63        if(this==&other) return *this;
64        ((FileCatalogueWriter &) *this) = other;
65        this->itsTableName=other.itsTableName;
66        this->itsTableDescription=other.itsTableDescription;
67        return *this;
68    }
69 
70    void VOTableCatalogueWriter::writeHeader()
71    {
72        if(this->itsOpenFlag){
73
74            this->writeNamespace();
75
76            this->writeSTC();
77
78            this->itsFileStream<<"  <RESOURCE name=\"Duchamp Output\">\n";
79            this->itsFileStream<<"    <TABLE name=\""<<this->itsTableName<<"\">\n";
80            this->itsFileStream<<"      <DESCRIPTION>"<<this->itsTableDescription<<"</DESCRIPTION>\n";
81
82     
83        }
84    }
85
86    void VOTableCatalogueWriter::writeNamespace()
87    {
88        if(this->itsOpenFlag){
89            this->itsFileStream<<"<?xml version=\"1.0\"?>\n";
90            this->itsFileStream<<"<VOTABLE version=\"1.3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
91                               <<" xmlns=\"http://www.ivoa.net/xml/VOTable/v1.3\"\n"
92                               <<" xmlns:stc=\"http://www.ivoa.net/xml/STC/v1.30\"\n"
93                               <<" xsi:schemaLocation=\"http://www.ivoa.net/xml/VOTable/VOTable/v1.1\" >\n";
94        }
95    }
96
97    void VOTableCatalogueWriter::writeSTC()
98    {
99        if(this->itsOpenFlag){
100            std::string ID,equinox,system;
101            if(std::string(this->itsHead->WCS().lngtyp)=="RA") { //J2000 or B1950
102                if(this->itsHead->WCS().equinox==2000.){
103                    ID=equinox= "J2000";
104                    system="eq_FK5";
105                }
106                else{
107                    ID=equinox="B1950";
108                    system="eq_FK4";
109                }
110                this->itsFileStream <<"  <COOSYS ID=\""<<ID<<"\" equinox=\""<<equinox<<"\" system=\""<<system<<"\"/>\n";
111            }
112            else{
113                ID=system="galactic";
114                this->itsFileStream <<"  <COOSYS ID=\""<<ID<<"\" system=\""<<system<<"\"/>\n";
115            }
116
117        }
118    }
119
120
121    void VOTableCatalogueWriter::writeParameter(VOParam param)
122    {
123        if(this->itsOpenFlag){
124            this->itsFileStream << "      ";
125            param.printParam(this->itsFileStream);
126        }
127    }
128
129    void VOTableCatalogueWriter::writeParameters()
130    {
131        if(this->itsOpenFlag){
132            std::vector<VOParam> paramList = this->itsParam->getVOParams();
133            for(std::vector<VOParam>::iterator param=paramList.begin();param<paramList.end();param++){
134                this->writeParameter(*param);
135            }   
136        }
137    }
138
139    void VOTableCatalogueWriter::writeStats()
140    {
141        if(this->itsOpenFlag){
142     
143            VOParam threshParam("thresholdActual","","float",this->itsStats->getThreshold(),0,this->itsHead->getFluxUnits());
144            this->itsFileStream << "      ";
145            threshParam.printParam(this->itsFileStream);
146            if(!this->itsParam->getFlagUserThreshold()){
147                VOParam middleParam("noiseMeanActual","","float",this->itsStats->getMiddle(),0,this->itsHead->getFluxUnits());
148                this->itsFileStream << "      ";
149                middleParam.printParam(this->itsFileStream);
150                VOParam spreadParam("noiseSpreadActual","","float",this->itsStats->getSpread(),0,this->itsHead->getFluxUnits());
151                this->itsFileStream << "      ";
152                spreadParam.printParam(this->itsFileStream);
153            }
154        }
155    }
156
157    void VOTableCatalogueWriter::writeTableHeader()
158    {
159        std::map<std::string,std::string> posUCDmap;
160        posUCDmap.insert(std::pair<std::string,std::string>("ra","pos.eq.ra"));
161        posUCDmap.insert(std::pair<std::string,std::string>("dec","pos.eq.dec"));
162        posUCDmap.insert(std::pair<std::string,std::string>("glon","pos.galactic.lng"));
163        posUCDmap.insert(std::pair<std::string,std::string>("glat","pos.galactic.lat"));
164        Catalogues::Column &raCol=this->itsColumnSpecification->column("RAJD");
165        std::string lngUCDbase = posUCDmap[makelower(raCol.getName())];
166        Catalogues::Column &decCol=this->itsColumnSpecification->column("DECJD");
167        std::string latUCDbase = posUCDmap[makelower(decCol.getName())];
168
169        std::map<std::string,std::string> specUCDmap;
170        specUCDmap.insert(std::pair<std::string,std::string>("VELO","phys.veloc;spect.dopplerVeloc"));
171        specUCDmap.insert(std::pair<std::string,std::string>("VOPT","phys.veloc;spect.dopplerVeloc.opt"));
172        specUCDmap.insert(std::pair<std::string,std::string>("VRAD","phys.veloc;spect.dopplerVeloc.rad"));
173        specUCDmap.insert(std::pair<std::string,std::string>("FREQ","em.freq"));
174        specUCDmap.insert(std::pair<std::string,std::string>("ENER","em.energy"));
175        specUCDmap.insert(std::pair<std::string,std::string>("WAVN","em.wavenumber"));
176        specUCDmap.insert(std::pair<std::string,std::string>("WAVE","em.wl"));
177        specUCDmap.insert(std::pair<std::string,std::string>("AWAV","em.wl"));
178        specUCDmap.insert(std::pair<std::string,std::string>("ZOPT","src.redshift"));
179        specUCDmap.insert(std::pair<std::string,std::string>("BETA","src.redshift; spect.dopplerVeloc"));
180        std::string specUCDbase = specUCDmap[this->itsColumnSpecification->column("VEL").getName()];
181
182        for(size_t i=0;i<this->itsColumnSpecification->size();i++){
183     
184            Catalogues::Column *col = this->itsColumnSpecification->pCol(i);
185            bool useFint=this->itsHead->isSpecOK();
186            if(col->doCol(Catalogues::VOTABLE,useFint)){
187                VOField field(*col);
188                if(col->type()=="RA")    field.setUCD(lngUCDbase);
189                if(col->type()=="RAJD")  field.setUCD(lngUCDbase+";meta.main");
190                if(col->type()=="WRA")   field.setUCD("phys.angSize;"+lngUCDbase);
191                if(col->type()=="DEC")   field.setUCD(latUCDbase);
192                if(col->type()=="DECJD") field.setUCD(latUCDbase+";meta.main");
193                if(col->type()=="WDEC")  field.setUCD("phys.angSize;"+latUCDbase);     
194                if(col->type()=="VEL")   field.setUCD(specUCDbase+";meta.main");
195                if(col->type()=="W20")   field.setUCD("spect.line.width.20;"+specUCDbase);
196                if(col->type()=="W50")   field.setUCD("spect.line.width.50;"+specUCDbase);
197                if(col->type()=="WVEL")  field.setUCD("spect.line.width.full;"+specUCDbase);
198                if(!this->itsHead->is2D()){
199                    if(col->type()=="FINT") field.setUCD(field.UCD()+";spect.line.intensity");
200                    if(col->type()=="FTOT") field.setUCD(field.UCD()+";spect.line.intensity");
201                    if(col->type()=="FPEAK") field.setUCD(field.UCD()+";spect.line.intensity");
202                }
203                this->itsFileStream << "      ";
204                field.printField(this->itsFileStream);
205            }
206
207        }
208
209        this->itsFileStream<<"      <DATA>\n"
210                           <<"        <TABLEDATA>\n";
211
212    }
213
214    void VOTableCatalogueWriter::writeEntry(Detection *object)
215    {
216        this->itsFileStream.setf(std::ios::fixed); 
217
218        this->itsFileStream<<"        <TR>\n";
219        this->itsFileStream<<"          ";
220        for(size_t i=0;i<this->itsColumnSpecification->size();i++){
221            Catalogues::Column *col = this->itsColumnSpecification->pCol(i);
222            if(col->doCol(Catalogues::VOTABLE,this->itsHead->isSpecOK())){
223                this->itsFileStream<<"<TD>";
224                object->printTableEntry(this->itsFileStream, *col);
225                this->itsFileStream<<"</TD>";
226            }
227        }
228        this->itsFileStream<<"\n";
229        this->itsFileStream<<"        </TR>\n";
230
231    }
232
233    void VOTableCatalogueWriter::writeFooter()
234    {
235        this->itsFileStream<<"        </TABLEDATA>\n";
236        this->itsFileStream<<"      </DATA>\n";
237        this->itsFileStream<<"    </TABLE>\n";
238        this->itsFileStream<<"  </RESOURCE>\n";
239        this->itsFileStream<<"</VOTABLE>\n";
240        std::resetiosflags(std::ios::fixed);
241    }
242
243
244
245
246}
Note: See TracBrowser for help on using the repository browser.