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

Last change on this file since 2975 was 2975, checked in by ansari, 19 years ago

Ajout flag int lev ds les adaptateurs NObjMgrAdapter::Print(ostream& os, int lev=0) et NamedObjMgr::Print() + modif commande print - Reza 20 Juin 2006

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