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
00166
00167
00168
00169
00170 #ifdef HAVE_CONFIG_H
00171 # include <config.h>
00172 #endif
00173
00174 #include <uves_dfs.h>
00175 #include <uves_parameters.h>
00176 #include <uves_utils_wrappers.h>
00177 #include <uves_test_simulate.h>
00178 #include <uves_pfits.h>
00179 #include <uves_error.h>
00180
00181 #include <cpl_test.h>
00182 #include <cpl.h>
00183
00184 #include <float.h>
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00198
00202
00206
00207 static void
00208 parse_midas_poly(void)
00209 {
00210 uves_propertylist *header = uves_propertylist_new();
00211 polynomial *p = NULL;
00212
00213 const char *data[] = {"",
00214 "'COEFFI','I*4',1,7,'7I10'",
00215 " 53889 2 3 2 1 4 5",
00216 "",
00217 "'COEFFR','R*4',1,5,'5E14.7'",
00218 " 4.3300000E+02 4.0880000E+03 1.0000000E+00 2.1000000E+01 0.0000000E+00",
00219 "",
00220 "'COEFFD','R*8',1,30,'3E23.15'",
00221 " -7.097005629698889E+01 4.050908371864904E-02 -2.886756545398909E-06",
00222 " 5.504345508879626E-10 -5.583004967206025E-14 7.624532125635992E+01",
00223 " -2.428213567964009E-03 1.819158447566360E-06 -5.090366383338846E-10",
00224 " 5.198098506055602E-14 3.513177145982783E-01 5.570332137951829E-04",
00225 " -3.876157463910250E-07 1.113253735718822E-10 -1.132455173423791E-14",
00226 " 2.977232589499959E-02 -5.389240622889887E-05 3.777456726044612E-08",
00227 " -1.083863050648735E-11 1.098450510939580E-15 -1.093309039442914E-03",
00228 " 2.402609262989674E-06 -1.688416547941747E-09 4.839101712729582E-13",
00229 " -4.884504488944702E-17 1.919853952642526E-05 -4.004133160220927E-08",
00230 " 2.816206503824200E-11 -8.051313882805877E-15 8.090579180112579E-19",
00231 " ",
00232 "'TAB_IN_OUT_YSHIFT','R*8',1,1,'3E23.15'",
00233 " 4.180818583555659E+01 ",
00234 " "};
00235 unsigned int i;
00236
00237
00238 for (i = 0; i < 8000; i++)
00239 {
00240 uves_propertylist_append_string(
00241 header, "HISTORY",
00242 " 35834 35835 35836 35837 35838 35839 35840");
00243 }
00244
00245 for (i = 0; i < sizeof(data)/sizeof(char *); i++)
00246 {
00247 uves_propertylist_append_string(
00248 header, "HISTORY",
00249 data[i]);
00250 }
00251
00252 check_nomsg( p = uves_polynomial_convert_from_plist_midas(header,
00253 "COEFF",-1));
00254
00255 assure( uves_polynomial_get_dimension(p) == 2, CPL_ERROR_ILLEGAL_OUTPUT,
00256 "Dimension is %d, 2 expected", uves_polynomial_get_dimension(p));
00257
00258 cleanup:
00259 uves_free_propertylist(&header);
00260 uves_polynomial_delete(&p);
00261
00262 return;
00263 }
00264
00265
00269
00270 static void
00271 test_load_linetable(void)
00272 {
00273 const char * const filename = "linetable.fits";
00274 cpl_table *linetable_in = NULL;
00275 polynomial *dispersion = NULL;
00276 polynomial *absorder = NULL;
00277 uves_propertylist *header = uves_propertylist_new();
00278 uves_propertylist *eheader = uves_propertylist_new();
00279
00280 cpl_frame *f = cpl_frame_new();
00281 cpl_frameset *frames = cpl_frameset_new();
00282 bool flames = false;
00283 const char *const chip_id = "CCD42";
00284 cpl_table *ordertable = NULL;
00285 cpl_table *tracetable = NULL;
00286 polynomial *order_locations = NULL;
00287 int firstabs, lastabs;
00288 enum uves_chip chip = UVES_CHIP_BLUE;
00289 int minorder = 1;
00290 int maxorder = 5;
00291 int nx = 150;
00292
00293 int trace_id = 0;
00294 int window = 1;
00295
00296
00297 const char *linetable_filename;
00298 cpl_table *linetable_out = NULL;
00299 uves_propertylist *header_out = NULL;
00300 polynomial *dispersion_out = NULL;
00301 polynomial *absorder_out = NULL;
00302
00303
00304 check_nomsg( create_order_table(&ordertable, &order_locations, &tracetable,
00305 minorder, maxorder, nx));
00306 check_nomsg( create_line_table(&linetable_in, &dispersion, &absorder,
00307 &firstabs, &lastabs,
00308 minorder, maxorder, nx));
00309 check_nomsg( uves_propertylist_append_string(header, UVES_CHIP_ID(chip), chip_id));
00310 check_nomsg( uves_propertylist_append_string(header, UVES_DRS_ID, "CPL"));
00311
00312 check_nomsg( uves_pfits_set_firstabsorder(eheader, firstabs) );
00313 check_nomsg( uves_pfits_set_lastabsorder(eheader, lastabs) );
00314
00315 check_nomsg( uves_pfits_set_traceid(eheader, trace_id) );
00316 check_nomsg( uves_pfits_set_windownumber(eheader, window) );
00317
00318 check_nomsg( uves_table_save(linetable_in, header, eheader, filename, CPL_IO_DEFAULT) );
00319 check_nomsg( uves_save_polynomial(dispersion, filename, eheader) );
00320 check_nomsg( uves_save_polynomial(absorder, filename, eheader) );
00321
00322 cpl_test_eq(cpl_error_get_code(), CPL_ERROR_NONE);
00323
00324 cpl_frame_set_filename(f, filename);
00325 cpl_frame_set_tag(f, "LINE_TABLE_BLUE");
00326 cpl_frameset_insert(frames, f);
00327
00328 check_nomsg( uves_load_linetable(frames,
00329 flames,
00330 chip_id,
00331 order_locations, minorder, maxorder,
00332 &linetable_filename,
00333 &linetable_out,
00334 &header_out,
00335 &dispersion_out,
00336 &absorder_out,
00337 chip, trace_id, window));
00338
00339 cpl_test_eq(cpl_error_get_code(), CPL_ERROR_NONE);
00340
00341 cpl_test( linetable_out != NULL );
00342 cpl_test( header_out != NULL );
00343 cpl_test( dispersion_out != NULL );
00344 cpl_test( absorder_out != NULL );
00345
00346 cpl_test_eq( cpl_table_get_nrow(linetable_in),
00347 cpl_table_get_nrow(linetable_out) );
00348
00349
00350
00351
00352
00353 cpl_test_eq_string( filename, linetable_filename );
00354
00355 {
00356 int order;
00357 int x;
00358
00359 for (order = minorder; order <= maxorder; order++) {
00360 for (x = 1; x <= nx; x += nx/6) {
00361 cpl_test_rel(
00362 uves_polynomial_evaluate_2d(absorder , x, order),
00363 uves_polynomial_evaluate_2d(absorder_out, x, order), 0.001);
00364
00365 cpl_test_rel(
00366 uves_polynomial_evaluate_2d(dispersion , x, order),
00367 uves_polynomial_evaluate_2d(dispersion_out, x, order), 0.001);
00368 }
00369 }
00370 }
00371
00372 cleanup:
00373 uves_free_frameset(&frames);
00374 uves_free_table(&linetable_in);
00375 uves_free_table(&linetable_out);
00376 uves_polynomial_delete(&dispersion);
00377 uves_polynomial_delete(&absorder);
00378 uves_polynomial_delete(&dispersion_out);
00379 uves_polynomial_delete(&absorder_out);
00380 uves_free_propertylist(&header_out);
00381 uves_free_propertylist(&header);
00382 uves_free_propertylist(&eheader);
00383 uves_free_table(&ordertable);
00384 uves_free_table(&tracetable);
00385 uves_polynomial_delete(&order_locations);
00386
00387 return;
00388 }
00389
00390
00391
00395
00396 static void
00397 convert_midas_array(void)
00398 {
00399 const char *values[] = {"HISTORY", "",
00400 "HISTORY", "'FIBREPOS','R*8',1,9,'3E23.15'",
00401 "HISTORY", " -3.243571124678650E+01 -2.309646501161805E+01 -1.402902770375962E+01",
00402 "HISTORY", " -4.772375924542811E+00 4.827040349175236E+00 1.378761244187003E+01",
00403 "HISTORY", " 2.321337764943556E+01 3.243571124678650E+01 -3.552713678800501E-15",
00404 "HISTORY", "",
00405 "HISTORY", "'COEFFR','R*4',1,20,'5E14.7'",
00406 "HISTORY", "9.4893160E+00 4.0716226E+03 0.0000000E+00 2.3000000E+01 1.8538159E-04",
00407 "HISTORY", "0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00",
00408 "HISTORY", "",
00409 "HISTORY", "'INTVAL','I",
00410 "HISTORY", "1 2 3 4 5 6",
00411 "HISTORY", "7",
00412 "HISTORY", "",
00413 "HISTORY", "'LEGAL','C",
00414 "HISTORY", " a sdfasdf",
00415 "HISTORY", "",
00416 "HISTORY", "'ILLEGAL','C",
00417 "HISTORY", "1bsdf",
00418 "HISTORY", "bsdf",
00419 "HISTORY", "",
00420 "HISTORY", "'CHIPCHOICE','C",
00421 "HISTORY", "abcd",
00422 "HISTORY", ""};
00423
00424 int N = sizeof(values) / sizeof(const char *) / 2;
00425
00426 uves_propertylist *header = NULL;
00427 double *resultd = NULL;
00428 int *resulti = NULL;
00429 float *resultf = NULL;
00430 const char *results = NULL;
00431 int result_length, i;
00432 int nkeys;
00433 cpl_type result_type;
00434
00435 header = uves_propertylist_new();
00436 for (i = 0; i < N; i++)
00437 {
00438 uves_propertylist_append_string(header, values[i*2], values[i*2+1]);
00439 }
00440
00441 check_nomsg(resultd = uves_read_midas_array(header, "FIBREPOS", &result_length,
00442 &result_type, &nkeys));
00443
00444 cpl_test_eq(result_type, CPL_TYPE_DOUBLE);
00445 cpl_test_eq(result_length, 9);
00446 cpl_test_eq(nkeys, 5);
00447
00448
00449 cpl_test_rel(resultd[0], -32, 0.10);
00450 cpl_test_rel(resultd[3], -4.7, 0.10);
00451 cpl_test_rel(resultd[6], 23, 0.10);
00452 cpl_test( fabs(resultd[8]) < 0.001);
00453
00454
00455 check_nomsg(resultf = uves_read_midas_array(header, "COEFFR", &result_length,
00456 &result_type, &nkeys));
00457 cpl_test_eq(result_type, CPL_TYPE_FLOAT);
00458 cpl_test_eq(result_length, 10);
00459 cpl_test_eq(nkeys, 4);
00460
00461 cpl_test_rel(resultf[0], 9.489, 0.01);
00462 cpl_test_rel(resultf[1], 4071, 0.01);
00463 cpl_test_abs(resultf[2], 0.000, 0.01);
00464 cpl_test_rel(resultf[3], 23.00, 0.01);
00465
00466
00467 check_nomsg(resulti = uves_read_midas_array(header, "INTVAL", &result_length,
00468 &result_type, &nkeys));
00469
00470 cpl_test_eq(result_type, CPL_TYPE_INT);
00471 cpl_test_eq(result_length, 7);
00472 cpl_test_eq(nkeys, 4);
00473 for (i = 1; i <= 7; i++)
00474 {
00475 cpl_test_eq(resulti[i-1], i);
00476 }
00477
00478
00479
00480 check_nomsg( results = uves_read_midas_array(header, "LEGAL", &result_length,
00481 &result_type, &nkeys) );
00482
00483 cpl_test_eq(result_type, CPL_TYPE_STRING);
00484 cpl_test_eq(result_length, 10);
00485 cpl_test_eq(nkeys, 3);
00486 cpl_test_eq_string(results, " a sdfasdf");
00487
00488 cpl_test(uves_read_midas_array(header, "ILLEGAL2", &result_length,
00489 &result_type, &nkeys) == NULL);
00490 uves_error_reset();
00491
00492 uves_free_string_const(&results);
00493 check_nomsg(results = uves_read_midas_array(header, "CHIPCHOICE", &result_length,
00494 &result_type, &nkeys));
00495
00496 cpl_test_eq(result_type, CPL_TYPE_STRING);
00497 cpl_test_eq(result_length, 4);
00498 cpl_test_eq(nkeys, 3);
00499 cpl_test_eq_string(results, "abcd");
00500
00501
00502 N = 9000;
00503 uves_free_propertylist(&header);
00504 header = uves_propertylist_new();
00505 uves_propertylist_append_string(header, "HISTORY", "'SELIDX','I*4',1,48389,'7I10'");
00506 for (i = 0; i < N; i++)
00507 {
00508 uves_propertylist_append_string(
00509 header, "HISTORY",
00510 " 64605 64606 64607 64608 64609 64610 64611");
00511 }
00512 uves_propertylist_append_string(header, "HISTORY", "");
00513
00514 uves_free_int(&resulti);
00515 check_nomsg( resulti = uves_read_midas_array(header, "SELIDX", &result_length,
00516 &result_type, &nkeys));
00517
00518 cpl_test_eq(result_type, CPL_TYPE_INT);
00519 cpl_test_eq(result_length, N*7);
00520 cpl_test_eq(nkeys, 1+N+1);
00521
00522 cleanup:
00523 uves_free_propertylist(&header);
00524 uves_free_double(&resultd);
00525 uves_free_int(&resulti);
00526 uves_free_float(&resultf);
00527 uves_free_string_const(&results);
00528 return;
00529 }
00530
00531
00535
00536 static void
00537 test_save_image(void)
00538 {
00539 const int N = 100;
00540 int i;
00541
00542 cpl_image *image = cpl_image_new(N, 1, CPL_TYPE_DOUBLE);
00543
00544 double inf = DBL_MAX;
00545 for (i = 1; i <= N; i++)
00546 {
00547 cpl_image_set(image, i, 1, -FLT_MAX*200);
00548 inf *= 10;
00549 }
00550
00551
00552 cpl_image_set(image, 1, 1, inf);
00553 cpl_image_set(image, 2, 1, inf/inf);
00554
00555 uves_save_image(image, "dfs.fits", NULL, true, true);
00556 uves_free_image(&image);
00557
00558 return;
00559 }
00560
00561
00565
00566 static void
00567 test_save_frame(void)
00568 {
00569 cpl_frameset *frames = cpl_frameset_new();
00570 cpl_parameterlist *parameters = cpl_parameterlist_new();
00571 int nx = 1500;
00572 int ny = 1024;
00573 int nkey = 360;
00574 cpl_image *image = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00575 uves_propertylist *raw_header = uves_propertylist_new();
00576 uves_propertylist *product_header = uves_propertylist_new();
00577 const char *starttime;
00578 const char *recipe_id = "uves_cal_phony";
00579 const char *tag = "PHONY_TAG";
00580 const char *raw_filename = "raw_file.fits";
00581
00582 uves_define_global_parameters(parameters);
00583 cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00584
00585
00586 {
00587 cpl_image *raw_image = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00588 cpl_frame *raw_frame = cpl_frame_new();
00589
00590 {
00591 int i;
00592 for (i = 0; i < nkey; i++)
00593 {
00594 const char *key_name = uves_sprintf("KEY%d", i);
00595 uves_propertylist_append_int(raw_header, key_name, i);
00596 uves_free_string_const(&key_name);
00597 }
00598 uves_propertylist_append_string(raw_header, "ORIGIN", "unknown...");
00599 }
00600
00601 uves_image_save(raw_image,
00602 raw_filename,
00603 CPL_BPP_IEEE_FLOAT,
00604 raw_header,
00605 CPL_IO_DEFAULT);
00606 cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00607
00608 uves_free_image(&raw_image);
00609
00610
00611 cpl_frame_set_tag(raw_frame, "BIAS_BLUE");
00612
00613
00614 cpl_frame_set_filename(raw_frame, raw_filename);
00615 cpl_frameset_insert(frames, raw_frame);
00616 }
00617
00618 starttime = uves_initialize(frames, parameters, recipe_id,
00619 "This recipe does not do anything");
00620 cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00621
00622 uves_frameset_insert(frames,
00623 image,
00624 CPL_FRAME_GROUP_PRODUCT,
00625 CPL_FRAME_TYPE_IMAGE,
00626 CPL_FRAME_LEVEL_INTERMEDIATE,
00627 "dfs_product.fits",
00628 tag,
00629 raw_header,
00630 product_header,
00631 NULL,
00632 parameters,
00633 recipe_id,
00634 PACKAGE "/" PACKAGE_VERSION,
00635 NULL,
00636 starttime,
00637 false,
00638 0 );
00639
00640 cpl_test_eq( cpl_error_get_code(), CPL_ERROR_NONE );
00641 cpl_test( cpl_frameset_find(frames, tag) != NULL);
00642
00643 uves_free_frameset(&frames);
00644 uves_free_parameterlist(¶meters);
00645 uves_free_image(&image);
00646 uves_free_propertylist(&raw_header);
00647 uves_free_propertylist(&product_header);
00648 uves_free_string_const(&starttime);
00649 return;
00650 }
00651
00652
00653
00657
00658
00659 int main(void)
00660 {
00661
00662 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00663 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 0, 0)
00664 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00665 #endif
00666
00667
00668 check( parse_midas_poly(),
00669 "Test of MIDAS array conversion");
00670
00671
00672 check_nomsg( test_load_linetable() );
00673
00674 check( convert_midas_array(),
00675 "Test of MIDAS array conversion failed");
00676
00677 check_nomsg( test_save_image() );
00678
00679 check_nomsg( test_save_frame() );
00680
00681 cleanup:
00682 if (cpl_error_get_code() != CPL_ERROR_NONE)
00683 {
00684 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 0, 0)
00685 cpl_errorstate_dump(initial_errorstate,CPL_FALSE,NULL);
00686 #else
00687 uves_error_dump();
00688 #endif
00689 }
00690 return cpl_test_end(0);
00691 }
00692
00693