| 1 | // Creation d'une sphere de masquee a partir d'une sphere de masques | 
|---|
| 2 | //         cmv 15/6/01 | 
|---|
| 3 | #include "sopnamsp.h" | 
|---|
| 4 | #include "machdefs.h" | 
|---|
| 5 | #include <unistd.h> | 
|---|
| 6 | #include <stdexcept> | 
|---|
| 7 | #include <iostream> | 
|---|
| 8 | #include <stdio.h> | 
|---|
| 9 | #include <stdlib.h> | 
|---|
| 10 | #include "skymapinit.h" | 
|---|
| 11 | #include "skymap.h" | 
|---|
| 12 | #include "fitsspherehealpix.h" | 
|---|
| 13 |  | 
|---|
| 14 | /*! | 
|---|
| 15 | \ingroup PrgMap | 
|---|
| 16 | \file msksphere.cc | 
|---|
| 17 | \brief \b msksphere: mask a sphere of datas with a mask sphere. | 
|---|
| 18 | \verbatim | 
|---|
| 19 | csh> msksphere -h | 
|---|
| 20 | msksphere [-m min -M max -v valmsk] sphval.fits sphmask.fits sphout.fits | 
|---|
| 21 | sphval.fits : inpout sphere of datas where the pixels have to be masked | 
|---|
| 22 | sphmask.fits : inpout masked sphere used to mask pixels | 
|---|
| 23 | sphout.fits : output masked sphere of datas | 
|---|
| 24 | -m min : min mask_sphere_value for MASKING the sphere pixel | 
|---|
| 25 | -M max : max mask_sphere_value for MASKING the sphere pixel | 
|---|
| 26 | condition for MASKING sphere pixel is (min<=mask_sphere_value<=max) | 
|---|
| 27 | -n : negate the previous condition, condition for MASKING sphere pixel | 
|---|
| 28 | becomes: (mask_sphere_value<minval || maxval<mask_sphere_value) | 
|---|
| 29 | Default (no -m and -M): mask pixel if mask_sphere_value<0. | 
|---|
| 30 | -v valmsk : set sphere value for masked pixels (def=0.) | 
|---|
| 31 | \endverbatim | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | void usage(); | 
|---|
| 35 | void usage() | 
|---|
| 36 | { | 
|---|
| 37 | cout<<"msksphere [-m min -M max -v valmsk]" | 
|---|
| 38 | <<" sphval.fits sphmask.fits sphout.fits"<<endl | 
|---|
| 39 | <<" sphval.fits : inpout sphere of datas where the pixels have to be masked"<<endl | 
|---|
| 40 | <<" sphmask.fits : inpout masked sphere used to mask pixels"<<endl | 
|---|
| 41 | <<" sphout.fits : output masked sphere of datas"<<endl | 
|---|
| 42 | <<" -m min : min mask_sphere_value for MASKING the sphere pixel"<<endl | 
|---|
| 43 | <<" -M max : max mask_sphere_value for MASKING the sphere pixel"<<endl | 
|---|
| 44 | <<"    condition for MASKING sphere pixel is (min<=mask_sphere_value<=max)"<<endl | 
|---|
| 45 | <<" -n : negate the previous condition, condition for MASKING sphere pixel"<<endl | 
|---|
| 46 | <<"      becomes: (mask_sphere_value<minval || maxval<mask_sphere_value)"<<endl | 
|---|
| 47 | <<" Default (no -m and -M): mask pixel if mask_sphere_value<0."<<endl | 
|---|
| 48 | <<" -v valmsk : set sphere value for masked pixels (def=0.)"<<endl; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | int main(int narg, char* arg[]) | 
|---|
| 52 | { | 
|---|
| 53 | double vmask=0.,vmin=-1.e30,vmax=1.e30; | 
|---|
| 54 | bool tstmin=false,tstmax=false,negate=false; | 
|---|
| 55 | string dum; | 
|---|
| 56 | int c; | 
|---|
| 57 | while((c = getopt(narg,arg,"hnm:M:v:")) != -1) { | 
|---|
| 58 | switch (c) { | 
|---|
| 59 | case 'n' : | 
|---|
| 60 | negate = true; | 
|---|
| 61 | break; | 
|---|
| 62 | case 'm' : | 
|---|
| 63 | sscanf(optarg,"%lf",&vmin); | 
|---|
| 64 | tstmin = true; | 
|---|
| 65 | break; | 
|---|
| 66 | case 'M' : | 
|---|
| 67 | sscanf(optarg,"%lf",&vmax); | 
|---|
| 68 | tstmax = true; | 
|---|
| 69 | break; | 
|---|
| 70 | case 'v' : | 
|---|
| 71 | sscanf(optarg,"%lf",&vmask); | 
|---|
| 72 | break; | 
|---|
| 73 | case 'h' : | 
|---|
| 74 | default: | 
|---|
| 75 | usage(); exit(1); | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | if(optind+2>=narg) {usage(); exit(1);} | 
|---|
| 80 | char * sphval = arg[optind]; | 
|---|
| 81 | char * sphmsk = arg[optind+1]; | 
|---|
| 82 | char * sphout = arg[optind+2]; | 
|---|
| 83 |  | 
|---|
| 84 | if(!tstmin && !tstmax) {tstmin=true; vmin=0.; negate=true;} | 
|---|
| 85 |  | 
|---|
| 86 | if(negate) dum = ".NOT."; else dum=""; | 
|---|
| 87 | cout<<"Sphere values : "<<sphval<<endl | 
|---|
| 88 | <<"Sphere mask   : "<<sphmsk<<endl | 
|---|
| 89 | <<"Sphere out    : "<<sphout<<endl | 
|---|
| 90 | <<"  ...min("<<tstmin<<") "<<vmin<<endl | 
|---|
| 91 | <<"     max("<<tstmax<<") "<<vmax<<endl | 
|---|
| 92 | <<"  ...mask set value "<<vmask<<endl | 
|---|
| 93 | <<"  Condition for masking pixel is :"<<endl | 
|---|
| 94 | <<"    "<<dum<<"( "<<vmin<<" <= V <= "<<vmax<<" )"<<endl; | 
|---|
| 95 |  | 
|---|
| 96 | // Lecture de la sphere Healpix des valeurs | 
|---|
| 97 | SphereHEALPix<r_8> sph; | 
|---|
| 98 | {FitsInFile sfits(sphval); sfits >> sph;} | 
|---|
| 99 | cout<<"Opening Sphere HEALPix for testing values :"<<endl | 
|---|
| 100 | <<"          Type of map : "<<sph.TypeOfMap()<<endl | 
|---|
| 101 | <<"     Number of pixels : "<<sph.NbPixels()<<endl | 
|---|
| 102 | <<"                 Nlat : "<<sph.SizeIndex()<<endl; | 
|---|
| 103 |  | 
|---|
| 104 | // Lecture de la sphere Healpix des masques | 
|---|
| 105 | SphereHEALPix<r_8> msph; | 
|---|
| 106 | {FitsInFile sfits(sphmsk); sfits >> msph;} | 
|---|
| 107 | cout<<"Opening Sphere HEALPix for mask :"<<endl | 
|---|
| 108 | <<"          Type of map : "<<msph.TypeOfMap()<<endl | 
|---|
| 109 | <<"     Number of pixels : "<<msph.NbPixels()<<endl | 
|---|
| 110 | <<"                 Nlat : "<<msph.SizeIndex()<<endl; | 
|---|
| 111 | if(msph.SizeIndex()!=sph.SizeIndex()) { | 
|---|
| 112 | cout<<"Sphere of values and sphere of mask must have same NLat"<<endl; | 
|---|
| 113 | exit(1); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | // Filling Mask Sphere | 
|---|
| 117 | cout<<"Testing For Masked Pixels"<<endl; | 
|---|
| 118 | uint_4 nmask = 0; | 
|---|
| 119 | for(int_4 i=0;i<sph.NbPixels();i++) { | 
|---|
| 120 |  | 
|---|
| 121 | bool skp = (tstmin || tstmax) ? negate : false; | 
|---|
| 122 | if((tstmin && msph(i)<vmin) || (tstmax && msph(i)>vmax)) skp = !negate; | 
|---|
| 123 | if(skp) continue;   // Do nothing | 
|---|
| 124 |  | 
|---|
| 125 | sph(i) = vmask; | 
|---|
| 126 | nmask++; | 
|---|
| 127 | } | 
|---|
| 128 | cout<<"    .... Number of values masked   : "<<nmask<<endl; | 
|---|
| 129 | cout<<"    .... Fraction of values masked : " | 
|---|
| 130 | <<(r_8)nmask/sph.NbPixels()*100.<<" %"<<endl; | 
|---|
| 131 |  | 
|---|
| 132 | // Ecriture de la sphere Healpix sur fits | 
|---|
| 133 | { | 
|---|
| 134 | dum = "rm -f "; dum += sphout; system(dum.c_str()); | 
|---|
| 135 | FitsOutFile sfits(sphout, FitsFile::clear); | 
|---|
| 136 | cout<<"Writing Output Masked Sphere Fits file"<<endl; | 
|---|
| 137 | sfits<<sph; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | exit(0); | 
|---|
| 141 | } | 
|---|