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 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00038
00041
00042
00043
00044
00045 #include <xsh_data_pre.h>
00046 #include <xsh_error.h>
00047 #include <xsh_msg.h>
00048 #include <xsh_data_instrument.h>
00049 #include <xsh_drl.h>
00050 #include <xsh_pfits.h>
00051 #include <xsh_badpixelmap.h>
00052 #include <cpl.h>
00053 #include <stdbool.h>
00054 #include <xsh_data_order.h>
00055 #include <xsh_utils.h>
00056 #include <xsh_pfits.h>
00057 #include <string.h>
00058 #include <tests.h>
00059 #include <xsh_cpl_size.h>
00060
00061
00062
00063
00064
00065
00066
00067
00068 cpl_image* xsh_test_create_bias_image( const char* name, int nx, int ny,
00069 xsh_instrument* instrument)
00070 {
00071 cpl_image* res = NULL;
00072 cpl_propertylist* header = NULL;
00073 XSH_INSTRCONFIG * config = NULL;
00074 double ron = 0.0;
00075 double conad = 0.0;
00076 double ron_adu = 0.0;
00077 double medval = 85.0;
00078
00079 xsh_msg("config update %d", instrument->update);
00080 config = xsh_instrument_get_config( instrument);
00081 ron = config->ron;
00082 conad = config->conad;
00083 ron_adu = ron / conad;
00084 res = cpl_image_new( nx, ny, XSH_PRE_DATA_TYPE) ;
00085
00086 header = cpl_propertylist_new();
00087 setHeader(header, config, nx, ny, 1. );
00088
00089
00090 cpl_image_fill_noise_uniform ( res, medval-ron_adu, medval+ron_adu);
00091 cpl_image_save( res, name, XSH_PRE_DATA_BPP, header, CPL_IO_DEFAULT);
00092
00093 xsh_free_propertylist(&header);
00094 return res;
00095
00096 }
00097
00098 cpl_frame* xsh_test_create_frame(const char* name,int nx, int ny,
00099 const char* tag, cpl_frame_group group, xsh_instrument* instrument)
00100 {
00101
00102 XSH_INSTRCONFIG *iconfig = NULL ;
00103 cpl_propertylist *header = NULL;
00104 cpl_image *data = NULL;
00105 cpl_frame *frame = NULL;
00106
00107 XSH_ASSURE_NOT_NULL( instrument);
00108 XSH_ASSURE_NOT_NULL( name);
00109
00110 iconfig = xsh_instrument_get_config( instrument);
00111
00112 header = cpl_propertylist_new();
00113 setHeader(header, iconfig, nx, ny, 1.);
00114
00115 data = cpl_image_new(nx,ny,XSH_PRE_DATA_TYPE);
00116 cpl_image_fill_gaussian(data,
00117 nx / 3.0, ny / 3.0,
00118 50,
00119 nx, ny / 8.0);
00120
00121 check( cpl_image_save(data, name, XSH_PRE_DATA_BPP,header,
00122 CPL_IO_DEFAULT));
00123
00124
00125 frame = cpl_frame_new();
00126 cpl_frame_set_filename(frame,name);
00127 cpl_frame_set_group(frame,group);
00128 cpl_frame_set_tag(frame,tag);
00129
00130 cleanup:
00131 xsh_free_propertylist(&header);
00132 xsh_free_image(&data);
00133 return frame;
00134 }
00135
00144
00145 void tests_set_defaults(cpl_parameterlist * parlist)
00146 {
00147 cpl_parameter *p = NULL;
00148
00149 p = cpl_parameterlist_get_first(parlist);
00150 while (p != NULL) {
00151 bool parameter_is_set = cpl_parameter_get_default_flag(p);
00152
00153 if (!parameter_is_set) {
00154 cpl_type ptype = cpl_parameter_get_type(p);
00155 switch (ptype) {
00156 case CPL_TYPE_BOOL:
00157 cpl_parameter_set_bool(p, cpl_parameter_get_default_bool(p));
00158 break;
00159 case CPL_TYPE_INT:
00160 cpl_parameter_set_int(p, cpl_parameter_get_default_int(p));
00161 break;
00162 case CPL_TYPE_DOUBLE:
00163 cpl_parameter_set_double(p, cpl_parameter_get_default_double(p));
00164 break;
00165 case CPL_TYPE_STRING:
00166 cpl_parameter_set_string(p, cpl_parameter_get_default_string(p));
00167 break;
00168 default:
00169 assure(false, CPL_ERROR_ILLEGAL_INPUT,
00170 "Unknown type of parameter '%s'", cpl_parameter_get_name(p));
00171 }
00172 }
00173 p = cpl_parameterlist_get_next(parlist);
00174 }
00175
00176 cleanup:
00177 return;
00178 }
00179
00193 cpl_propertylist * mkHeader( XSH_INSTRCONFIG *iconfig,
00194 int nx, int ny, double exptime )
00195 {
00196 cpl_propertylist * header = cpl_propertylist_new() ;
00197
00198
00199
00200 check_msg( cpl_propertylist_append_double( header, XSH_CRPIX1, 1. ),
00201 "Cant append CRPIX1" ) ;
00202 check_msg( cpl_propertylist_append_double( header, XSH_CRPIX2, 1. ),
00203 "Cant append CRPIX2" ) ;
00204 check_msg( cpl_propertylist_append_double( header, XSH_CRVAL1, 1. ),
00205 "Cant append CRVAL1" ) ;
00206 check_msg( cpl_propertylist_append_double( header, XSH_CRVAL2, 1. ),
00207 "Cant append CRVAL2" ) ;
00208
00209
00210 check_msg( cpl_propertylist_append_int( header, XSH_OUT_NX, nx ),
00211 "Cant append NX" ) ;
00212 check_msg( cpl_propertylist_append_int( header, XSH_OUT_NY, ny ),
00213 "Cant append NY" ) ;
00214 check_msg( cpl_propertylist_append_int( header, XSH_OVSCX, iconfig->ovscx ),
00215 "Cant append OVSCX" ) ;
00216 check_msg( cpl_propertylist_append_int( header, XSH_OVSCY, iconfig->ovscy ),
00217 "Cant append OVSCY" ) ;
00218 check_msg( cpl_propertylist_append_int( header, XSH_PRSCX, iconfig->prscx ),
00219 "Cant append PRSCX" ) ;
00220 check_msg( cpl_propertylist_append_int( header, XSH_PRSCY, iconfig->prscy ),
00221 "Cant append PRSCY" ) ;
00222 check_msg( cpl_propertylist_append_double( header, XSH_RON, iconfig->ron ),
00223 "Cant append RON" ) ;
00224 check_msg( cpl_propertylist_append_double( header, XSH_CONAD,
00225 iconfig->conad ) ,
00226 "Cant append CONAD" ) ;
00227 check_msg( cpl_propertylist_append_double( header, XSH_PSZX, 15. ),
00228 "Cant append PSZX" ) ;
00229 check_msg( cpl_propertylist_append_double( header, XSH_PSZY, 15. ),
00230 "Cant append PSZY" ) ;
00231 check_msg( cpl_propertylist_append_double( header, XSH_EXPTIME, exptime ),
00232 "Cant append EXPTIME" ) ;
00233 check_msg( cpl_propertylist_append_double( header, XSH_DET_GAIN,2.78 ),
00234 "Cant append GAIN" ) ;
00235 check_msg( cpl_propertylist_append_int( header, XSH_WIN_BINX,1 ),
00236 "Cant append BINX" ) ;
00237 check_msg( cpl_propertylist_append_int( header, XSH_WIN_BINY,1 ),
00238 "Cant append BINY" ) ;
00239 return header ;
00240 cleanup:
00241 return NULL ;
00242 }
00243
00256 void setHeader( cpl_propertylist * header, XSH_INSTRCONFIG *iconfig,
00257 int nx, int ny, double exptime)
00258 {
00259
00260 XSH_ASSURE_NOT_NULL( header);
00261
00262
00263 check_msg( cpl_propertylist_append_int( header, XSH_OUT_NX, nx ),
00264 "Cant append NX" ) ;
00265 check_msg( cpl_propertylist_append_int( header, XSH_OUT_NY, ny ),
00266 "Cant append NY" ) ;
00267 check_msg( cpl_propertylist_append_int( header, XSH_WIN_BINX,1 ),
00268 "Cant append BINX" ) ;
00269 check_msg( cpl_propertylist_append_int( header, XSH_WIN_BINY,1 ),
00270 "Cant append BINY" ) ;
00271
00272 check_msg( cpl_propertylist_append_int( header, XSH_CHIP_NX, nx ),
00273 "Cant append NX" ) ;
00274 check_msg( cpl_propertylist_append_int( header, XSH_CHIP_NY, ny ),
00275 "Cant append NY" ) ;
00276
00277 check_msg( cpl_propertylist_append_double( header, XSH_PSZX, 15. ),
00278 "Cant append PSZX" ) ;
00279 check_msg( cpl_propertylist_append_double( header, XSH_PSZY, 15. ),
00280 "Cant append PSZY" ) ;
00281 check_msg( cpl_propertylist_append_double( header, XSH_EXPTIME, exptime ),
00282 "Cant append EXPTIME" ) ;
00283 check_msg( cpl_propertylist_append_double( header, XSH_DET_GAIN,2.78 ),
00284 "Cant append GAIN" ) ;
00285
00286 check_msg( cpl_propertylist_append_int( header, XSH_OVSCX, iconfig->ovscx ),
00287 "Cant append OVSCX" ) ;
00288 check_msg( cpl_propertylist_append_int( header, XSH_OVSCY, iconfig->ovscy ),
00289 "Cant append OVSCY" ) ;
00290 check_msg( cpl_propertylist_append_int( header, XSH_PRSCX, iconfig->prscx ),
00291 "Cant append PRSCX" ) ;
00292 check_msg( cpl_propertylist_append_int( header, XSH_PRSCY, iconfig->prscy ),
00293 "Cant append PRSCY" ) ;
00294 check_msg( cpl_propertylist_append_double( header, XSH_RON, iconfig->ron ),
00295 "Cant append RON" ) ;
00296 check_msg( cpl_propertylist_append_double( header, XSH_CONAD,
00297 iconfig->conad ) ,
00298 "Cant append CONAD" ) ;
00299
00300 if ( iconfig->bitpix == 32){
00301 check_msg( cpl_propertylist_append_double( header, XSH_DET_PXSPACE,
00302 iconfig->pxspace),
00303 "Cant append PXSPACE keyword" ) ;
00304 }
00305
00306 cleanup:
00307 return;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 cpl_image * create_order_image( xsh_order_list* list,
00326 int nx, int ny )
00327 {
00328 int i ;
00329 double pixmax = 100. ;
00330 cpl_image * image = NULL ;
00331
00332 check( image = cpl_image_new( nx, ny, CPL_TYPE_DOUBLE ) ) ;
00333
00334 for( i = 0 ; i<list->size ; i++ ) {
00335
00336 int ixl, ixu, ixc, iy;
00337 cpl_size zero = 0 ;
00338 double cxl, cxu ;
00339 double pixstep ;
00340
00341 cxl = cpl_polynomial_get_coeff( list->list[i].edglopoly, &zero ) ;
00342 cxu = cpl_polynomial_get_coeff( list->list[i].edguppoly, &zero ) ;
00343 pixstep = (2*pixmax)/(cxu-cxl) ;
00344
00345
00346
00347 for( iy = list->list[i].starty ; iy < list->list[i].endy ;
00348 iy++ ) {
00349 int k ;
00350 double pixval ;
00351
00352
00353 ixc = cpl_polynomial_eval_1d( list->list[i].cenpoly,
00354 (double)iy, NULL ) ;
00355 ixl = cpl_polynomial_eval_1d( list->list[i].edglopoly,
00356 (double)iy, NULL ) ;
00357 ixu = cpl_polynomial_eval_1d( list->list[i].edguppoly,
00358 (double)iy, NULL ) ;
00359
00360
00361 for( pixval = 0., k = ixl ; k<ixc ; k++, pixval += pixstep) {
00362 cpl_image_set( image, k, iy, pixval ) ;
00363 }
00364 cpl_image_set( image, ixc+1, iy+1, pixmax ) ;
00365 for( pixval = pixval, k = ixc ; k<=ixu ; k++, pixval -= pixstep) {
00366 cpl_image_set( image, k, iy, pixval ) ;
00367 }
00368 }
00369 }
00370 cleanup:
00371 return image ;
00372 }
00373
00374 xsh_order_list * create_order_list( int norder, xsh_instrument* instrument )
00375 {
00376 xsh_order_list* result = NULL ;
00377
00378 XSH_CALLOC(result, xsh_order_list, 1);
00379 result->size = norder ;
00380 result->instrument = instrument ;
00381 XSH_CALLOC( result->list, xsh_order, result->size);
00382 XSH_NEW_PROPERTYLIST( result->header );
00383
00384 cleanup:
00385 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00386 xsh_order_list_free(&result);
00387 }
00388 return result;
00389
00390 }
00391
00392
00393
00394
00395
00396 void add_to_order_list( xsh_order_list * list, int order,
00397 int absorder,
00398 cpl_polynomial *poly, int xdelta,
00399 int starty, int endy )
00400 {
00401 cpl_size i = 0 ;
00402 double coeff0 ;
00403
00404 coeff0 = cpl_polynomial_get_coeff( poly, &i ) ;
00405
00406 list->list[order].order = absorder ;
00407 list->list[order].absorder = absorder ;
00408 list->list[order].cenpoly = cpl_polynomial_duplicate( poly ) ;
00409 list->list[order].edguppoly = cpl_polynomial_duplicate( poly ) ;
00410 cpl_polynomial_set_coeff( list->list[order].edguppoly, &i,
00411 coeff0 + (double)xdelta ) ;
00412 list->list[order].edglopoly = cpl_polynomial_duplicate( poly ) ;
00413 cpl_polynomial_set_coeff( list->list[order].edglopoly, &i,
00414 coeff0 - (double)xdelta ) ;
00415 list->list[order].starty = starty ;
00416 list->list[order].endy = endy ;
00417 }
00418
00419
00420
00421 #define NB_LAMBDA 100
00422 #define FIRST_LAMBDA 500.0
00423 #define LAST_LAMBDA 510.0
00424
00425 #define NB_SLIT 40
00426 #define FIRST_SLIT -5
00427 #define LAST_SLIT 5
00428
00429 #define POS_PLUS 30
00430 #define COEFF0_PLUS 30.
00431 #define COEFF1_PLUS 0.03
00432 #define POS_CENTER 30
00433 #define POS_MINUS 5
00434 #define COEFF0_MINUS 10.
00435 #define COEFF1_MINUS 0.03
00436
00437 #define WIDTH 5
00438 #define HALF_WIDTH 2
00439
00440 static float Flux[WIDTH] = {
00441 20., 40., 100., 40., 20. } ;
00442
00443 static cpl_polynomial * poly_plus = NULL, * poly_minus = NULL ;
00444
00445 cpl_frame * create_rectify_nod_list( int sign, const char * fname,
00446 xsh_instrument * instr )
00447 {
00448 xsh_rec_list* result = NULL;
00449 int i, j, k, nb, order ;
00450 double * plambda ;
00451 float * pslit ;
00452 double step ;
00453 const char * tag = NULL ;
00454 cpl_frame * res_frame = NULL ;
00455
00456 XSH_CALLOC(result, xsh_rec_list, 1 );
00457 result->size = instr->config->orders ;
00458 XSH_CALLOC(result->list, xsh_rec, result->size);
00459 XSH_NEW_PROPERTYLIST(result->header);
00460
00461 for( nb = 0, order = instr->config->order_min ;
00462 order <= instr->config->order_max ;
00463 order++, nb++ ) {
00464 result->list[nb].order = order ;
00465 result->list[nb].nlambda = NB_LAMBDA ;
00466 result->list[nb].nslit = NB_SLIT ;
00467 XSH_CALLOC( result->list[nb].slit, float, NB_SLIT ) ;
00468 XSH_CALLOC( result->list[nb].lambda, double, NB_LAMBDA ) ;
00469 XSH_CALLOC( result->list[nb].data1, float, NB_LAMBDA*NB_SLIT ) ;
00470 XSH_CALLOC( result->list[nb].errs1, float, NB_LAMBDA*NB_SLIT ) ;
00471 XSH_CALLOC( result->list[nb].qual1, int, NB_LAMBDA*NB_SLIT ) ;
00472
00473 xsh_msg_dbg_high( "Fill lambda" ) ;
00474
00475 plambda = result->list[nb].lambda ;
00476 step = (double)(LAST_LAMBDA-FIRST_LAMBDA)/(double)NB_LAMBDA ;
00477 for ( i = 0 ; i<NB_LAMBDA ; i++, plambda++ )
00478 *plambda = FIRST_LAMBDA + i*step ;
00479
00480 pslit = result->list[nb].slit ;
00481 step = (double)(LAST_SLIT-FIRST_SLIT)/(double)NB_SLIT ;
00482 xsh_msg_dbg_high( "Fill slit (%lf)", step ) ;
00483 for ( i = 0 ; i<NB_SLIT ; i++, pslit++ )
00484 *pslit = FIRST_SLIT + i*step ;
00485
00486 check( poly_plus = cpl_polynomial_new( 1 ) ) ;
00487 check( poly_minus = cpl_polynomial_new( 1 ) ) ;
00488
00489 {
00490 cpl_size kc ;
00491
00492 kc = 0 ;
00493 cpl_polynomial_set_coeff( poly_plus, &kc, COEFF0_PLUS ) ;
00494 kc = 1 ;
00495 cpl_polynomial_set_coeff( poly_plus, &kc, COEFF1_PLUS ) ;
00496
00497 kc = 0 ;
00498 cpl_polynomial_set_coeff( poly_minus, &kc, COEFF0_MINUS ) ;
00499 kc = 1 ;
00500 cpl_polynomial_set_coeff( poly_minus, &kc, COEFF1_MINUS ) ;
00501 }
00502
00503 plambda = result->list[nb].lambda ;
00504 for( j = 0 ; j<NB_LAMBDA ; j++, plambda++ ) {
00505 float datump, datumm ;
00506 int ns ;
00507
00508 check( datump = cpl_polynomial_eval_1d( poly_plus, (double)j, NULL ) ) ;
00509 check( datumm = cpl_polynomial_eval_1d( poly_minus, (double)j, NULL ) ) ;
00510
00511 for ( k = 0, ns = (int)datump - HALF_WIDTH ; k<WIDTH ; k++, ns++ ) {
00512 float val ;
00513 if ( sign > 0 ) val = Flux[k] ;
00514 else val = -Flux[k] ;
00515 result->list[nb].data1[j + (int)ns*NB_LAMBDA] = val ;
00516 }
00517 for ( k = 0, ns = (int)datumm - HALF_WIDTH ; k<WIDTH ; k++, ns++ ) {
00518 float val ;
00519 if ( sign > 0 ) val = -Flux[k] ;
00520 else val = Flux[k] ;
00521 result->list[nb].data1[j + (int)ns*NB_LAMBDA] = val ;
00522 }
00523 }
00524 }
00525 result->instrument = instr;
00526
00527 xsh_msg_dbg_high( "Save frame" ) ;
00528 res_frame = xsh_rec_list_save( result, fname, "TAG", 1 ) ;
00529 tag = XSH_GET_TAG_FROM_ARM( XSH_ORDER2D,instr);
00530 check(cpl_frame_set_tag(res_frame, tag )) ;
00531
00532 cleanup:
00533 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00534 xsh_rec_list_free(&result);
00535 }
00536 return res_frame;
00537 }
00538
00539
00540 cpl_frameset * sof_to_frameset( const char* sof_name)
00541 {
00542 FILE *sof_file = NULL;
00543 cpl_frameset *result = NULL;
00544 char sof_line[200];
00545
00546 XSH_ASSURE_NOT_NULL( sof_name);
00547
00548 check( result = cpl_frameset_new());
00549
00550 sof_file = fopen( sof_name, "r");
00551
00552 if (sof_file != NULL){
00553
00554 while ( fgets( sof_line, 200, sof_file)){
00555 char name[200];
00556 char tag[200];
00557 cpl_frame *frame = NULL;
00558
00559 if ( sof_line[0] == '#'){
00560 xsh_msg("skip line %s", sof_line);
00561 }
00562 else{
00563 sscanf( sof_line, "%s %s", name, tag);
00564 check( frame = cpl_frame_new());
00565 check( cpl_frame_set_filename( frame, name));
00566 check( cpl_frame_set_tag( frame, tag));
00567 check( cpl_frameset_insert( result, frame));
00568 }
00569 }
00570 fclose( sof_file);
00571 }
00572 else{
00573 xsh_msg("File %s not found", sof_name);
00574 XSH_ASSURE_NOT_NULL( sof_file);
00575 }
00576
00577 cleanup:
00578 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00579 xsh_free_frameset( &result);
00580 }
00581 return result;
00582 }
00583
00584 xsh_instrument * create_instrument( const char *filename)
00585 {
00586 xsh_instrument *instr = NULL;
00587 cpl_propertylist *header = NULL;
00588 const char *catg = NULL;
00589
00590 check( header = cpl_propertylist_load( filename, 0));
00591 check( catg = xsh_pfits_get_pcatg( header));
00592
00593 check( instr = xsh_instrument_new());
00594
00595 if (strstr( catg, "UVB") != NULL){
00596 xsh_instrument_set_arm( instr, XSH_ARM_UVB);
00597 }
00598 else {
00599 if (strstr( catg, "VIS") != NULL){
00600 xsh_instrument_set_arm( instr, XSH_ARM_VIS);
00601 }
00602 else{
00603 if (strstr( catg, "NIR") != NULL){
00604 xsh_instrument_set_arm( instr, XSH_ARM_NIR);
00605 }
00606 }
00607 }
00608
00609 cleanup:
00610 xsh_free_propertylist( &header);
00611 return instr;
00612 }