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 |
|
---|
12 | struct hand_list_el {
|
---|
13 | FitsHandlerInterface * fhi;
|
---|
14 | int glev;
|
---|
15 | string desc;
|
---|
16 | };
|
---|
17 |
|
---|
18 | typedef list<hand_list_el> HandlerList;
|
---|
19 |
|
---|
20 | static HandlerList * hlistp = NULL;
|
---|
21 |
|
---|
22 | static 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 | */
|
---|
33 | int 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 |
|
---|
51 | int 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 | FitsHandlerInterface* FitsManager::FindHandler(AnyDataObj & o)
|
---|
67 | {
|
---|
68 | ChkHLP();
|
---|
69 | FitsHandlerInterface * fhi = NULL;
|
---|
70 | HandlerList::iterator it;
|
---|
71 | int hfg = 0;
|
---|
72 | int bhfg = 0;
|
---|
73 | int clev = 0;
|
---|
74 | int blev = 0;
|
---|
75 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
76 | hfg = (*it).fhi->CheckHandling(o);
|
---|
77 | if (hfg < 1) continue;
|
---|
78 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
79 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | if (fhi == NULL) {
|
---|
83 | string msg = "FitsManager::FindHandler() Handler not found for ";
|
---|
84 | msg += typeid(o).name();
|
---|
85 | throw NotFoundExc(msg);
|
---|
86 | }
|
---|
87 | else return fhi;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void FitsManager::Write(FitsInOutFile& os, AnyDataObj & o)
|
---|
91 | {
|
---|
92 | FitsHandlerInterface * fhi2 = FindHandler(o)->Clone();
|
---|
93 | fhi2->SetDataObj(o);
|
---|
94 | fhi2->Write(os);
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void FitsManager::Read(FitsInOutFile& is, AnyDataObj & o)
|
---|
99 | {
|
---|
100 | FitsHandlerInterface * fhi2 = FindHandler(o)->Clone();
|
---|
101 | fhi2->SetDataObj(o);
|
---|
102 | fhi2->Read(is);
|
---|
103 | delete fhi2;
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | FitsHandlerInterface * FitsManager::FindReader(FitsInOutFile& is)
|
---|
108 | {
|
---|
109 | ChkHLP();
|
---|
110 | FitsHandlerInterface * fhi = NULL;
|
---|
111 | HandlerList::iterator it;
|
---|
112 | int hfg = 0;
|
---|
113 | int bhfg = 0;
|
---|
114 | int blev = 0;
|
---|
115 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
116 | hfg = (*it).fhi->CheckReadability(is);
|
---|
117 | if (hfg < 1) continue;
|
---|
118 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
119 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | if (fhi == NULL) {
|
---|
123 | string msg = "FitsManager::FindReader() Reader/Handler not found ";
|
---|
124 | msg += is.FileName();
|
---|
125 | char buff[64];
|
---|
126 | sprintf(buff, " HDU= %d Type= %d", (int)(is.CurrentHDU()),
|
---|
127 | (int)(is.CurrentHDUType()) );
|
---|
128 | msg += buff;
|
---|
129 | throw NotFoundExc(msg);
|
---|
130 | }
|
---|
131 | else return fhi;
|
---|
132 | }
|
---|
133 |
|
---|
134 | FitsHandlerInterface * FitsManager::Read(FitsInOutFile& is)
|
---|
135 | {
|
---|
136 | FitsHandlerInterface * fhi2 = FindReader(is)->Clone();
|
---|
137 | fhi2->Read(is);
|
---|
138 | return fhi2;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*!
|
---|
142 | \param filename : FITS file name to be scanned
|
---|
143 | \param os : infomation will be sent to formatted stream os
|
---|
144 | \param slev : scan level , bit 0 (1/3) print HDU keywords,
|
---|
145 | bit 2 (2,3) try to read HDU data using the appropraite handler
|
---|
146 | \param Rc : return number of scanned HDU's
|
---|
147 | */
|
---|
148 | int FitsManager::ScanFile(string filename, ostream& os, int slev)
|
---|
149 | {
|
---|
150 | FitsInOutFile is(filename, FitsInOutFile::Fits_RO);
|
---|
151 | os << "=== FitsManager::ScanFile( " << filename << " ) NbHDUs= "
|
---|
152 | << is.NbHDUs() << endl;
|
---|
153 | int rc = 0;
|
---|
154 | for(int k=0; k<is.NbHDUs(); k++) {
|
---|
155 | os << " ------ HDU No " << is.CurrentHDU() << " Type= "
|
---|
156 | << is.CurrentHDUTypeStr() << endl;
|
---|
157 | int hdutyp = is.CurrentHDUType();
|
---|
158 | if (hdutyp == IMAGE_HDU) {
|
---|
159 | long naxes[5] = {0,0,0,0,0};
|
---|
160 | int naxis=5;
|
---|
161 | int imgtyp = is.GetImageHDUInfo(naxis, naxes);
|
---|
162 | os << ">> IMAGE_HDU: naxis= " << naxis << " : ";
|
---|
163 | for(int i=0; i<naxis; i++) {
|
---|
164 | if (i>0) os << " x " ;
|
---|
165 | os << naxes[i];
|
---|
166 | }
|
---|
167 | os << endl;
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | vector<string> colnames;
|
---|
171 | vector<int> coltypes;
|
---|
172 | vector<long> repcnt;
|
---|
173 | vector<long> width;
|
---|
174 | int ncols = is.GetColInfo(colnames, coltypes, repcnt, width);
|
---|
175 | if (hdutyp == BINARY_TBL) os << ">> BINARY_TBL : NRows= " << is.GetNbRows();
|
---|
176 | else os << ">> ASCII_TBL : NRows= " << is.GetNbRows();
|
---|
177 | os << " x NCols= " << ncols << endl;
|
---|
178 | for(int kk=0; kk<colnames.size(); kk++) {
|
---|
179 | os << "Col[" << kk+1 << "] Name= " << colnames[kk]
|
---|
180 | << " Type= " << FitsTypes::DataTypeToTypeString(coltypes[kk])
|
---|
181 | << " Repeat= " << repcnt[kk]
|
---|
182 | << " W= " << width[kk] << endl;
|
---|
183 | }
|
---|
184 | }
|
---|
185 | // Fin the appropriate handler :
|
---|
186 | ChkHLP();
|
---|
187 | FitsHandlerInterface * fhi = NULL;
|
---|
188 | HandlerList::iterator it;
|
---|
189 | string hdesc;
|
---|
190 | int hfg = 0;
|
---|
191 | int bhfg = 0;
|
---|
192 | int blev = 0;
|
---|
193 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
194 | hfg = (*it).fhi->CheckReadability(is);
|
---|
195 | if (hfg < 1) continue;
|
---|
196 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
197 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev; hdesc = (*it).desc;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | if (fhi == NULL)
|
---|
201 | os << ">>> Warning : No handler found for this HDU ... " << endl;
|
---|
202 | else
|
---|
203 | os << ">>> Reader/handler: " << hdesc << " : "
|
---|
204 | << typeid(*fhi).name() << " HandLevel= " << blev << ", " << bhfg << endl;
|
---|
205 | if (fhi && (slev >= 2)) {
|
---|
206 | os << ">>> Trying to read HDU data using the handler ..." << endl;
|
---|
207 | FitsHandlerInterface* fhic = fhi->Clone();
|
---|
208 | fhic->Read(is);
|
---|
209 | os << " FitsHandler.Read() OK " << endl;
|
---|
210 | }
|
---|
211 | if ( (slev == 1) || (slev == 3) ) {
|
---|
212 | os << ">>>> HDU keywords list : " << endl;
|
---|
213 | DVList dvl;
|
---|
214 | is.GetHeaderRecords(dvl);
|
---|
215 | os << dvl;
|
---|
216 | }
|
---|
217 | os << " --------------------- " << endl;
|
---|
218 | is.MoveToNextHDU();
|
---|
219 | rc++;
|
---|
220 | }
|
---|
221 | os << "===================================================" << endl;
|
---|
222 | return rc;
|
---|
223 | }
|
---|