00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef HAVE_CONFIG_H
00023 #include <config.h>
00024 #endif
00025
00026
00027
00028
00029 #include <string.h>
00030
00031 #include <muse.h>
00032 #include "muse_exp_combine_z.h"
00033
00034
00035
00036
00037
00038
00047
00048 int
00049 muse_exp_combine_compute(muse_processing *aProcessing,
00050 muse_exp_combine_params_t *aParams)
00051 {
00052
00053 cpl_table *exposures = muse_processing_sort_exposures(aProcessing);
00054 int nexposures = cpl_table_get_nrow(exposures);
00055 if (nexposures < 2) {
00056 cpl_msg_error(__func__, "This recipe only makes sense with multiple "
00057 "exposures!");
00058 cpl_table_delete(exposures);
00059 return -1;
00060 }
00061 const char *savevalid = "cube,combined";
00062 if (!muse_postproc_check_save_param(aParams->save, savevalid)) {
00063 return -1;
00064 }
00065
00066 cpl_table *offsets = muse_table_load(aProcessing, MUSE_TAG_OFFSET_LIST, 0);
00067 if (offsets && muse_cpltable_check(offsets, muse_offset_list_def) != CPL_ERROR_NONE) {
00068 cpl_msg_warning(__func__, "Input %s has unexpected format, proceeding "
00069 "without offset and flux scales!", MUSE_TAG_OFFSET_LIST);
00070 cpl_table_delete(offsets);
00071 offsets = NULL;
00072 }
00073
00074
00075
00076 muse_pixtable **pixtables = cpl_calloc(nexposures + 1, sizeof(muse_pixtable *));
00077 int i;
00078 for (i = 0; i < nexposures; i++) {
00079 cpl_table *thisexp = cpl_table_extract(exposures, i, 1);
00080 pixtables[i] = muse_pixtable_load_merge_channels(thisexp,
00081 aParams->lambdamin,
00082 aParams->lambdamax);
00083 cpl_table_delete(thisexp);
00084
00085 cpl_propertylist_erase_regexp(pixtables[i]->header, "ESO QC ", 0);
00086 }
00087 cpl_table_delete(exposures);
00088
00089 do {
00090
00091 muse_xcombine_types weight = muse_postproc_get_weight_type(aParams->weight_s);
00092 cpl_error_code rc = muse_xcombine_weights(pixtables, weight);
00093 if (rc != CPL_ERROR_NONE) {
00094 cpl_msg_error(__func__, "weighting the pixel tables didn't work: %s",
00095 cpl_error_get_message());
00096 break;
00097 }
00098
00099 muse_pixtable *bigpixtable = muse_xcombine_tables(pixtables, offsets);
00100 if (!bigpixtable) {
00101 cpl_msg_error(__func__, "combining the pixel tables didn't work: %s",
00102 cpl_error_get_message());
00103 break;
00104 }
00105 cpl_free(pixtables);
00106
00107 if (strstr(aParams->save, "cube")) {
00108 muse_resampling_type resample
00109 = muse_postproc_get_resampling_type(aParams->resample_s);
00110 muse_resampling_params *rp = muse_resampling_params_new(resample);
00111 rp->dx = aParams->dx;
00112 rp->dy = aParams->dy;
00113 rp->dlambda = aParams->dlambda;
00114 rp->crtype = muse_postproc_get_cr_type(aParams->crtype_s);
00115 rp->crsigma = aParams->crsigma;
00116 rp->ld = aParams->ld;
00117 rp->rc = aParams->rc;
00118 rp->pfx = aParams->pixfrac;
00119 rp->pfy = aParams->pixfrac;
00120 rp->pfl = aParams->pixfrac;
00121 cpl_propertylist *outwcs = muse_postproc_cube_load_output_wcs(aProcessing);
00122 muse_resampling_params_set_wcs(rp, outwcs);
00123 cpl_propertylist_delete(outwcs);
00124 muse_cube_type format = muse_postproc_get_cube_format(aParams->format_s);
00125 rc = muse_postproc_cube_resample_and_collapse(aProcessing, bigpixtable,
00126 format, rp, aParams->filter);
00127 muse_resampling_params_delete(rp);
00128 }
00129 if (strstr(aParams->save, "combined")) {
00130 muse_processing_save_table(aProcessing, -1, bigpixtable, NULL,
00131 MUSE_TAG_PIXTABLE_COMBINED,
00132 MUSE_TABLE_TYPE_PIXTABLE);
00133 }
00134 muse_pixtable_delete(bigpixtable);
00135 cpl_table_delete(offsets);
00136 return rc == CPL_ERROR_NONE ? 0 : -1;
00137 } while (0);
00138 for (i = 0; i < nexposures; i++) {
00139 muse_pixtable_delete(pixtables[i]);
00140 }
00141 cpl_free(pixtables);
00142 cpl_table_delete(offsets);
00143 return -1;
00144 }