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
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 #ifdef HAVE_CONFIG_H
00111 # include <config.h>
00112 #endif
00113
00114
00120
00124
00125
00126
00127
00128 #include <uves_merge.h>
00129
00130 #include <uves_pfits.h>
00131 #include <uves_utils.h>
00132 #include <uves_utils_wrappers.h>
00133 #include <uves_dump.h>
00134 #include <uves_error.h>
00135
00136 #include <irplib_access.h>
00137 #include <cpl.h>
00138 #include <float.h>
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00175
00176
00177 cpl_image *
00178 uves_merge_orders(const cpl_image *spectrum, const cpl_image *spectrum_noise,
00179 const uves_propertylist *spectrum_header,
00180 merge_method m_method,
00181 int n_traces,
00182 uves_propertylist **merged_header,
00183 cpl_image **merged_noise)
00184 {
00185 cpl_image *merged = NULL;
00186
00187 const double *spectrum_data = NULL;
00188 const cpl_mask *spectrum_badmap = NULL;
00189 const cpl_binary *spectrum_bad = NULL;
00190
00191 const double *noise_data = NULL;
00192 const cpl_mask *noise_badmap = NULL;
00193 const cpl_binary *noise_bad = NULL;
00194
00195 int nbins, ny, norders;
00196 double wavestep;
00197 int bin_min = 0, bin_max = 0;
00198 int total_bins;
00199 int order, trace;
00200
00201 passure( spectrum != NULL, " ");
00202 passure( spectrum_noise != NULL, " ");
00203 passure( spectrum_header != NULL, " ");
00204 passure( merged_header != NULL, " ");
00205 passure( merged_noise != NULL, " ");
00206
00207 assure( m_method == MERGE_OPTIMAL || m_method == MERGE_SUM, CPL_ERROR_ILLEGAL_INPUT,
00208 "Unknown merge method: %d", m_method);
00209
00210 assure( cpl_image_get_type(spectrum) == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
00211 "Spectrum must have type double. It is '%s'",
00212 uves_tostring_cpl_type(cpl_image_get_type(spectrum)));
00213
00214 assure( cpl_image_get_type(spectrum_noise) == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
00215 "Spectrum noise must have type double. It is '%s'",
00216 uves_tostring_cpl_type(cpl_image_get_type(spectrum_noise)));
00217
00218
00219 nbins = cpl_image_get_size_x(spectrum);
00220 ny = cpl_image_get_size_y(spectrum);
00221
00222 assure( cpl_image_get_size_x(spectrum_noise) == nbins &&
00223 cpl_image_get_size_y(spectrum_noise) == ny,
00224 CPL_ERROR_INCOMPATIBLE_INPUT,
00225 "Incompatible spectrum/noise image sizes: %dx%d vs. %dx%d",
00226 nbins, ny,
00227 cpl_image_get_size_x(spectrum_noise),
00228 cpl_image_get_size_y(spectrum_noise));
00229
00230 assure( ny % n_traces == 0, CPL_ERROR_INCOMPATIBLE_INPUT,
00231 "Spectrum image height (%d) is not a multiple of "
00232 "the number of traces (%d). Confused, bailing out",
00233 ny, n_traces);
00234
00235 norders = ny / n_traces;
00236
00237 check( wavestep = uves_pfits_get_cdelt1(spectrum_header),
00238 "Error reading bin width");
00239
00240
00241 spectrum_data = irplib_image_get_data_double_const(spectrum);
00242 spectrum_badmap = irplib_image_get_bpm_const(spectrum);
00243 spectrum_bad = irplib_mask_get_data_const(spectrum_badmap);
00244
00245 noise_data = irplib_image_get_data_double_const(spectrum_noise);
00246 noise_badmap = irplib_image_get_bpm_const(spectrum_noise);
00247 noise_bad = irplib_mask_get_data_const(noise_badmap);
00248
00249
00250 for (order = 1; order <= norders; order++)
00251 {
00252 double wstart, wend;
00253 check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00254 "Error reading start wavelength for order #%d", order);
00255
00256 check( wend = uves_pfits_get_wend(spectrum_header, order),
00257 "Error reading end wavelength for order #%d", order);
00258
00259 uves_msg_debug("Order #%d: wstart - wend = %f - %f wlu", order, wstart, wend);
00260
00261 if (order == 1)
00262 {
00263 bin_min = uves_round_double(wstart/wavestep);
00264 bin_max = uves_round_double(wend /wavestep);
00265 }
00266
00267 bin_min = uves_min_int(bin_min, uves_round_double(wstart/wavestep));
00268 bin_max = uves_max_int(bin_max, uves_round_double(wend /wavestep));
00269 }
00270
00271 total_bins = (bin_max - bin_min) + 1;
00272
00273 uves_msg_debug("Merging orders into %d bins covering wavelengths %.3f - %.3f wlu",
00274 total_bins, bin_min * wavestep, bin_max * wavestep);
00275
00276
00277 merged = cpl_image_new(total_bins, n_traces, CPL_TYPE_DOUBLE);
00278 *merged_noise = cpl_image_new(total_bins, n_traces, CPL_TYPE_DOUBLE);
00279 cpl_image_add_scalar(*merged_noise, -1.0);
00280
00281
00282 for (order = 1; order <= norders; order++)
00283 {
00284 double wstart, wend;
00285 int wstart_bin, wend_bin;
00286
00287 check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00288 "Error reading start wavelength for order #%d", order);
00289
00290 check( wend = uves_pfits_get_wend(spectrum_header, order),
00291 "Error reading end wavelength for order #%d", order);
00292
00293 wstart_bin = uves_round_double(wstart/wavestep);
00294 wend_bin = uves_round_double(wend/wavestep);
00295
00296
00297 for (trace = 1; trace <= n_traces; trace++)
00298 {
00299 int spectrum_row = (order - 1)*n_traces + trace;
00300 int rel_bin;
00301
00302 for (rel_bin = 1; rel_bin <= wend_bin - wstart_bin + 1; rel_bin++)
00303 {
00304 double flux, noise;
00305 double current_flux, new_flux;
00306 double current_noise, new_noise;
00307 double weight;
00308 int pis_rejected, noise_rejected;
00309
00310
00311 int merged_bin = (wstart_bin - bin_min) + rel_bin;
00312
00313 passure(1 <= merged_bin && merged_bin <= total_bins,
00314 "%d %d %d", rel_bin, merged_bin, total_bins);
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 flux = spectrum_data[(rel_bin-1) + (spectrum_row-1) * nbins];
00325 pis_rejected = spectrum_bad [(rel_bin-1) + (spectrum_row-1) * nbins];
00326
00327 noise = noise_data[(rel_bin-1) + (spectrum_row-1) * nbins];
00328 noise_rejected = noise_bad [(rel_bin-1) + (spectrum_row-1) * nbins];
00329
00330 if (!pis_rejected && !noise_rejected)
00331 {
00332 check(( current_flux = cpl_image_get(
00333 merged , merged_bin, trace, &pis_rejected),
00334 current_noise = cpl_image_get(
00335 *merged_noise, merged_bin, trace, &pis_rejected)),
00336 "Error reading merged spetrum");
00337
00338 weight = 1/(noise*noise);
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 if (current_noise > 0)
00360 {
00361 if (m_method == MERGE_OPTIMAL)
00362 {
00363 new_noise = 1/(current_noise*current_noise);
00364 new_noise += weight;
00365 new_noise = 1/sqrt(new_noise);
00366 }
00367 else if (m_method == MERGE_SUM)
00368 {
00369 new_noise = sqrt(current_noise*current_noise
00370 + noise*noise);
00371 }
00372 else
00373 {
00374
00375 passure( false, "%d", m_method);
00376 }
00377 }
00378 else
00379 {
00380
00381 new_noise = noise;
00382 }
00383
00384 if (current_noise > 0)
00385 {
00386 if (m_method == MERGE_OPTIMAL)
00387 {
00388 new_flux = (current_flux /
00389 (current_noise*current_noise)
00390 + flux * weight) *
00391 (new_noise*new_noise);
00392 }
00393 else if (m_method == MERGE_SUM)
00394 {
00395 new_flux = current_flux + flux;
00396 }
00397 else
00398 {
00399
00400 passure( false, "%d", m_method);
00401 }
00402 }
00403 else
00404 {
00405 new_flux = flux;
00406 }
00407
00408 check( cpl_image_set(
00409 merged , merged_bin, trace, new_flux),
00410 "Error updating merged spectrum");
00411 check( cpl_image_set(
00412 *merged_noise, merged_bin, trace, new_noise),
00413 "Error updating weights");
00414
00415
00416
00417
00418
00419
00420 }
00421
00422 }
00423
00424 }
00425
00426 }
00427
00428
00429
00430
00431 check( cpl_image_threshold(*merged_noise,
00432 0, DBL_MAX,
00433 1, 1),
00434 "Error setting undefined noise");
00435
00436 if (merged_header != NULL)
00437 {
00438 check( *merged_header = uves_initialize_image_header(
00439 "WAVELENGTH", (n_traces > 1) ? "PIXEL" : " ", "FLUX",
00440 bin_min * wavestep, 1.0,
00441 1.0, 1.0,
00442 wavestep, 1.0),
00443 "Error initializing merged spectrum header");
00444 }
00445
00446 cleanup:
00447 return merged;
00448 }
00449
00450
00460
00461 merge_method
00462 uves_get_merge_method(const cpl_parameterlist *parameters, const char *context,
00463 const char *subcontext)
00464 {
00465 const char *mm = "";
00466 merge_method result = 0;
00467
00468 check( uves_get_parameter(parameters, context, subcontext, "merge", CPL_TYPE_STRING, &mm),
00469 "Could not read parameter");
00470
00471 if (strcmp(mm, "optimal") == 0) result = MERGE_OPTIMAL;
00472 else if (strcmp(mm, "sum" ) == 0) result = MERGE_SUM;
00473 else
00474 {
00475 assure(false, CPL_ERROR_ILLEGAL_INPUT, "No such merging method: '%s'", mm);
00476 }
00477
00478 cleanup:
00479 return result;
00480 }
00481
00482