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

Last change on this file since 1344 was 1334, checked in by ansari, 25 years ago

modifs returns de autoreader

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