00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 #ifdef HAVE_CONFIG_H
00087 # include <config.h>
00088 #endif
00089
00090
00094
00097
00098
00099
00100
00101 #include <uves_response_utils.h>
00102
00103 #include <uves_utils.h>
00104 #include <uves_utils_wrappers.h>
00105 #include <uves_pfits.h>
00106 #include <uves_error.h>
00107 #include <uves_msg.h>
00108
00109 #include <cpl.h>
00110
00111 #include <stdbool.h>
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00141
00142 cpl_image *
00143 uves_calculate_response(const cpl_image *spectrum, const uves_propertylist *spectrum_header,
00144 const cpl_table *flux_table,
00145 const uves_propertylist *raw_header,
00146 double PACCURACY,
00147 bool inverse,
00148 char **ref_obj_id)
00149 {
00150 cpl_image *response = NULL;
00151 cpl_table *catalogue_flux = NULL;
00152 int nx, norders;
00153
00154 nx = cpl_image_get_size_x(spectrum);
00155 norders = cpl_image_get_size_y(spectrum);
00156
00157 response = cpl_image_new(nx, norders, CPL_TYPE_DOUBLE);
00158
00159 check( catalogue_flux = uves_align(raw_header, flux_table, PACCURACY, ref_obj_id),
00160 "Cannot read catalogue flux");
00161
00162
00163 {
00164 double dlambda;
00165 int order;
00166
00167 check( dlambda = uves_pfits_get_cdelt1(spectrum_header),
00168 "Error reading bin width from header");
00169
00170 for (order = 1; order <= norders; order++)
00171 {
00172 double lambda_start;
00173 int x;
00174
00175
00176
00177
00178 if (norders == 1)
00179 {
00180 check( lambda_start = uves_pfits_get_crval1(spectrum_header),
00181 "Error reading start wavelength from header");
00182 }
00183 else
00184 {
00185 check( lambda_start = uves_pfits_get_wstart(spectrum_header, order),
00186 "Error reading start wavelength from header");
00187 }
00188
00189 for (x = 1; x <= nx; x++)
00190 {
00191 int pis_rejected;
00192 double lambda;
00193 double flux, std_flux, resp;
00194 int istart = 0;
00195
00196 lambda = lambda_start + (x-1) * dlambda;
00197
00198 check( flux = cpl_image_get(spectrum, x, order, &pis_rejected),
00199 "Error reading flux");
00200
00201 if (!pis_rejected)
00202 {
00203
00204 check( std_flux = uves_spline_hermite_table(
00205 lambda, catalogue_flux,
00206 "LAMBDA", "F_LAMBDA", &istart),
00207 "Error interpolating catalogue flux");
00208
00209 if (inverse)
00210 {
00211 resp = (flux == 0) ? 0 : std_flux / flux;
00212 }
00213 else
00214 {
00215 resp = (std_flux == 0) ? 0 : flux / std_flux;
00216 }
00217
00218 check( cpl_image_set(response, x, order, resp),
00219 "Error writing response image");
00220 }
00221 else
00222 {
00223 cpl_image_reject(response, x, order);
00224 }
00225 }
00226 }
00227 }
00228
00229 cleanup:
00230 uves_free_table(&catalogue_flux);
00231 if (cpl_error_get_code() != CPL_ERROR_NONE)
00232 {
00233 uves_free_image(&response);
00234 }
00235 return response;
00236 }
00237
00238
00239
00258
00259 cpl_table *
00260 uves_align(const uves_propertylist *object_header, const cpl_table *flux_table,
00261 double accuracy,
00262 char **ref_name_dynamic)
00263 {
00264 double obj_ra, obj_dec;
00265 int nident = 0;
00266 int match_row = 0;
00267 double min_dist = 0;
00268 double std_ra = 0, std_dec = 0;
00269 const char *ref_type = NULL;
00270
00271 cpl_table *result = NULL;
00272
00273 int i;
00274
00275 assure_nomsg( ref_name_dynamic != NULL, CPL_ERROR_NULL_INPUT );
00276 *ref_name_dynamic = NULL;
00277
00278 check( obj_ra = uves_pfits_get_ra (object_header), "Could not read right ascension");
00279 check( obj_dec = uves_pfits_get_dec(object_header), "Could not read declination");
00280
00281 uves_msg("Object RA, DEC = (%e, %e)", obj_ra, obj_dec);
00282
00283 nident = 0;
00284 for (i = 0; i < cpl_table_get_nrow(flux_table); i++)
00285 {
00286 double ref_ra, ref_dec;
00287 double dist;
00288
00289 check( ref_ra = cpl_table_get_double(flux_table, "RA_DEG", i, NULL),
00290 "Could not read catalogue star right ascension");
00291 check( ref_dec = cpl_table_get_double(flux_table, "DEC_DEG", i, NULL),
00292 "Could not read catalogue star declination");
00293
00294
00295
00296
00297
00298
00299
00300 dist =
00301 SIN_DEG(obj_dec)*SIN_DEG(ref_dec) +
00302 COS_DEG(obj_dec)*COS_DEG(ref_dec)*COS_DEG(obj_ra - ref_ra);
00303
00304 dist = uves_max_double(dist, -1);
00305 dist = uves_min_double(dist, 1);
00306
00307 dist = ACOS_DEG(dist) * 3600;
00308
00309 uves_msg_debug("Angular separation = %f arcseconds", dist);
00310
00311
00312
00313 if (i == 0 || dist < min_dist)
00314 {
00315 min_dist = dist;
00316 std_ra = ref_ra;
00317 std_dec = ref_dec;
00318 }
00319
00320
00321
00322 if (dist < accuracy)
00323 {
00324 nident += 1;
00325 match_row = i;
00326 min_dist = dist;
00327 std_ra = ref_ra;
00328 std_dec = ref_dec;
00329 }
00330 }
00331
00332 assure( nident >= 1, CPL_ERROR_INCOMPATIBLE_INPUT,
00333 "No catalogue object within %f arcsecs. "
00334 "Nearest object is %f arcsecs away at (RA, DEC) = (%f, %f)",
00335 accuracy, min_dist, std_ra, std_dec);
00336
00337 assure( nident <= 1, CPL_ERROR_INCOMPATIBLE_INPUT,
00338 "%d matching catalogue objects found. Confused. "
00339 "Decrease pointing accuracy (currently %f arcsecs) to get fewer matches",
00340 nident, accuracy);
00341
00342 check( *ref_name_dynamic = cpl_strdup(
00343 cpl_table_get_string(flux_table, "OBJECT", match_row)),
00344 "Could not read reference object name");
00345
00346 check( ref_type = cpl_table_get_string(flux_table, "TYPE", match_row),
00347 "Could not read reference object type");
00348
00349 uves_msg("Object ID is '%s', type = '%s'. Residual between header/catalogue = %f arcsecs",
00350 *ref_name_dynamic, ref_type, min_dist);
00351
00352
00353
00354 {
00355 const char *columns[3] = {"LAMBDA", "BIN_WIDTH", "F_LAMBDA"};
00356 int ndata;
00357
00358 check( ndata = cpl_table_get_int(flux_table, "NDATA", match_row, NULL),
00359 "Error reading length of flux array");
00360
00361 result = cpl_table_new(ndata);
00362
00363 for(i = 0; i < 3; i++)
00364 {
00365 const cpl_array *data;
00366 int indx;
00367
00368 cpl_table_new_column(result, columns[i], CPL_TYPE_DOUBLE);
00369
00370 data = cpl_table_get_array(flux_table, columns[i], match_row);
00371
00372
00373
00374 uves_msg_debug("3d table array size = %" CPL_SIZE_FORMAT ", ndata = %d",
00375 cpl_array_get_size(data), ndata);
00376
00377 assure( cpl_array_get_size(data) >= ndata,
00378 CPL_ERROR_ILLEGAL_INPUT,
00379 "Flux table row %d: column '%s' depth (%" CPL_SIZE_FORMAT ") "
00380 "is less than NDATA (%d)",
00381 match_row, columns[i], cpl_array_get_size(data), ndata);
00382
00383 for (indx = 0; indx < ndata; indx++)
00384 {
00385
00386 cpl_table_set_double(result, columns[i], indx,
00387 cpl_array_get_float(data, indx, NULL));
00388 }
00389 }
00390 }
00391
00392 cleanup:
00393 if (cpl_error_get_code() != CPL_ERROR_NONE)
00394 {
00395 uves_free_table(&result);
00396 if (ref_name_dynamic != NULL)
00397 {
00398 cpl_free(*ref_name_dynamic);
00399 *ref_name_dynamic = NULL;
00400 }
00401 }
00402
00403 return result;
00404 }
00405