OMEGA Pipeline Reference Manual  1.0.6
omega_shutter.c
1 /*
2  * omega_shutter.c
3  *
4  *
5  * This file is part of the OMEGA Pipeline
6  * Copyright (C) 2002,2003 European Southern Observatory
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  *
23  * Created on: Jan 4, 2011
24  * Author: agabasch
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 
31 
32 #include "omega_recipe.h"
33 
34 /*----------------------------------------------------------------------------*/
56 /*-----------------------------------------------------------------------------
57  Functions prototypes
58  -----------------------------------------------------------------------------*/
59 
60 static int omega_shutter_create(cpl_plugin *);
61 static int omega_shutter_exec(cpl_plugin *);
62 static int omega_shutter_destroy(cpl_plugin *);
63 static int omega_shutter(cpl_frameset *, cpl_parameterlist *);
64 
65 /*-----------------------------------------------------------------------------
66  Private Functions
67  -----------------------------------------------------------------------------*/
68 
69 static void omega_shutter_init(void);
70 static void omega_shutter_tidy(void);
71 static cpl_error_code omega_shutter_basicsteps(cpl_frameset * frameset,
72  cpl_image * mbias, cpl_imagelist * ilist_shortexp_up,
73  float exptime_lower, float exptime_upper,
74  const char * direction, int extension);
75 static cpl_matrix * omega_fill_ccdlayout(void);
76 static cpl_error_code omega_average_ccd_table(cpl_imagelist * ilist_in,
77  cpl_table * table_out );
78 static cpl_error_code omega_table_insert_xcoordinates(cpl_table * table);
79 static float omega_get_exptime_limit(cpl_frameset * frameset);
80 
81 /*-----------------------------------------------------------------------------
82  Static structures
83  -----------------------------------------------------------------------------*/
84 
85 static struct {
86  /* Inputs. Parameters */
87  int oc;
88 }omega_shutter_config;
89 
90 /* Input and Output */
91 static struct {
92 
93  cpl_size *labels;
94  const cpl_frame *mbframe;
95  cpl_frameset *domelist;
96 }ps;
97 
98 /*-----------------------------------------------------------------------------
99  Static global variables
100  -----------------------------------------------------------------------------*/
101 
102 /* Static variables */
103 #define RECIPE "omega_shutter"
104 
105 static char omega_shutter_man[] =
106 
107  "This recipe tests illumination variations due to inhomogeneities "
108  "in the \nshutter blade speed.\n"
109  "\n"
110  "Input files:\n\n"
111  " DO category: Type: Explanation: Required: \n"
112  " DOME_SHUTTERTEST Raw Domeflat Y \n"
113  " and \n"
114  " MASTER_BIAS Raw Master bias Y \n\n"
115  "Output files:\n\n"
116  " DO category: Data type: Explanation: \n"
117  " SHUTTER_DOWN Products: Collapsed CCD array \n"
118  " and \n"
119  " SHUTTER_UP Products: Collapsed CCD array \n\n";
120 
121 #define omega_shutter_exit(message) \
122  { \
123  if (message) cpl_msg_error(cpl_func, message); \
124  cpl_imagelist_delete(ilist_shortexp_up); \
125  cpl_imagelist_delete(ilist_longexp_up); \
126  cpl_imagelist_delete(ilist_shortexp_down); \
127  cpl_imagelist_delete(ilist_longexp_down); \
128  cpl_imagelist_delete(ilist_up); \
129  cpl_imagelist_delete(ilist_down); \
130  cpl_image_delete(mbias); \
131  cpl_table_delete(table_up); \
132  cpl_table_delete(table_down); \
133  cpl_propertylist_delete(plistHeader); \
134  cpl_msg_indent_less(); \
135  return -1; \
136  }
137 
138 #define omega_average_ccd_table_exit(message) \
139  { \
140  if (message) cpl_msg_error(cpl_func, message); \
141  cpl_matrix_delete(ccd_mapping); \
142  cpl_imagelist_delete(ilist_tmp); \
143  cpl_image_delete(image_tmp); \
144  cpl_image_delete(image_collapsed); \
145  cpl_table_delete(table_local); \
146  cpl_msg_indent_less(); \
147  return cpl_error_set(cpl_func, cpl_error_get_code()); \
148  }
149 
150 #define omega_shutter_basicsteps_exit(message) \
151  { \
152  if (message) cpl_msg_error(cpl_func, message); \
153  cpl_propertylist_delete(plist); \
154  cpl_image_delete(image_local); \
155  cpl_image_delete(image_local_collapsed); \
156  cpl_free(direction_local); \
157  cpl_msg_indent_less(); \
158  return cpl_error_set(cpl_func, cpl_error_get_code()); \
159  }
160 
161 /*----------------------------------------------------------------------------*/
169 /*----------------------------------------------------------------------------*/
170 int cpl_plugin_get_info(cpl_pluginlist * list)
171 {
172  cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
173  cpl_plugin * plugin = &recipe->interface ;
174 
175  if (cpl_plugin_init(plugin,
176  CPL_PLUGIN_API,
177  OMEGA_BINARY_VERSION,
178  CPL_PLUGIN_TYPE_RECIPE,
179  "omega_shutter",
180  "OMEGA - Test the shutter homogeneity",
181  omega_shutter_man,
182  "Armin Gabasch",
183  PACKAGE_BUGREPORT,
185  omega_shutter_create,
186  omega_shutter_exec,
187  omega_shutter_destroy)) {
188  cpl_msg_error(cpl_func, "Plugin initialization failed");
189  (void)cpl_error_set_where(cpl_func);
190  return 1;
191  }
192 
193  if (cpl_pluginlist_append(list, plugin)) {
194  cpl_msg_error(cpl_func, "Error adding plugin to list");
195  (void)cpl_error_set_where(cpl_func);
196  return 1;
197  }
198 
199  return 0;
200 }
201 
202 /*----------------------------------------------------------------------------*/
211 /*----------------------------------------------------------------------------*/
212 static int omega_shutter_create(cpl_plugin * plugin)
213 {
214  cpl_recipe * recipe;
215  cpl_parameter * p ;
216 
217  /* Do not create the recipe if an error code is already set */
218  if (cpl_error_get_code() != CPL_ERROR_NONE) {
219  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
220  cpl_func, __LINE__, cpl_error_get_where());
221  return (int)cpl_error_get_code();
222  }
223 
224  if (plugin == NULL) {
225  cpl_msg_error(cpl_func, "Null plugin");
226  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
227  }
228 
229  /* Verify plugin type */
230  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
231  cpl_msg_error(cpl_func, "Plugin is not a recipe");
232  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
233  }
234 
235  /* Get the recipe */
236  recipe = (cpl_recipe *)plugin;
237 
238  /* Create the parameters list in the cpl_recipe object */
239  recipe->parameters = cpl_parameterlist_new() ;
240 
241  if (recipe->parameters == NULL) {
242  cpl_msg_error(cpl_func, "Parameter list allocation failed");
243  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
244  }
245 
246  /* Fill the parameters list */
247 
248  /* General Parameters */
249 
250 
251  p = cpl_parameter_new_range("omega.omega_shutter.OverscanMethod",
252  CPL_TYPE_INT,
253  "Overscan Correction Method",
254  "omega_shutter",
255  0, 0, 6);
256  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"oc-meth") ;
257  cpl_parameterlist_append(recipe->parameters, p) ;
258 
259  p = cpl_parameter_new_value("omega.omega_shutter.ExtensionNumber",
260  CPL_TYPE_INT,
261  "DUMMY parameter to ensure Paranal DFS compatibility!!",
262  "omega_shutter",
263  -1) ;
264 
265  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"ext") ;
266  cpl_parameterlist_append(recipe->parameters, p) ;
267 
268 
269 
270  return 0;
271 }
272 
273 /*----------------------------------------------------------------------------*/
279 /*----------------------------------------------------------------------------*/
280 static int omega_shutter_exec(cpl_plugin * plugin)
281 {
282  cpl_recipe * recipe;
283  int recipe_status;
284  cpl_errorstate initial_errorstate = cpl_errorstate_get();
285 
286  /* Return immediately if an error code is already set */
287  if (cpl_error_get_code() != CPL_ERROR_NONE) {
288  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
289  cpl_func, __LINE__, cpl_error_get_where());
290  return (int)cpl_error_get_code();
291  }
292 
293  if (plugin == NULL) {
294  cpl_msg_error(cpl_func, "Null plugin");
295  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
296  }
297 
298  /* Verify plugin type */
299  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
300  cpl_msg_error(cpl_func, "Plugin is not a recipe");
301  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
302  }
303 
304  /* Get the recipe */
305  recipe = (cpl_recipe *)plugin;
306 
307  /* Verify parameter and frame lists */
308  if (recipe->parameters == NULL) {
309  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
310  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
311  }
312  if (recipe->frames == NULL) {
313  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
314  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
315  }
316 
317  /* Invoke the recipe */
318  recipe_status = omega_shutter(recipe->frames, recipe->parameters);
319 
320  /* Ensure DFS-compliance of the products */
321  if (cpl_dfs_update_product_header(recipe->frames)) {
322  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
323  }
324 
325  if (!cpl_errorstate_is_equal(initial_errorstate)) {
326  /* Dump the error history since recipe execution start.
327  At this point the recipe cannot recover from the error */
328  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
329  }
330 
331  return recipe_status;
332 
333 }
334 
335 /*----------------------------------------------------------------------------*/
341 /*----------------------------------------------------------------------------*/
342 static int omega_shutter_destroy(cpl_plugin * plugin)
343 {
344  cpl_recipe *recipe;
345 
346  if (plugin == NULL) {
347  cpl_msg_error(cpl_func, "Null plugin");
348  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
349  }
350 
351  /* Verify plugin type */
352  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
353  cpl_msg_error(cpl_func, "Plugin is not a recipe");
354  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
355  }
356 
357  /* Get the recipe */
358  recipe = (cpl_recipe *)plugin;
359 
360  cpl_parameterlist_delete(recipe->parameters);
361 
362  return 0 ;
363 }
364 
365 static int omega_shutter(cpl_frameset *set, cpl_parameterlist *pars)
366 {
367 
368  int j=0,jst=0,jfn=0;
369  cpl_size nlab=0;
370  int nflats = 0;
371  int oscan1 = 0;
372  cpl_parameter *par =NULL;
373  cpl_imagelist * ilist_shortexp_up =NULL;
374  cpl_imagelist * ilist_longexp_up =NULL;
375  cpl_imagelist * ilist_shortexp_down =NULL;
376  cpl_imagelist * ilist_longexp_down =NULL;
377  cpl_imagelist * ilist_up =NULL;
378  cpl_imagelist * ilist_down =NULL;
379  cpl_image * mbias=NULL;
380  cpl_table * table_up=NULL;
381  cpl_table * table_down=NULL;
382  cpl_propertylist * plistHeader=NULL;
383  float shutter_explimit=0.;
384  //cpl_errorstate prestate=cpl_errorstate_get();
385 
386  /*Start the recipe */
387 
388  if (!pars) {
389  cpl_msg_error (cpl_func, "Parameters list not found");
390  return -1;
391  }
392 
393  if (cpl_frameset_is_empty(set) == 1) {
394  cpl_msg_error (cpl_func, "Frameset not found");
395  return -1;
396  }
397 
398  /* Retrieve input parameters */
399  par = cpl_parameterlist_find(pars, "omega.omega_shutter.OverscanMethod") ;
400  omega_shutter_config.oc = cpl_parameter_get_int(par) ;
401 
402  /* Identify the RAW and CALIB frames in the input frameset */
403  if (oc_dfs_set_groups(set)) {
404  cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames") ;
405  return -1 ;
406  }
407 
408  /*Initialized things*/
409  omega_shutter_init();
410 
411  /* Verify the frameset contents. */
412  if ((ps.labels = cpl_frameset_labelise(set,omega_compare_tags,
413  &nlab)) == NULL) {
414  cpl_msg_error(cpl_func,"Cannot labelise the input frameset");
415  omega_shutter_tidy();
416  return -1;
417  }
418  if ((ps.domelist = omega_frameset_subgroup(set,ps.labels,nlab,
419  SHUTTER_RAW)) == NULL) {
420  cpl_msg_error(cpl_func,"Cannot find dome frames in input frameset");
421  omega_shutter_tidy();
422  return -1;
423  }
424 
425  nflats = cpl_frameset_count_tags(ps.domelist, SHUTTER_RAW);
426  if (nflats < 4) {
427  cpl_msg_error (cpl_func, "Need at least 4 (%s) frames to run "
428  "this recipe", SHUTTER_RAW);
429  omega_shutter_tidy();
430  return -1;
431  }
432 
433  cpl_msg_info (cpl_func,"There are %d %s in frame set",nflats, SHUTTER_RAW);
434 
435  /* Check for calibration frames */
436  /* Master Bias */
437  ps.mbframe = cpl_frameset_find_const(set, OMEGA_CALIB_BIAS);
438  if (ps.mbframe == NULL) {
439  cpl_msg_error (cpl_func, "Need at least 1 (%s) frames to run "
440  "this recipe", OMEGA_CALIB_BIAS);
441  omega_shutter_tidy();
442  return -1;
443  }
444  else{
445  cpl_msg_info(cpl_func,"Using %s %s",OMEGA_CALIB_BIAS,
446  cpl_frame_get_filename(ps.mbframe));
447  }
448 
449 
450  /* Loop for each of the image extensions */
451  jst=1; /*First extension*/
452  jfn=32; /*Last extension*/
453 
454 
455  ilist_shortexp_up =cpl_imagelist_new();
456  ilist_longexp_up =cpl_imagelist_new();
457  ilist_shortexp_down =cpl_imagelist_new();
458  ilist_longexp_down =cpl_imagelist_new();
459 
460  shutter_explimit=omega_get_exptime_limit(ps.domelist);
461  cpl_msg_info(cpl_func, "Limit to discriminate long and short exposures:"
462  " %f second",shutter_explimit);
463 
464  for (j = jst; j <= jfn; j++) {
465  cpl_msg_info(cpl_func,"Beginning work on extension %02d",j);
466  cpl_msg_indent_more();
467 
468  /* Check overscan correction method consistency */
469  if(ps.mbframe != NULL){
470  oscan1 = omega_pfits_get_overscan(ps.mbframe, j);
471  if(oscan1 != omega_shutter_config.oc) {
472  cpl_msg_warning (cpl_func, "Overscan correction mode for Master"
473  " Bias (oc = %d) differs from the one used here "
474  "(oc = %d)", oscan1, omega_shutter_config.oc);
475  }
476  }
477 
478  /*Load master bias*/
479  mbias = cpl_image_load(cpl_frame_get_filename(ps.mbframe),
480  CPL_TYPE_FLOAT,0,j);
481  if(mbias==NULL){
482  cpl_msg_error (cpl_func, "Unable to load extension %d of the master"
483  " bias file %s", j, cpl_frame_get_filename(ps.mbframe));
484  omega_shutter_tidy();
485  omega_shutter_exit(NULL);
486  }
487 
488  /*Overscan correction, bias subtraction, exposure time normalisation*/
489 
490  if(omega_shutter_basicsteps(ps.domelist, mbias, ilist_shortexp_up ,
491  FLT_MIN , shutter_explimit, "up" ,j) != CPL_ERROR_NONE) {
492  cpl_msg_error (cpl_func, "Unable to perform basic image calibration"
493  " on extension %d ", j);
494  omega_shutter_tidy();
495  omega_shutter_exit(NULL);
496  }
497 
498  if(omega_shutter_basicsteps(ps.domelist, mbias, ilist_longexp_up ,
499  shutter_explimit, FLT_MAX , "up" ,j) != CPL_ERROR_NONE){
500  cpl_msg_error (cpl_func, "Unable to perform basic image calibration"
501  " on extension %d ", j);
502  omega_shutter_tidy();
503  omega_shutter_exit(NULL);
504  }
505 
506  if(omega_shutter_basicsteps(ps.domelist, mbias, ilist_shortexp_down,
507  FLT_MIN , shutter_explimit, "down",j)!= CPL_ERROR_NONE){
508  cpl_msg_error (cpl_func, "Unable to perform basic image calibration"
509  " on extension %d ", j);
510  omega_shutter_tidy();
511  omega_shutter_exit(NULL);
512  }
513 
514  if(omega_shutter_basicsteps(ps.domelist, mbias, ilist_longexp_down ,
515  shutter_explimit, FLT_MAX , "down",j)!= CPL_ERROR_NONE){
516  cpl_msg_error (cpl_func, "Unable to perform basic image calibration"
517  " on extension %d ", j);
518  omega_shutter_tidy();
519  omega_shutter_exit(NULL);
520  }
521 
522  cpl_image_delete(mbias); mbias=NULL;
523  cpl_msg_indent_less();
524  }
525 
526  /*Divide the short exposure by the long exposure*/
527 
528  cpl_msg_info(cpl_func,"Dividing long exposures through short exposures");
529 
530  if(cpl_imagelist_divide(ilist_shortexp_up,ilist_longexp_up)!=
531  CPL_ERROR_NONE){
532  omega_shutter_tidy();
533  omega_shutter_exit("Can not divide long exposure through short exposure");
534  }
535 
536  if(cpl_imagelist_divide(ilist_shortexp_down,ilist_longexp_down)!=
537  CPL_ERROR_NONE){
538  omega_shutter_tidy();
539  omega_shutter_exit("Can not divide long exposure through short exposure");
540  }
541 
542 
543  ilist_up=cpl_imagelist_new();
544  ilist_down=cpl_imagelist_new();
545  table_up=cpl_table_new(0);
546  table_down=cpl_table_new(0);
547 
548  cpl_table_new_column(table_up,"RowAveraged",CPL_TYPE_FLOAT);
549  cpl_table_new_column(table_down,"RowAveraged",CPL_TYPE_FLOAT);
550 
551 
552  cpl_msg_info(cpl_func,"Final average-collapsing the full CCD array in "
553  "y-direction");
554 
555  if(omega_average_ccd_table(ilist_shortexp_up, table_up)!= CPL_ERROR_NONE){
556  omega_shutter_tidy();
557  omega_shutter_exit("Can not collapse CCD array in y-direction");
558  }
559 
560  if(omega_average_ccd_table(ilist_shortexp_down, table_down)!=CPL_ERROR_NONE){
561  omega_shutter_tidy();
562  omega_shutter_exit("Can not collapse CCD array in y-direction");
563  }
564 
565  if(omega_table_insert_xcoordinates(table_up)!= CPL_ERROR_NONE ||
566  omega_table_insert_xcoordinates(table_down)!= CPL_ERROR_NONE){
567  omega_shutter_tidy();
568  omega_shutter_exit("Can not insert x-coordinates in the product table");
569  }
570 
571 
572  plistHeader=cpl_propertylist_new();
573  cpl_propertylist_append_string(plistHeader, CPL_DFS_PRO_CATG, "SHUTTER_UP");
574 
575  if(cpl_dfs_save_table(set, NULL, pars, set, NULL, table_up, NULL,
576  "omega_shutter", plistHeader, NULL, PACKAGE "/" PACKAGE_VERSION ,
577  "omega_shutter_up.fits") != CPL_ERROR_NONE){
578  omega_shutter_tidy();
579  omega_shutter_exit("Can not save the product SHUTTER_UP");
580  }
581 
582  cpl_propertylist_update_string(plistHeader, CPL_DFS_PRO_CATG, "SHUTTER_DOWN");
583  if (cpl_dfs_save_table(set, NULL, pars, set, NULL, table_down, NULL,
584  "omega_shutter", plistHeader, NULL, PACKAGE "/" PACKAGE_VERSION ,
585  "omega_shutter_down.fits")!=CPL_ERROR_NONE){
586  omega_shutter_tidy();
587  omega_shutter_exit("Can not save the product SHUTTER_DOWN");
588  }
589  cpl_propertylist_delete(plistHeader); plistHeader=NULL;
590 
591  /*Clean up */
592  cpl_imagelist_delete(ilist_shortexp_up); ilist_shortexp_up=NULL;
593  cpl_imagelist_delete(ilist_longexp_up); ilist_longexp_up=NULL;
594  cpl_imagelist_delete(ilist_shortexp_down); ilist_shortexp_down=NULL;
595  cpl_imagelist_delete(ilist_longexp_down); ilist_longexp_down=NULL;
596  cpl_imagelist_delete(ilist_up); ilist_up=NULL;
597  cpl_imagelist_delete(ilist_down); ilist_down=NULL;
598  cpl_table_delete(table_up); table_up=NULL;
599  cpl_table_delete(table_down); table_down=NULL;
600  omega_shutter_tidy();
601 
602  return 0;
603 }
604 
614 static cpl_error_code omega_average_ccd_table(cpl_imagelist * ilist_in,
615  cpl_table * table_out ){
616 
617  cpl_matrix * ccd_mapping=NULL;
618  cpl_imagelist * ilist_tmp=NULL;
619  cpl_image * image_tmp=NULL;
620  cpl_image * image_collapsed=NULL;
621  cpl_table * table_local=NULL;;
622  int nrow=0, ncol=0, row=0, col=0;
623  int ccd_index=0;
624  cpl_errorstate prestate=cpl_errorstate_get();
625 
626  ccd_mapping=omega_fill_ccdlayout();
627  //Test: cpl_error_set(cpl_func,CPL_ERROR_ACCESS_OUT_OF_RANGE);
628  if (!cpl_errorstate_is_equal(prestate)){
629  omega_average_ccd_table_exit(NULL);
630  }
631 
632  nrow=cpl_matrix_get_nrow(ccd_mapping); /*4*/
633  ncol=cpl_matrix_get_ncol(ccd_mapping); /*8*/
634  if (!cpl_errorstate_is_equal(prestate)){
635  omega_average_ccd_table_exit(NULL);
636  }
637 
638  for (col=0; col<ncol;col++){
639  cpl_msg_debug(cpl_func,"working on column: %d", col);
640  cpl_msg_indent_more();
641  ilist_tmp=cpl_imagelist_new();
642  for (row=0; row<nrow;row++){
643  cpl_msg_debug(cpl_func,"working on row: %d", row);
644  ccd_index=cpl_matrix_get(ccd_mapping,row,col)-1;
645  cpl_msg_debug(cpl_func,"fits file extension: %d", ccd_index+1);
646  image_tmp=cpl_image_duplicate(cpl_imagelist_get(ilist_in,ccd_index));
647  cpl_imagelist_set(ilist_tmp,image_tmp,row);
648  }
649  if (!cpl_errorstate_is_equal(prestate)){
650  omega_average_ccd_table_exit(NULL);
651  }
652 
653  image_collapsed=cpl_imagelist_collapse_create(ilist_tmp);
654  if (!cpl_errorstate_is_equal(prestate)){
655  omega_average_ccd_table_exit(NULL);
656  }
657  table_local=cpl_table_new(cpl_image_get_size_x(image_collapsed));
658  cpl_table_new_column(table_local,"RowAveraged",CPL_TYPE_FLOAT);
659  cpl_table_copy_data_float(table_local,"RowAveraged",
660  cpl_image_get_data_float(image_collapsed));
661  if (!cpl_errorstate_is_equal(prestate)){
662  omega_average_ccd_table_exit(NULL);
663  }
664 
665  cpl_table_insert(table_out, table_local, cpl_table_get_nrow(table_out));
666  cpl_table_delete(table_local); table_local=NULL;
667  cpl_image_delete(image_collapsed); image_collapsed=NULL;
668  cpl_imagelist_delete(ilist_tmp); ilist_tmp=NULL;
669  cpl_msg_indent_less();
670  }
671 
672  cpl_matrix_delete(ccd_mapping); ccd_mapping=NULL;
673 
674  /* Propagate error, if any */
675  return cpl_error_set(cpl_func, cpl_error_get_code());
676 
677 }
678 
679 
687 static cpl_matrix * omega_fill_ccdlayout(void){
688  cpl_matrix * ccd=NULL;
689 
690 
691  ccd=cpl_matrix_new(4,8);
692 
693  cpl_matrix_set(ccd,0,0 ,1 );
694  cpl_matrix_set(ccd,0,1 ,2 );
695  cpl_matrix_set(ccd,0,2 ,3 );
696  cpl_matrix_set(ccd,0,3 ,4 );
697  cpl_matrix_set(ccd,0,4 ,17);
698  cpl_matrix_set(ccd,0,5 ,18);
699  cpl_matrix_set(ccd,0,6 ,19);
700  cpl_matrix_set(ccd,0,7 ,20);
701 
702  cpl_matrix_set(ccd,1,0 ,5 );
703  cpl_matrix_set(ccd,1,1 ,6 );
704  cpl_matrix_set(ccd,1,2 ,7 );
705  cpl_matrix_set(ccd,1,3 ,8 );
706  cpl_matrix_set(ccd,1,4 ,21);
707  cpl_matrix_set(ccd,1,5 ,22);
708  cpl_matrix_set(ccd,1,6 ,23);
709  cpl_matrix_set(ccd,1,7 ,24);
710 
711  cpl_matrix_set(ccd,2,0 ,9);
712  cpl_matrix_set(ccd,2,1 ,10);
713  cpl_matrix_set(ccd,2,2 ,11);
714  cpl_matrix_set(ccd,2,3 ,12);
715  cpl_matrix_set(ccd,2,4 ,25);
716  cpl_matrix_set(ccd,2,5 ,26);
717  cpl_matrix_set(ccd,2,6 ,27);
718  cpl_matrix_set(ccd,2,7 ,28);
719 
720 
721  cpl_matrix_set(ccd,3,0 ,13);
722  cpl_matrix_set(ccd,3,1 ,14);
723  cpl_matrix_set(ccd,3,2 ,15);
724  cpl_matrix_set(ccd,3,3 ,16);
725  cpl_matrix_set(ccd,3,4 ,29);
726  cpl_matrix_set(ccd,3,5 ,30);
727  cpl_matrix_set(ccd,3,6 ,31);
728  cpl_matrix_set(ccd,3,7 ,32);
729 
730  return ccd;
731 }
732 
749 static cpl_error_code omega_shutter_basicsteps(cpl_frameset * frameset,
750  cpl_image * mbias, cpl_imagelist * ilist, float exptime_lower,
751  float exptime_upper, const char * direction, int extension)
752 {
753  char * direction_local=NULL;
754  double local_TMOPEN=0.;
755  float exptime_local=0;
756  cpl_propertylist * plist=NULL;
757  cpl_image * image_local=NULL;
758  cpl_image * image_local_collapsed=NULL;
759  cpl_frame * cur_frame=NULL;
760  cpl_errorstate prestate = cpl_errorstate_get();
761  int nframes = 0;
762  int i = 0;
763 
764 
765 
766  /* Initialize */
767  nframes = cpl_frameset_get_size(frameset) ;
768 
769  /*Looping over frames*/
770  for (i=0 ; i<nframes ; i++)
771  {
772  cur_frame=cpl_frameset_get_position(frameset, i) ;
773  cpl_free(direction_local); direction_local=NULL;
774  plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0);
775  local_TMOPEN=cpl_propertylist_get_double(plist,
776  "ESO DET SHUT TMOPEN");
777  if(cpl_error_get_code() != CPL_ERROR_NONE)
778  {
779  omega_shutter_basicsteps_exit("Missing keyword "
780  "ESO DET SHUT TMOPEN in scientific header");
781  }
782  cpl_msg_debug(cpl_func, "ESO DET SHUT TMOPEN: %g", local_TMOPEN);
783  if(local_TMOPEN>0.){
784  direction_local= cpl_sprintf("up");
785  }
786  else {
787  direction_local= cpl_sprintf("down");
788  }
789 
790  exptime_local=cpl_propertylist_get_float(plist, "EXPTIME");
791 
792  if ((cpl_error_get_code() != CPL_ERROR_NONE) || (exptime_local <=0)){
793  cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
794  omega_shutter_basicsteps_exit("Missing keyword "
795  "EXPTIME in scientific header or EXPTIME <= ZERO");
796  }
797 
798  if(strcmp(direction_local,direction)==0 && exptime_local >exptime_lower
799  && exptime_local <=exptime_upper)
800  {
801  cpl_msg_debug(cpl_func, "Loop: ESO DET SHUT TMOPEN: %g", local_TMOPEN);
802  cpl_msg_debug(cpl_func, "Loop: direction_local: %s", direction_local);
803  cpl_msg_debug(cpl_func, "Loop: direction: %s", direction);
804 
805  cpl_msg_info(cpl_func,"Trimming and overscan correcting");
806 
807  image_local=TrimOscanCorrect(cur_frame, omega_shutter_config.oc,
808  extension);
809 
810  if(image_local==NULL){
811  omega_shutter_basicsteps_exit("Can not trim and correct the "
812  "overscan ");
813  }
814 
815  //Subtract Bias !!!
816  cpl_msg_info(cpl_func,"Subtracting the bias");
817  cpl_image_subtract(image_local,mbias);
818  if (!cpl_errorstate_is_equal(prestate)){
819  omega_shutter_basicsteps_exit("Can not subtract the bias");
820  }
821 
822  /*Collapse image in y direction by using the median*/
823  cpl_msg_info(cpl_func,"Median collapsing in y-direction");
824  image_local_collapsed=cpl_image_collapse_median_create(
825  image_local, 0, 0, 0);
826  if (!cpl_errorstate_is_equal(prestate)){
827  omega_shutter_basicsteps_exit("Can not collapse the image in "
828  "y-direction");
829  }
830 
831  /*Devide image through the exposure time*/
832  cpl_msg_info(cpl_func,"Normalizing the exposure time to 1 sec");
833  cpl_image_divide_scalar(image_local_collapsed,exptime_local);
834  if (!cpl_errorstate_is_equal(prestate)){
835  omega_shutter_basicsteps_exit("can not normalize the exposure "
836  "time to unity");
837  }
838 
839  /*Insert image in the imagelist*/
840  cpl_imagelist_set(ilist,image_local_collapsed,extension-1);
841 
842  /*Free memory*/
843  cpl_image_delete(image_local); image_local=NULL;
844 
845  }
846  cpl_propertylist_delete(plist); plist=NULL;
847  cpl_free(direction_local); direction_local=NULL;
848  }
849 
850 
851  if (cpl_imagelist_get_size(ilist)<1){
852  cpl_error_set_message(cpl_func,CPL_ERROR_ILLEGAL_INPUT,"Imagelist is "
853  "empty! Please check the input raw frames");
854  }
855 
856  /* Propagate error, if any */
857  return cpl_error_set(cpl_func, cpl_error_get_code());
858 
859 }
867 static cpl_error_code omega_table_insert_xcoordinates(cpl_table * intable)
868 {
869  /*Fill the column x_coordinate to better plot the results*/
870  int * ptablecol=NULL;
871  int i = 0;
872  cpl_errorstate prestate = cpl_errorstate_get();
873 
874  cpl_table_new_column(intable, "x_coordinate", CPL_TYPE_INT);
875  cpl_table_fill_column_window_int(intable, "x_coordinate", 0,
876  cpl_table_get_nrow(intable), 0);
877  ptablecol = cpl_table_get_data_int(intable, "x_coordinate");
878 
879  if (!cpl_errorstate_is_equal(prestate)){
880  return cpl_error_set(cpl_func, cpl_error_get_code());
881  }
882  else {
883 
884  for(i = 0; i < cpl_table_get_nrow(intable); i++){
885  ptablecol[i] = i + 1;
886  }
887  }
888  /* Propagate error, if any */
889  return cpl_error_set(cpl_func, cpl_error_get_code());
890 }
891 
892 /* Initialize the pointers */
893 static void omega_shutter_init(void) {
894  ps.labels = NULL;
895  ps.mbframe = NULL;
896  ps.domelist = NULL;
897 
898 }
899 
900 /* Free any allocated memory */
901 static void omega_shutter_tidy(void) {
902  freespace(ps.labels);
903  freeframeset(ps.domelist);
904 
905 }
906 
915 static float omega_get_exptime_limit(cpl_frameset * frameset){
916 
917  cpl_vector * exptime_vector=NULL;
918  cpl_propertylist * plist=NULL;
919  cpl_frame * cur_frame=NULL;
920  int i=0;
921  float exptime_limit=0;
922  cpl_frameset_iterator * it = NULL;
923 
924 
925  exptime_vector=cpl_vector_new(cpl_frameset_get_size(frameset));
926 
927  it = cpl_frameset_iterator_new(frameset);
928 
929  i=0;
930  /*Looping over frames*/
931  while((cur_frame = cpl_frameset_iterator_get(it)))
932  {
933  plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0);
934  if(!cpl_propertylist_has(plist, "EXPTIME"))
935  {
936  cpl_msg_warning(cpl_func,"Can not find EXPTIME in file %s",
937  cpl_frame_get_filename(cur_frame));
938  cpl_propertylist_delete(plist);
939  cpl_vector_delete(exptime_vector);
940  cpl_frameset_iterator_delete(it);
941  return 0.;
942  }
943 
944  cpl_vector_set(exptime_vector, i,
945  cpl_propertylist_get_float(plist, "EXPTIME"));
946  cpl_propertylist_delete(plist);
947  i++;
948  cpl_frameset_iterator_advance(it, 1);
949  }
950  cpl_frameset_iterator_delete(it);
951 
952  exptime_limit=(cpl_vector_get_min(exptime_vector)+
953  cpl_vector_get_max(exptime_vector))/2.;
954  cpl_vector_delete(exptime_vector);
955 
956  return exptime_limit;
957 
958 }
959 
int omega_pfits_get_overscan(const cpl_frame *frame, int xn)
Get the DRS keyword of the overscan method used to reduce the image.
Definition: omega_pfits.c:912
cpl_image * TrimOscanCorrect(const cpl_frame *frame, int oscan, int extn)
This method loads a raw image, trims it and (optionally) performs an overscan correction, using the statistics of the pre and overscan regions. This can be done with a single value for the whole image or on a per-row basis.
Definition: omega_trim.c:87
int omega_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Definition: omega_utils.c:206
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
const char * omega_get_license(void)
Get the pipeline copyright and license.
Definition: omega_utils.c:67
cpl_frameset * omega_frameset_subgroup(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Definition: omega_utils.c:257