source: Sophya/trunk/SophyaPI/PIext/nomskymapadapter.cc@ 2613

Last change on this file since 2613 was 2613, checked in by ansari, 21 years ago

MAJ Makefile et piaversion.h, modification NomSkyMapAdapter apres ajout SphereECP<T> - Reza , 7 Septembre 2004

File size: 13.2 KB
Line 
1#include "machdefs.h"
2#include <stdlib.h>
3#include <math.h>
4#include <typeinfo>
5#include <iostream>
6#include <string>
7#include <complex>
8
9#include "datatype.h"
10
11#include "nomskymapadapter.h"
12#include "skymap.h"
13#include "pitvmaad.h"
14#include "complexios.h"
15
16#include "fitsspherehealpix.h"
17#include "fitslocalmap.h"
18
19// Valeur par defaut pour la projection MolleWeide, en dehors de la sphere
20static double _prjmol_defval = 0.;
21void Set_Project_Mol_DefVal(double def)
22{
23 _prjmol_defval = def;
24 return;
25}
26
27// Classe array adapter pour localMap
28template <class T>
29class LocalMapArrAdapter : public P2DArrayAdapter {
30public:
31 LocalMapArrAdapter(LocalMap<T>* lm, bool d=false) :
32 P2DArrayAdapter(lm->SizeX(), lm->SizeY())
33 { ad = d; map = lm; }
34
35 virtual ~LocalMapArrAdapter() { if (ad) delete map; }
36 virtual double Value(int ix, int iy) { return((*map)(ix, iy)); }
37
38protected :
39 bool ad;
40 LocalMap<T>* map;
41};
42
43/* --Methode-- */
44DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
45double LocalMapArrAdapter< complex<float> >::Value(int ix, int iy)
46{
47double re,im;
48re = (*map)(iy, ix).real();
49im = (*map)(iy, ix).imag();
50return(sqrt(re*re+im*im));
51}
52/* --Methode-- */
53DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
54double LocalMapArrAdapter< complex<double> >::Value(int ix, int iy)
55{
56double re,im;
57re = (*map)(iy, ix).real();
58im = (*map)(iy, ix).imag();
59return(sqrt(re*re+im*im));
60}
61
62//----------------------------------------------------------------
63// Class Adaptateur d'objet (Pour NamedObjMgr) d'objet PixelMap<T>
64//----------------------------------------------------------------
65
66
67/* --Methode-- */
68template <class T>
69NOMAdapter_PixelMap<T>::NOMAdapter_PixelMap(PixelMap<T> * o)
70 : NObjMgrAdapter(o)
71{
72mMap = o;
73}
74
75/* --Methode-- */
76template <class T>
77NOMAdapter_PixelMap<T>::~NOMAdapter_PixelMap()
78{
79}
80
81/* --Methode-- */
82template <class T>
83NObjMgrAdapter* NOMAdapter_PixelMap<T>::Clone(AnyDataObj* o)
84{
85PixelMap<T>* m = dynamic_cast<PixelMap<T> *>(o);
86if (m) return ( new NOMAdapter_PixelMap<T>(m) );
87return ( new NObjMgrAdapter(o) );
88}
89
90/* --Methode-- */
91template <class T>
92string NOMAdapter_PixelMap<T>::GetDataObjType()
93{
94string type = "PixelMap< ";
95LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
96if (lm != NULL) type = "LocalMap< ";
97SphereThetaPhi<T>* st = dynamic_cast< SphereThetaPhi<T> * >(mMap);
98if (st != NULL) type = "SphereThetaPhi< ";
99SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
100if (sg != NULL) type = "SphereHEALPix< ";
101SphereECP<T>* se = dynamic_cast< SphereECP<T> * >(mMap);
102if (se != NULL) type = "SphereECP< ";
103
104// type += DecodeTypeIdName(typeid(T).name());
105type += DataTypeInfo<T>::getTypeName();
106type += " > ";
107return(type);
108}
109
110/* --Methode-- */
111template <class T>
112AnyDataObj* NOMAdapter_PixelMap<T>::CloneDataObj(bool share)
113{
114LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
115if (lm != NULL) return( new LocalMap<T>(*lm, share) );
116SphereThetaPhi<T>* st = dynamic_cast< SphereThetaPhi<T> * >(mMap);
117if (st != NULL) return( new SphereThetaPhi<T>(*st, share) );
118SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
119if (sg != NULL) return( new SphereHEALPix<T>(*sg, share) );
120SphereECP<T>* se = dynamic_cast< SphereECP<T> * >(mMap);
121if (se != NULL) return( new SphereECP<T>(*se, share) );
122return(NULL);
123}
124
125/* --Methode-- */
126template <class T>
127void NOMAdapter_PixelMap<T>::ReadFits(string const & flnm)
128{
129LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
130if (lm != NULL) {
131 FitsInFile fis(flnm);
132 fis >> (*lm);
133 return;
134}
135SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
136if (sg != NULL) {
137 FitsInFile fis(flnm);
138 fis >> (*sg);
139 return;
140}
141string s = typeid(*mMap).name();
142cout << " NOMAdapter_PixelMap<T>::ReadFits() - Error "
143 << " Not supported for " << s << endl;
144return;
145}
146
147
148/* --Methode-- */
149template <class T>
150void NOMAdapter_PixelMap<T>::SaveFits(string const & flnm)
151{
152LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
153if (lm != NULL) {
154 FitsOutFile fos(flnm);
155 fos << (*lm);
156 return;
157}
158SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
159if (sg != NULL) {
160 FitsOutFile fos(flnm);
161 fos << (*sg);
162 return;
163}
164string s = typeid(*mMap).name();
165cout << " NOMAdapter_PixelMap<T>::SaveFits() - Error "
166 << " Not supported for " << s << endl;
167return;
168}
169
170// ---- Specialisation pour complexes -----
171DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
172void NOMAdapter_PixelMap< complex<r_4> >::ReadFits(string const & flnm)
173{
174cout << " void NOMAdapter_PixelMap< complex<r_4> >::ReadFits() - Error "
175 << " Not supported (complex data type)" << endl;
176}
177DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
178void NOMAdapter_PixelMap< complex<r_4> >::SaveFits(string const & flnm)
179{
180cout << " void NOMAdapter_PixelMap< complex<r_4> >::SaveFits() - Error "
181 << " Not supported (complex data type)" << endl;
182}
183DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
184void NOMAdapter_PixelMap< complex<r_8> >::ReadFits(string const & flnm)
185{
186cout << " void NOMAdapter_PixelMap< complex<r_4> >::ReadFits() - Error "
187 << " Not supported (complex data type)" << endl;
188}
189DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
190void NOMAdapter_PixelMap< complex<r_8> >::SaveFits(string const & flnm)
191{
192cout << " void NOMAdapter_PixelMap< complex<r_8> >::SaveFits() - Error "
193 << " Not supported (complex data type)" << endl;
194}
195
196
197/* --Methode-- */
198template <class T>
199void NOMAdapter_PixelMap<T>::SavePPF(POutPersist& pos, string const & nom)
200{
201LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
202if (lm != NULL) {
203 FIO_LocalMap<T> fio(lm);
204 fio.Write(pos, nom);
205 return;
206 }
207SphereThetaPhi<T>* st = dynamic_cast< SphereThetaPhi<T> * >(mMap);
208if (st != NULL) {
209 FIO_SphereThetaPhi<T> fio(st);
210 fio.Write(pos, nom);
211 return;
212 }
213SphereHEALPix<T>* sg = dynamic_cast< SphereHEALPix<T> * >(mMap);
214if (sg != NULL) {
215 FIO_SphereHEALPix<T> fio(sg);
216 fio.Write(pos, nom);
217 return;
218 }
219SphereECP<T>* se = dynamic_cast< SphereECP<T> * >(mMap);
220if (se != NULL) {
221 FIO_SphereECP<T> fio(se);
222 fio.Write(pos, nom);
223 return;
224 }
225string s = typeid(*mMap).name();
226cout << "NOMAdapter_PixelMap<T>::SavePPF() - Error : Not supported for " << s << endl;
227}
228
229/* --Methode-- */
230template <class T>
231void NOMAdapter_PixelMap<T>::Print(ostream& os)
232{
233string s = typeid(*mMap).name();
234T moy, sig;
235MeanSig(moy, sig);
236cout << "SkyMap Type: " << s << " NbPixels= " << mMap->NbPixels() << endl;
237cout << " Mean= " << moy << " Sig2= " << sig << endl;
238}
239
240// -- Fonction pour convertir un X,Y en Theta-Phi sur projection MolleWeide --
241char * _SphMollweideProj_XY2ThetaPhi(P2DArrayAdapter * aa, int ix, int iy);
242
243/* --Methode-- */
244template <class T>
245P2DArrayAdapter* NOMAdapter_PixelMap<T>::Get2DArray(string & opt)
246{
247LocalMap<T>* lm = dynamic_cast< LocalMap<T> * >(mMap);
248if (lm != NULL) return(new LocalMapArrAdapter<T>(lm, false));
249int nr = 250;
250int nc = 500;
251SphereECP<T>* se = dynamic_cast< SphereECP<T> * >(mMap);
252size_t olen = opt.length();
253if ((se != NULL) && (opt.find("ecparray") < olen)) {
254 // On veut la carte bi-dim complete de la sphere ECP
255 TMatrix<T> * mtxecp = new TMatrix<T>(se->GetPixelArray(), true); // on partage les donnees
256 POTMatrixAdapter<T> * potmtxecp = new POTMatrixAdapter<T>(mtxecp, true);
257 if (opt.find("ecpscaledeg") < olen) // Echelle angulaire en degre
258 potmtxecp->DefineXYCoordinates(se->MinPhi()*180./M_PI, se->MinTheta()*180./M_PI,
259 se->DeltaPhi()*180./M_PI, se->DeltaTheta()*180./M_PI);
260 else if (opt.find("ecpscalerad") < olen) // Echelle angulaire en radian
261 potmtxecp->DefineXYCoordinates(se->MinPhi(), se->MinTheta(), se->DeltaPhi(), se->DeltaTheta());
262
263 return ( potmtxecp );
264}
265SphericalMap<T>* sm = dynamic_cast< SphericalMap<T> *>(mMap);
266if (sm != NULL) { nr = sqrt(0.75*mMap->NbPixels()); nc = 2*nr; }
267TMatrix<T> * mtx = new TMatrix<T>(nr, nc);
268Project_Mol(*mtx, (T)_prjmol_defval);
269POTMatrixAdapter<T> * potma = new POTMatrixAdapter<T>(mtx, true);
270potma->SetInfoStringFunction(_SphMollweideProj_XY2ThetaPhi);
271return ( potma );
272}
273
274/* --Methode-- */
275template <class T>
276NTupleInterface* NOMAdapter_PixelMap<T>::GetNTupleInterface(bool& adel)
277{
278adel = true;
279return( new NTupInt_PixelMap<T>(mMap) );
280}
281
282/* --Methode-- */
283template <class T>
284void NOMAdapter_PixelMap<T>::MeanSig(T& gmoy, T& gsig)
285{
286 gmoy=0.;
287 gsig = 0.;
288 T valok;
289 for(int k=0; k<mMap->NbPixels(); k++) {
290 valok = (*mMap)(k);
291 gmoy += valok; gsig += valok*valok;
292 }
293 gmoy /= (T)mMap->NbPixels();
294 gsig = gsig/(T)mMap->NbPixels() - gmoy*gmoy;
295
296}
297
298/* --Methode-- */
299template <class T>
300void NOMAdapter_PixelMap<T>::Project_Mol(TMatrix<T> & mtx, T defval)
301{
302 r_8 xa, yd, teta,phi, facteur;
303 int_4 l,c,k;
304 int_4 nl = mtx.NRows();
305 int_4 nc = mtx.NCols();
306 mtx = defval; // On met tout a defval
307// cout << " NRows= " << nl << " NCols= " << nc << endl;
308 for(l=0; l<nl; l++) {
309 yd = (r_8)(l+0.5)/(r_8)nl-0.5;
310 facteur=2.*M_PI/sin(acos((double)yd*2));
311 teta = (yd+0.5)*Pi;
312 // teta = (0.5-yd)*M_PI;
313 for(c=0; c<nc; c++) {
314 xa = (r_8)(c+0.5)/(r_8)nc-0.5;
315 phi = xa*facteur+M_PI;
316 if ( (phi <= 2*M_PI) && (phi >= 0.) ) {
317 k = mMap->PixIndexSph(teta, phi);
318 mtx(l,c) = (*mMap)(k);
319 }
320 }
321 }
322}
323
324static char m_buff[128];
325char * _SphMollweideProj_XY2ThetaPhi(P2DArrayAdapter * aa, int ix, int iy)
326{
327 r_8 xa, yd, teta,phi, facteur;
328 int_4 nl = aa->YSize();
329 int_4 nc = aa->XSize();
330 int l, c;
331 aa->IJ2xy(ix, iy, c, l);
332 yd = (r_8)(l+0.5)/(r_8)nl-0.5;
333 facteur=2.*M_PI/sin(acos((double)yd*2));
334 teta = (yd+0.5)*Pi;
335 xa = (r_8)(c+0.5)/(r_8)nc-0.5;
336 phi = xa*facteur+M_PI;
337 if ( (phi <= 2*M_PI) && (phi >= 0.) )
338 sprintf(m_buff,"(l,b=%5.1f,%5.1f)", phi*180./M_PI, 90.-teta*180./M_PI);
339 else
340 sprintf(m_buff,"(l,b=?,?)");
341
342 return (m_buff);
343}
344
345// -------------------------------------------------------------
346
347/* --Methode-- */
348template <class T>
349NTupInt_PixelMap<T>::NTupInt_PixelMap(PixelMap<T>* m)
350{
351mMap = m;
352}
353
354/* --Methode-- */
355template <class T>
356NTupInt_PixelMap<T>::~NTupInt_PixelMap()
357{
358}
359
360/* --Methode-- */
361template <class T>
362uint_4 NTupInt_PixelMap<T>::NbLines() const
363{
364return( mMap->NbPixels() );
365}
366
367/* --Methode-- */
368template <class T>
369uint_4 NTupInt_PixelMap<T>::NbColumns() const
370{
371return(8);
372}
373
374/* --Methode-- */
375template <class T>
376r_8* NTupInt_PixelMap<T>::GetLineD(int n) const
377{
378int i;
379if ((n < 0) || (n >= (int)(mMap->NbPixels()) ))
380 for(i=0; i<8; i++) mRet[i] = 0.;
381else {
382 double teta,phi;
383 mMap->PixThetaPhi(n, teta, phi);
384 mRet[0] = n; mRet[1] = mMap->PixVal(n);
385 mRet[2] = mRet[1]; mRet[3] = 0.;
386 mRet[4] = mRet[1]; mRet[5] = 0.;
387 mRet[6] = teta; mRet[7] = phi;
388 }
389return(mRet);
390}
391
392/* --Methode-- */
393template <class T>
394string NTupInt_PixelMap<T>::VarList_C(const char* nx) const
395{
396string nomx;
397if (nx) nomx = nx;
398else nomx = "_xh_";
399string vardec = "double i,k,val,real,imag,mod,phas,teta,phi; \n";
400vardec += "i = " + nomx + "[0]; k = " + nomx + "[0]; val = " + nomx + "[1]; \n";
401vardec += "real = " + nomx + "[2]; imag = " + nomx + "[3]; \n";
402vardec += "mod = " + nomx + "[4]; phas = " + nomx + "[5]; \n";
403vardec += "teta = " + nomx + "[6]; phi = " + nomx + "[7]; \n";
404return(vardec);
405}
406
407
408DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
409r_8* NTupInt_PixelMap< complex<float> >::GetLineD(int n) const
410{
411int i;
412if ((n < 0) || (n >= (int)(mMap->NbPixels()) ))
413 for(i=0; i<8; i++) mRet[i] = 0.;
414else {
415 double teta,phi;
416 mMap->PixThetaPhi(n, teta, phi);
417 mRet[0] = n;
418 mRet[2] = mMap->PixVal(n).real(); mRet[3] = mMap->PixVal(n).imag();
419 mRet[1] = mRet[4] = sqrt(mRet[2]*mRet[2]+mRet[3]*mRet[3]);
420 mRet[5] = atan2(mRet[3], mRet[2]);
421 mRet[6] = teta; mRet[7] = phi;
422 }
423return(mRet);
424}
425
426DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
427r_8* NTupInt_PixelMap< complex<double> >::GetLineD(int n) const
428{
429int i;
430if ((n < 0) || (n >= (int)(mMap->NbPixels()) ))
431 for(i=0; i<8; i++) mRet[i] = 0.;
432else {
433 double teta,phi;
434 mMap->PixThetaPhi(n, teta, phi);
435 mRet[0] = n;
436 mRet[2] = mMap->PixVal(n).real(); mRet[3] = mMap->PixVal(n).imag();
437 mRet[1] = mRet[4] = sqrt(mRet[2]*mRet[2]+mRet[3]*mRet[3]);
438 mRet[5] = atan2(mRet[3], mRet[2]);
439 mRet[6] = teta; mRet[7] = phi;
440 }
441return(mRet);
442}
443
444
445#ifdef __CXX_PRAGMA_TEMPLATES__
446#pragma define_template NOMAdapter_PixelMap<int_4>
447#pragma define_template NOMAdapter_PixelMap<r_4>
448#pragma define_template NOMAdapter_PixelMap<r_8>
449#pragma define_template NOMAdapter_PixelMap< complex<float> >
450#pragma define_template NOMAdapter_PixelMap< complex<double> >
451#pragma define_template NTupInt_PixelMap<int_4>
452#pragma define_template NTupInt_PixelMap<r_4>
453#pragma define_template NTupInt_PixelMap<r_8>
454#pragma define_template NTupInt_PixelMap< complex<float> >
455#pragma define_template NTupInt_PixelMap< complex<double> >
456#endif
457
458#if defined(ANSI_TEMPLATES)
459template class NOMAdapter_PixelMap<int_4>;
460template class NOMAdapter_PixelMap<r_4>;
461template class NOMAdapter_PixelMap<r_8>;
462template class NOMAdapter_PixelMap< complex<float> >;
463template class NOMAdapter_PixelMap< complex<double> >;
464template class NTupInt_PixelMap<int_4>;
465template class NTupInt_PixelMap<r_4>;
466template class NTupInt_PixelMap<r_8>;
467template class NTupInt_PixelMap< complex<float> >;
468template class NTupInt_PixelMap< complex<double> >;
469#endif
Note: See TracBrowser for help on using the repository browser.