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_standard_z.h"
00033
00034
00035
00036
00037
00038
00051
00052 int
00053 muse_standard_compute(muse_processing *aProcessing,
00054 muse_standard_params_t *aParams)
00055 {
00056 cpl_errorstate state = cpl_errorstate_get();
00057
00058 muse_postproc_properties *prop = muse_postproc_properties_new(MUSE_POSTPROC_STANDARD);
00059
00060 prop->lambdamin = aParams->lambdamin;
00061 prop->lambdamax = aParams->lambdamax;
00062 prop->lambdaref = aParams->lambdaref;
00063 prop->darcheck = MUSE_POSTPROC_DARCHECK_NONE;
00064 if (aParams->darcheck == MUSE_STANDARD_PARAM_DARCHECK_CHECK) {
00065 prop->darcheck = MUSE_POSTPROC_DARCHECK_CHECK;
00066 } else if (aParams->darcheck == MUSE_STANDARD_PARAM_DARCHECK_CORRECT) {
00067 prop->darcheck = MUSE_POSTPROC_DARCHECK_CORRECT;
00068 }
00069
00070 prop->profile = MUSE_FLUX_PROFILE_GAUSSIAN;
00071 if (aParams->profile == MUSE_STANDARD_PARAM_PROFILE_MOFFAT) {
00072 prop->profile = MUSE_FLUX_PROFILE_MOFFAT;
00073 } else if (aParams->profile == MUSE_STANDARD_PARAM_PROFILE_CIRCLE) {
00074 prop->profile = MUSE_FLUX_PROFILE_CIRCLE;
00075 } else if (aParams->profile == MUSE_STANDARD_PARAM_PROFILE_SQUARE) {
00076 prop->profile = MUSE_FLUX_PROFILE_EQUAL_SQUARE;
00077 } else if (aParams->profile != MUSE_STANDARD_PARAM_PROFILE_GAUSSIAN) {
00078 cpl_msg_error(__func__, "unknown profile \"%s\"", aParams->profile_s);
00079 muse_postproc_properties_delete(prop);
00080 return -1;
00081 }
00082
00083 prop->refframe = muse_frameset_find_master(aProcessing->inframes,
00084 MUSE_TAG_STD_FLUX_TABLE, 0);
00085 if (!prop->refframe) {
00086 cpl_msg_error(__func__, "Required input %s not found in input files",
00087 MUSE_TAG_STD_FLUX_TABLE);
00088 cpl_error_set_message(__func__, CPL_ERROR_NULL_INPUT,
00089 MUSE_TAG_STD_FLUX_TABLE" missing");
00090 muse_postproc_properties_delete(prop);
00091 return -1;
00092 }
00093 muse_processing_append_used(aProcessing, prop->refframe,
00094 CPL_FRAME_GROUP_CALIB, 1);
00095 prop->select = MUSE_FLUX_SELECT_BRIGHTEST;
00096 if (aParams->select == MUSE_STANDARD_PARAM_SELECT_DISTANCE) {
00097 prop->select = MUSE_FLUX_SELECT_NEAREST;
00098 } else if (aParams->select != MUSE_STANDARD_PARAM_SELECT_FLUX) {
00099 cpl_msg_error(__func__, "unknown selection \"%s\"", aParams->select_s);
00100 muse_postproc_properties_delete(prop);
00101 return -1;
00102 }
00103 prop->smooth = MUSE_FLUX_SMOOTH_PPOLY;
00104 if (aParams->smooth == MUSE_STANDARD_PARAM_SMOOTH_MEDIAN) {
00105 prop->smooth = MUSE_FLUX_SMOOTH_MEDIAN;
00106 } else if (aParams->smooth == MUSE_STANDARD_PARAM_SMOOTH_NONE) {
00107 prop->smooth = MUSE_FLUX_SMOOTH_NONE;
00108 } else if (aParams->smooth != MUSE_STANDARD_PARAM_SMOOTH_PPOLY) {
00109 cpl_msg_error(__func__, "unknown smoothing \"%s\"", aParams->smooth_s);
00110 muse_postproc_properties_delete(prop);
00111 return -1;
00112 }
00113 prop->extinction = muse_table_load(aProcessing, MUSE_TAG_EXTINCT_TABLE, 0);
00114
00115 prop->tellregions = muse_table_load(aProcessing, MUSE_TAG_TELLREG, 0);
00116 if (!prop->tellregions) {
00117 cpl_msg_debug(__func__, "%s could not be found or loaded, using defaults",
00118 MUSE_TAG_TELLREG);
00119 }
00120
00121
00122 prop->exposures = muse_processing_sort_exposures(aProcessing);
00123 if (!prop->exposures) {
00124 cpl_msg_error(__func__, "No standard star exposures found in input!");
00125 muse_postproc_properties_delete(prop);
00126 return -1;
00127 }
00128 int nexposures = cpl_table_get_nrow(prop->exposures);
00129
00130
00131 muse_flux_object **fluxobjs = cpl_calloc(nexposures, sizeof(muse_flux_object *));
00132 int i;
00133 for (i = 0; i < nexposures; i++) {
00134 fluxobjs[i] = muse_postproc_process_exposure(prop, i, NULL);
00135 if (!fluxobjs[i]) {
00136 int i2;
00137 for (i2 = 0; i2 <= i; i2++) {
00138 muse_flux_object_delete(fluxobjs[i2]);
00139 }
00140 cpl_free(fluxobjs);
00141 muse_postproc_properties_delete(prop);
00142 return -1;
00143 }
00144 }
00145 muse_postproc_properties_delete(prop);
00146
00147
00148
00149 cpl_array *filters = muse_cplarray_new_from_delimited_string(aParams->filter,
00150 ",");
00151 for (i = 0; i < nexposures; i++) {
00152 int ifilt, ipos = 0, nfilt = cpl_array_get_size(filters);
00153 for (ifilt = 0; ifilt < nfilt; ifilt++) {
00154
00155 cpl_table *filter = muse_table_load_filter(aProcessing,
00156 cpl_array_get_string(filters, ifilt));
00157 if (!filter) {
00158 continue;
00159 }
00160 muse_image *fov = muse_datacube_collapse(fluxobjs[i]->cube, filter);
00161 if (!fluxobjs[i]->cube->recimages) {
00162 fluxobjs[i]->cube->recimages = muse_imagelist_new();
00163 fluxobjs[i]->cube->recnames = cpl_array_new(0, CPL_TYPE_STRING);
00164 }
00165 muse_imagelist_set(fluxobjs[i]->cube->recimages, fov, ipos);
00166 cpl_array_set_size(fluxobjs[i]->cube->recnames, ipos+1);
00167 cpl_array_set_string(fluxobjs[i]->cube->recnames, ipos,
00168 cpl_array_get_string(filters, ifilt));
00169 cpl_table_delete(filter);
00170 ipos++;
00171 }
00172 muse_postproc_qc_fwhm(aProcessing, fluxobjs[i]->cube);
00173 muse_datacube_convert_dq(fluxobjs[i]->cube);
00174 muse_processing_save_cube(aProcessing, -1, fluxobjs[i]->cube,
00175 MUSE_TAG_CUBE_STD, MUSE_CUBE_TYPE_FITS);
00176
00177 muse_processing_save_image(aProcessing, -1, fluxobjs[i]->intimage,
00178 MUSE_TAG_STD_INTIMAGE);
00179
00180
00181 cpl_propertylist *header = cpl_propertylist_duplicate(fluxobjs[i]->cube->header);
00182 cpl_propertylist_erase_regexp(header, "^NAXIS|^EXPTIME$|"MUSE_WCS_KEYS, 0);
00183 char *objorig = cpl_strdup(cpl_propertylist_get_string(header, "OBJECT"));
00184 char *object = cpl_sprintf("Response curve (%s)", objorig);
00185 cpl_propertylist_update_string(header, "OBJECT", object);
00186 cpl_error_code rc = rc = muse_processing_save_table(aProcessing, -1,
00187 fluxobjs[i]->response,
00188 header,
00189 MUSE_TAG_STD_RESPONSE,
00190 MUSE_TABLE_TYPE_CPL);
00191 cpl_free(object);
00192 if (rc != CPL_ERROR_NONE) {
00193 cpl_free(objorig);
00194 cpl_propertylist_delete(header);
00195 for ( ; i < nexposures; i++) {
00196 muse_flux_object_delete(fluxobjs[i]);
00197 }
00198 break;
00199 }
00200 object = cpl_sprintf("Telluric correction (%s)", objorig);
00201 cpl_propertylist_update_string(header, "OBJECT", object);
00202 rc = muse_processing_save_table(aProcessing, -1, fluxobjs[i]->telluric, header,
00203 MUSE_TAG_STD_TELLURIC, MUSE_TABLE_TYPE_CPL);
00204 cpl_free(object);
00205 cpl_free(objorig);
00206 cpl_propertylist_delete(header);
00207 muse_flux_object_delete(fluxobjs[i]);
00208 if (rc != CPL_ERROR_NONE) {
00209 for ( ; i < nexposures; i++) {
00210 muse_flux_object_delete(fluxobjs[i]);
00211 }
00212 break;
00213 }
00214 }
00215 cpl_array_delete(filters);
00216 cpl_free(fluxobjs);
00217
00218 return cpl_errorstate_is_equal(state) ? 0 : -1;
00219 }