MUSE Pipeline Reference Manual  2.1.1
muse_scipost_combine_pixtables.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2005-2014 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 /*---------------------------------------------------------------------------*
27  * Includes *
28  *---------------------------------------------------------------------------*/
29 #include <muse.h>
30 #include "muse_scipost_combine_pixtables_z.h"
31 
32 /*----------------------------------------------------------------------------*/
41 /*----------------------------------------------------------------------------*/
42 int
43 muse_scipost_combine_pixtables_compute(muse_processing *aProcessing,
45 {
46  /* setup and check weighting scheme */
48 
49  /* sort input pixel tables into different exposures */
50  cpl_table *exposures = muse_processing_sort_exposures(aProcessing);
51  if (!exposures) {
52  cpl_msg_error(__func__, "no science exposures found in input");
53  return -1;
54  }
55  int nexposures = cpl_table_get_nrow(exposures);
56  /* load the optional offsets table */
57  cpl_table *offsets = muse_processing_load_ctable(aProcessing, MUSE_TAG_OFFSET_LIST, 0);
58  if (offsets && muse_cpltable_check(offsets, muse_offset_list_def) != CPL_ERROR_NONE) {
59  cpl_msg_warning(__func__, "Input %s has unexpected format, proceeding "
60  "without offset and flux scales!", MUSE_TAG_OFFSET_LIST);
61  cpl_table_delete(offsets);
62  offsets = NULL;
63  } /* if offsets have wrong format */
64 
65  /* now process all the pixel tables, do it separately for each exposure */
66  /* allocate one additional element for NULL termination */
67  muse_pixtable **pixtables = cpl_calloc(nexposures + 1, sizeof(muse_pixtable *));
68  int i;
69  for (i = 0; i < nexposures; i++) {
70  cpl_table *thisexp = cpl_table_extract(exposures, i, 1);
71  pixtables[i] = muse_pixtable_load_merge_channels(thisexp,
72  aParams->lambdamin,
73  aParams->lambdamax);
74  cpl_table_delete(thisexp);
75  if (pixtables[i]) {
76  /* erase pre-existing QC parameters */
77  cpl_propertylist_erase_regexp(pixtables[i]->header, "ESO QC ", 0);
78  } /* if */
79  }
80  cpl_table_delete(exposures);
81 
82  do { /* loop to break out of in case of error */
83  /* now combine the possibly more than one exposures */
84  muse_pixtable *bigpixtable = NULL;
85  if (nexposures > 1) {
86  cpl_error_code rc = muse_xcombine_weights(pixtables, weight);
87  if (rc != CPL_ERROR_NONE) {
88  cpl_msg_error(__func__, "weighting the pixel tables didn't work: %s",
89  cpl_error_get_message());
90  break;
91  }
92  /* combine individual pixel tables and delete them */
93  bigpixtable = muse_xcombine_tables(pixtables, offsets);
94  if (!bigpixtable) {
95  cpl_msg_error(__func__, "combining the pixel tables didn't work: %s",
96  cpl_error_get_message());
97  break;
98  }
99  } else {
100  bigpixtable = pixtables[0];
101  }
102  cpl_free(pixtables);
103 
104  muse_processing_save_table(aProcessing, -1, bigpixtable, NULL,
105  MUSE_TAG_PIXTABLE_COMBINED,
107  muse_pixtable_delete(bigpixtable);
108  cpl_table_delete(offsets);
109  return 0;
110  } while (0); /* end of "error-free" loop, error handling follows */
111  for (i = 0; i < nexposures; i++) {
112  muse_pixtable_delete(pixtables[i]);
113  } /* for i (exposures) */
114  cpl_free(pixtables);
115  cpl_table_delete(offsets);
116  return -1;
117 } /* muse_scipost_combine_pixtables_compute() */
Structure to hold the parameters of the muse_scipost_combine_pixtables recipe.
muse_xcombine_types muse_postproc_get_weight_type(const char *aWeightString)
Select correct weighting type for weight string.
const muse_cpltable_def muse_offset_list_def[]
cpl_error_code muse_xcombine_weights(muse_pixtable **aPixtables, muse_xcombine_types aWeighting)
compute the weights for combination of two or more exposures
double lambdamin
Cut off the data below this wavelength after loading the pixel table(s).
cpl_error_code muse_cpltable_check(const cpl_table *aTable, const muse_cpltable_def *aDef)
Check whether the table contains the fields of the definition.
const char * weight_s
Type of weighting scheme to use when combining multiple exposures. "exptime" just uses the exposure t...
Structure definition of MUSE pixel table.
muse_pixtable * muse_xcombine_tables(muse_pixtable **aPixtables, const cpl_table *aOffsets)
combine the pixel tables of several exposures into one
double lambdamax
Cut off the data above this wavelength after loading the pixel table(s).
cpl_table * muse_processing_load_ctable(muse_processing *aProcessing, const char *aTag, unsigned char aIFU)
Load a CPL table according to its tag and IFU/channel number.
muse_pixtable * muse_pixtable_load_merge_channels(cpl_table *aExposureList, double aLambdaMin, double aLambdaMax)
Load and merge the pixel tables of the 24 MUSE sub-fields.
cpl_error_code muse_processing_save_table(muse_processing *aProcessing, int aIFU, void *aTable, cpl_propertylist *aHeader, const char *aTag, muse_table_type aType)
Save a computed table to disk.
cpl_table * muse_processing_sort_exposures(muse_processing *aProcessing)
Sort input frames (containing lists of pixel table filenames) into different exposures.
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
muse_xcombine_types
Xposure combination types.
Definition: muse_xcombine.h:44