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