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

Last change on this file since 2683 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 4.1 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
[1136]57//void FITS_NTuple::Read(char inputfile[],int hdunum)
58//{
59// ReadF(inputfile,hdunum);
60//}
[1047]61void FITS_NTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
62{
63 fistLineToBeRead_ = firstLine;
64 numberOfLinesToBeRead_ = numberOfLines;
[1136]65 Read(inputfile,hdunum);
[1047]66}
67
68
69
[1136]70//void FITS_NTuple::Write(char outputfile[], bool OldFile)
71//{
72// WriteF(outputfile, OldFile);
73//}
[860]74
[1136]75void FITS_NTuple::ReadFromFits(FitsInFile& is)
[860]76{
[1136]77 if (!is.IsFitsTable())
[860]78 {
79 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
80 }
81 int nbcols, nbentries;
[1136]82 nbcols = is.NbColsFromFits();
[860]83 nbentries = 0;
[923]84 int k;
[1136]85 for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
[860]86
87 char ** ColName = new char*[nbcols];
88
[923]89 for (k=0; k<nbcols;k++)
[860]90 {
91 ColName[k] = new char[LONNOM1];
[1136]92 strncpy(ColName[k], is.ColNameFromFits(k).c_str(),LONNOM);
[860]93 ColName[k][LONNOM] = '\0';
94 }
[923]95 for (k=0; k<nbcols;k++)
[860]96 {
[1300]97 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
[860]98 string type;
[1300]99 if (ss != FitsFile::FitsDataType_float)
[860]100 {
[1300]101 if (ss == FitsFile::FitsDataType_double) type= string("double");
[860]102 else
[1300]103 if (ss == FitsFile::FitsDataType_int) type= string("integer");
[860]104 else
[1300]105 if (ss == FitsFile::FitsDataType_char) type = string("char*");
[860]106 else
107 type = string("unknown");
108 cout << " WARNING: the column " << k << " on fits file is not float but : " << type << endl;
109 }
110 }
111 if(dobj_ == NULL)
112 {
113 dobj_= new NTuple(nbcols,ColName);
[1047]114 ownobj_ = true;
[860]115 }
116 else
117 {
118 dobj_->Clean();
119 (*dobj_) = NTuple(nbcols,ColName);
120 }
[923]121 for (k=0; k<nbcols;k++)
[860]122 {
123 delete [] ColName[k];
124 }
125 delete [] ColName;
126
[1143]127 float* ligne = new float[nbcols];
[1047]128
[1136]129 int firstln, lastln;
130 if (numberOfLinesToBeRead_ > 0)
131 {
132 firstln = fistLineToBeRead_;
133 lastln = firstln + numberOfLinesToBeRead_;
134 }
135 else
136 {
137 firstln = 0;
138 lastln = nbentries;
139 }
140 int numLigne;
141 for (numLigne=firstln; numLigne < lastln; numLigne++)
142 {
143 is.GetBinTabLine(numLigne, ligne);
[1143]144 dobj_->Fill((r_4*)ligne);
[1136]145 }
146 delete [] ligne;
[1047]147
[1136]148 dobj_->Info()=is.DVListFromFits();
149}
[1047]150
[1136]151void FITS_NTuple::WriteToFits(FitsOutFile& os)
[860]152{
153 if(dobj_ == NULL)
154 {
155 cout << " WriteToFits:: dobj_= null " << endl;
156 return;
157 }
158
159 // table will have 'ncols' columns
160 int ncols = dobj_->NVar();
161
162 // table will have 'nrows' rows
163 int nentries = dobj_->NEntry();
164
165 // get names and values from the join DVList object
166 DVList dvl= dobj_->Info();
[1300]167 dvl["Content"]= "NTuple";
168 dvl.SetComment("Content", "name of SOPHYA object");
[860]169 // extension name
[1194]170 string extname("NTuple_Binary_tbl");
[860]171
[1194]172 vector<string> Noms(ncols);
[923]173 int k;
174 for (k=0; k< ncols; k++)
[860]175 {
[1194]176 Noms[k]= dobj_->NomIndex(k);
[860]177 }
[1136]178
[1194]179 string type(ncols, 'E');
[860]180 vector<int> dummy;
[1136]181
[1221]182 os.makeHeaderBntblOnFits(type,Noms, nentries, ncols, &dvl, extname, dummy);
[1136]183 float* column = new float[nentries];
184 for (k=0; k<ncols;k++)
185 {
[2209]186 int j;
187 for(j = 0; j < nentries; j++) column[j]= dobj_->GetVal(j,k);
[1210]188 os.PutColToFits(k, nentries, column);
[1136]189 }
190 delete [] column;
[860]191
192}
193
Note: See TracBrowser for help on using the repository browser.