| 1 | // Pour boucher les trous d'une sphere HEALPIX en la clusterisant
 | 
|---|
| 2 | // dans une sphere plus petite puis en la re-extrapolant.
 | 
|---|
| 3 | //                    cmv 13/6/01
 | 
|---|
| 4 | // extrapsph -r 2 sph143k05.fits sph143k05_e.fits
 | 
|---|
| 5 | //                sphout.fits sphoutw.fits sphred.fits sphredw.fits
 | 
|---|
| 6 | #include "machdefs.h"
 | 
|---|
| 7 | #include <unistd.h>
 | 
|---|
| 8 | #include <stdexcept>
 | 
|---|
| 9 | #include <iostream>
 | 
|---|
| 10 | #include <stdio.h>
 | 
|---|
| 11 | #include <stdlib.h>
 | 
|---|
| 12 | #include "skymapinit.h"
 | 
|---|
| 13 | #include "skymap.h"
 | 
|---|
| 14 | #include "fitsspherehealpix.h" 
 | 
|---|
| 15 | 
 | 
|---|
| 16 | /*!
 | 
|---|
| 17 |   \ingroup PrgMap
 | 
|---|
| 18 |   \file extrapsph.cc
 | 
|---|
| 19 |   \brief \b extrapsph: Fill holes in a sphere of datas by reducing
 | 
|---|
| 20 |   it size and re-extrapolating.
 | 
|---|
| 21 |   \verbatim
 | 
|---|
| 22 | csh> extrapsph -h
 | 
|---|
| 23 | extrapsph [-r reduc_factor] sphin.fits sphinw.fits
 | 
|---|
| 24 |           sphout.fits [sphout_w.fits sphred.fits sphred_w.fits]
 | 
|---|
| 25 |  -r reduc : reduction factor for clustering (must be 2^n, def=2)
 | 
|---|
| 26 |  sphin.fits    : input sphere
 | 
|---|
| 27 |  sphin_w.fits  : input sphere filling weight
 | 
|---|
| 28 |  sphout.fits   : output sphere
 | 
|---|
| 29 |  sphout_w.fits : output sphere filling weight
 | 
|---|
| 30 |  sphred.fits   : output reducted sphere
 | 
|---|
| 31 |  sphred_w.fits : output reducted sphere filling weight
 | 
|---|
| 32 |   \endverbatim
 | 
|---|
| 33 | */
 | 
|---|
| 34 | 
 | 
|---|
| 35 | void usage();
 | 
|---|
| 36 | void usage()
 | 
|---|
| 37 | {
 | 
|---|
| 38 | cout<<"extrapsph [-r reduc_factor] sphin.fits sphinw.fits"<<endl
 | 
|---|
| 39 |     <<"          sphout.fits [sphout_w.fits"
 | 
|---|
| 40 |     <<" sphred.fits sphred_w.fits]"<<endl
 | 
|---|
| 41 |     <<" -r reduc : reduction factor for clustering (must be 2^n, def=2)"<<endl
 | 
|---|
| 42 |     <<" sphin.fits    : input sphere"<<endl
 | 
|---|
| 43 |     <<" sphin_w.fits  : input sphere filling weight"<<endl
 | 
|---|
| 44 |     <<" sphout.fits   : output sphere"<<endl
 | 
|---|
| 45 |     <<" sphout_w.fits : output sphere filling weight"<<endl
 | 
|---|
| 46 |     <<" sphred.fits   : output reducted sphere"<<endl
 | 
|---|
| 47 |     <<" sphred_w.fits : output reducted sphere filling weight"<<endl;
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | int main(int narg, char* arg[])
 | 
|---|
| 51 | {
 | 
|---|
| 52 | int red=2;
 | 
|---|
| 53 | string dum;
 | 
|---|
| 54 | int c;
 | 
|---|
| 55 | while((c = getopt(narg,arg,"hr:")) != -1) {
 | 
|---|
| 56 |   switch (c) {
 | 
|---|
| 57 |   case 'r' :
 | 
|---|
| 58 |     sscanf(optarg,"%d",&red);
 | 
|---|
| 59 |     break;
 | 
|---|
| 60 |   case 'h' :
 | 
|---|
| 61 |   default:
 | 
|---|
| 62 |     usage(); exit(1);
 | 
|---|
| 63 |   }
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | if(optind+2>=narg) {usage(); exit(1);}
 | 
|---|
| 67 | char * fsphin   = arg[optind];
 | 
|---|
| 68 | char * fsphinw  = arg[optind+1];
 | 
|---|
| 69 | char * fsphout  = arg[optind+2];
 | 
|---|
| 70 | char * fsphoutw = NULL;
 | 
|---|
| 71 | char * fsphred = NULL;
 | 
|---|
| 72 | char * fsphredw = NULL;
 | 
|---|
| 73 | if(optind+3<narg) fsphoutw = arg[optind+3];
 | 
|---|
| 74 | if(optind+4<narg) fsphred = arg[optind+4];
 | 
|---|
| 75 | if(optind+5<narg) fsphredw = arg[optind+5];
 | 
|---|
| 76 | 
 | 
|---|
| 77 | cout<<"Input Sphere : "<<fsphin<<endl
 | 
|---|
| 78 |     <<"Input Weight Sphere : "<<fsphinw<<endl
 | 
|---|
| 79 |     <<"Output Sphere : "<<fsphout<<endl
 | 
|---|
| 80 |     <<"Output Weight Sphere : "<<fsphoutw<<endl
 | 
|---|
| 81 |     <<"Output Reducted Sphere : "<<fsphred<<endl
 | 
|---|
| 82 |     <<"Output Reducted Weight Sphere : "<<fsphredw<<endl
 | 
|---|
| 83 |     <<"Reduction : "<<red<<endl;
 | 
|---|
| 84 | 
 | 
|---|
| 85 | // Lecture des spheres
 | 
|---|
| 86 | SphereHEALPix<r_8> sphin;
 | 
|---|
| 87 | {FitsInFile sfits(fsphin); sfits >> sphin;}
 | 
|---|
| 88 | int nlat = sphin.SizeIndex();
 | 
|---|
| 89 | int nlatr = nlat/red;
 | 
|---|
| 90 | cout<<"Opening Input Sphere :"<<endl
 | 
|---|
| 91 |     <<"          Type of map : "<<sphin.TypeOfMap()<<endl
 | 
|---|
| 92 |     <<"     Number of pixels : "<<sphin.NbPixels()<<endl
 | 
|---|
| 93 |     <<"                 Nlat : "<<sphin.SizeIndex()<<endl;
 | 
|---|
| 94 | 
 | 
|---|
| 95 | SphereHEALPix<r_8> sphinw;
 | 
|---|
| 96 | {FitsInFile sfits(fsphinw); sfits >> sphinw;}
 | 
|---|
| 97 | cout<<"Opening Input Weight Sphere :"<<endl
 | 
|---|
| 98 |     <<"          Type of map : "<<sphinw.TypeOfMap()<<endl
 | 
|---|
| 99 |     <<"     Number of pixels : "<<sphinw.NbPixels()<<endl
 | 
|---|
| 100 |     <<"                 Nlat : "<<sphinw.SizeIndex()<<endl;
 | 
|---|
| 101 | if(sphinw.SizeIndex()!=nlat) {
 | 
|---|
| 102 |   cout<<"Incompatible sphin sphinw"<<endl;
 | 
|---|
| 103 |   exit(2);
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | // Creation des spheres reduites
 | 
|---|
| 107 | cout<<"Creating Reducted Spheres : nlatr = "<<nlatr<<endl;
 | 
|---|
| 108 | SphereHEALPix<r_8> sphred(nlatr);
 | 
|---|
| 109 |   sphred.SetPixels(0.);
 | 
|---|
| 110 | SphereHEALPix<r_8> sphredw(nlatr);
 | 
|---|
| 111 |   sphredw.SetPixels(0.);
 | 
|---|
| 112 | cout<<"     Number of pixels : "<<sphred.NbPixels()<<endl
 | 
|---|
| 113 |     <<"                 Nlat : "<<sphred.SizeIndex()<<endl;
 | 
|---|
| 114 | 
 | 
|---|
| 115 | // Filling reducted spheres
 | 
|---|
| 116 | cout<<"...Filling Reducted Spheres"<<endl;
 | 
|---|
| 117 | uint_4 n=0;
 | 
|---|
| 118 | int_4 i, ir;
 | 
|---|
| 119 | for(i=0;i<sphin.NbPixels();i++) {
 | 
|---|
| 120 |   if(sphinw(i)<=0.) continue;
 | 
|---|
| 121 |   double theta,phi;
 | 
|---|
| 122 |   sphin.PixThetaPhi(i,theta,phi);
 | 
|---|
| 123 |   ir = sphred.PixIndexSph(theta,phi);
 | 
|---|
| 124 |   sphred(ir)  += sphin(i)*sphinw(i);
 | 
|---|
| 125 |   sphredw(ir) += sphinw(i);
 | 
|---|
| 126 |   n++;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | cout<<"      Input Sphere: Number of filled pixels "<<n<<endl;
 | 
|---|
| 129 | cout<<"...Computing Reducted Spheres"<<endl;
 | 
|---|
| 130 | n=0;
 | 
|---|
| 131 | for(ir=0;ir<sphred.NbPixels();ir++)
 | 
|---|
| 132 |   if(sphredw(ir) > 0.) {sphred(ir) /= sphredw(ir); n++;}
 | 
|---|
| 133 | cout<<"      Reducted Sphere: Number of pixels filled "<<n<<endl;
 | 
|---|
| 134 | 
 | 
|---|
| 135 | // Filling hole for Output Spheres
 | 
|---|
| 136 | cout<<"...Filling hole for Output Spheres"<<endl;
 | 
|---|
| 137 | n=0;
 | 
|---|
| 138 | for(i=0;i<sphin.NbPixels();i++) {
 | 
|---|
| 139 |   if(sphinw(i)>0.) continue;
 | 
|---|
| 140 |   double theta,phi;
 | 
|---|
| 141 |   sphin.PixThetaPhi(i,theta,phi);
 | 
|---|
| 142 |   int_4 ir = sphred.PixIndexSph(theta,phi);
 | 
|---|
| 143 |   if(sphredw(ir)<=0.) continue;
 | 
|---|
| 144 |   sphin(i)  = sphred(ir);
 | 
|---|
| 145 |   // On passe en negatif les pixels qui ont ete extrapoles
 | 
|---|
| 146 |   sphinw(i) = -sphredw(ir);
 | 
|---|
| 147 |   n++;
 | 
|---|
| 148 | }
 | 
|---|
| 149 | cout<<"      Output Sphere: Number of pixels filled "<<n<<endl;
 | 
|---|
| 150 | 
 | 
|---|
| 151 | // Ecriture des spheres
 | 
|---|
| 152 | {
 | 
|---|
| 153 | dum = "rm -f "; dum += fsphout; system(dum.c_str());
 | 
|---|
| 154 | FitsOutFile sfits(fsphout, FitsFile::clear);
 | 
|---|
| 155 | cout<<"Writing Output Sphere Fits file"<<endl;
 | 
|---|
| 156 | sfits<<sphin;
 | 
|---|
| 157 | }
 | 
|---|
| 158 | if(fsphoutw) {
 | 
|---|
| 159 | dum = "rm -f "; dum += fsphoutw; system(dum.c_str());
 | 
|---|
| 160 | FitsOutFile sfits(fsphoutw, FitsFile::clear);
 | 
|---|
| 161 | cout<<"Writing Output Sphere Weight Fits file"<<endl;
 | 
|---|
| 162 | sfits<<sphinw;
 | 
|---|
| 163 | }
 | 
|---|
| 164 | if(fsphred) {
 | 
|---|
| 165 | dum = "rm -f "; dum += fsphred; system(dum.c_str());
 | 
|---|
| 166 | FitsOutFile sfits(fsphred, FitsFile::clear);
 | 
|---|
| 167 | cout<<"Writing Reducted Sphere Fits file"<<endl;
 | 
|---|
| 168 | sfits<<sphred;
 | 
|---|
| 169 | }
 | 
|---|
| 170 | if(fsphredw) {
 | 
|---|
| 171 | dum = "rm -f "; dum += fsphredw; system(dum.c_str());
 | 
|---|
| 172 | FitsOutFile sfits(fsphredw, FitsFile::clear);
 | 
|---|
| 173 | cout<<"Writing Reducted Sphere Weight Fits file"<<endl;
 | 
|---|
| 174 | sfits<<sphredw;
 | 
|---|
| 175 | }
 | 
|---|
| 176 | 
 | 
|---|
| 177 | exit(0);
 | 
|---|
| 178 | }
 | 
|---|