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

Last change on this file since 921 was 873, checked in by ansari, 25 years ago

impressions inutiles

File size: 3.9 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 nbentries = 0;
59 for (int k=0; k<nbcols; k++) nbentries=max( nbentries, fn.NentriesFromFits(k) );
60
61 char ** ColName = new char*[nbcols];
62
63 for (int k=0; k<nbcols;k++)
64 {
65 ColName[k] = new char[LONNOM1];
66 strncpy(ColName[k], fn.ColNameFromFits(k).c_str(),LONNOM);
67 ColName[k][LONNOM] = '\0';
68 }
69 for (int k=0; k<nbcols;k++)
70 {
71 char ss= fn.ColTypeFromFits(k);
72 string type;
73 if (ss != 'E')
74 {
75 if (ss == 'D') type= string("double");
76 else
77 if (ss == 'I') type= string("integer");
78 else
79 if (ss == 'A') type = string("char*");
80 else
81 type = string("unknown");
82 cout << " WARNING: the column " << k << " on fits file is not float but : " << type << endl;
83 }
84 }
85 if(dobj_ == NULL)
86 {
87 dobj_= new NTuple(nbcols,ColName);
88 ownobj= true;
89 }
90 else
91 {
92 dobj_->Clean();
93 (*dobj_) = NTuple(nbcols,ColName);
94 }
95 for (int k=0; k<nbcols;k++)
96 {
97 delete [] ColName[k];
98 }
99 delete [] ColName;
100 if (column_ != NULL) delete [] column_;
101 column_ = new float[nbentries];
102
103 // j'initialise le NTuple a zero, pour le dimensionner
104 // (SetVal suppose que le ntuple est deja dimensionne)
105 r_4* ligne = new r_4[nbcols];
106 for (int k=0; k<nbcols; k++) ligne[k]=0.;
107 for (int k=0; k<nbentries;k++) dobj_->Fill(ligne);
108 delete [] ligne;
109 for (int k=0; k<nbcols;k++)
110 {
111 fn.GetBinTabFCol(column_, nbentries, k);
112 for (int nent=0; nent<nbentries; nent++) dobj_->SetVal(nent,k, column_[nent]);
113 }
114 dobj_->Info()=fn.DVListFromFits();
115}
116void FITS_NTuple::WriteToFits(const FitsFile& fn)
117{
118 if(dobj_ == NULL)
119 {
120 cout << " WriteToFits:: dobj_= null " << endl;
121 return;
122 }
123
124 // table will have 'ncols' columns
125 int ncols = dobj_->NVar();
126
127 // table will have 'nrows' rows
128 int nentries = dobj_->NEntry();
129
130 // get names and values from the join DVList object
131 DVList dvl= dobj_->Info();
132 // extension name
133 char* extname = "NTuple_Binary_tbl";
134 dvl.Print();
135
136 char** Noms = new char*[ncols];
137 for (int k=0; k< ncols; k++)
138 {
139 Noms[k]= new char[LONNOM1];
140 strncpy(Noms[k],dobj_->NomIndex(k),LONNOM1);
141 }
142 // la librairie fitsio ecrit colonne par colonne
143 char* type= new char[ncols+1];
144 for (int k=0;k<ncols+1;k++) type[k]='E';
145 type[ncols]='\0';
146 vector<int> dummy;
147 fn.makeHeaderBntblOnFits(type,Noms, nentries, ncols, dvl, extname, dummy);
148 for (int k=0; k< ncols; k++)
149 {
150 delete [] Noms[k];
151 }
152 delete [] Noms;
153 delete [] type;
154 for (int k=0; k<ncols;k++) putColToFits(k, nentries, getColFromObj(k));
155
156}
157
158float* FITS_NTuple::getColFromObj(int colNr)
159{
160 if (column_ != NULL)
161 {
162 delete [] column_;
163 column_ = NULL;
164 }
165 column_ = new float[dobj_->NEntry()];
166 for(int j = 0; j < dobj_->NEntry(); j++) column_[j]= dobj_->GetVal(j,colNr);
167 return column_;
168}
Note: See TracBrowser for help on using the repository browser.