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

Last change on this file since 2322 was 2082, checked in by ansari, 23 years ago

Modifs HEALPixUtils.cc concernant la transformation des fmod() en operateur % et les floor() en div entier, + instanciation ecriture spherehealpix et autres cartes en int_4 (PPF et FITS) - Reza 3/7/2002

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