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
Line 
1#include "sopnamsp.h"
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
14/*!
15 \class SOPHYA::FITS_NTuple
16 \ingroup FitsIOServer
17 FITS format I/O handler for SOPHYA::NTuple objects.
18*/
19
20FITS_NTuple::FITS_NTuple()
21{
22 dobj_ = new NTuple;
23 InitNull();
24 ownobj_ = true;
25}
26
27FITS_NTuple::FITS_NTuple(char inputfile[],int hdunum)
28{
29 dobj_ = new NTuple;
30 InitNull();
31 ownobj_ = true;
32
33 Read(inputfile,hdunum);
34}
35
36
37FITS_NTuple::FITS_NTuple(const NTuple & obj)
38{
39 dobj_ = new NTuple(obj);
40 InitNull();
41 ownobj_ = true;
42}
43FITS_NTuple::FITS_NTuple(NTuple* obj)
44{
45 dobj_ = obj;
46 InitNull();
47 ownobj_ = false;
48}
49
50
51FITS_NTuple::~FITS_NTuple()
52{
53 if (ownobj_ && dobj_ != NULL) delete dobj_;
54 // if (column_ != NULL) delete [] column_;
55}
56
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
66//void FITS_NTuple::Read(char inputfile[],int hdunum)
67//{
68// ReadF(inputfile,hdunum);
69//}
70void FITS_NTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
71{
72 fistLineToBeRead_ = firstLine;
73 numberOfLinesToBeRead_ = numberOfLines;
74 Read(inputfile,hdunum);
75}
76
77
78
79//void FITS_NTuple::Write(char outputfile[], bool OldFile)
80//{
81// WriteF(outputfile, OldFile);
82//}
83
84void FITS_NTuple::ReadFromFits(FitsInFile& is)
85{
86 if (!is.IsFitsTable())
87 {
88 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
89 }
90 int nbcols, nbentries;
91 nbcols = is.NbColsFromFits();
92 nbentries = 0;
93 int k;
94 for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
95
96 char ** ColName = new char*[nbcols];
97
98 for (k=0; k<nbcols;k++)
99 {
100 ColName[k] = new char[LONNOM1];
101 strncpy(ColName[k], is.ColNameFromFits(k).c_str(),LONNOM);
102 ColName[k][LONNOM] = '\0';
103 }
104 for (k=0; k<nbcols;k++)
105 {
106 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
107 string type;
108 if (ss != FitsFile::FitsDataType_float)
109 {
110 if (ss == FitsFile::FitsDataType_double) type= string("double");
111 else
112 if (ss == FitsFile::FitsDataType_int) type= string("integer");
113 else
114 if (ss == FitsFile::FitsDataType_char) type = string("char*");
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);
123 ownobj_ = true;
124 }
125 else
126 {
127 dobj_->Clean();
128 (*dobj_) = NTuple(nbcols,ColName);
129 }
130 for (k=0; k<nbcols;k++)
131 {
132 delete [] ColName[k];
133 }
134 delete [] ColName;
135
136 float* ligne = new float[nbcols];
137
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);
153 dobj_->Fill((r_4*)ligne);
154 }
155 delete [] ligne;
156
157 dobj_->Info()=is.DVListFromFits();
158}
159
160void FITS_NTuple::WriteToFits(FitsOutFile& os)
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();
176 dvl["Content"]= "NTuple";
177 dvl.SetComment("Content", "name of SOPHYA object");
178 dvl["SOPCLSNM"]= "SOPHYA::NTuple";
179 dvl.SetComment("SOPCLSNM", "SOPHYA class name");
180 // extension name
181 // string extname("NTuple_Binary_tbl");
182 string extname = os.NextExtensionName();
183
184 vector<string> Noms(ncols);
185 int k;
186 for (k=0; k< ncols; k++)
187 {
188 Noms[k]= dobj_->NomIndex(k);
189 }
190
191 string type(ncols, 'E');
192 vector<int> dummy;
193
194 os.makeHeaderBntblOnFits(type,Noms, nentries, ncols, &dvl, extname, dummy);
195 float* column = new float[nentries];
196 for (k=0; k<ncols;k++)
197 {
198 int j;
199 for(j = 0; j < nentries; j++) column[j]= dobj_->GetVal(j,k);
200 os.PutColToFits(k, nentries, column);
201 }
202 delete [] column;
203
204}
205
Note: See TracBrowser for help on using the repository browser.