source: Sophya/trunk/SophyaExt/FitsIOServer/fitsntuple.cc@ 2897

Last change on this file since 2897 was 2897, checked in by ansari, 20 years ago

Mise en place de l heritage FitsIOHandler (ancien) de FitsHandlerInterface pour une gestion commune - a partir de FitsManager - Reza 13/01/2006

File size: 4.5 KB
RevLine 
[2615]1#include "sopnamsp.h"
[860]2#include "pexceptions.h"
3#include "fitsntuple.h"
4///////////////////////////////////////////////////////////
5// Les objets delegues pour la gestion de persistance sur fichiers fits
6// pout NTuple
7///////////////////////////////////////////////////////////
8
9
10#define LONNOM 8
11#define LONNOM1 (LONNOM+1)
12
13
[1371]14/*!
15 \class SOPHYA::FITS_NTuple
16 \ingroup FitsIOServer
17 FITS format I/O handler for SOPHYA::NTuple objects.
18*/
[860]19
20FITS_NTuple::FITS_NTuple()
21{
22 dobj_ = new NTuple;
[1047]23 InitNull();
24 ownobj_ = true;
[860]25}
26
27FITS_NTuple::FITS_NTuple(char inputfile[],int hdunum)
28{
29 dobj_ = new NTuple;
[1047]30 InitNull();
31 ownobj_ = true;
[860]32
[1136]33 Read(inputfile,hdunum);
[860]34}
35
36
37FITS_NTuple::FITS_NTuple(const NTuple & obj)
38{
39 dobj_ = new NTuple(obj);
[1047]40 InitNull();
41 ownobj_ = true;
[860]42}
[1047]43FITS_NTuple::FITS_NTuple(NTuple* obj)
44{
45 dobj_ = obj;
46 InitNull();
47 ownobj_ = false;
48}
[860]49
50
51FITS_NTuple::~FITS_NTuple()
52{
[1047]53 if (ownobj_ && dobj_ != NULL) delete dobj_;
[1136]54 // if (column_ != NULL) delete [] column_;
[860]55}
56
[2897]57int FITS_NTuple::CheckReadability(FitsInOutFile& is)
58{
59 if (is.CurrentHDUType() == IMAGE_HDU ) return 0;
60 string key = "SOPCLSNM";
61 string clsnm = is.KeyValue(key);
62 if (clsnm == "SOPHYA::NTuple") return 2;
63 else return 1;
64}
65
[1136]66//void FITS_NTuple::Read(char inputfile[],int hdunum)
67//{
68// ReadF(inputfile,hdunum);
69//}
[1047]70void FITS_NTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
71{
72 fistLineToBeRead_ = firstLine;
73 numberOfLinesToBeRead_ = numberOfLines;
[1136]74 Read(inputfile,hdunum);
[1047]75}
76
77
78
[1136]79//void FITS_NTuple::Write(char outputfile[], bool OldFile)
80//{
81// WriteF(outputfile, OldFile);
82//}
[860]83
[1136]84void FITS_NTuple::ReadFromFits(FitsInFile& is)
[860]85{
[1136]86 if (!is.IsFitsTable())
[860]87 {
88 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
89 }
90 int nbcols, nbentries;
[1136]91 nbcols = is.NbColsFromFits();
[860]92 nbentries = 0;
[923]93 int k;
[1136]94 for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
[860]95
96 char ** ColName = new char*[nbcols];
97
[923]98 for (k=0; k<nbcols;k++)
[860]99 {
100 ColName[k] = new char[LONNOM1];
[1136]101 strncpy(ColName[k], is.ColNameFromFits(k).c_str(),LONNOM);
[860]102 ColName[k][LONNOM] = '\0';
103 }
[923]104 for (k=0; k<nbcols;k++)
[860]105 {
[1300]106 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
[860]107 string type;
[1300]108 if (ss != FitsFile::FitsDataType_float)
[860]109 {
[1300]110 if (ss == FitsFile::FitsDataType_double) type= string("double");
[860]111 else
[1300]112 if (ss == FitsFile::FitsDataType_int) type= string("integer");
[860]113 else
[1300]114 if (ss == FitsFile::FitsDataType_char) type = string("char*");
[860]115 else
116 type = string("unknown");
117 cout << " WARNING: the column " << k << " on fits file is not float but : " << type << endl;
118 }
119 }
120 if(dobj_ == NULL)
121 {
122 dobj_= new NTuple(nbcols,ColName);
[1047]123 ownobj_ = true;
[860]124 }
125 else
126 {
127 dobj_->Clean();
128 (*dobj_) = NTuple(nbcols,ColName);
129 }
[923]130 for (k=0; k<nbcols;k++)
[860]131 {
132 delete [] ColName[k];
133 }
134 delete [] ColName;
135
[1143]136 float* ligne = new float[nbcols];
[1047]137
[1136]138 int firstln, lastln;
139 if (numberOfLinesToBeRead_ > 0)
140 {
141 firstln = fistLineToBeRead_;
142 lastln = firstln + numberOfLinesToBeRead_;
143 }
144 else
145 {
146 firstln = 0;
147 lastln = nbentries;
148 }
149 int numLigne;
150 for (numLigne=firstln; numLigne < lastln; numLigne++)
151 {
152 is.GetBinTabLine(numLigne, ligne);
[1143]153 dobj_->Fill((r_4*)ligne);
[1136]154 }
155 delete [] ligne;
[1047]156
[1136]157 dobj_->Info()=is.DVListFromFits();
158}
[1047]159
[1136]160void FITS_NTuple::WriteToFits(FitsOutFile& os)
[860]161{
162 if(dobj_ == NULL)
163 {
164 cout << " WriteToFits:: dobj_= null " << endl;
165 return;
166 }
167
168 // table will have 'ncols' columns
169 int ncols = dobj_->NVar();
170
171 // table will have 'nrows' rows
172 int nentries = dobj_->NEntry();
173
174 // get names and values from the join DVList object
175 DVList dvl= dobj_->Info();
[1300]176 dvl["Content"]= "NTuple";
177 dvl.SetComment("Content", "name of SOPHYA object");
[2897]178 dvl["SOPCLSNM"]= "SOPHYA::NTuple";
179 dvl.SetComment("SOPCLSNM", "SOPHYA class name");
[860]180 // extension name
[2897]181 // string extname("NTuple_Binary_tbl");
182 string extname = os.NextExtensionName();
[860]183
[1194]184 vector<string> Noms(ncols);
[923]185 int k;
186 for (k=0; k< ncols; k++)
[860]187 {
[1194]188 Noms[k]= dobj_->NomIndex(k);
[860]189 }
[1136]190
[1194]191 string type(ncols, 'E');
[860]192 vector<int> dummy;
[1136]193
[1221]194 os.makeHeaderBntblOnFits(type,Noms, nentries, ncols, &dvl, extname, dummy);
[1136]195 float* column = new float[nentries];
196 for (k=0; k<ncols;k++)
197 {
[2209]198 int j;
199 for(j = 0; j < nentries; j++) column[j]= dobj_->GetVal(j,k);
[1210]200 os.PutColToFits(k, nentries, column);
[1136]201 }
202 delete [] column;
[860]203
204}
205
Note: See TracBrowser for help on using the repository browser.