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 <muse.h>
00030 #include "muse_scipost_subtract_sky_z.h"
00031
00032
00040
00041 int
00042 muse_scipost_subtract_sky_compute(muse_processing *aProcessing,
00043 muse_scipost_subtract_sky_params_t *aParams)
00044 {
00045 cpl_table *continuum = muse_sky_continuum_load(aProcessing);
00046 cpl_table *lines = muse_sky_lines_load(aProcessing);
00047 cpl_errorstate prestate = cpl_errorstate_get();
00048 muse_lsf_cube **lsfCubes = NULL;
00049 #ifdef USE_LSF_PARAMS
00050 muse_lsf_params **lsfParams = NULL;
00051 #endif
00052 if (lines != NULL) {
00053 lsfCubes = muse_lsf_cube_load_all(aProcessing);
00054 if (lsfCubes == NULL) {
00055 #ifdef USE_LSF_PARAMS
00056 lsfParams = muse_processing_lsf_params_load(aProcessing, 0);
00057 if (lsfParams == NULL) {
00058 #endif
00059 cpl_msg_error(__func__, "Could not load LSF");
00060 cpl_table_delete(continuum);
00061 cpl_table_delete(lines);
00062 return CPL_ERROR_NULL_INPUT;
00063 #ifdef USE_LSF_PARAMS
00064 }
00065 cpl_errorstate_set(prestate);
00066 #endif
00067 }
00068 } else {
00069 cpl_msg_warning(__func__, "Could not load sky lines");
00070 }
00071 cpl_frameset *inframes = muse_frameset_find_tags(aProcessing->inframes,
00072 aProcessing->intags, 0,
00073 CPL_FALSE);
00074 cpl_error_code rc = CPL_ERROR_NONE;
00075 cpl_size iframe, nframes = cpl_frameset_get_size(inframes);
00076 for (iframe = 0; iframe < nframes; iframe++) {
00077 cpl_frame *frame = cpl_frameset_get_position(inframes, iframe);
00078 const char *fn = cpl_frame_get_filename(frame);
00079 muse_pixtable *pixtable =
00080 muse_pixtable_load_restricted_wavelength(fn,
00081 aParams->lambdamin,
00082 aParams->lambdamax);
00083 if (pixtable == NULL) {
00084 cpl_msg_error(__func__, "NULL pixel table for %s", fn);
00085 rc = CPL_ERROR_NULL_INPUT;
00086 break;
00087 }
00088 muse_processing_append_used(aProcessing, frame, CPL_FRAME_GROUP_RAW, 1);
00089
00090
00091 cpl_propertylist_erase_regexp(pixtable->header, "ESO QC ", 0);
00092 if (muse_pixtable_is_fluxcal(pixtable) != CPL_TRUE) {
00093 cpl_msg_error(__func__, "Pixel table %s not flux calibrated",
00094 cpl_frame_get_filename(frame));
00095 muse_pixtable_delete(pixtable);
00096 rc = CPL_ERROR_ILLEGAL_INPUT;
00097 break;
00098 }
00099
00100 if (muse_pixtable_is_skysub(pixtable) == CPL_TRUE) {
00101 cpl_msg_error(__func__, "Pixel table %s already sky subtracted",
00102 cpl_frame_get_filename(frame));
00103 muse_pixtable_delete(pixtable);
00104 rc = CPL_ERROR_ILLEGAL_INPUT;
00105 break;
00106 }
00107
00108
00109 double fluxs = cpl_propertylist_get_double(pixtable->header,
00110 MUSE_HDR_FLAT_FLUX_SKY);
00111 double fluxl = cpl_propertylist_get_double(pixtable->header,
00112 MUSE_HDR_FLAT_FLUX_LAMP);
00113 cpl_errorstate_set(prestate);
00114
00115 if (aParams->orig != NULL) {
00116 cpl_table_duplicate_column(pixtable->table, aParams->orig,
00117 pixtable->table, MUSE_PIXTABLE_DATA);
00118 }
00119
00120 double scale = 1.;
00121
00122 if (aParams->flux_sky > 0. && fluxs > 0.) {
00123 scale = fluxs / aParams->flux_sky;
00124 cpl_msg_debug(__func__, "Scaling by SKY %e/%e = %f",
00125 fluxs, aParams->flux_sky, scale);
00126 } else if (aParams->flux_lamp > 0. && fluxl > 0.) {
00127 scale = fluxl / aParams->flux_lamp;
00128 cpl_msg_debug(__func__, "Scaling by LAMP %e/%e = %f",
00129 fluxl, aParams->flux_lamp, scale);
00130 }
00131
00132 cpl_table_divide_scalar(pixtable->table, MUSE_PIXTABLE_DATA, scale);
00133
00134 if (continuum != NULL) {
00135 rc = muse_sky_subtract_continuum(pixtable, continuum);
00136 }
00137 if (rc != CPL_ERROR_NONE) {
00138 cpl_msg_error(__func__, "while muse_sky_subtract_continuum(%s)",
00139 cpl_frame_get_filename(frame));
00140 muse_pixtable_delete(pixtable);
00141 break;
00142 }
00143 if (lines != NULL) {
00144 if (lsfCubes != NULL) {
00145 rc = muse_sky_subtract_lines(pixtable, lines, lsfCubes);
00146 #ifdef USE_LSF_PARAMS
00147 } else if (lsfParams != NULL) {
00148 rc = muse_sky_subtract_lines_old(pixtable, lines, lsfParams);
00149 #endif
00150 }
00151 if (rc != CPL_ERROR_NONE) {
00152 cpl_msg_error(__func__, "while muse_sky_subtract_lines(%s)",
00153 cpl_frame_get_filename(frame));
00154 muse_pixtable_delete(pixtable);
00155 break;
00156 }
00157 }
00158 cpl_table_multiply_scalar(pixtable->table, MUSE_PIXTABLE_DATA, scale);
00159 if (pixtable->header) {
00160
00161 cpl_propertylist_update_bool(pixtable->header, MUSE_HDR_PT_SKYSUB,
00162 CPL_TRUE);
00163 cpl_propertylist_set_comment(pixtable->header, MUSE_HDR_PT_SKYSUB,
00164 MUSE_HDR_PT_SKYSUB_COMMENT);
00165 }
00166 muse_processing_save_table(aProcessing, -1, pixtable, NULL,
00167 MUSE_TAG_PIXTABLE_REDUCED,
00168 MUSE_TABLE_TYPE_PIXTABLE);
00169 muse_pixtable_delete(pixtable);
00170 }
00171 cpl_frameset_delete(inframes);
00172 cpl_table_delete(continuum);
00173 cpl_table_delete(lines);
00174 muse_lsf_cube_delete_all(lsfCubes);
00175
00176 return cpl_error_get_code();
00177 }