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

Last change on this file since 1208 was 1194, checked in by ansari, 25 years ago

changement de char* en string

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