1 | #include "sopnamsp.h"
|
---|
2 | #include "pexceptions.h"
|
---|
3 | #include "fitsspherethetaphi.h"
|
---|
4 | #include "tarray.h"
|
---|
5 | #include "fitstarray.h"
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 | ///////////////////////////////////////////////////////////////////////
|
---|
10 | // ----objets delegues pour la gestion de persistance I/O format fits--
|
---|
11 | // SphereThetaPhi
|
---|
12 | ////////////////////////////////////////////////////////////////////
|
---|
13 |
|
---|
14 | /*!
|
---|
15 | \class SOPHYA::FITS_SphereThetaPhi
|
---|
16 | \ingroup FitsIOServer
|
---|
17 | FITS format I/O handler for SOPHYA::SphereThetaPhi objects.
|
---|
18 | */
|
---|
19 |
|
---|
20 | template <class T>
|
---|
21 | FITS_SphereThetaPhi<T>::FITS_SphereThetaPhi()
|
---|
22 | {
|
---|
23 | dobj_= new SphereThetaPhi<T>;
|
---|
24 | ownobj_= true;
|
---|
25 | }
|
---|
26 |
|
---|
27 | template <class T>
|
---|
28 | FITS_SphereThetaPhi<T>::FITS_SphereThetaPhi(const char inputfile[],int hdunum)
|
---|
29 | {
|
---|
30 | dobj_= new SphereThetaPhi<T>;
|
---|
31 | ownobj_= true;
|
---|
32 | Read(inputfile,hdunum);
|
---|
33 | }
|
---|
34 |
|
---|
35 | template <class T>
|
---|
36 | FITS_SphereThetaPhi<T>::FITS_SphereThetaPhi(const SphereThetaPhi<T>& obj)
|
---|
37 | {
|
---|
38 | dobj_= new SphereThetaPhi<T>(obj);
|
---|
39 | ownobj_= true;
|
---|
40 | }
|
---|
41 |
|
---|
42 | template <class T>
|
---|
43 | FITS_SphereThetaPhi<T>::FITS_SphereThetaPhi(SphereThetaPhi<T>* obj)
|
---|
44 | {
|
---|
45 | dobj_= obj;
|
---|
46 | ownobj_= false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | template <class T>
|
---|
50 | FITS_SphereThetaPhi<T>::~FITS_SphereThetaPhi()
|
---|
51 | {
|
---|
52 | if (ownobj_ && dobj_) delete dobj_;
|
---|
53 | }
|
---|
54 |
|
---|
55 | template <class T>
|
---|
56 | AnyDataObj* FITS_SphereThetaPhi<T>::DataObj()
|
---|
57 | {
|
---|
58 | return(dobj_);
|
---|
59 | }
|
---|
60 |
|
---|
61 | template <class T>
|
---|
62 | void FITS_SphereThetaPhi<T>::SetDataObj(AnyDataObj & o)
|
---|
63 | {
|
---|
64 | SphereThetaPhi<T> * po = dynamic_cast< SphereThetaPhi<T> * >(&o);
|
---|
65 | if (po == NULL) return;
|
---|
66 | if (ownobj_ && dobj_) delete dobj_;
|
---|
67 | dobj_ = po;
|
---|
68 | ownobj_ = false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | template <class T>
|
---|
72 | int FITS_SphereThetaPhi<T>::CheckHandling(AnyDataObj & o)
|
---|
73 | {
|
---|
74 | if ( typeid(o) == typeid( SphereThetaPhi< T > ) ) return 2;
|
---|
75 | SphereThetaPhi<T> * po = dynamic_cast< SphereThetaPhi<T> * >(&o);
|
---|
76 | if (po == NULL) return 0;
|
---|
77 | else return 1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | template <class T>
|
---|
81 | int FITS_SphereThetaPhi<T>::CheckReadability(FitsInOutFile& is)
|
---|
82 | {
|
---|
83 | if (is.CurrentHDUType() != BINARY_TBL ) return 0;
|
---|
84 | string key;
|
---|
85 | key = "PIXTYPE";
|
---|
86 | if ( is.KeyValue(key) != "TETAFI") return 0;
|
---|
87 | key = "Content";
|
---|
88 | if ( is.KeyValue(key) != "SphereThetaPhi") return 0;
|
---|
89 |
|
---|
90 | vector<string> colnames;
|
---|
91 | vector<int> coltypes;
|
---|
92 | vector<LONGLONG> repcnt, width;
|
---|
93 | long ncols = is.GetColInfo(colnames, coltypes, repcnt, width);
|
---|
94 | if (ncols < 1) return 0;
|
---|
95 | T x = 0;
|
---|
96 | if (coltypes[0] == FitsTypes::DataType(x)) return 2 ;
|
---|
97 | else return 1;
|
---|
98 | }
|
---|
99 |
|
---|
100 | template <class T>
|
---|
101 | FitsHandlerInterface* FITS_SphereThetaPhi<T>::Clone()
|
---|
102 | {
|
---|
103 | return new FITS_SphereThetaPhi<T>() ;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | template <class T>
|
---|
108 | void FITS_SphereThetaPhi<T>::WriteToFits(FitsOutFile& os)
|
---|
109 | {
|
---|
110 | if(dobj_ == NULL)
|
---|
111 | {
|
---|
112 | cout << " WriteToFits:: dobj_= null " << endl;
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | DVList dvl( dobj_->Info() );
|
---|
117 |
|
---|
118 | SphereCoordSys* cs= dobj_->GetCoordSys();
|
---|
119 | string description= cs->description();
|
---|
120 | dvl["COORDSYS"]= description;
|
---|
121 |
|
---|
122 | dvl["PIXTYPE"] = "TETAFI";
|
---|
123 | dvl.SetComment("PIXTYPE", "Theta-Phi-LAL Pixelization");
|
---|
124 | dvl["ORDERING"]= dobj_->TypeOfMap();
|
---|
125 | dvl.SetComment("ORDERING", " (unrelevant) ");
|
---|
126 |
|
---|
127 | int IndTheta= dobj_->SizeIndex();
|
---|
128 | dvl["INDTHMAX"]= (int_4) IndTheta;
|
---|
129 | dvl.SetComment("INDTHMAX","Resolution parameter for Theta-Phi pixelization sch." );
|
---|
130 |
|
---|
131 | int nPix= dobj_->NbPixels();
|
---|
132 | dvl["FIRSTPIX"]= (int_4) 0;
|
---|
133 | dvl.SetComment("FIRSTPIX", "First pixel # (0 based)");
|
---|
134 | dvl["LASTPIX"]= (int_4) nPix-1;
|
---|
135 | dvl.SetComment("LASTPIX", "Last pixel # (0 based)");
|
---|
136 | dvl["Content"]= "SphereThetaPhi";
|
---|
137 | dvl.SetComment("Content", "name of SOPHYA object");
|
---|
138 | dvl["SOPCLSNM"]= "SOPHYA::SphereThetaPhi<T>";
|
---|
139 | dvl.SetComment("SOPCLSNM", "SOPHYA class name");
|
---|
140 |
|
---|
141 | // On ecrit les dataBlocks
|
---|
142 |
|
---|
143 | // the BINTABLE consits of 4 columns
|
---|
144 | // first column : pixels values (type : T ), length : number of pixels
|
---|
145 | // second column : number of pixels of each theta slice (type : integer) ,
|
---|
146 | // length : number of theta-slices (parameter INDTHMAX above +1)
|
---|
147 | // third column : cumulated number of pixels until the beginning of each
|
---|
148 | // theta slice (type : integer) , same length as the previous one
|
---|
149 | // fourth column : theta values of each slice (type : double_) same length
|
---|
150 | // as the previous one.
|
---|
151 | vector<string> Noms(4);
|
---|
152 | Noms[0] = dvl.GetS("Content");
|
---|
153 | Noms[1] = "NphiParBande";
|
---|
154 | Noms[2] = "CumulPixParBande";
|
---|
155 | Noms[3] = "ThetaBande";
|
---|
156 | //string extname("SIMULATION");
|
---|
157 | string extname = os.NextExtensionName();
|
---|
158 |
|
---|
159 | string Type;
|
---|
160 | if (typeid(T) == typeid(r_8) ) Type+='D';
|
---|
161 | else if (typeid(T) == typeid(r_4) ) Type+='E';
|
---|
162 | else if (typeid(T) == typeid(int_4) ) Type+='J';
|
---|
163 | else
|
---|
164 | {
|
---|
165 | cout << "FITS_SphereThetaPhi<T>::WriteToFits - Unsupported data type ("
|
---|
166 | << typeid(T).name() << ") throwing IOExc " << endl;
|
---|
167 | throw IOExc("FITS_SphereThetaPhi<T>::WriteToFits() unsupported data type");
|
---|
168 | }
|
---|
169 |
|
---|
170 | Type += 'J';
|
---|
171 | Type += 'J';
|
---|
172 | Type += 'D';
|
---|
173 |
|
---|
174 | vector<int> dummy;
|
---|
175 | os.makeHeaderBntblOnFits(Type, Noms, nPix, 4, &dvl, extname, dummy);
|
---|
176 | os.PutColToFits(0, nPix, dobj_->pixels_.Data());
|
---|
177 | os.PutColToFits(1, IndTheta+1, dobj_->NPhi_.Data());
|
---|
178 | os.PutColToFits(2, IndTheta+1, dobj_->TNphi_.Data());
|
---|
179 | os.PutColToFits(3, IndTheta+1, dobj_->Theta_.Data());
|
---|
180 | }
|
---|
181 |
|
---|
182 | template <class T>
|
---|
183 | void FITS_SphereThetaPhi<T>::ReadFromFits(FitsInFile& is)
|
---|
184 | {
|
---|
185 |
|
---|
186 | if(dobj_ == NULL)
|
---|
187 | {
|
---|
188 | dobj_= new SphereThetaPhi<T>;
|
---|
189 | ownobj_= true;
|
---|
190 | }
|
---|
191 | int nbcols, nbentries;
|
---|
192 |
|
---|
193 | nbcols = is.NbColsFromFits();
|
---|
194 | if (nbcols != 4)
|
---|
195 | {
|
---|
196 | throw IOExc("le fichier fits n'est pas une sphere ThetaPhi");
|
---|
197 | }
|
---|
198 | DVList dvl=is.DVListFromFits();
|
---|
199 | nbentries = is.NentriesFromFits(0);
|
---|
200 | int lastpix=dvl.GetI("LASTPIX");
|
---|
201 | if (lastpix>0)
|
---|
202 | {
|
---|
203 | if (nbentries!=lastpix+1)
|
---|
204 | {
|
---|
205 | nbentries=lastpix+1;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | // Let's Read the SphereCoordSys object
|
---|
210 | int id= SphereCoordSys::NEUTRAL;
|
---|
211 | string description= "NEUTRAL SphereCoordSystem";
|
---|
212 | string coordsys= dvl.GetS("COORDSYS");
|
---|
213 | if(coordsys.substr(0,8) == "ROTATION")
|
---|
214 | {
|
---|
215 | id= SphereCoordSys::ROTATION;
|
---|
216 | description= "ROTATION SphereCoordSystem";
|
---|
217 | }
|
---|
218 | else if(coordsys.substr(0,5) == "OTHER" )
|
---|
219 | {
|
---|
220 | id= SphereCoordSys::OTHER;
|
---|
221 | description= "OTHER SphereCoordSystem";
|
---|
222 | }
|
---|
223 | dobj_->SetCoordSys(new SphereCoordSys(id,description));
|
---|
224 |
|
---|
225 | int IndThetaMax= dvl.GetI("INDTHMAX");
|
---|
226 | int nPix = 0;
|
---|
227 | if(IndThetaMax <= 0)
|
---|
228 | {
|
---|
229 | throw IOExc("FITS_SphereTheta:: No resol parameter INDTHMAX");
|
---|
230 | }
|
---|
231 | nPix = nbentries;
|
---|
232 | double Omega= 4.0*Pi/nPix;
|
---|
233 | dobj_->setParameters(IndThetaMax,nPix,Omega);
|
---|
234 |
|
---|
235 | // On lit les DataBlocks;
|
---|
236 | //
|
---|
237 | // the BINTABLE consits of 4 columns
|
---|
238 | // first column : pixels values (type : T ), length : number of pixels
|
---|
239 | // second column : number of pixels of each theta slice (type : integer) ,
|
---|
240 | // length : number of theta-slices (parameter INDTHMAX above +1)
|
---|
241 | // third column : cumulated number of pixels until the beginning of each
|
---|
242 | // theta slice (type : integer) , same length as the previous one
|
---|
243 | // fourth column : theta values of each slice (type : double_) same length
|
---|
244 | // as the previous one.
|
---|
245 |
|
---|
246 | dobj_->pixels_.ReSize(nPix);
|
---|
247 | is.GetBinTabFCol(dobj_->pixels_.Data(),nPix,0);
|
---|
248 | dobj_->NPhi_.ReSize(IndThetaMax+1);
|
---|
249 | is.GetBinTabFCol(dobj_->NPhi_.Data(),IndThetaMax+1,1);
|
---|
250 | dobj_->TNphi_.ReSize(IndThetaMax+1);
|
---|
251 | is.GetBinTabFCol(dobj_->TNphi_.Data(),IndThetaMax+1,2);
|
---|
252 | dobj_->Theta_.ReSize(IndThetaMax+1);
|
---|
253 | is.GetBinTabFCol(dobj_->Theta_.Data(),IndThetaMax+1,3);
|
---|
254 | }
|
---|
255 |
|
---|
256 | template <class T>
|
---|
257 | void FITS_SphereThetaPhi<T>::Mollweide_picture_projection(const char filename[])
|
---|
258 | {
|
---|
259 | int ni = 300;
|
---|
260 | int nj = 600;
|
---|
261 | TMatrix<float> map(ni, nj);
|
---|
262 |
|
---|
263 | int i,j;
|
---|
264 | for(i=0; i < ni; i++)
|
---|
265 | {
|
---|
266 | double yd = (i+0.5)/ni-0.5;
|
---|
267 | double facteur=2.*Pi/sin(acos(yd*2));
|
---|
268 | double theta = (0.5-yd)*Pi;
|
---|
269 | for(j=0; j < nj; j++)
|
---|
270 | {
|
---|
271 | double xa = (j+0.5)/nj-0.5;
|
---|
272 | double phi = xa*facteur+Pi;
|
---|
273 | float th=float(theta);
|
---|
274 | float ff=float(phi);
|
---|
275 | if (phi<2*Pi && phi>= 0)
|
---|
276 | {
|
---|
277 | map(i,j) = (float)dobj_->PixValSph(th, ff);
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | FitsOutFile fout(filename);
|
---|
282 | fout.firstImageOnPrimaryHeader(true);
|
---|
283 | FITS_TArray<r_4> fta(map);
|
---|
284 | fta.Write(fout);
|
---|
285 |
|
---|
286 | }
|
---|
287 | template <class T>
|
---|
288 | void FITS_SphereThetaPhi<T>::sinus_picture_projection(const char filename[])
|
---|
289 | {
|
---|
290 | int ni = 300;
|
---|
291 | int nj = 600;
|
---|
292 | TMatrix<float> map(ni, nj);
|
---|
293 |
|
---|
294 | int i,j;
|
---|
295 | for(i=0; i < ni; i++)
|
---|
296 | {
|
---|
297 | double yd = (i+0.5)/ni-0.5;
|
---|
298 | double theta = (0.5-yd)*Pi;
|
---|
299 | double facteur=1./sin(theta);
|
---|
300 | for(j=0; j < nj; j++)
|
---|
301 | {
|
---|
302 | double xa = (j+0.5)/nj-0.5;
|
---|
303 | double phi = 2.*Pi*xa*facteur+Pi;
|
---|
304 | float th=float(theta);
|
---|
305 | float ff=float(phi);
|
---|
306 | if (phi<2*Pi && phi>= 0)
|
---|
307 | {
|
---|
308 | map(i,j) = (float)dobj_->PixValSph(th, ff);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 | FitsOutFile fout(filename);
|
---|
313 | fout.firstImageOnPrimaryHeader(true);
|
---|
314 | FITS_TArray<r_4> fta(map);
|
---|
315 | fta.Write(fout);
|
---|
316 |
|
---|
317 |
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
322 | #pragma define_template FITS_SphereThetaPhi<r_8>
|
---|
323 | #pragma define_template FITS_SphereThetaPhi<r_4>
|
---|
324 | #pragma define_template FITS_SphereThetaPhi<int_4>
|
---|
325 | #endif
|
---|
326 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
327 | namespace SOPHYA {
|
---|
328 | template class FITS_SphereThetaPhi<r_8>;
|
---|
329 | template class FITS_SphereThetaPhi<r_4>;
|
---|
330 | template class FITS_SphereThetaPhi<int_4>;
|
---|
331 | }
|
---|
332 | #endif
|
---|