source: Sophya/trunk/SophyaExt/FitsIOServer/fitsmanager.cc@ 2906

Last change on this file since 2906 was 2901, checked in by ansari, 20 years ago

Petites modifs + commentaires doc ds fitsmanager.cc - Reza 16/01/2006

  • Property svn:executable set to *
File size: 7.6 KB
Line 
1#include "machdefs.h"
2#include "sopnamsp.h"
3
4#include <stdio.h>
5#include <string.h>
6#include <iostream>
7#include <typeinfo>
8
9#include "fitsmanager.h"
10#include "fitshandler.h"
11
12struct hand_list_el {
13 FitsHandlerInterface * fhi;
14 int glev;
15 string desc;
16};
17
18typedef list<hand_list_el> HandlerList;
19
20static HandlerList * hlistp = NULL;
21
22static inline void ChkHLP()
23{
24 if (hlistp == NULL) hlistp = new HandlerList;
25}
26/*!
27 \param fhi : handler object pointer (created by new) which will be kept in the
28 handler list
29 \param glev : global priority level associated with the handler. Used when different
30 handlers return the same value for CheckHandling() or CheckReadability().
31 \param desc : classe name description associated with the handler
32*/
33int FitsManager::RegisterHandler(FitsHandlerInterface * fhi, int glev, string desc)
34{
35 ChkHLP();
36 if (fhi == NULL)
37 throw NullPtrError("FitsManager::RegisterHandler() fhi=NULL ");
38 HandlerList::iterator it;
39 for(it = hlistp->begin(); it != hlistp->end(); it++) {
40 if (typeid(*((*it).fhi)) == typeid(*fhi))
41 throw DuplicateIdExc("FitsManager::RegisterHandler() Already registered handler");
42 }
43 hand_list_el hle;
44 hle.fhi = fhi;
45 hle.glev = glev;
46 hle.desc = desc;
47 hlistp->push_back(hle);
48 return hlistp->size();
49}
50
51int FitsManager::ListHandlers()
52{
53 ChkHLP();
54 int kk=0;
55 cout << "---- FitsManager::ListHandlers() NbHandlers= " << hlistp->size()
56 << endl;
57 HandlerList::iterator it;
58 for(it = hlistp->begin(); it != hlistp->end(); it++) {
59 kk++;
60 cout << kk << "- " << (*it).desc << " : "
61 << typeid(*((*it).fhi)).name() << " glev= " <<(*it).glev << endl;
62 }
63 return hlistp->size();
64}
65
66/*!
67 Return a NULL pointer if no handler is registered for object \b o .
68 The returned pointer should not be used directly and should NOT
69 be deleted. For read/write operations, the Clone() method should
70 be called on the returned object.
71*/
72FitsHandlerInterface* FitsManager::FindHandler(AnyDataObj & o)
73{
74 ChkHLP();
75 FitsHandlerInterface * fhi = NULL;
76 HandlerList::iterator it;
77 int hfg = 0;
78 int bhfg = 0;
79 int clev = 0;
80 int blev = 0;
81 for(it = hlistp->begin(); it != hlistp->end(); it++) {
82 hfg = (*it).fhi->CheckHandling(o);
83 if (hfg < 1) continue;
84 if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
85 fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
86 }
87 }
88 return fhi ;
89}
90
91/*!
92 Throws an exception ( NotFoundExc ) if the write operation can not be performed.
93*/
94void FitsManager::Write(FitsInOutFile& os, AnyDataObj & o)
95{
96 FitsHandlerInterface * fhi = FindHandler(o);
97 if (fhi == NULL) {
98 string msg = "FitsManager::Write()/FindHandler() Handler not found for ";
99 msg += typeid(o).name();
100 throw NotFoundExc(msg);
101 }
102 FitsHandlerInterface * fhi2 = fhi->Clone();
103 fhi2->SetDataObj(o);
104 fhi2->Write(os);
105 delete fhi2 ;
106 return;
107}
108
109/*!
110 Throws an exception ( NotFoundExc ) if the read operation can not be performed.
111*/
112void FitsManager::Read(FitsInOutFile& is, AnyDataObj & o)
113{
114 FitsHandlerInterface * fhi = FindHandler(o);
115 if (fhi == NULL) {
116 string msg = "FitsManager::Read()/FindHandler() Handler not found for ";
117 msg += typeid(o).name();
118 throw NotFoundExc(msg);
119 }
120 FitsHandlerInterface * fhi2 = fhi->Clone();
121 fhi2->SetDataObj(o);
122 fhi2->Read(is);
123 delete fhi2;
124 return;
125}
126
127/*!
128 Return a NULL pointer if no handler is found capable of reading
129 the current HDU.
130 The returned pointer should not be used directly and should NOT
131 be deleted. For read/write operations, the Clone() method should
132 be called on the returned object.
133*/
134FitsHandlerInterface * FitsManager::FindReader(FitsInOutFile& is)
135{
136 ChkHLP();
137 FitsHandlerInterface * fhi = NULL;
138 HandlerList::iterator it;
139 int hfg = 0;
140 int bhfg = 0;
141 int blev = 0;
142 for(it = hlistp->begin(); it != hlistp->end(); it++) {
143 hfg = (*it).fhi->CheckReadability(is);
144 if (hfg < 1) continue;
145 if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
146 fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
147 }
148 }
149 return fhi;
150}
151
152/*!
153 Throws an exception ( NotFoundExc ) if the read operation can not be performed.
154 In case of success, the calling function is responsible for deleting
155 the returned FitsHandlerInterface object, when the correspondind data object
156 is not needed any more.
157*/
158FitsHandlerInterface * FitsManager::Read(FitsInOutFile& is)
159{
160 FitsHandlerInterface * fhi = FindReader(is);
161 if (fhi == NULL) {
162 string msg = "FitsManager::Read()/FindReader() Reader/Handler not found ";
163 msg += is.FileName();
164 char buff[64];
165 sprintf(buff, " HDU= %d Type= %d", (int)(is.CurrentHDU()),
166 (int)(is.CurrentHDUType()) );
167 msg += buff;
168 throw NotFoundExc(msg);
169 }
170 FitsHandlerInterface * fhi2 = FindReader(is)->Clone();
171 fhi2->Read(is);
172 return fhi2;
173}
174
175/*!
176 \param filename : FITS file name to be scanned
177 \param os : infomation will be sent to formatted stream os
178 \param slev : scan level , bit 0 (1/3) print HDU keywords,
179 bit 2 (2,3) try to read HDU data using the appropraite handler
180 \param Rc : return number of scanned HDU's
181 */
182int FitsManager::ScanFile(string filename, ostream& os, int slev)
183{
184 FitsInOutFile is(filename, FitsInOutFile::Fits_RO);
185 os << "=== FitsManager::ScanFile( " << filename << " ) NbHDUs= "
186 << is.NbHDUs() << endl;
187 int rc = 0;
188 for(int k=0; k<is.NbHDUs(); k++) {
189 os << " ------ HDU No " << is.CurrentHDU() << " Type= "
190 << is.CurrentHDUTypeStr() << endl;
191 int hdutyp = is.CurrentHDUType();
192 if (hdutyp == IMAGE_HDU) {
193 long naxes[5] = {0,0,0,0,0};
194 int naxis=5;
195 int imgtyp = is.GetImageHDUInfo(naxis, naxes);
196 os << ">> IMAGE_HDU: naxis= " << naxis << " : ";
197 for(int i=0; i<naxis; i++) {
198 if (i>0) os << " x " ;
199 os << naxes[i];
200 }
201 os << endl;
202 }
203 else {
204 vector<string> colnames;
205 vector<int> coltypes;
206 vector<long> repcnt;
207 vector<long> width;
208 int ncols = is.GetColInfo(colnames, coltypes, repcnt, width);
209 if (hdutyp == BINARY_TBL) os << ">> BINARY_TBL : NRows= " << is.GetNbRows();
210 else os << ">> ASCII_TBL : NRows= " << is.GetNbRows();
211 os << " x NCols= " << ncols << endl;
212 for(int kk=0; kk<colnames.size(); kk++) {
213 os << "Col[" << kk+1 << "] Name= " << colnames[kk]
214 << " Type= " << FitsTypes::DataTypeToTypeString(coltypes[kk])
215 << " Repeat= " << repcnt[kk]
216 << " W= " << width[kk] << endl;
217 }
218 }
219 // Fin the appropriate handler :
220 ChkHLP();
221 FitsHandlerInterface * fhi = NULL;
222 HandlerList::iterator it;
223 string hdesc;
224 int hfg = 0;
225 int bhfg = 0;
226 int blev = 0;
227 for(it = hlistp->begin(); it != hlistp->end(); it++) {
228 hfg = (*it).fhi->CheckReadability(is);
229 if (hfg < 1) continue;
230 if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
231 fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev; hdesc = (*it).desc;
232 }
233 }
234 if (fhi == NULL)
235 os << ">>> Warning : No handler found for this HDU ... " << endl;
236 else
237 os << ">>> Reader/handler: " << hdesc << " : "
238 << typeid(*fhi).name() << " HandLevel= " << blev << ", " << bhfg << endl;
239 if (fhi && (slev >= 2)) {
240 os << ">>> Trying to read HDU data using the handler ..." << endl;
241 FitsHandlerInterface* fhic = fhi->Clone();
242 fhic->Read(is);
243 os << " FitsHandler.Read() OK " << endl;
244 }
245 if ( (slev == 1) || (slev == 3) ) {
246 os << ">>>> HDU keywords list : " << endl;
247 DVList dvl;
248 is.GetHeaderRecords(dvl);
249 os << dvl;
250 }
251 os << " --------------------- " << endl;
252 is.MoveToNextHDU();
253 rc++;
254 }
255 os << "===================================================" << endl;
256 return rc;
257}
Note: See TracBrowser for help on using the repository browser.