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

Last change on this file since 1673 was 1371, checked in by ansari, 25 years ago

MAJ documentation, Makefile, ... - Reza 5/1/2001

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