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

Last change on this file since 2669 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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