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

Last change on this file since 1353 was 1353, checked in by lemeur, 25 years ago

fonction de manipulation directe de headers

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