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 <stdio.h>
00030 #include <float.h>
00031 #include <math.h>
00032 #include <string.h>
00033 #include <cpl.h>
00034
00035 #include <muse.h>
00036 #include "muse_scibasic_z.h"
00037
00038
00039
00040
00041
00042
00059
00060 static int
00061 muse_scibasic_per_exposure(muse_processing *aProcessing,
00062 muse_scibasic_params_t *aParams,
00063 cpl_table *aTrace, cpl_table *aWave, cpl_table *aGeo,
00064 muse_image *aImage, cpl_array *aSkyLines,
00065 cpl_table *aAttached, muse_datacube **aTwilights)
00066 {
00067 cpl_ensure(aImage && aTrace && aWave, CPL_ERROR_NULL_INPUT, -1);
00068
00069
00070 muse_basicproc_qc_saturated(aImage, QC_SCIBASIC_PREFIX);
00071
00072
00073
00074 char *intag = cpl_strdup(cpl_propertylist_get_string(aImage->header,
00075 MUSE_HDR_TMP_INTAG));
00076 if (aParams->saveimage) {
00077 char *tag = cpl_sprintf("%s_RED", intag);
00078 muse_processing_save_image(aProcessing, aParams->nifu, aImage, tag);
00079 cpl_free(tag);
00080 }
00081
00082
00083 muse_pixtable *pixtable = muse_pixtable_create(aImage, aTrace, aWave, aGeo);
00084 if (!pixtable) {
00085 cpl_msg_error(__func__, "Pixel table was not created for IFU %d: %s",
00086 aParams->nifu, cpl_error_get_message());
00087 cpl_free(intag);
00088 return -1;
00089 }
00090 if (aSkyLines) {
00091 cpl_array *rejpars = muse_cplarray_new_from_delimited_string(aParams->skyreject, ",");
00092 int nrej = cpl_array_get_size(rejpars);
00093 float lsigma = 15.,
00094 hsigma = 15.;
00095 unsigned char niter = 1;
00096 if (nrej > 0 && cpl_array_get_string(rejpars, 0)) {
00097
00098 hsigma = atof(cpl_array_get_string(rejpars, 0));
00099 }
00100 if (nrej > 1 && cpl_array_get_string(rejpars, 1)) {
00101
00102 lsigma = atof(cpl_array_get_string(rejpars, 1));
00103 }
00104 if (nrej > 2 && cpl_array_get_string(rejpars, 2)) {
00105
00106 niter = atoi(cpl_array_get_string(rejpars, 2));
00107 }
00108 cpl_array_delete(rejpars);
00109 muse_basicproc_shift_pixtable(pixtable, aSkyLines, aParams->skyhalfwidth,
00110 aParams->skybinsize, lsigma, hsigma, niter);
00111 }
00112
00113 if (aParams->resample) {
00114
00115
00116 muse_image *image = muse_resampling_image(pixtable,
00117 MUSE_RESAMPLE_WEIGHTED_RENKA,
00118 1., aParams->dlambda);
00119 char *tag = cpl_sprintf("%s_RESAMPLED", intag);
00120
00121 cpl_propertylist_erase_regexp(image->header, QC_SCIBASIC_PREFIX, 0);
00122 muse_processing_save_image(aProcessing, aParams->nifu, image, tag);
00123 cpl_free(tag);
00124 muse_image_delete(image);
00125 }
00126
00127
00128
00129
00130 const double llimN = 4750. - FLT_EPSILON, hlimN = 9350. + FLT_EPSILON,
00131 llimE = 4600. - FLT_EPSILON, hlimE = 9350. + FLT_EPSILON;
00132 if (aParams->crop) {
00133 if (muse_pfits_get_mode(pixtable->header) > MUSE_MODE_WFM_NONAO_X) {
00134 cpl_msg_info(__func__, "Nominal mode: cropping the pixel table of IFU %d"
00135 " to %.1f...%.1f Angstrom", aParams->nifu, llimN, hlimN);
00136 muse_pixtable_restrict_wavelength(pixtable, llimN, hlimN);
00137 } else {
00138 cpl_msg_info(__func__, "Extended mode: cropping the pixel table of IFU %d"
00139 " to %.1f...%.1f Angstrom", aParams->nifu, llimE, hlimE);
00140 muse_pixtable_restrict_wavelength(pixtable, llimE, hlimE);
00141 }
00142 }
00143
00144 if (aAttached) {
00145 muse_basicproc_apply_illum(pixtable, aAttached);
00146 }
00147
00148
00149 int icor;
00150 for (icor = 0; aTwilights && aTwilights[icor]; icor++) {
00151 cpl_msg_info(__func__, "Starting twilight correction %d in IFU %d",
00152 icor + 1, aParams->nifu);
00153 muse_basicproc_apply_twilight(pixtable, aTwilights[icor]);
00154 }
00155
00156
00157 char *outtag = cpl_sprintf("PIXTABLE_%s", intag);
00158 muse_processing_save_table(aProcessing, aParams->nifu, pixtable, NULL, outtag,
00159 MUSE_TABLE_TYPE_PIXTABLE);
00160 cpl_free(outtag);
00161 cpl_free(intag);
00162 muse_pixtable_delete(pixtable);
00163
00164 return 0;
00165 }
00166
00167
00174
00175 int
00176 muse_scibasic_compute(muse_processing *aProcessing,
00177 muse_scibasic_params_t *aParams)
00178 {
00179 muse_imagelist *images = NULL;
00180 if (muse_processing_check_intags(aProcessing, "REDUCED", 8)) {
00181 cpl_msg_warning(__func__, "Found REDUCED files on input, ignoring all "
00182 "others inputs!");
00183 images = muse_basicproc_load_reduced(aProcessing, aParams->nifu);
00184 cpl_size i;
00185 cpl_size n = muse_imagelist_get_size(images);
00186 for (i = 0; i < n; i++) {
00187 muse_image *img = muse_imagelist_get(images, i);
00188 cpl_propertylist_update_string(img->header, MUSE_HDR_TMP_INTAG,
00189 cpl_array_get_string(aProcessing->intags, 0));
00190 }
00191 } else {
00192 muse_basicproc_params *bpars = muse_basicproc_params_new(aProcessing->parameters,
00193 "muse.muse_scibasic");
00194 images = muse_basicproc_load(aProcessing, aParams->nifu, bpars);
00195 muse_basicproc_params_delete(bpars);
00196 }
00197 cpl_ensure(images, cpl_error_get_code(), -1);
00198
00199 cpl_table *tracetable = muse_table_load(aProcessing, MUSE_TAG_TRACE_TABLE,
00200 aParams->nifu);
00201 cpl_table *wavecaltable = muse_table_load(aProcessing, MUSE_TAG_WAVECAL_TABLE,
00202 aParams->nifu);
00203 cpl_table *geotable = muse_table_load(aProcessing, MUSE_TAG_GEOMETRY_TABLE, 0);
00204 if (!tracetable || !wavecaltable || !geotable) {
00205 cpl_msg_error(__func__, "Calibration could not be loaded for IFU %d:%s%s%s",
00206 aParams->nifu, !tracetable ? " "MUSE_TAG_TRACE_TABLE : "",
00207 !wavecaltable ? " "MUSE_TAG_WAVECAL_TABLE : "",
00208 !geotable ? " "MUSE_TAG_GEOMETRY_TABLE : "");
00209
00210 muse_imagelist_delete(images);
00211 cpl_table_delete(tracetable);
00212 cpl_table_delete(wavecaltable);
00213 cpl_table_delete(geotable);
00214 return -1;
00215 }
00216
00217
00218 cpl_frameset *fset = muse_frameset_find(aProcessing->inframes,
00219 MUSE_TAG_TWILIGHT_CUBE, 0, 0);
00220 int i, ntwilight = cpl_frameset_get_size(fset);
00221 muse_datacube **twilights = cpl_calloc(ntwilight + 1, sizeof(muse_datacube *));
00222 for (i = 0; i < ntwilight; i++) {
00223 cpl_frame *ftwilight = cpl_frameset_get_position(fset, i);
00224 const char *fn = cpl_frame_get_filename(ftwilight);
00225 twilights[i] = muse_datacube_load(fn);
00226 if (!twilights[i]) {
00227 cpl_msg_warning(__func__, "Could not load %s from \"%s\"",
00228 MUSE_TAG_TWILIGHT_CUBE, fn);
00229 break;
00230 }
00231 const char *catg = muse_pfits_get_pro_catg(twilights[i]->header);
00232 if (catg && strncmp(MUSE_TAG_TWILIGHT_CUBE, catg,
00233 strlen(MUSE_TAG_TWILIGHT_CUBE) + 1)) {
00234 cpl_msg_warning(__func__, "Supposed %s (\"%s\") has wrong PRO.CATG: %s",
00235 MUSE_TAG_TWILIGHT_CUBE, fn, catg);
00236 }
00237 muse_processing_append_used(aProcessing, ftwilight, CPL_FRAME_GROUP_CALIB, 1);
00238 }
00239 cpl_frameset_delete(fset);
00240
00241 cpl_array *lines = muse_cplarray_new_from_delimited_string(aParams->skylines, ","),
00242 *skylines = muse_cplarray_string_to_double(lines);
00243 cpl_array_delete(lines);
00244 int rc = 0;
00245 muse_combinepar *cpars = muse_combinepar_new(aProcessing->parameters,
00246 "muse.muse_scibasic");
00247 if (cpars->combine == MUSE_COMBINE_NONE) {
00248
00249
00250 cpl_table *tattached = muse_basicproc_get_illum(images, tracetable,
00251 wavecaltable, geotable);
00252 unsigned int k, nimages = muse_imagelist_get_size(images);
00253 for (k = 0; k < nimages && !rc; k++) {
00254 muse_image *image = muse_imagelist_get(images, k);
00255 rc = muse_scibasic_per_exposure(aProcessing, aParams, tracetable,
00256 wavecaltable, geotable, image, skylines,
00257 tattached, twilights);
00258 }
00259 cpl_table_delete(tattached);
00260 } else {
00261 int ntags = cpl_array_get_size(aProcessing->intags);
00262 if (ntags > 1) {
00263 cpl_msg_warning(__func__, "Combining images of %d different tags, but "
00264 "will use %s for output!", ntags,
00265 cpl_array_get_string(aProcessing->intags, 0));
00266 } else {
00267 cpl_msg_debug(__func__, "Combining images with %d tag", ntags);
00268 }
00269 muse_image *image = muse_combine_images(cpars, images);
00270
00271 cpl_propertylist_update_string(image->header, MUSE_HDR_TMP_INTAG,
00272 cpl_array_get_string(aProcessing->intags, 0));
00273 rc = muse_scibasic_per_exposure(aProcessing, aParams, tracetable,
00274 wavecaltable, geotable, image, skylines,
00275 NULL, twilights);
00276 muse_image_delete(image);
00277 }
00278 cpl_array_delete(skylines);
00279 muse_combinepar_delete(cpars);
00280
00281
00282 muse_imagelist_delete(images);
00283 cpl_table_delete(tracetable);
00284 cpl_table_delete(wavecaltable);
00285 cpl_table_delete(geotable);
00286 for (i = 0; twilights[i]; i++) {
00287 muse_datacube_delete(twilights[i]);
00288 }
00289 cpl_free(twilights);
00290
00291 return rc;
00292 }