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

Last change on this file since 2089 was 2084, checked in by ansari, 23 years ago

Ajout des templates d'adaptateur pour cartes en int_4 (LocalMap, SphericalMap) , Reza 3/7/2002

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