VIRCAM Pipeline  2.3.10
vircam_sky.c
1 /* $Id: vircam_sky.c,v 1.10 2013-10-15 16:51:47 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2015 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2013-10-15 16:51:47 $
24  * $Revision: 1.10 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <cpl.h>
35 #include <math.h>
36 #include <string.h>
37 
38 #include <casu_utils.h>
39 #include <casu_mods.h>
40 #include <casu_mask.h>
41 #include <casu_stats.h>
42 #include <casu_wcsutils.h>
43 
44 #include "vircam_sky.h"
45 #include "vircam_mods.h"
46 #include "vircam_pfits.h"
47 
48 typedef struct {
49  casu_fits **in;
50  int n;
51  int nalloc;
52  int status;
53  casu_fits *xsky;
54 } xsky_struct;
55 
56 
59 /*---------------------------------------------------------------------------*/
92 /*---------------------------------------------------------------------------*/
93 
94 extern int vircam_tilesky(casu_fits **inlist, int nfiles, casu_mask *mask,
95  casu_fits **skyout, int *status) {
96  int i,njit,nx,ny,ji,ns;
97  unsigned char *inbpm,*rejmask,*rejplus;
98  cpl_mask *cplmask;
99  xsky_struct *xskys;
100  cpl_propertylist *drs;
101  cpl_image *outim;
102  casu_fits **xsky_fits;
103  const char *fctid = "vircam_tilesky";
104 
105  /* Inherited status */
106 
107  *skyout = NULL;
108  if (*status != CASU_OK)
109  return(*status);
110 
111  /* If there aren't any images, then get out of here */
112 
113  if (nfiles == 0) {
114  cpl_msg_error(fctid,"Sky correction impossible. No science frames");
115  return(CASU_FATAL);
116  }
117 
118  /* Wrap the input mask into a cpl_mask structure and use it to set the
119  internal mask for each of the input images */
120 
121  inbpm = casu_mask_get_data(mask);
122  nx = casu_mask_get_size_x(mask);
123  ny = casu_mask_get_size_y(mask);
124  cplmask = cpl_mask_wrap((cpl_size)nx,(cpl_size)ny,(cpl_binary *)inbpm);
125  for (i = 0; i < nfiles; i++)
126  cpl_image_reject_from_mask(casu_fits_get_image(inlist[i]),cplmask);
127  cpl_mask_unwrap(cplmask);
128 
129  /* How many jitter steps are there? */
130 
131  (void)vircam_pfits_get_njsteps(casu_fits_get_phu(inlist[0]),&njit);
132 
133  /* Allocate some workspce */
134 
135  xskys = cpl_malloc(njit*sizeof(xsky_struct));
136  for (i = 0; i < njit; i++) {
137  xskys[i].n = 0;
138  xskys[i].in = cpl_malloc(8*sizeof(casu_fits *));
139  xskys[i].nalloc = 8;
140  xskys[i].xsky = NULL;
141  xskys[i].status = CASU_OK;
142  }
143  xsky_fits = cpl_malloc(njit*sizeof(casu_fits *));
144 
145  /* Now loop through all the files and put them into the relevant
146  structure depending upon where they are in the jitter pattern.
147  NB: We don't have to check on the status of the individual
148  frames because only good frames should have been passed in by
149  the calling routine. */
150 
151  for (i = 0; i < nfiles; i++) {
152  (void)vircam_pfits_get_jitteri(casu_fits_get_phu(inlist[i]),&ji);
153  ji--;
154  if (xskys[ji].n == xskys[ji].nalloc) {
155  xskys[ji].in = cpl_realloc(xskys[ji].in,
156  (xskys[ji].nalloc+8)*sizeof(casu_fits *));
157  xskys[ji].nalloc += 8;
158  }
159  xskys[ji].in[xskys[ji].n] = inlist[i];
160  xskys[ji].n += 1;
161  }
162 
163  /* Now loop through each of these groups and form a sky file with
164  rejection */
165 
166  ns = 0;
167  for (i = 0; i < njit; i++) {
168  *status = CASU_OK;
169  if (xskys[i].n == 0) {
170  xskys[i].xsky = NULL;
171  continue;
172  } else if (xskys[i].n == 1) {
173  xskys[i].xsky = NULL;
174  xsky_fits[ns++] = xskys[i].in[0];
175  continue;
176  }
177  outim = NULL;
178  (void)casu_imcombine(xskys[i].in,NULL,xskys[i].n,1,1,0,1.0,
179  "EXPTIME",&outim,NULL,&rejmask,
180  &rejplus,&drs,status);
181  freespace(rejmask);
182  freespace(rejplus);
183  freepropertylist(drs);
184  if (*status == CASU_OK) {
185  xskys[i].xsky = casu_fits_wrap(outim,xskys[i].in[0],NULL,NULL);
186  xsky_fits[ns++] = xskys[i].xsky;
187  } else {
188  freeimage(outim);
189  }
190  }
191 
192  /* Now combine all the intermediate skies to form the final sky */
193 
194  if (ns != 0) {
195  outim = NULL;
196  (void)casu_imcombine(xsky_fits,NULL,ns,1,1,0,1.0,"EXPTIME",&outim,
197  NULL,&rejmask,&rejplus,&drs,status);
198  freespace(rejmask);
199  freespace(rejplus);
200  freepropertylist(drs);
201  if (*status == CASU_OK) {
202  *skyout = casu_fits_wrap(outim,inlist[0],NULL,NULL);
203  (void)casu_inpaint(*skyout,64,status);
204  } else {
205  freeimage(outim);
206  *skyout = NULL;
207  }
208  } else {
209  *skyout = NULL;
210  }
211 
212  /* Tidy and exit */
213 
214  freespace(xsky_fits);
215  for (i = 0; i < njit; i++) {
216  freespace(xskys[i].in);
217  freefits(xskys[i].xsky);
218  }
219  freespace(xskys);
220  return(*status);
221 }
222 
223 
226 /*
227 
228 $Log: not supported by cvs2svn $
229 Revision 1.9 2012/01/27 12:25:10 jim
230 Fixed some casts
231 
232 Revision 1.8 2012/01/15 17:40:09 jim
233 Minor modifications to take into accout the changes in cpl API for v6
234 
235 Revision 1.7 2010/06/30 12:42:00 jim
236 A few fixes to stop compiler compaints
237 
238 Revision 1.6 2009/02/23 10:46:26 jim
239 Modified tilesky to try and get rid of some of the low level glow around
240 bright and extended objects
241 
242 Revision 1.5 2009/02/20 11:01:13 jim
243 Added vircam_tilesky. Commented out vircam_skycombine as it's not being
244 used at present and it's generating a compiler error. May delete it
245 completely later
246 
247 Revision 1.4 2008/11/25 06:23:09 jim
248 Added some routine prologues
249 
250 Revision 1.3 2008/11/21 10:12:36 jim
251 Fixed typo
252 
253 Revision 1.2 2008/10/24 10:57:19 jim
254 Fixed bug in pawsky_mask so that the bad pixels in the confidence map are
255 folded into the object mask
256 
257 Revision 1.1 2008/10/13 08:13:21 jim
258 New entry
259 
260 */
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
casu_fits * casu_fits_wrap(cpl_image *im, casu_fits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: casu_fits.c:883
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
Definition: casu_fits.c:531
unsigned char * casu_mask_get_data(casu_mask *m)
Definition: casu_mask.c:544
int casu_mask_get_size_y(casu_mask *m)
Definition: casu_mask.c:498
int casu_mask_get_size_x(casu_mask *m)
Definition: casu_mask.c:475
int casu_imcombine(casu_fits **fset, casu_fits **fsetv, int nfits, int combtype, int scaletype, int xrej, float thresh, const char *expkey, cpl_image **outimage, cpl_image **outvimage, unsigned char **rejmask, unsigned char **rejplus, cpl_propertylist **drs, int *status)
Stack images into a mean or median image with rejection.
int casu_inpaint(casu_fits *in, int nbsize, int *status)
Inpaint pixels or patches in a map.
Definition: casu_inpaint.c:83
int vircam_tilesky(casu_fits **inlist, int nfiles, casu_mask *mask, casu_fits **skyout, int *status)
Work sky estimate from an input tile series.
Definition: vircam_sky.c:94
int vircam_pfits_get_jitteri(const cpl_propertylist *plist, int *jitteri)
Get the position number of an observations in a jitter sequence.
Definition: vircam_pfits.c:530
int vircam_pfits_get_njsteps(const cpl_propertylist *plist, int *njsteps)
Get the value of the number of observations in a jitter sequence.
Definition: vircam_pfits.c:476