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 #ifdef HAVE_CONFIG_H
00078 # include <config.h>
00079 #endif
00080
00081 #include <uves_extract.h>
00082 #include <uves_extract_iterate.h>
00083 #include <uves_extract_profile.h>
00084 #include <uves_test_simulate.h>
00085 #include <uves.h>
00086 #include <uves_parameters.h>
00087 #include <uves_pfits.h>
00088 #include <uves_utils_wrappers.h>
00089 #include <uves_utils_polynomial.h>
00090 #include <uves_dfs.h>
00091 #include <uves_chip.h>
00092 #include <uves_error.h>
00093 #include <irplib_test.h>
00094
00095 #include <cpl.h>
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00109
00112
00116
00117
00118 static cpl_image *
00119 create_spectrum(int nx, int ny, int minorder, int maxorder, const polynomial *order_locations,
00120 slit_geometry sg, cpl_image **sky_spectrum)
00121 {
00122 int norders = maxorder - minorder + 1;
00123 cpl_image *spectrum = cpl_image_new(nx, norders, CPL_TYPE_DOUBLE);
00124 cpl_image *dummy = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00125 uves_iterate_position *pos = NULL;
00126
00127 cpl_binary *bpm = NULL;
00128 bool loop_y = false;
00129
00130 double tot_flux = 3000;
00131
00132 pos = uves_iterate_new(nx, ny, order_locations, minorder, maxorder, sg);
00133 *sky_spectrum = cpl_image_new(nx, norders, CPL_TYPE_DOUBLE);
00134
00135 {
00136 int x, order;
00137 for (x = 1; x <= nx; x++)
00138 for (order = 1; order <= norders; order++)
00139 {
00140 cpl_image_reject(spectrum, x, order);
00141 cpl_image_reject(*sky_spectrum, x, order);
00142 }
00143 }
00144
00145 for (uves_iterate_set_first(pos,
00146 1, nx,
00147 minorder, maxorder,
00148 bpm,
00149 loop_y);
00150 !uves_iterate_finished(pos);
00151 uves_iterate_increment(pos))
00152 {
00153 int spectrum_row = pos->order - minorder + 1;
00154
00155 double sky = 1000+20000*(pos->order - minorder)*1.0/(maxorder - minorder);
00156
00157 check_nomsg( cpl_image_set(spectrum , pos->x, spectrum_row, tot_flux) );
00158 check_nomsg( cpl_image_set(*sky_spectrum, pos->x, spectrum_row, sky) );
00159
00160 }
00161
00162 cleanup:
00163 uves_iterate_delete(&pos);
00164 uves_free_image(&dummy);
00165 return spectrum;
00166 }
00167
00168
00169
00173
00174
00175 static void
00176 test_extract(void)
00177 {
00178 polynomial *order_locations = NULL;
00179 int minorder = 1;
00180
00181
00182
00183 int maxorder = 8;
00184 int nx = 500;
00185 int ny = 600;
00186 slit_geometry sg = {30.0, 0.0};
00187 cpl_image *in_spectrum = NULL;
00188 cpl_image *in_sky = NULL;
00189 cpl_image *image = NULL;
00190 cpl_image *image_noise = NULL;
00191 uves_propertylist *image_header = NULL;
00192 cpl_table *ordertable = NULL;
00193 cpl_parameterlist * parameters = NULL;
00194 const char *test_id = "uves_extract-test";
00195 bool extract_partial = false;
00196 bool DEBUG = true;
00197 bool blue = true;
00198 enum uves_chip chip = uves_chip_get_first(blue);
00199 cpl_image *cosmics = NULL;
00200 uves_iterate_position *pos = NULL;
00201 uves_extract_profile *profile = NULL;
00202
00203
00204 cpl_image *out_spectrum = NULL;
00205 cpl_image *out_sky = NULL;
00206 cpl_image *out_sky_noise = NULL;
00207
00208
00209 cpl_table *cosmic_mask = NULL;
00210 cpl_image *cosmic_image = NULL;
00211
00212 cpl_image *weights = NULL;
00213 cpl_table *info_tbl = NULL;
00214 cpl_table *order_trace = NULL;
00215
00216
00217 check_nomsg( create_order_table(NULL, &order_locations, NULL,
00218 minorder, maxorder, nx) );
00219
00220 check_nomsg( in_spectrum = create_spectrum(nx, ny,
00221 minorder, maxorder,
00222 order_locations,
00223 sg,
00224 &in_sky) );
00225
00226 pos = uves_iterate_new(nx, ny, order_locations, minorder, maxorder, sg);
00227
00228 profile = uves_extract_profile_new(uves_gauss,
00229 uves_gauss_derivative,
00230 4, 0, 0);
00231
00232 profile->y0 = uves_polynomial_new_zero(2);
00233 profile->sigma = uves_polynomial_new_zero(2);
00234 uves_polynomial_shift(profile->sigma, 0, 2.5);
00235
00236 check_nomsg( image = uves_create_image(pos,
00237 chip,
00238 in_spectrum, in_sky,
00239 cosmics,
00240 profile,
00241 &image_noise,
00242 &image_header) );
00243
00244 uves_save_image(image, "image.fits", NULL, true, true);
00245 uves_save_image(image_noise, "noise.fits", NULL, true, true);
00246
00247 ordertable = cpl_table_new(2);
00248 cpl_table_new_column(ordertable, "Order", CPL_TYPE_INT);
00249 cpl_table_set_int(ordertable, "Order", 0, minorder);
00250 cpl_table_set_int(ordertable, "Order", 1, maxorder);
00251
00252
00253 parameters = cpl_parameterlist_new();
00254 check_nomsg( uves_propagate_parameters_step(UVES_EXTRACT_ID,
00255 parameters,
00256 test_id,
00257 NULL));
00258
00259 {
00260 const char *value = "optimal";
00261 uves_set_parameter(parameters, test_id, UVES_EXTRACT_ID ".method", CPL_TYPE_STRING, &value);
00262 }
00263
00264 check( out_spectrum =
00265 uves_extract(image,
00266 image_noise,
00267 image_header,
00268 ordertable,
00269 order_locations,
00270 sg.length,
00271 sg.offset,
00272 parameters,
00273 test_id,
00274 extract_partial,
00275 DEBUG,
00276 chip,
00277 NULL,
00278 NULL,
00279 &out_sky,
00280 &out_sky_noise,
00281 &cosmic_mask,
00282 &cosmic_image,
00283 NULL,
00284 &weights,
00285 &info_tbl,
00286 &order_trace),
00287 "Error during extraction");
00288
00289 uves_save_image(out_spectrum, "spectrum.fits", NULL, true, true);
00290
00291
00292 {
00293 int x, order;
00294
00295 for (order = minorder; order <= maxorder; order++)
00296 {
00297 int spectrum_row = order - minorder + 1;
00298
00299 for (x = 1; x <= nx; x++)
00300 {
00301 int in_bad, out_bad;
00302 double in = cpl_image_get( in_spectrum, x, spectrum_row, &in_bad);
00303 double sky = cpl_image_get( in_sky , x, spectrum_row, &in_bad);
00304 double out = cpl_image_get(out_spectrum, x, spectrum_row, &out_bad);
00305 double osky = cpl_image_get(out_sky , x, spectrum_row, &out_bad);
00306
00307 #if 0
00308 assure( out_bad || in_bad ||
00309 float_equal(out, in + sky, 0.001),
00310 CPL_ERROR_ILLEGAL_OUTPUT,
00311 "At (x, order) = (%d, %d): In = %f + %f (%d); Out = %f (%d)",
00312 x, order, sky, in, in_bad, out, out_bad);
00313
00314 #else
00315
00316 #if 0
00317 assure( out_bad || in_bad ||
00318 float_equal(out, in, 0.02),
00319 CPL_ERROR_ILLEGAL_OUTPUT,
00320 "Object spectrum differs at (x, order) = (%d, %d): In = %f (%d); Out = %f (%d)",
00321 x, order, in, in_bad, out, out_bad);
00322
00323 assure( out_bad || in_bad ||
00324 float_equal(osky, sky, 0.01),
00325 CPL_ERROR_ILLEGAL_OUTPUT,
00326 "Sky spectrum differs at (x, order) = (%d, %d), sky: In = %f (%d); Out = %f (%d)",
00327 x, order, sky, in_bad, osky, out_bad);
00328 #endif
00329 if (!out_bad && !in_bad)
00330 {
00331 irplib_test_rel(out, in, 0.02);
00332 irplib_test_rel(osky, sky, 0.01);
00333 }
00334 #endif
00335
00336 }
00337 }
00338 }
00339
00340 cleanup:
00341 uves_free_image(&in_spectrum);
00342 uves_free_image(&in_sky);
00343 uves_free_image(&image);
00344 uves_free_image(&image_noise);
00345 uves_free_propertylist(&image_header);
00346 uves_polynomial_delete(&order_locations);
00347 uves_free_parameterlist(¶meters);
00348 uves_free_table(&ordertable);
00349 uves_iterate_delete(&pos);
00350 uves_extract_profile_delete(&profile);
00351 uves_free_image(&cosmics);
00352
00353 uves_free_image(&out_spectrum);
00354 uves_free_image(&out_sky);
00355 uves_free_image(&out_sky_noise);
00356 uves_free_image(&weights);
00357 uves_free_table(&cosmic_mask);
00358 uves_free_image(&cosmic_image);
00359 uves_free_table(&info_tbl);
00360 uves_free_table(&order_trace);
00361 return;
00362 }
00363
00364
00365
00369
00370
00371 static void
00372 test_iterate(void)
00373 {
00374 polynomial *order_locations;
00375 uves_iterate_position *pos = NULL;
00376 cpl_binary *bpm = NULL;
00377 bool loop_y = true;
00378 int nx = 2000;
00379 int ny = 1000;
00380 cpl_image *image = cpl_image_new(2000, 1000, CPL_TYPE_DOUBLE);
00381 int minorder = 3;
00382 int maxorder = 15;
00383 slit_geometry sg = {30.0, 0.0};
00384
00385 check_nomsg( create_order_table(NULL, &order_locations, NULL,
00386 minorder, maxorder, nx) );
00387
00388
00389 pos = uves_iterate_new(nx, ny,
00390 order_locations,
00391 minorder,
00392 maxorder,
00393 sg);
00394
00395
00396 check( uves_iterate_set_first(pos,
00397 1, nx,
00398 minorder, maxorder,
00399 bpm,
00400 loop_y),
00401 "Set first position failed");
00402
00403 assure( pos->x == 1 && pos->order == minorder,
00404 CPL_ERROR_ILLEGAL_OUTPUT,
00405 "Set first position failed: x, order, minorder = %d %d %d",
00406 pos->x, pos->order, minorder);
00407
00408 {
00409 int y = pos->y;
00410
00411 assure_nomsg( !uves_iterate_finished(pos), CPL_ERROR_ILLEGAL_OUTPUT );
00412
00413 check( uves_iterate_increment(pos), "Increment failed");
00414 check( uves_iterate_increment(pos), "Increment failed");
00415 check( uves_iterate_increment(pos), "Increment failed");
00416 check( uves_iterate_increment(pos), "Increment failed");
00417
00418
00419 assure( pos->x == 1 && pos->y == y+4 &&
00420 pos->order == minorder, CPL_ERROR_ILLEGAL_OUTPUT,
00421 "Increment failed: x, y, order = %d, %d (%d), %d",
00422 pos->x, pos->y, y+1, pos->order);
00423 }
00424
00425
00426 while(pos->x < nx)
00427 {
00428 uves_iterate_increment(pos);
00429 }
00430 while(pos->x != 1)
00431 {
00432 uves_iterate_increment(pos);
00433 }
00434
00435 {
00436 int y = pos->y;
00437 uves_iterate_increment(pos);
00438
00439 irplib_test_eq( pos->x, 1 );
00440 irplib_test_eq( pos->y, y+1 );
00441 irplib_test_eq( pos->order, minorder+1 );
00442
00443
00444 }
00445
00446
00447
00448 cleanup:
00449 uves_free_image(&image);
00450 uves_polynomial_delete(&order_locations);
00451 uves_iterate_delete(&pos);
00452 return;
00453 }
00454
00455
00459
00460
00461 int main(void)
00462 {
00463 IRPLIB_TEST_INIT;
00464
00465
00466
00467 test_iterate();
00468
00469 test_extract();
00470
00471 IRPLIB_TEST_END;
00472 }
00473
00474