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

Last change on this file since 863 was 860, checked in by ansari, 25 years ago

I/O sur fichier fits

File size: 4.4 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 column_ = NULL;
18 ownobj=true;
19}
20
21FITS_NTuple::FITS_NTuple(char inputfile[],int hdunum)
22{
23 dobj_ = new NTuple;
24 column_ = NULL;
25 ownobj=true;
26
27 ReadF(inputfile,hdunum);
28}
29
30
31FITS_NTuple::FITS_NTuple(const NTuple & obj)
32{
33 dobj_ = new NTuple(obj);
34 column_ = NULL;
35 ownobj=true;
36}
37
38
39FITS_NTuple::~FITS_NTuple()
40{
41 if (ownobj && dobj_ != NULL) delete dobj_;
42 if (column_ != NULL) delete [] column_;
43}
44
45void FITS_NTuple::Write(char outputfile[], int hdunum)
46{
47 WriteF(outputfile, hdunum);
48}
49
50void FITS_NTuple::ReadFromFits(const FitsFile& fn)
51{
52 if (!fn.IsFitsTable())
53 {
54 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
55 }
56 int nbcols, nbentries;
57 nbcols = fn.NbColsFromFits();
58 cout << " nbcols= " << nbcols << endl;
59 nbentries = 0;
60 for (int k=0; k<nbcols; k++) nbentries=max( nbentries, fn.NentriesFromFits(k) );
61
62 char ** ColName = new char*[nbcols];
63
64 for (int k=0; k<nbcols;k++)
65 {
66 ColName[k] = new char[LONNOM1];
67 strncpy(ColName[k], fn.ColNameFromFits(k).c_str(),LONNOM);
68 ColName[k][LONNOM] = '\0';
69 }
70 for (int k=0; k<nbcols;k++)
71 {
72 char ss= fn.ColTypeFromFits(k);
73 string type;
74 if (ss != 'E')
75 {
76 if (ss == 'D') type= string("double");
77 else
78 if (ss == 'I') type= string("integer");
79 else
80 if (ss == 'A') type = string("char*");
81 else
82 type = string("unknown");
83 cout << " WARNING: the column " << k << " on fits file is not float but : " << type << endl;
84 }
85 }
86 if(dobj_ == NULL)
87 {
88 dobj_= new NTuple(nbcols,ColName);
89 ownobj= true;
90 }
91 else
92 {
93 dobj_->Clean();
94 (*dobj_) = NTuple(nbcols,ColName);
95 }
96 for (int k=0; k<nbcols;k++)
97 {
98 delete [] ColName[k];
99 }
100 delete [] ColName;
101 cout << " preparation du tabeau column" << endl;
102 if (column_ != NULL) delete [] column_;
103 column_ = new float[nbentries];
104
105 // j'initialise le NTuple a zero, pour le dimensionner
106 // (SetVal suppose que le ntuple est deja dimensionne)
107 cout << " nbcol= " << nbcols << endl;
108 r_4* ligne = new r_4[nbcols];
109 for (int k=0; k<nbcols; k++) ligne[k]=0.;
110 for (int k=0; k<nbentries;k++) dobj_->Fill(ligne);
111 cout << " ntuple initialise a zero" << endl;
112 delete [] ligne;
113 cout << "debut boucle nbcol= " << nbcols << endl;
114 for (int k=0; k<nbcols;k++)
115 {
116 cout << " avant lecture col. no " << k << endl;
117 fn.GetBinTabFCol(column_, nbentries, k);
118 cout << " lecture effectuee " << endl;
119 for (int nent=0; nent<nbentries; nent++) dobj_->SetVal(nent,k, column_[nent]);
120 cout << " valeurs inserre dans ntuple " << endl;
121 }
122 dobj_->Info()=fn.DVListFromFits();
123}
124void FITS_NTuple::WriteToFits(const FitsFile& fn)
125{
126 if(dobj_ == NULL)
127 {
128 cout << " WriteToFits:: dobj_= null " << endl;
129 return;
130 }
131
132 // table will have 'ncols' columns
133 int ncols = dobj_->NVar();
134
135 // table will have 'nrows' rows
136 int nentries = dobj_->NEntry();
137
138 // get names and values from the join DVList object
139 DVList dvl= dobj_->Info();
140 // extension name
141 char* extname = "NTuple_Binary_tbl";
142 dvl.Print();
143
144 char** Noms = new char*[ncols];
145 for (int k=0; k< ncols; k++)
146 {
147 Noms[k]= new char[LONNOM1];
148 strncpy(Noms[k],dobj_->NomIndex(k),LONNOM1);
149 cout << " nom entre dans ntuple: " << Noms[k] << endl;
150 }
151 // la librairie fitsio ecrit colonne par colonne
152 char* type= new char[ncols+1];
153 for (int k=0;k<ncols+1;k++) type[k]='E';
154 type[ncols]='\0';
155 vector<int> dummy;
156 fn.makeHeaderBntblOnFits(type,Noms, nentries, ncols, dvl, extname, dummy);
157 for (int k=0; k< ncols; k++)
158 {
159 delete [] Noms[k];
160 }
161 delete [] Noms;
162 delete [] type;
163 for (int k=0; k<ncols;k++) putColToFits(k, nentries, getColFromObj(k));
164
165 cout << " fin d'ecriture "<< endl;
166}
167
168float* FITS_NTuple::getColFromObj(int colNr)
169{
170 if (column_ != NULL)
171 {
172 delete [] column_;
173 column_ = NULL;
174 }
175 column_ = new float[dobj_->NEntry()];
176 for(int j = 0; j < dobj_->NEntry(); j++) column_[j]= dobj_->GetVal(j,colNr);
177 return column_;
178}
Note: See TracBrowser for help on using the repository browser.