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
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 #ifdef HAVE_CONFIG_H
00166 # include <config.h>
00167 #endif
00168
00169
00175
00179
00180
00181
00182
00183 #include <uves_merge.h>
00184
00185 #include <uves_pfits.h>
00186 #include <uves_utils.h>
00187 #include <uves_utils_wrappers.h>
00188 #include <uves_dump.h>
00189 #include <uves_dfs.h>
00190 #include <uves_error.h>
00191
00192 #include <cpl.h>
00193 #include <float.h>
00194 #include <string.h>
00195
00196
00197
00198
00199
00200
00201
00202
00203
00230
00231
00232 cpl_image *
00233 uves_merge_orders(const cpl_image *spectrum,
00234 const cpl_image *spectrum_noise,
00235 const uves_propertylist *spectrum_header,
00236 merge_method m_method,
00237 int n_traces,
00238 uves_propertylist **merged_header,
00239 const double delt1,
00240 const double delt2,
00241 enum uves_chip chip,
00242 cpl_image **merged_noise)
00243 {
00244 cpl_image *merged = NULL;
00245
00246 const double *spectrum_data_double = NULL;
00247 const float *spectrum_data_float = NULL;
00248 const cpl_mask *spectrum_badmap = NULL;
00249 const cpl_binary *spectrum_bad = NULL;
00250
00251 const double *noise_data_double = NULL;
00252 const float *noise_data_float = NULL;
00253 const cpl_mask *noise_badmap = NULL;
00254 const cpl_binary *noise_bad = NULL;
00255 cpl_type type;
00256
00257 int nbins, ny, norders;
00258 double wavestep;
00259 int bin_min = 0, bin_max = 0;
00260 int total_bins;
00261 int order, trace;
00262 int spectrum_sx=0;
00263 int spectrum_sy=0;
00264 double delt1_bin=0;
00265 double delt2_bin=0;
00266 char* filename=NULL;
00267
00268 cpl_vector* image_1d=NULL;
00269 uves_propertylist* hext=NULL;
00270
00271 passure( spectrum != NULL, " ");
00272 passure( spectrum_noise != NULL, " ");
00273 passure( spectrum_header != NULL, " ");
00274 passure( merged_header != NULL, " ");
00275 passure( merged_noise != NULL, " ");
00276
00277 assure( m_method == MERGE_OPTIMAL ||
00278 m_method == MERGE_SUM ||
00279 m_method == MERGE_NOAPPEND,
00280 CPL_ERROR_ILLEGAL_INPUT,
00281 "Unknown merge method: %d", m_method);
00282
00283 assure( cpl_image_get_type(spectrum) == CPL_TYPE_DOUBLE ||
00284 cpl_image_get_type(spectrum) == CPL_TYPE_FLOAT,
00285 CPL_ERROR_TYPE_MISMATCH,
00286 "Spectrum must have type double or float. It is '%s'",
00287 uves_tostring_cpl_type(cpl_image_get_type(spectrum)));
00288
00289 assure( cpl_image_get_type(spectrum_noise) == CPL_TYPE_DOUBLE ||
00290 cpl_image_get_type(spectrum_noise) == CPL_TYPE_FLOAT,
00291 CPL_ERROR_TYPE_MISMATCH,
00292 "Spectrum noise must have type double. It is '%s'",
00293 uves_tostring_cpl_type(cpl_image_get_type(spectrum_noise)));
00294
00295 assure( cpl_image_get_type(spectrum) ==
00296 cpl_image_get_type(spectrum_noise),
00297 CPL_ERROR_TYPE_MISMATCH,
00298 "Spectrum and spectrum noise must have same type. They are "
00299 "%s and %s, respectively",
00300 uves_tostring_cpl_type(cpl_image_get_type(spectrum)),
00301 uves_tostring_cpl_type(cpl_image_get_type(spectrum_noise)) );
00302
00303 type = cpl_image_get_type(spectrum);
00304
00305
00306 nbins = cpl_image_get_size_x(spectrum);
00307 ny = cpl_image_get_size_y(spectrum);
00308
00309 assure( cpl_image_get_size_x(spectrum_noise) == nbins &&
00310 cpl_image_get_size_y(spectrum_noise) == ny,
00311 CPL_ERROR_INCOMPATIBLE_INPUT,
00312 "Incompatible spectrum/noise image sizes: %dx%d vs. %" CPL_SIZE_FORMAT "x%" CPL_SIZE_FORMAT "",
00313 nbins, ny,
00314 cpl_image_get_size_x(spectrum_noise),
00315 cpl_image_get_size_y(spectrum_noise) );
00316
00317 assure( ny % n_traces == 0, CPL_ERROR_INCOMPATIBLE_INPUT,
00318 "Spectrum image height (%d) is not a multiple of "
00319 "the number of traces (%d). Confused, bailing out",
00320 ny, n_traces);
00321
00322 norders = ny / n_traces;
00323
00324 check( wavestep = uves_pfits_get_cdelt1(spectrum_header),
00325 "Error reading bin width");
00326
00327
00328
00329 if (type == CPL_TYPE_DOUBLE) {
00330 spectrum_data_double = cpl_image_get_data_double_const(spectrum);
00331 }
00332 else {
00333 spectrum_data_float = cpl_image_get_data_float_const(spectrum);
00334 }
00335
00336 spectrum_sx=cpl_image_get_size_x(spectrum);
00337 spectrum_sy=cpl_image_get_size_y(spectrum);
00338
00339 spectrum_badmap = cpl_image_get_bpm_const(spectrum);
00340 spectrum_bad = cpl_mask_get_data_const(spectrum_badmap);
00341
00342 if (type == CPL_TYPE_DOUBLE) {
00343 noise_data_double = cpl_image_get_data_double_const(spectrum_noise);
00344 }
00345 else {
00346 noise_data_float = cpl_image_get_data_float_const(spectrum_noise);
00347 }
00348 noise_badmap = cpl_image_get_bpm_const(spectrum_noise);
00349 noise_bad = cpl_mask_get_data_const(noise_badmap);
00350 uves_msg("delt1=%f delt2=%f",delt1,delt2);
00351
00352 for (order = 1; order <= norders; order++)
00353 {
00354 double wstart, wend;
00355 check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00356 "Error reading start wavelength for order #%d", order);
00357
00358 check( wend = uves_pfits_get_wend(spectrum_header, order),
00359 "Error reading end wavelength for order #%d", order);
00360
00361 uves_msg_debug("Order #%d: wstart - wend = %f - %f wlu", order, wstart, wend);
00362
00363
00364
00365
00366 if (order == 1)
00367 {
00368 bin_min = uves_round_double(wstart/wavestep);
00369 bin_max = uves_round_double(wend /wavestep);
00370 }
00371
00372 bin_min = uves_min_int(bin_min, uves_round_double(wstart/wavestep));
00373 bin_max = uves_max_int(bin_max, uves_round_double(wend /wavestep));
00374 }
00375 total_bins = (bin_max - bin_min) + 1;
00376
00377 uves_msg_debug("Merging orders into %d bins covering wavelengths %.3f - %.3f wlu",
00378 total_bins, bin_min * wavestep, bin_max * wavestep);
00379
00380
00381 if(m_method == MERGE_NOAPPEND) {
00382 merged = cpl_image_new(total_bins, n_traces*norders, type);
00383 *merged_noise = cpl_image_new(total_bins, n_traces*norders, type);
00384 } else {
00385 merged = cpl_image_new(total_bins, n_traces, type);
00386 *merged_noise = cpl_image_new(total_bins, n_traces, type);
00387 }
00388 cpl_image_add_scalar(*merged_noise, -1.0);
00389
00390
00391
00392 for (order = 1; order <= norders; order++)
00393 {
00394 double wstart, wend;
00395 int wstart_bin, wend_bin;
00396
00397
00398
00399 check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00400 "Error reading start wavelength for order #%d", order);
00401
00402 check( wend = uves_pfits_get_wend(spectrum_header, order),
00403 "Error reading end wavelength for order #%d", order);
00404
00405
00406
00407
00408 wstart_bin = uves_round_double(wstart/wavestep);
00409 wend_bin = uves_round_double(wend/wavestep);
00410 delt1_bin = uves_round_double(delt1/wavestep);
00411 delt2_bin = uves_round_double(delt2/wavestep);
00412
00413
00414 int bin_min_ord = uves_round_double(wstart/wavestep);
00415 int bin_max_ord = uves_round_double(wend /wavestep);
00416 int nbins_ord = (bin_max_ord - bin_min_ord) + 1;
00417 cpl_image* merged_ord=NULL;
00418 cpl_image * noise_ord=NULL;
00419
00420 if(m_method == MERGE_NOAPPEND) {
00421 merged_ord = cpl_image_new(nbins_ord, n_traces, type);
00422 noise_ord = cpl_image_new(nbins_ord, n_traces, type);
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432 for (trace = 1; trace <= n_traces; trace++)
00433 {
00434 int merged_row = 0;
00435 int spectrum_row = (order - 1)*n_traces + trace;
00436 if(m_method == MERGE_NOAPPEND) {
00437 merged_row = (order - 1)*n_traces + trace;
00438 } else {
00439 merged_row = trace;
00440
00441 }
00442 int rel_bin;
00443
00444 for (rel_bin = 1+delt1_bin;
00445 (rel_bin <= wend_bin - wstart_bin + 1-delt2_bin) &&
00446 (rel_bin <(spectrum_sx*spectrum_sy+1-(spectrum_row-1)*nbins));
00447 rel_bin++)
00448 {
00449 double flux, noise;
00450 double current_flux, new_flux;
00451 double current_noise, new_noise;
00452 double weight;
00453 int pis_rejected, noise_rejected;
00454
00455
00456 int merged_bin = (wstart_bin - bin_min) + rel_bin;
00457 int merged_bin_ord = rel_bin;
00458
00459 passure(1 <= merged_bin && merged_bin <= total_bins,
00460 "%d %d %d", rel_bin, merged_bin, total_bins);
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 if (type == CPL_TYPE_DOUBLE) {
00471 flux = spectrum_data_double[(rel_bin-1) + (spectrum_row-1) * nbins];
00472 noise = noise_data_double [(rel_bin-1) + (spectrum_row-1) * nbins];
00473
00474 }
00475 else {
00476 flux = spectrum_data_float[(rel_bin-1) + (spectrum_row-1) * nbins];
00477 noise = noise_data_float [(rel_bin-1) + (spectrum_row-1) * nbins];
00478 }
00479
00480 pis_rejected = spectrum_bad[(rel_bin-1) + (spectrum_row-1) * nbins];
00481 noise_rejected = noise_bad [(rel_bin-1) + (spectrum_row-1) * nbins];
00482
00483 if (!pis_rejected && !noise_rejected)
00484 {
00485
00486 if(m_method == MERGE_NOAPPEND) {
00487 check(( current_flux = cpl_image_get(merged,
00488 merged_bin,
00489 merged_row,
00490 &pis_rejected),
00491 current_noise = cpl_image_get(*merged_noise,
00492 merged_bin,
00493 merged_row,
00494 &pis_rejected)),
00495 "Error reading merged spetrum");
00496 } else {
00497 check(( current_flux = cpl_image_get(
00498 merged , merged_bin, trace, &pis_rejected),
00499 current_noise = cpl_image_get(
00500 *merged_noise, merged_bin, trace, &pis_rejected)),
00501 "Error reading merged spetrum");
00502 }
00503 weight = 1/(noise*noise);
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524 if (current_noise > 0)
00525 {
00526 if (m_method == MERGE_OPTIMAL)
00527 {
00528 new_noise = 1/(current_noise*current_noise);
00529 new_noise += weight;
00530 new_noise = 1/sqrt(new_noise);
00531 }
00532 else if (m_method == MERGE_SUM)
00533 {
00534 new_noise = sqrt(current_noise*current_noise
00535 + noise*noise);
00536 }
00537 else if (m_method == MERGE_NOAPPEND)
00538 {
00539 new_noise = current_noise;
00540 }
00541 else
00542 {
00543
00544 passure( false, "%d", m_method);
00545 }
00546 }
00547 else
00548 {
00549
00550 new_noise = noise;
00551 }
00552
00553 if (current_noise > 0)
00554 {
00555 if (m_method == MERGE_OPTIMAL)
00556 {
00557 new_flux = (current_flux /
00558 (current_noise*current_noise)
00559 + flux * weight) *
00560 (new_noise*new_noise);
00561 }
00562 else if (m_method == MERGE_SUM)
00563 {
00564 new_flux = current_flux + flux;
00565 }
00566 else if (m_method == MERGE_NOAPPEND)
00567 {
00568 new_flux = flux;
00569 }
00570 else
00571 {
00572
00573 passure( false, "%d", m_method);
00574 }
00575 }
00576 else
00577 {
00578 new_flux = flux;
00579 }
00580
00581 if(m_method == MERGE_NOAPPEND) {
00582
00583
00584
00585
00586
00587
00588 check( cpl_image_set(
00589 merged,
00590 merged_bin,
00591 merged_row,
00592 new_flux),
00593 "Error updating merged spectrum");
00594
00595 check( cpl_image_set(
00596 *merged_noise,
00597 merged_bin,
00598 merged_row,
00599 new_noise),
00600 "Error updating weights");
00601
00602
00603 check( cpl_image_set(
00604 merged_ord,
00605 merged_bin_ord,
00606 trace,
00607 new_flux),
00608 "Error updating merged spectrum");
00609
00610 check( cpl_image_set(
00611 noise_ord,
00612 merged_bin_ord,
00613 trace,
00614 new_noise),
00615 "Error updating merged spectrum");
00616
00617
00618 } else {
00619 check( cpl_image_set(
00620 merged , merged_bin, trace, new_flux),
00621 "Error updating merged spectrum");
00622 check( cpl_image_set(
00623 *merged_noise, merged_bin, trace, new_noise),
00624 "Error updating weights");
00625 }
00626
00627
00628
00629
00630
00631 }
00632
00633 }
00634
00635 }
00636
00637
00638 if (merged_header == NULL)
00639 {
00640 uves_free_propertylist(merged_header);
00641 check( *merged_header = uves_initialize_image_header(
00642 "WAVELENGTH", (n_traces > 1) ? "PIXEL" : " ", "FLUX",
00643 bin_min_ord * wavestep, 1.0,
00644 1.0, 1.0,
00645 wavestep, 1.0),
00646 "Error initializing merged spectrum header");
00647 }
00648
00649 if(m_method == MERGE_NOAPPEND) {
00650 filename=uves_sprintf("merged_data_noappend_%s.fits",
00651 uves_chip_tostring_upper(chip));
00652 image_1d = cpl_vector_wrap(cpl_image_get_size_x(merged_ord),
00653 cpl_image_get_data_double(merged_ord));
00654 uves_free_propertylist(&hext);
00655 hext=uves_propertylist_new();
00656 uves_propertylist_append_double(hext,"CRVAL1",wstart-delt1);
00657 uves_propertylist_append_double(hext,"CDELT1",wavestep);
00658 uves_propertylist_append_double(hext,"CRPIX1",0);
00659
00660 if(order==1) {
00661 uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext,
00662 CPL_IO_DEFAULT);
00663 } else {
00664 uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext,
00665 CPL_IO_EXTEND);
00666 }
00667 cpl_vector_unwrap(image_1d);
00668
00669 image_1d = cpl_vector_wrap(cpl_image_get_size_x(noise_ord),
00670 cpl_image_get_data_double(noise_ord));
00671
00672 uves_free(filename);
00673
00674 filename=uves_sprintf("merged_sigma_noappend_%s.fits",
00675 uves_chip_tostring_upper(chip));
00676 if(order==1) {
00677 uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext,
00678 CPL_IO_DEFAULT);
00679 } else {
00680 uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext,
00681 CPL_IO_EXTEND);
00682 }
00683 cpl_vector_unwrap(image_1d);
00684
00685 uves_free(filename);
00686 uves_free_image(&merged_ord);
00687 uves_free_image(&noise_ord);
00688 uves_free_propertylist(&hext);
00689 uves_free_propertylist(merged_header);
00690 }
00691
00692 }
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734 check( cpl_image_threshold(*merged_noise,
00735 0, DBL_MAX,
00736 1, 1),
00737 "Error setting undefined noise");
00738
00739
00740 if (merged_header != NULL)
00741 {
00742 check( *merged_header = uves_initialize_image_header(
00743 "WAVELENGTH [Ang]", (n_traces > 1) ? "PIXEL" : " ", "ADU",
00744 bin_min * wavestep, 1.0,
00745 1.0, 1.0,
00746 wavestep, 1.0),
00747 "Error initializing merged spectrum header");
00748 }
00749
00750
00751 cleanup:
00752 return merged;
00753 }
00754
00755
00765
00766 merge_method
00767 uves_get_merge_method(const cpl_parameterlist *parameters,
00768 const char *context,
00769 const char *subcontext)
00770 {
00771 const char *mm = "";
00772 merge_method result = 0;
00773
00774 check( uves_get_parameter(parameters, context, subcontext, "merge", CPL_TYPE_STRING, &mm),
00775 "Could not read parameter");
00776
00777 if (strcmp(mm, "optimal") == 0) result = MERGE_OPTIMAL;
00778 else if (strcmp(mm, "sum" ) == 0) result = MERGE_SUM;
00779 else if (strcmp(mm, "noappend") == 0) result = MERGE_NOAPPEND;
00780 else
00781 {
00782 assure(false, CPL_ERROR_ILLEGAL_INPUT, "No such merging method: '%s'", mm);
00783 }
00784
00785 cleanup:
00786 return result;
00787 }
00788
00789