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