| 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 | 
 | 
|---|
| 21 | template <class T>
 | 
|---|
| 22 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix()
 | 
|---|
| 23 | {
 | 
|---|
| 24 |   dobj_= new SphereHEALPix<T>;
 | 
|---|
| 25 |   ownobj_= true;
 | 
|---|
| 26 | }
 | 
|---|
| 27 |   
 | 
|---|
| 28 | template <class T>
 | 
|---|
| 29 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix(char inputfile[],int hdunum)
 | 
|---|
| 30 | {
 | 
|---|
| 31 |     dobj_= new SphereHEALPix<T>;
 | 
|---|
| 32 |     ownobj_= true;
 | 
|---|
| 33 |   Read(inputfile,hdunum);
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | 
 | 
|---|
| 37 | template <class T>
 | 
|---|
| 38 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix(const SphereHEALPix<T>& obj)
 | 
|---|
| 39 | {
 | 
|---|
| 40 |   dobj_= new SphereHEALPix<T>(obj);
 | 
|---|
| 41 |   ownobj_= true;
 | 
|---|
| 42 | }
 | 
|---|
| 43 | 
 | 
|---|
| 44 | template <class T>
 | 
|---|
| 45 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix(SphereHEALPix<T>* obj)
 | 
|---|
| 46 | {
 | 
|---|
| 47 |   dobj_= obj;
 | 
|---|
| 48 |   ownobj_= false;
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | template <class T>
 | 
|---|
| 52 | FITS_SphereHEALPix<T>::~FITS_SphereHEALPix()
 | 
|---|
| 53 | {
 | 
|---|
| 54 |   if (ownobj_ && dobj_) delete dobj_;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | template <class T>
 | 
|---|
| 58 | AnyDataObj* FITS_SphereHEALPix<T>::DataObj()
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   return(dobj_);
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | template <class T>
 | 
|---|
| 64 | void 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 | template <class T>
 | 
|---|
| 74 | int FITS_SphereHEALPix<T>::CheckHandling(AnyDataObj & o)
 | 
|---|
| 75 | {
 | 
|---|
| 76 |   if ( typeid(o) == typeid( SphereHEALPix< T > ) ) return 2;
 | 
|---|
| 77 |   SphereHEALPix<T> * po = dynamic_cast< SphereHEALPix<T> * >(&o);
 | 
|---|
| 78 |   if (po == NULL) return 0;
 | 
|---|
| 79 |   else return 1;
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | template <class T>
 | 
|---|
| 83 | int FITS_SphereHEALPix<T>::CheckReadability(FitsInOutFile& is)
 | 
|---|
| 84 | {
 | 
|---|
| 85 |   if (is.CurrentHDUType() !=  BINARY_TBL ) return 0;
 | 
|---|
| 86 |   string key;
 | 
|---|
| 87 |   key = "PIXTYPE";
 | 
|---|
| 88 |   if ( is.KeyValue(key) != "HEALPIX")  return 0;
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   bool nosk = false;
 | 
|---|
| 91 |   key = "ORDERING";
 | 
|---|
| 92 |   is.KeyValue(key, nosk);
 | 
|---|
| 93 |   bool nosk2 = false;
 | 
|---|
| 94 |   key = "NSIDE";
 | 
|---|
| 95 |   is.KeyValue(key, nosk2);
 | 
|---|
| 96 |   if (nosk || nosk2) return 0;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   vector<string> colnames; 
 | 
|---|
| 99 |   vector<int> coltypes;
 | 
|---|
| 100 |   vector<LONGLONG> repcnt, width;
 | 
|---|
| 101 |   long ncols = is.GetColInfo(colnames, coltypes, repcnt, width); 
 | 
|---|
| 102 |   if (ncols < 1) return 0;
 | 
|---|
| 103 |   T x = 0;
 | 
|---|
| 104 |   if (coltypes[0] == FitsTypes::DataType(x)) return 2 ;
 | 
|---|
| 105 |   else return 1;
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | template <class T>
 | 
|---|
| 109 | FitsHandlerInterface* FITS_SphereHEALPix<T>::Clone()
 | 
|---|
| 110 | {
 | 
|---|
| 111 |   return new FITS_SphereHEALPix<T>() ; 
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | 
 | 
|---|
| 115 | template <class T>
 | 
|---|
| 116 | void FITS_SphereHEALPix<T>::WriteToFits(FitsOutFile& os) 
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   if(dobj_ == NULL) 
 | 
|---|
| 119 |     { 
 | 
|---|
| 120 |       cout << " WriteToFits:: dobj_= null " << endl;
 | 
|---|
| 121 |       return;
 | 
|---|
| 122 |     }
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   DVList dvl( dobj_->Info() );  
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   SphereCoordSys* cs= dobj_->GetCoordSys();
 | 
|---|
| 127 |   string description= cs->description();
 | 
|---|
| 128 |   dvl["COORDSYS"]= description;
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   dvl["PIXTYPE"] = "HEALPIX";
 | 
|---|
| 131 |   dvl.SetComment("PIXTYPE", "HEALPIX Pixelization");
 | 
|---|
| 132 |   dvl["ORDERING"]= dobj_->TypeOfMap();
 | 
|---|
| 133 |   dvl.SetComment("ORDERING", "Pixel ordering scheme, either RING or NESTED");
 | 
|---|
| 134 |   
 | 
|---|
| 135 |   int nSide= dobj_->SizeIndex();
 | 
|---|
| 136 |   dvl["NSIDE"]= (int_4) nSide;
 | 
|---|
| 137 |   dvl.SetComment("NSIDE","Resolution parameter for HEALPIX" );
 | 
|---|
| 138 | 
 | 
|---|
| 139 |   int nPix= dobj_->NbPixels();
 | 
|---|
| 140 |   dvl["FIRSTPIX"]= (int_4) 0;
 | 
|---|
| 141 |   dvl.SetComment("FIRSTPIX", "First pixel # (0 based)");
 | 
|---|
| 142 |   dvl["LASTPIX"]= (int_4) nPix-1;
 | 
|---|
| 143 |   dvl.SetComment("LASTPIX", "Last pixel # (0 based)");
 | 
|---|
| 144 |   dvl["Content"]= "SphereHEALPix";
 | 
|---|
| 145 |   dvl.SetComment("Content", "name of SOPHYA object");
 | 
|---|
| 146 |   dvl["SOPCLSNM"]= "SOPHYA::SphereHEALPix<T>";
 | 
|---|
| 147 |   dvl.SetComment("SOPCLSNM", "SOPHYA class name");
 | 
|---|
| 148 |   
 | 
|---|
| 149 |   // On ecrit les dataBlocks 
 | 
|---|
| 150 |   vector<string> Noms;   
 | 
|---|
| 151 |   Noms.push_back(dvl.GetS("Content"));
 | 
|---|
| 152 |   //  string extname("SIMULATION");
 | 
|---|
| 153 |   string extname = os.NextExtensionName();
 | 
|---|
| 154 | 
 | 
|---|
| 155 |   string Type;
 | 
|---|
| 156 |   if (typeid(T) == typeid(r_8) ) Type+='D';
 | 
|---|
| 157 |   else if (typeid(T) == typeid(r_4) )  Type+='E';
 | 
|---|
| 158 |     else if (typeid(T) == typeid(int_4) )  Type+='J';
 | 
|---|
| 159 |     else
 | 
|---|
| 160 |       {
 | 
|---|
| 161 |         cout <<  "FITS_SphereHEALPix<T>::WriteToFits()- Unsupported data type (" 
 | 
|---|
| 162 |              <<  typeid(T).name() << ") throwing IOExc " << endl;
 | 
|---|
| 163 |         throw IOExc("FITS_SphereHEALPix<T>::WriteToFits() unsupported data type");
 | 
|---|
| 164 |       }
 | 
|---|
| 165 |   vector<int> dummy;
 | 
|---|
| 166 |   os.makeHeaderBntblOnFits(Type, Noms, nPix, 1, &dvl, extname, dummy);
 | 
|---|
| 167 |   os.PutColToFits(0,  nPix, dobj_->pixels_.Data()); 
 | 
|---|
| 168 | } 
 | 
|---|
| 169 | 
 | 
|---|
| 170 | template <class T> 
 | 
|---|
| 171 | void FITS_SphereHEALPix<T>::ReadFromFits(FitsInFile& is)
 | 
|---|
| 172 | {
 | 
|---|
| 173 |   if (is.CurrentHDUType() != BINARY_TBL )
 | 
|---|
| 174 |     throw FitsIOException("FITS_SphereHEALPix<T>::ReadFromFits Not a binary table HDU");
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   if(dobj_ == NULL) {
 | 
|---|
| 177 |     dobj_= new SphereHEALPix<T>;
 | 
|---|
| 178 |     ownobj_= true;      
 | 
|---|
| 179 |   }
 | 
|---|
| 180 |   int nbcols, nbentries;
 | 
|---|
| 181 |   //
 | 
|---|
| 182 | 
 | 
|---|
| 183 |   nbcols = is.NbColsFromFits();
 | 
|---|
| 184 |   if (nbcols != 1) 
 | 
|---|
| 185 |     cout << "FITS_SphereHEALPix<T>::ReadFromFits()/Warning Fits::NbCols="
 | 
|---|
| 186 |          << nbcols << " not equal 1" << endl;
 | 
|---|
| 187 |   
 | 
|---|
| 188 |   DVList dvl=is.DVListFromFits();
 | 
|---|
| 189 |    nbentries = is.NentriesFromFits(0);
 | 
|---|
| 190 |   int lastpix=dvl.GetI("LASTPIX");
 | 
|---|
| 191 |   if (lastpix>0)
 | 
|---|
| 192 |     {
 | 
|---|
| 193 |       if (nbentries!=lastpix+1) 
 | 
|---|
| 194 |         {
 | 
|---|
| 195 |           nbentries=lastpix+1;
 | 
|---|
| 196 |         }
 | 
|---|
| 197 |     }
 | 
|---|
| 198 |   // Let's Read the SphereCoordSys object
 | 
|---|
| 199 |   int id= SphereCoordSys::NEUTRAL;
 | 
|---|
| 200 |   string description= "NEUTRAL SphereCoordSystem";
 | 
|---|
| 201 |   string coordsys= dvl.GetS("COORDSYS");
 | 
|---|
| 202 |   // $CHECK$ - Reza 2/05/2000 - compare(size_t, size_t,...) passe pas sur g++     
 | 
|---|
| 203 |   //  if(coordsys.compare(0,7,"ROTATION",0,7) == 0)  $CHECK$ 
 | 
|---|
| 204 |   if(coordsys.substr(0,8) == "ROTATION")
 | 
|---|
| 205 |     {
 | 
|---|
| 206 |       id= SphereCoordSys::ROTATION;
 | 
|---|
| 207 |       description= "ROTATION SphereCoordSystem";
 | 
|---|
| 208 |     } 
 | 
|---|
| 209 |   //  else if(coordsys.compare(0,4,"OTHER",0,4) == 0) $CHECK$ Reza 2/05/2000 
 | 
|---|
| 210 |   else if(coordsys.substr(0,5)  == "OTHER" )
 | 
|---|
| 211 |     {
 | 
|---|
| 212 |       id= SphereCoordSys::OTHER;
 | 
|---|
| 213 |       description= "OTHER SphereCoordSystem";
 | 
|---|
| 214 |     }
 | 
|---|
| 215 |   dobj_->SetCoordSys(new SphereCoordSys(id,description));   
 | 
|---|
| 216 | 
 | 
|---|
| 217 |   string ordering= dvl.GetS("ORDERING");
 | 
|---|
| 218 |   if ( (ordering.substr(0,4) != "RING" ) && (ordering.substr(0,6) != "NESTED" ) )  { 
 | 
|---|
| 219 |     cerr << "FITS_SphereHEALPix/Error Not supported ORDERING= " << ordering 
 | 
|---|
| 220 |          << " != RING/NESTED " << endl; 
 | 
|---|
| 221 |     throw FitsIOException("FITS_SphereHEALPix/Error ordering != RING/NESTED ");
 | 
|---|
| 222 |   }
 | 
|---|
| 223 | 
 | 
|---|
| 224 |   bool fgring = true;
 | 
|---|
| 225 |   if (ordering.substr(0,4) != "RING" )  fgring = false;
 | 
|---|
| 226 | 
 | 
|---|
| 227 |   int nside= dvl.GetI("NSIDE");
 | 
|---|
| 228 |   int nPix = 0;
 | 
|---|
| 229 |   if(nside <= 0)
 | 
|---|
| 230 |     {
 | 
|---|
| 231 |       //      throw IOExc("FITS_SphereHEALPix:: No resol parameter nside");
 | 
|---|
| 232 |       if (lastpix>0)
 | 
|---|
| 233 |         {
 | 
|---|
| 234 |           nside = sqrt((double)(nbentries/12));
 | 
|---|
| 235 |         }
 | 
|---|
| 236 |       else
 | 
|---|
| 237 |         {
 | 
|---|
| 238 |           //      dvl.Print();
 | 
|---|
| 239 |           // int naxis2 = dvl.GetI("NAXIS2");
 | 
|---|
| 240 |           // if (naxis2 > 0 ) 
 | 
|---|
| 241 |           //  {
 | 
|---|
| 242 |           nside = sqrt((double)(nbentries/12));
 | 
|---|
| 243 |               // }
 | 
|---|
| 244 |               // else
 | 
|---|
| 245 |               //  throw IOExc("FITS_SphereHEALPix:: No NSIDE nor LASTPIX nor NAXIS2 on file");
 | 
|---|
| 246 |         }
 | 
|---|
| 247 |     }
 | 
|---|
| 248 |   nPix = 12*nside*nside;
 | 
|---|
| 249 |   if (nPix != nbentries)
 | 
|---|
| 250 |     {
 | 
|---|
| 251 |       cout << "WARNING: le nombre de pixels relu " << nbentries << " est incoherent avec la pixelisation Healpix qui donne nPix= " << nPix << endl; 
 | 
|---|
| 252 |       }
 | 
|---|
| 253 |   double Omega= 4.0*Pi/nPix;
 | 
|---|
| 254 |   dobj_->setParameters(nside,nPix,Omega,fgring);
 | 
|---|
| 255 |   // On lit les DataBlocks;
 | 
|---|
| 256 |   //
 | 
|---|
| 257 |    dobj_->pixels_.ReSize(nPix);
 | 
|---|
| 258 |   is.GetSingleColumn(dobj_->pixels_.Data(),nPix);
 | 
|---|
| 259 |   //
 | 
|---|
| 260 | 
 | 
|---|
| 261 |   // on effectue le decoupage en tranches 
 | 
|---|
| 262 |   dobj_->SetThetaSlices();
 | 
|---|
| 263 |   //
 | 
|---|
| 264 |   dobj_->Info()  = is.DVListFromFits();
 | 
|---|
| 265 | }
 | 
|---|
| 266 | 
 | 
|---|
| 267 | template <class T>
 | 
|---|
| 268 | void FITS_SphereHEALPix<T>::Mollweide_picture_projection(char filename[])
 | 
|---|
| 269 | {
 | 
|---|
| 270 |   int ni = 300;
 | 
|---|
| 271 |   int nj = 600;
 | 
|---|
| 272 |   TMatrix<float> map(ni, nj);
 | 
|---|
| 273 | 
 | 
|---|
| 274 |    int i,j;
 | 
|---|
| 275 |    for(i=0; i < ni; i++) 
 | 
|---|
| 276 |      {
 | 
|---|
| 277 |        double yd = (i+0.5)/ni-0.5;
 | 
|---|
| 278 |        double facteur=2.*Pi/sin(acos(yd*2));
 | 
|---|
| 279 |        double theta = (0.5-yd)*Pi;
 | 
|---|
| 280 |        for(j=0; j < nj; j++)  
 | 
|---|
| 281 |          {
 | 
|---|
| 282 |            double xa = (j+0.5)/nj-0.5;
 | 
|---|
| 283 |            double phi =  xa*facteur+Pi;
 | 
|---|
| 284 |            float th=float(theta);
 | 
|---|
| 285 |            float ff=float(phi);
 | 
|---|
| 286 |            if (phi<2*Pi && phi>= 0) 
 | 
|---|
| 287 |              {
 | 
|---|
| 288 |                map(i,j) = (float)dobj_->PixValSph(th, ff);
 | 
|---|
| 289 |              }
 | 
|---|
| 290 |          }
 | 
|---|
| 291 |      }
 | 
|---|
| 292 |    FitsOutFile  fout(filename);
 | 
|---|
| 293 |    fout.firstImageOnPrimaryHeader(true);
 | 
|---|
| 294 |    FITS_TArray<r_4> fta(map);
 | 
|---|
| 295 |    fta.Write(fout);
 | 
|---|
| 296 |    
 | 
|---|
| 297 | }
 | 
|---|
| 298 | template <class T>
 | 
|---|
| 299 | void FITS_SphereHEALPix<T>::sinus_picture_projection(char filename[])
 | 
|---|
| 300 | {
 | 
|---|
| 301 |   int ni = 300;
 | 
|---|
| 302 |   int nj = 600;
 | 
|---|
| 303 |   TMatrix<float> map(ni, nj);
 | 
|---|
| 304 | 
 | 
|---|
| 305 |   int i,j;
 | 
|---|
| 306 |    for(i=0; i < ni; i++) 
 | 
|---|
| 307 |      {
 | 
|---|
| 308 |        double yd = (i+0.5)/ni-0.5;
 | 
|---|
| 309 |        double theta = (0.5-yd)*Pi;
 | 
|---|
| 310 |         double facteur=1./sin(theta);
 | 
|---|
| 311 |        for(j=0; j < nj; j++)  
 | 
|---|
| 312 |          {
 | 
|---|
| 313 |            double xa = (j+0.5)/nj-0.5;
 | 
|---|
| 314 |            double phi = 2.*Pi*xa*facteur+Pi;
 | 
|---|
| 315 |            float th=float(theta);
 | 
|---|
| 316 |            float ff=float(phi);
 | 
|---|
| 317 |            if (phi<2*Pi && phi>= 0) 
 | 
|---|
| 318 |              {
 | 
|---|
| 319 |                map(i,j) = (float)dobj_->PixValSph(th, ff);
 | 
|---|
| 320 |              }
 | 
|---|
| 321 |          }
 | 
|---|
| 322 |      }
 | 
|---|
| 323 |    FitsOutFile  fout(filename);
 | 
|---|
| 324 |    fout.firstImageOnPrimaryHeader(true);
 | 
|---|
| 325 |    FITS_TArray<r_4> fta(map);
 | 
|---|
| 326 |    fta.Write(fout);
 | 
|---|
| 327 | 
 | 
|---|
| 328 |    
 | 
|---|
| 329 | }
 | 
|---|
| 330 | 
 | 
|---|
| 331 | 
 | 
|---|
| 332 | #ifdef __CXX_PRAGMA_TEMPLATES__
 | 
|---|
| 333 | #pragma define_template FITS_SphereHEALPix<r_8>
 | 
|---|
| 334 | #pragma define_template FITS_SphereHEALPix<r_4>
 | 
|---|
| 335 | #pragma define_template FITS_SphereHEALPix<int_4>
 | 
|---|
| 336 | #endif
 | 
|---|
| 337 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
 | 
|---|
| 338 | namespace SOPHYA {
 | 
|---|
| 339 | template class FITS_SphereHEALPix<r_8>;
 | 
|---|
| 340 | template class FITS_SphereHEALPix<r_4>;
 | 
|---|
| 341 | template class FITS_SphereHEALPix<int_4>;
 | 
|---|
| 342 | }
 | 
|---|
| 343 | #endif
 | 
|---|