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