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

Last change on this file since 3619 was 3619, checked in by cmv, 16 years ago

add various #include<> for g++ 4.3 (jaunty 9.04), cmv 05/05/2009

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