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

Last change on this file since 2857 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
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
57//void FITS_NTuple::Read(char inputfile[],int hdunum)
58//{
59// ReadF(inputfile,hdunum);
60//}
61void FITS_NTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
62{
63 fistLineToBeRead_ = firstLine;
64 numberOfLinesToBeRead_ = numberOfLines;
65 Read(inputfile,hdunum);
66}
67
68
69
70//void FITS_NTuple::Write(char outputfile[], bool OldFile)
71//{
72// WriteF(outputfile, OldFile);
73//}
74
75void FITS_NTuple::ReadFromFits(FitsInFile& is)
76{
77 if (!is.IsFitsTable())
78 {
79 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
80 }
81 int nbcols, nbentries;
82 nbcols = is.NbColsFromFits();
83 nbentries = 0;
84 int k;
85 for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
86
87 char ** ColName = new char*[nbcols];
88
89 for (k=0; k<nbcols;k++)
90 {
91 ColName[k] = new char[LONNOM1];
92 strncpy(ColName[k], is.ColNameFromFits(k).c_str(),LONNOM);
93 ColName[k][LONNOM] = '\0';
94 }
95 for (k=0; k<nbcols;k++)
96 {
97 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
98 string type;
99 if (ss != FitsFile::FitsDataType_float)
100 {
101 if (ss == FitsFile::FitsDataType_double) type= string("double");
102 else
103 if (ss == FitsFile::FitsDataType_int) type= string("integer");
104 else
105 if (ss == FitsFile::FitsDataType_char) type = string("char*");
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);
114 ownobj_ = true;
115 }
116 else
117 {
118 dobj_->Clean();
119 (*dobj_) = NTuple(nbcols,ColName);
120 }
121 for (k=0; k<nbcols;k++)
122 {
123 delete [] ColName[k];
124 }
125 delete [] ColName;
126
127 float* ligne = new float[nbcols];
128
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);
144 dobj_->Fill((r_4*)ligne);
145 }
146 delete [] ligne;
147
148 dobj_->Info()=is.DVListFromFits();
149}
150
151void FITS_NTuple::WriteToFits(FitsOutFile& os)
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();
167 dvl["Content"]= "NTuple";
168 dvl.SetComment("Content", "name of SOPHYA object");
169 // extension name
170 string extname("NTuple_Binary_tbl");
171
172 vector<string> Noms(ncols);
173 int k;
174 for (k=0; k< ncols; k++)
175 {
176 Noms[k]= dobj_->NomIndex(k);
177 }
178
179 string type(ncols, 'E');
180 vector<int> dummy;
181
182 os.makeHeaderBntblOnFits(type,Noms, nentries, ncols, &dvl, extname, dummy);
183 float* column = new float[nentries];
184 for (k=0; k<ncols;k++)
185 {
186 int j;
187 for(j = 0; j < nentries; j++) column[j]= dobj_->GetVal(j,k);
188 os.PutColToFits(k, nentries, column);
189 }
190 delete [] column;
191
192}
193
Note: See TracBrowser for help on using the repository browser.