source: Sophya/trunk/SophyaExt/FitsIOServer/fitsspherehealpix.cc@ 2875

Last change on this file since 2875 was 2874, checked in by ansari, 20 years ago

Portage/compilation sur AIX-XlC (regatta): Ajout namespace SOPHYA/qualification de nom pour instanciation explicite de template - Reza 3 Jan 2006

File size: 7.4 KB
Line 
1#include "sopnamsp.h"
2#include "pexceptions.h"
3#include "fitsspherehealpix.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// SphereHealpix
12////////////////////////////////////////////////////////////////////
13
14/*!
15 \class SOPHYA::FITS_SphereHEALPix
16 \ingroup FitsIOServer
17 FITS format I/O handler for SOPHYA::SphereHEALPix objects.
18*/
19
20
21template <class T>
22FITS_SphereHEALPix<T>::FITS_SphereHEALPix()
23{
24 dobj_= new SphereHEALPix<T>;
25 ownobj_= true;
26}
27
28template <class T>
29FITS_SphereHEALPix<T>::FITS_SphereHEALPix(char inputfile[],int hdunum)
30{
31 dobj_= new SphereHEALPix<T>;
32 ownobj_= true;
33 Read(inputfile,hdunum);
34}
35
36
37template <class T>
38FITS_SphereHEALPix<T>::FITS_SphereHEALPix(const SphereHEALPix<T>& obj)
39{
40 dobj_= new SphereHEALPix<T>(obj);
41 ownobj_= true;
42}
43
44template <class T>
45FITS_SphereHEALPix<T>::FITS_SphereHEALPix(SphereHEALPix<T>* obj)
46{
47 dobj_= obj;
48 ownobj_= false;
49}
50
51template <class T>
52FITS_SphereHEALPix<T>::~FITS_SphereHEALPix()
53{
54 if (ownobj_ && dobj_) delete dobj_;
55}
56
57template <class T>
58AnyDataObj* FITS_SphereHEALPix<T>::DataObj()
59{
60 return(dobj_);
61}
62
63template <class T>
64void FITS_SphereHEALPix<T>::SetDataObj(AnyDataObj & o)
65{
66 SphereHEALPix<T> * po = dynamic_cast< SphereHEALPix<T> * >(&o);
67 if (po == NULL) return;
68 if (ownobj_ && dobj_) delete dobj_;
69 dobj_ = po;
70 ownobj_ = false;
71}
72
73
74
75template <class T>
76void FITS_SphereHEALPix<T>::WriteToFits(FitsOutFile& os)
77{
78 if(dobj_ == NULL)
79 {
80 cout << " WriteToFits:: dobj_= null " << endl;
81 return;
82 }
83
84 DVList dvl( dobj_->Info() );
85
86 SphereCoordSys* cs= dobj_->GetCoordSys();
87 string description= cs->description();
88 dvl["COORDSYS"]= description;
89
90 dvl["PIXTYPE"] = "HEALPIX";
91 dvl.SetComment("PIXTYPE", "HEALPIX Pixelization");
92 dvl["ORDERING"]= dobj_->TypeOfMap();
93 dvl.SetComment("ORDERING", "Pixel ordering scheme, either RING or NESTED");
94
95 int nSide= dobj_->SizeIndex();
96 dvl["NSIDE"]= (int_4) nSide;
97 dvl.SetComment("NSIDE","Resolution parameter for HEALPIX" );
98
99 int nPix= dobj_->NbPixels();
100 dvl["FIRSTPIX"]= (int_4) 0;
101 dvl.SetComment("FIRSTPIX", "First pixel # (0 based)");
102 dvl["LASTPIX"]= (int_4) nPix-1;
103 dvl.SetComment("LASTPIX", "Last pixel # (0 based)");
104 dvl["Content"]= "SphereHEALPix";
105 dvl.SetComment("Content", "name of SOPHYA object");
106
107 // On ecrit les dataBlocks
108 vector<string> Noms;
109 Noms.push_back(dvl.GetS("Content"));
110 string extname("SIMULATION");
111
112 string Type;
113 if (typeid(T) == typeid(r_8) ) Type+='D';
114 else if (typeid(T) == typeid(r_4) ) Type+='E';
115 else if (typeid(T) == typeid(int_4) ) Type+='J';
116 else
117 {
118 cout << "FITS_SphereHEALPix<T>::WriteToFits()- Unsupported data type ("
119 << typeid(T).name() << ") throwing IOExc " << endl;
120 throw IOExc("FITS_SphereHEALPix<T>::WriteToFits() unsupported data type");
121 }
122 vector<int> dummy;
123 os.makeHeaderBntblOnFits(Type, Noms, nPix, 1, &dvl, extname, dummy);
124 os.PutColToFits(0, nPix, dobj_->pixels_.Data());
125}
126
127template <class T>
128void FITS_SphereHEALPix<T>::ReadFromFits(FitsInFile& is)
129{
130 if(dobj_ == NULL)
131 {
132 dobj_= new SphereHEALPix<T>;
133 ownobj_= true;
134 }
135 int nbcols, nbentries;
136 //
137
138 nbcols = is.NbColsFromFits();
139 if (nbcols != 1)
140 {
141 throw IOExc("le fichier fits n'est pas une sphere Healpix");
142 }
143 DVList dvl=is.DVListFromFits();
144 nbentries = is.NentriesFromFits(0);
145 int lastpix=dvl.GetI("LASTPIX");
146 if (lastpix>0)
147 {
148 if (nbentries!=lastpix+1)
149 {
150 nbentries=lastpix+1;
151 }
152 }
153 // Let's Read the SphereCoordSys object
154 int id= SphereCoordSys_NEUTRAL;
155 string description= "NEUTRAL SphereCoordSystem";
156 string coordsys= dvl.GetS("COORDSYS");
157 // $CHECK$ - Reza 2/05/2000 - compare(size_t, size_t,...) passe pas sur g++
158 // if(coordsys.compare(0,7,"ROTATION",0,7) == 0) $CHECK$
159 if(coordsys.substr(0,8) == "ROTATION")
160 {
161 id= SphereCoordSys_ROTATION;
162 description= "ROTATION SphereCoordSystem";
163 }
164 // else if(coordsys.compare(0,4,"OTHER",0,4) == 0) $CHECK$ Reza 2/05/2000
165 else if(coordsys.substr(0,5) == "OTHER" )
166 {
167 id= SphereCoordSys_OTHER;
168 description= "OTHER SphereCoordSystem";
169 }
170 dobj_->SetCoordSys(new SphereCoordSys(id,description));
171
172 string ordering= dvl.GetS("ORDERING");
173 // if(ordering.compare(0,3,"RING",0,3) != 0) $CHECK$ Reza 2/05/2000
174 if(ordering.substr(0,4) != "RING" )
175 {
176 cerr << "FITS_SphereHEALPix/Error Not supported ORDERING= "
177 << ordering << " substr(0,4)=[" << ordering.substr(0,4) << "]" << endl;
178 throw IOExc(" FITS_SphereHEALPix:: numerotation non RING");
179 }
180
181 int nside= dvl.GetI("NSIDE");
182 int nPix = 0;
183 if(nside <= 0)
184 {
185 // throw IOExc("FITS_SphereHEALPix:: No resol parameter nside");
186 if (lastpix>0)
187 {
188 nside = sqrt((double)(nbentries/12));
189 }
190 else
191 {
192 // dvl.Print();
193 // int naxis2 = dvl.GetI("NAXIS2");
194 // if (naxis2 > 0 )
195 // {
196 nside = sqrt((double)(nbentries/12));
197 // }
198 // else
199 // throw IOExc("FITS_SphereHEALPix:: No NSIDE nor LASTPIX nor NAXIS2 on file");
200 }
201 }
202 nPix = 12*nside*nside;
203 if (nPix != nbentries)
204 {
205 cout << "WARNING: le nombre de pixels relu " << nbentries << " est incoherent avec la pixelisation Healpix qui donne nPix= " << nPix << endl;
206 }
207 double Omega= 4.0*Pi/nPix;
208 dobj_->setParameters(nside,nPix,Omega);
209 // On lit les DataBlocks;
210 //
211 dobj_->pixels_.ReSize(nPix);
212 is.GetSingleColumn(dobj_->pixels_.Data(),nPix);
213 //
214
215 // on effectue le decoupage en tranches
216 dobj_->SetThetaSlices();
217 //
218 dobj_->Info() = is.DVListFromFits();
219}
220
221template <class T>
222void FITS_SphereHEALPix<T>::Mollweide_picture_projection(char filename[])
223{
224 int ni = 300;
225 int nj = 600;
226 TMatrix<float> map(ni, nj);
227
228 int i,j;
229 for(i=0; i < ni; i++)
230 {
231 double yd = (i+0.5)/ni-0.5;
232 double facteur=2.*Pi/sin(acos(yd*2));
233 double theta = (0.5-yd)*Pi;
234 for(j=0; j < nj; j++)
235 {
236 double xa = (j+0.5)/nj-0.5;
237 double phi = xa*facteur+Pi;
238 float th=float(theta);
239 float ff=float(phi);
240 if (phi<2*Pi && phi>= 0)
241 {
242 map(i,j) = (float)dobj_->PixValSph(th, ff);
243 }
244 }
245 }
246 FitsOutFile fout(filename);
247 fout.firstImageOnPrimaryHeader(true);
248 FITS_TArray<r_4> fta(map);
249 fta.Write(fout);
250
251}
252template <class T>
253void FITS_SphereHEALPix<T>::sinus_picture_projection(char filename[])
254{
255 int ni = 300;
256 int nj = 600;
257 TMatrix<float> map(ni, nj);
258
259 int i,j;
260 for(i=0; i < ni; i++)
261 {
262 double yd = (i+0.5)/ni-0.5;
263 double theta = (0.5-yd)*Pi;
264 double facteur=1./sin(theta);
265 for(j=0; j < nj; j++)
266 {
267 double xa = (j+0.5)/nj-0.5;
268 double phi = 2.*Pi*xa*facteur+Pi;
269 float th=float(theta);
270 float ff=float(phi);
271 if (phi<2*Pi && phi>= 0)
272 {
273 map(i,j) = (float)dobj_->PixValSph(th, ff);
274 }
275 }
276 }
277 FitsOutFile fout(filename);
278 fout.firstImageOnPrimaryHeader(true);
279 FITS_TArray<r_4> fta(map);
280 fta.Write(fout);
281
282
283}
284
285
286#ifdef __CXX_PRAGMA_TEMPLATES__
287#pragma define_template FITS_SphereHEALPix<r_8>
288#pragma define_template FITS_SphereHEALPix<r_4>
289#pragma define_template FITS_SphereHEALPix<int_4>
290#endif
291#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
292namespace SOPHYA {
293template class FITS_SphereHEALPix<r_8>;
294template class FITS_SphereHEALPix<r_4>;
295template class FITS_SphereHEALPix<int_4>;
296}
297#endif
Note: See TracBrowser for help on using the repository browser.