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