38 #include "vircam_sky.h"
39 #include "vircam_mask.h"
40 #include "vircam_fits.h"
41 #include "vircam_mods.h"
42 #include "vircam_utils.h"
43 #include "vircam_stats.h"
44 #include "vircam_wcsutils.h"
46 static void masksky_zeros(
float **datas,
unsigned char **masks,
int nfiles,
47 int npts,
float **zeros);
48 static void combine(
int nfiles,
int stat,
int npts,
float **datas,
49 unsigned char **masks,
float **skyout,
50 unsigned char **skyout_bpm);
51 static void combine_mult(
int nfiles,
int stat,
int npts,
float **datas,
52 unsigned char **masks,
float *skyout);
53 static void domed(
float *buf,
int n,
float *val);
55 extern int vircam_pawsky_minus(vir_fits **infiles, vir_fits *conf,
56 vir_fits *objmaskfits,
int nfiles,
int nconfs,
57 vir_fits **skyout,
int *status) {
58 cpl_wcs *wcsmask,*wcsimg;
59 unsigned char **masks,*sky_bpm;
61 double *xin,*yin,*xout,*yout,*dx,*dy,ddx,ddy;
62 int nx,ny,npts,ind,i,j,*confdata,nx_mask,ny_mask,kind,ix,iy,jx,jy,*opm;
63 float **datas,*sky,*zeros,val,sig;
64 cpl_propertylist *plist;
65 const char *fctid =
"vircam_pawsky_minus";
70 if (*status != VIR_OK)
76 cpl_msg_error(fctid,
"Sky correction impossible. No science frames");
82 datas = cpl_malloc(nfiles*
sizeof(
float *));
83 masks = cpl_malloc(nfiles*
sizeof(
unsigned char *));
85 nx = cpl_image_get_size_x(im);
86 ny = cpl_image_get_size_y(im);
91 if (objmaskfits != NULL) {
100 xin = cpl_malloc(npts*
sizeof(
double));
101 yin = cpl_malloc(npts*
sizeof(
double));
102 xout = cpl_malloc(npts*
sizeof(
double));
103 yout = cpl_malloc(npts*
sizeof(
double));
104 dx = cpl_malloc(nfiles*
sizeof(
double));
105 dy = cpl_malloc(nfiles*
sizeof(
double));
110 for (j = 0; j < ny; j++) {
111 for (i = 0; i < nx; i++) {
112 xin[ind] = (double)(i+1);
113 yin[ind++] = (double)(j+1);
122 for (i = 0; i < nfiles; i++) {
129 cpl_wcs_delete(wcsimg);
131 cpl_wcs_delete(wcsmask);
138 for (i = 0; i < nfiles; i++) {
140 datas[i] = cpl_image_get_data_float(im);
141 masks[i] = cpl_calloc(npts,
sizeof(
unsigned char));
151 for (jy = 0; jy < ny; jy++) {
152 for (jx = 0; jx < nx; jx++) {
154 if (confdata[ind] == 0) {
156 }
else if (objmaskfits != NULL) {
157 kind = (int)(yout[ind] + dy[i] - 0.5)*nx_mask +
158 (int)(xout[ind] + dx[i] - 0.5);
159 masks[i][ind] = opm[kind];
167 if (objmaskfits != NULL) {
178 masksky_zeros(datas,masks,nfiles,npts,&zeros);
182 combine(nfiles,0,npts,datas,masks,&sky,&sky_bpm);
186 skyim = cpl_image_wrap_float(nx,ny,sky);
187 for (i = 0; i < npts; i++) {
189 ix = npts - (iy-1)*nx;
191 cpl_image_reject(skyim,(cpl_size)ix,(cpl_size)iy);
196 if (objmaskfits != NULL) {
197 cpl_propertylist_update_string(plist,
"ESO DRS MASKUSED",
199 cpl_propertylist_set_comment(plist,
"ESO DRS MASKUSED",
200 "Object masked used to make sky");
202 cpl_propertylist_update_string(plist,
"ESO DRS SKYALGO",
"pawsky_minus");
203 cpl_propertylist_set_comment(plist,
"ESO DRS SKYALGO",
204 "Sky estimation algorithm");
213 combine_mult(nfiles,0,npts,datas,masks,sky);
217 for (i = 0; i < nfiles; i++) {
218 vircam_qmedsig(datas[i],masks[i],(
long)npts,3.0,1,-1000.0,65535.0,&val,
220 val = zeros[i] - val;
221 for (j = 0; j < npts; j++)
228 for (i = 0; i < nfiles; i++) {
230 cpl_propertylist_update_string(plist,
"ESO DRS SKYSUB",
231 "Done with pawsky_minus");
239 static void masksky_zeros(
float **datas,
unsigned char **masks,
int nfiles,
240 int npts,
float **zeros) {
242 float sig,off,medval;
246 *zeros = cpl_malloc(nfiles*
sizeof(
float));
250 for (i = 0; i < nfiles; i++)
252 65535.0,*zeros+i,&sig);
260 for (i = 0; i < nfiles; i++) {
261 off = medval - (*zeros)[i];
262 for (j = 0; j < npts; j++)
267 static void combine(
int nfiles,
int stat,
int npts,
float **datas,
268 unsigned char **masks,
float **skyout,
269 unsigned char **skyout_bpm) {
275 *skyout = cpl_malloc(npts*
sizeof(
float));
276 *skyout_bpm = cpl_malloc(npts*
sizeof(
unsigned char));
277 buf = cpl_malloc(nfiles*
sizeof(
float));
281 for (j = 0; j < npts; j++) {
283 for (i = 0; i < nfiles; i++) {
284 if (masks[i][j] == 0)
285 buf[n++] = datas[i][j];
289 (*skyout_bpm)[j] = 1;
292 domed(buf,n,(*skyout)+j);
295 (*skyout_bpm)[j] = 0;
305 static void combine_mult(
int nfiles,
int stat,
int npts,
float **datas,
306 unsigned char **masks,
float *skyout) {
312 buf = cpl_malloc(nfiles*
sizeof(
float));
313 val = cpl_malloc(nfiles*
sizeof(
float));
317 for (j = 0; j < npts; j++) {
324 for (i = 0; i < nfiles; i++) {
325 if (masks[i][j] != 0) {
329 for (k = 0; k < nfiles; k++) {
330 if (k == i || masks[k][j] != 0)
332 buf[n++] = datas[k][j];
347 for (i = 0; i < nfiles; i++)
348 datas[i][j] -= val[i];
359 static void domed(
float *buf,
int n,
float *val) {
366 *val = 0.5*(buf[n2-1] + buf[n2]);
371 *val = 0.5*buf[n2] + 0.25*(buf[n2-1] + buf[n2+1]);
char * vircam_fits_get_filename(vir_fits *p)
void vircam_qmedsig(float *data, unsigned char *bpm, long npts, float thresh, int niter, float lowv, float highv, float *median, float *sigma)
void vircam_xytoxy_list(cpl_wcs *wcs1, cpl_wcs *wcs2, int nc, double *x_1, double *y_1, double *x_2, double *y_2)
int vircam_inpaint(vir_fits *in, int nbsize, int *status)
Inpaint pixels or patches in a map.
vir_fits * vircam_fits_wrap(cpl_image *im, vir_fits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
float vircam_mean(float *data, unsigned char *bpm, long npts)
void vircam_sort(float **a, int n, int m)
cpl_image * vircam_fits_get_image(vir_fits *p)
float vircam_med(float *data, unsigned char *bpm, long npts)
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
void vircam_prov(cpl_propertylist *p, vir_fits **inlist, int n)