VISIR Pipeline Reference Manual  4.1.7
visir_util_qc.c
1 /*
2  * This file is part of the VISIR Pipeline
3  * Copyright (C) 2012,2015 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 /*-----------------------------------------------------------------------------
25  Includes
26  -----------------------------------------------------------------------------*/
27 
28 #include "visir_recipe.h"
29 
30 #include <string.h>
31 
32 /*-----------------------------------------------------------------------------
33  Defines
34  -----------------------------------------------------------------------------*/
35 
36 #define RECIPE_STRING "visir_util_qc"
37 
38 /*-----------------------------------------------------------------------------
39  Private Functions prototypes
40  -----------------------------------------------------------------------------*/
41 
42 #ifdef VISIR_CHAIN
43 #define cpl_plugin_get_info visir_util_qc_get_info
44 #endif
45 // FIXME:
46 cpl_recipe_define(visir_util_qc, VISIR_BINARY_VERSION,
47  "Julian Taylor", PACKAGE_BUGREPORT, "2012",
48  "Prepares qc parameters from data where the beam has "
49  "been deteteced and bad images have been removed",
50  "The files listed in the Set Of Frames (sof-file) "
51  "must be tagged:\n"
52  "VISIR-chopnod-corrected-file.fits "VISIR_UTIL_CORRECTED
53  "\nThe product(s) will have a FITS card\n"
54  "'HIERARCH ESO PRO CATG' with a value of: "
55  VISIR_UTIL_QC_PROCATG"\n");
56 
57 /*----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------*/
62 
63 /*-----------------------------------------------------------------------------
64  Functions code
65  -----------------------------------------------------------------------------*/
66 
67 
68 /*----------------------------------------------------------------------------*/
76 /*----------------------------------------------------------------------------*/
77 static cpl_error_code
78 visir_util_qc_fill_parameterlist(cpl_parameterlist * self)
79 {
80  if (self) {}
81  return CPL_ERROR_NONE;
82 }
83 
84 
85 /*----------------------------------------------------------------------------*/
92 /*----------------------------------------------------------------------------*/
93 static int visir_util_qc(cpl_frameset * framelist,
94  const cpl_parameterlist * parlist)
95 {
96  cpl_frameset * usedframes = cpl_frameset_new();
97  cpl_propertylist * outlist = cpl_propertylist_new();
98  irplib_framelist * allframes = irplib_framelist_cast(framelist);
99  /* FIXME: macro */
100  irplib_framelist * rawframes =
101  irplib_framelist_extract(allframes, VISIR_UTIL_CORRECTED);
102  const int nrawframes = irplib_framelist_get_size(rawframes);
103  cpl_boolean undefined_beampos = CPL_FALSE;
104 
105  irplib_framelist_load_propertylist_all(rawframes, 0, ".*", CPL_FALSE);
106 
107  double t_exptime = 0;
108  double t_eff_exptime = 0;
109  cpl_size t_nimages = 0;
110  cpl_size t_neff_images = 0;
111  double t_bkg_sum = 0;
112  double t_min_obsstart = 1e300;
113  double t_max_filewrite = 0;
114 
115  for (int iframe = 0; iframe < nrawframes; iframe++) {
116  cpl_propertylist * plist =
117  irplib_framelist_get_propertylist(rawframes, iframe);
118  cpl_frame * frm = irplib_framelist_get(rawframes, iframe);
119  float imgwgt = visir_pfits_get_img_weight(plist);
120  visir_data_type dtype;
121  visir_get_data_type(frm, plist, &dtype, NULL);
122  t_max_filewrite =
123  CX_MAX(cpl_propertylist_get_double(plist, "ESO DRS DATE"),
124  t_max_filewrite);
125  t_min_obsstart =
126  CX_MIN(cpl_propertylist_get_double(plist, "ESO DRS DATE-OBS"),
127  t_min_obsstart);
128 
129  const int ndit = visir_pfits_get_ndit(plist);
130  const int navrg = visir_pfits_get_navrg(plist);
131 
132  const double dit = visir_pfits_get_dit(plist);
133  if (iframe == 0)
134  cpl_propertylist_append(outlist, plist);
135 
136  const cpl_size ncycles = visir_pfits_get_chop_ncycles(plist);
137  const char * beamid = cpl_propertylist_get_string(plist, "ESO QC BEAMID");
138 
139  double time_per_img;
140  cpl_size nimages;
141  if (visir_data_is_burst(dtype)) {
142  time_per_img = dit * navrg;
143  nimages = ncycles * ndit / navrg;
144  }
145  else if (dtype == VISIR_DATA_AQU_INT) {
146  time_per_img = ncycles * dit * ndit * navrg;
147  nimages = 1;
148  }
149  else {
150  time_per_img = dit * ndit * navrg;
151  nimages = ncycles / navrg;
152  }
153  const cpl_size neff_images = cpl_frame_get_nextensions(frm);
154 
155  double exptime = nimages * time_per_img;
156  double eff_exptime = neff_images * time_per_img;
157  double bkg_sum = 0;
158 
159  exptime *= imgwgt;
160  eff_exptime *= imgwgt;
161 
162  char filename[128];
163  sprintf(filename, "qc_header_%03d.fits", iframe);
164 
165  if (cpl_propertylist_has(plist, "ESO QC BACKGD MEAN"))
166  bkg_sum = cpl_propertylist_get_double(plist, "ESO QC BACKGD MEAN");
167 
168  if (strcmp(beamid, "UNDEFINED") == 0)
169  undefined_beampos = CPL_TRUE;
170 
171  cpl_frame_set_group(frm, CPL_FRAME_GROUP_RAW);
172  cpl_frameset_insert(usedframes, cpl_frame_duplicate(frm));
173 
174 
175  cpl_propertylist_append_double(outlist, "ESO DRS IMGWGT", imgwgt);
176  /* signal exptime, the real exptime is nbeams times larger due to chop/nodding */
177  cpl_propertylist_append_double(outlist, "ESO QC EXPTIME TOTAL", exptime);
178  cpl_propertylist_append_double(outlist, "ESO QC EXPTIME EFFECTIVE", eff_exptime);
179  cpl_propertylist_append_int(outlist, "ESO QC NIMAGES TOTAL", (int)nimages);
180  cpl_propertylist_append_int(outlist, "ESO QC NIMAGES EFFECTIVE",
181  (int)neff_images);
182  cpl_propertylist_append_double(outlist, "ESO QC BACKGD MEAN", bkg_sum);
183  cpl_propertylist_update_string(outlist, "ESO QC BEAMID", beamid);
184  cpl_propertylist_update_double(outlist, "ESO QC BEAMX",
185  cpl_propertylist_get_double(plist, "ESO QC BEAMX"));
186  cpl_propertylist_update_double(outlist, "ESO QC BEAMY",
187  cpl_propertylist_get_double(plist, "ESO QC BEAMY"));
188  cpl_propertylist_append_double(outlist, "ESO QC EXECTIME",
189  (t_max_filewrite - t_min_obsstart) * 24 * 3600);
190 
191  irplib_dfs_save_propertylist(framelist, parlist, usedframes,
192  RECIPE_STRING,
193  nrawframes > 1 ? VISIR_UTIL_QC_PROCATG :
194  VISIR_UTIL_QC_PROCATG "_COMBINED",
195  outlist, NULL,
196  visir_pipe_id, filename);
197  skip_if(0);
198 
199  t_nimages += nimages;
200  t_neff_images += neff_images;
201  t_exptime += exptime;
202  t_eff_exptime += eff_exptime;
203  t_bkg_sum += bkg_sum;
204  }
205 
206  double mean_bkg = t_bkg_sum / nrawframes;
207 
208  cpl_msg_info(cpl_func, "Total exposure time: %gs", t_exptime);
209  cpl_msg_info(cpl_func, "Total effective exposure time: %gs", t_eff_exptime);
210  cpl_msg_info(cpl_func, "Total input images: %d", (int)t_nimages);
211  cpl_msg_info(cpl_func, "Total accepted input images: %d", (int)t_neff_images);
212  cpl_msg_info(cpl_func, "Mean input background: %g", mean_bkg);
213 
214  if (nrawframes > 1) {
215  cpl_propertylist_append_double(outlist, "ESO DRS IMGWGT", 1.);
216  cpl_propertylist_append_double(outlist, "ESO QC EXPTIME TOTAL", t_exptime);
217  cpl_propertylist_append_double(outlist, "ESO QC EXPTIME EFFECTIVE", t_eff_exptime);
218  cpl_propertylist_append_int(outlist, "ESO QC NIMAGES TOTAL", (int)t_nimages);
219  cpl_propertylist_append_int(outlist, "ESO QC NIMAGES EFFECTIVE",
220  (int)t_neff_images);
221  cpl_propertylist_append_double(outlist, "ESO QC BACKGD MEAN", mean_bkg);
222  cpl_propertylist_append_string(outlist, "ESO QC BEAMID",
223  undefined_beampos ? "UNDEFINED" : "COMBINED");
224  cpl_propertylist_erase(outlist, "ESO QC BEAMX");
225  cpl_propertylist_erase(outlist, "ESO QC BEAMY");
226 
227  cpl_propertylist_append_double(outlist, "ESO QC EXECTIME",
228  (t_max_filewrite - t_min_obsstart) * 24 * 3600);
229 
230  skip_if(irplib_dfs_save_propertylist(framelist, parlist, usedframes,
231  RECIPE_STRING,
232  VISIR_UTIL_QC_PROCATG"_COMBINED",
233  outlist, NULL,
234  visir_pipe_id, "qc_header_all.fits"));
235  }
236 
237  end_skip;
238  cpl_frameset_delete(usedframes);
239  irplib_framelist_delete(allframes);
240  irplib_framelist_delete(rawframes);
241  cpl_propertylist_delete(outlist);
242 
243  return cpl_error_get_code();
244 }
int visir_pfits_get_navrg(const cpl_propertylist *self)
The NAVRG.
Definition: visir_pfits.c:323
cpl_error_code irplib_dfs_save_propertylist(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a propertylist as a DFS-compliant pipeline product.
Definition: irplib_utils.c:240
int visir_pfits_get_chop_ncycles(const cpl_propertylist *self)
The number of chopping cycles.
Definition: visir_pfits.c:237
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
int visir_pfits_get_ndit(const cpl_propertylist *self)
The NDIT keyword.
Definition: visir_pfits.c:502
cpl_frame * irplib_framelist_get(irplib_framelist *self, int pos)
Get the specified frame from the framelist.
double visir_pfits_get_img_weight(const cpl_propertylist *self)
The relative weight of the image compared to the other images.
Definition: visir_pfits.c:877
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
double visir_pfits_get_dit(const cpl_propertylist *self)
The DIT.
Definition: visir_pfits.c:309
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
cpl_propertylist * irplib_framelist_get_propertylist(irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.