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
00035
00036 #define XSH_RESIDX_THRESHOLD 0.1
00037
00040
00041
00042
00043 #include <xsh_data_instrument.h>
00044 #include <xsh_data_order_resid_tab.h>
00045 #include <xsh_utils.h>
00046 #include <xsh_error.h>
00047 #include <xsh_msg.h>
00048 #include <xsh_pfits.h>
00049 #include <cpl.h>
00050 #include <xsh_drl.h>
00051 #include <xsh_utils_table.h>
00052
00053
00054
00055
00056
00057
00077
00078 xsh_resid_order_tab * xsh_resid_order_create(int size, int * orders,
00079 double * posx, double * posy,
00080 double * resx, double* polx )
00081 {
00082 xsh_resid_order_tab* result = NULL;
00083 int i;
00084
00085
00086 XSH_ASSURE_NOT_ILLEGAL( size>=0);
00087 XSH_ASSURE_NOT_NULL( orders);
00088 XSH_ASSURE_NOT_NULL( posx);
00089 XSH_ASSURE_NOT_NULL( posy);
00090 XSH_ASSURE_NOT_NULL( resx);
00091 XSH_ASSURE_NOT_NULL( polx);
00092
00093 XSH_CALLOC( result, xsh_resid_order_tab, 1);
00094
00095 XSH_CALLOC( result->order, int, size);
00096 XSH_CALLOC( result->pos_x, double, size);
00097 XSH_CALLOC( result->pos_y, double, size);
00098 XSH_CALLOC( result->res_x, double, size);
00099 XSH_CALLOC( result->pol_x, double, size);
00100
00101
00102 check (result->header = cpl_propertylist_new());
00103
00104 result->size = size;
00105 xsh_msg_dbg_low( " xsh_resid_order_create( %d )", result->size ) ;
00106
00107 for( i=0; i<size; i++) {
00108 result->order[i] = orders[i];
00109 result->pos_x[i] = posx[i];
00110 result->pos_y[i] = posy[i];
00111 result->res_x[i] = resx[i];
00112 result->pol_x[i] = polx[i];
00113 }
00114
00115 cleanup:
00116 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00117 xsh_resid_order_free( &result);
00118 }
00119 return result;
00120 }
00121
00122
00132
00133
00134 xsh_resid_order_tab * xsh_resid_order_load( cpl_frame * resid_tab_frame)
00135 {
00136 xsh_resid_order_tab * result = NULL;
00137 cpl_table * table = NULL;
00138 const char * tablename = NULL;
00139 int i = 0;
00140 int size = 0;
00141
00142
00143 XSH_ASSURE_NOT_NULL( resid_tab_frame);
00144
00145
00146 check( tablename = cpl_frame_get_filename( resid_tab_frame));
00147
00148 XSH_TABLE_LOAD( table, tablename);
00149
00150 check( size = cpl_table_get_nrow(table));
00151
00152 XSH_CALLOC( result, xsh_resid_order_tab, 1);
00153 check (result->header = cpl_propertylist_load( tablename, 0));
00154 result->size = size;
00155
00156 XSH_CALLOC( result->order, int, size);
00157 XSH_CALLOC( result->pos_x, double, size);
00158 XSH_CALLOC( result->pos_y, double, size);
00159 XSH_CALLOC( result->res_x, double, size);
00160 XSH_CALLOC( result->pol_x, double, size);
00161
00162 for(i=0; i<size;i++){
00163
00164 check( xsh_get_table_value( table,
00165 XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00166 CPL_TYPE_INT, i, &result->order[i]));
00167
00168 check( xsh_get_table_value( table,
00169 XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00170 CPL_TYPE_DOUBLE, i, &result->pos_x[i]));
00171
00172 check( xsh_get_table_value( table,
00173 XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00174 CPL_TYPE_DOUBLE, i, &result->pos_y[i]));
00175
00176 check( xsh_get_table_value( table,
00177 XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00178 CPL_TYPE_DOUBLE, i, &result->res_x[i]));
00179
00180 check( xsh_get_table_value( table,
00181 XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00182 CPL_TYPE_DOUBLE, i, &result->pol_x[i]));
00183 }
00184
00185 cleanup:
00186 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00187 xsh_error_msg("can't load frame %s",
00188 cpl_frame_get_filename(resid_tab_frame));
00189 xsh_resid_order_free(&result);
00190 }
00191 XSH_TABLE_FREE( table);
00192 return result;
00193 }
00194
00195
00203
00204 void xsh_resid_order_free( xsh_resid_order_tab** resid) {
00205 if ( resid && *resid) {
00206 XSH_FREE( (*resid)->order);
00207 XSH_FREE( (*resid)->pos_x);
00208 XSH_FREE( (*resid)->pos_y);
00209 XSH_FREE( (*resid)->res_x);
00210 XSH_FREE( (*resid)->pol_x);
00211
00212 xsh_free_propertylist( &(*resid)->header);
00213
00214 cpl_free(*resid);
00215 *resid = NULL;
00216 }
00217
00218 }
00219
00220
00221
00239
00240 cpl_frame * xsh_resid_order_save( xsh_resid_order_tab * resid,
00241 const char* filename,
00242 xsh_instrument * instrument,
00243 ORDERPOS_QC_PARAM* ord_qc_param,
00244 const char* tag)
00245 {
00246 cpl_frame *result = NULL ;
00247 cpl_table *table = NULL;
00248 cpl_propertylist *header = NULL;
00249 int i = 0;
00250
00251
00252 cpl_table* sel=NULL;
00253
00254 XSH_ASSURE_NOT_NULL( resid);
00255 XSH_ASSURE_NOT_NULL( filename);
00256 XSH_ASSURE_NOT_NULL( instrument);
00257
00258 xsh_msg_dbg_high( " xsh_resid_order_save, size = %d", resid->size ) ;
00259
00260
00261 check( table = cpl_table_new( XSH_RESID_ORDER_TABLE_NB_COL));
00262 header = resid->header;
00263
00264
00265
00266
00267 check( xsh_pfits_set_qc( header, &resid->residmin,
00268 QC_ORD_ORDERPOS_RESIDMIN,
00269 instrument ) ) ;
00270 check( xsh_pfits_set_qc( header, &resid->residmax,
00271 QC_ORD_ORDERPOS_RESIDMAX,
00272 instrument ) ) ;
00273 check( xsh_pfits_set_qc( header, &resid->residavg,
00274 QC_ORD_ORDERPOS_RESIDAVG,
00275 instrument ) ) ;
00276 check( xsh_pfits_set_qc( header, &resid->residrms,
00277 QC_ORD_ORDERPOS_RESIDRMS,
00278 instrument ) ) ;
00279
00280
00281 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00282 XSH_RESID_ORDER_TABLE_UNIT_ORDER, CPL_TYPE_INT);
00283
00284 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00285 XSH_RESID_ORDER_TABLE_UNIT_POSX, CPL_TYPE_DOUBLE);
00286
00287 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00288 XSH_RESID_ORDER_TABLE_UNIT_POSY, CPL_TYPE_DOUBLE);
00289
00290 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00291 XSH_RESID_ORDER_TABLE_UNIT_RESX, CPL_TYPE_DOUBLE);
00292
00293 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00294 XSH_RESID_ORDER_TABLE_UNIT_POLX, CPL_TYPE_DOUBLE);
00295
00296 check(cpl_table_set_size( table, resid->size));
00297
00298
00299 for (i=0; i<resid->size; i++) {
00300 check(cpl_table_set_int(table, XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00301 i, resid->order[i]));
00302 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00303 i, resid->pos_x[i]));
00304 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00305 i, resid->pos_y[i]));
00306 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00307 i, resid->res_x[i]));
00308 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00309 i, resid->pol_x[i]));
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319 ord_qc_param->nposall = resid->size ;
00320
00321 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00322 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,XSH_RESIDX_THRESHOLD));
00323
00324 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00325 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_GREATER_THAN,-XSH_RESIDX_THRESHOLD));
00326
00327
00328 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00329 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,2*resid->residrms));
00330 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00331 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_GREATER_THAN,-2*resid->residrms));
00332 check(sel=cpl_table_extract_selected(table));
00333
00334
00335 check(resid->residavg_sel =
00336 cpl_table_get_column_mean(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00337
00338 check(resid->residmax_sel =
00339 cpl_table_get_column_max(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00340
00341 check(resid->residmin_sel =
00342 cpl_table_get_column_min(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00343
00344 check(resid->residrms_sel =
00345 cpl_table_get_column_stdev(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00346
00347 xsh_msg_dbg_high("after selection: avg=%g med=%g rms=%g min=%g max=%g",
00348 resid->residavg_sel,
00349 cpl_table_get_column_median(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX),
00350 resid->residrms_sel,
00351 resid->residmin_sel,
00352 resid->residmax_sel);
00353
00354
00355 check( xsh_pfits_set_qc( header, &resid->residmin_sel,
00356 QC_ORD_ORDERPOS_RESIDMIN_SEL,
00357 instrument ) ) ;
00358 check( xsh_pfits_set_qc( header, &resid->residmax_sel,
00359 QC_ORD_ORDERPOS_RESIDMAX_SEL,
00360 instrument ) ) ;
00361 check( xsh_pfits_set_qc( header, &resid->residavg_sel,
00362 QC_ORD_ORDERPOS_RESIDAVG_SEL,
00363 instrument ) ) ;
00364 check( xsh_pfits_set_qc( header, &resid->residrms_sel,
00365 QC_ORD_ORDERPOS_RESIDRMS_SEL,
00366 instrument ) ) ;
00367
00368
00369
00370 check( xsh_pfits_set_pcatg(header, tag));
00371 check( cpl_table_save(table, header,NULL,filename, CPL_IO_DEFAULT));
00372
00373
00374
00375 check(result=xsh_frame_product(filename,
00376 tag,
00377 CPL_FRAME_TYPE_TABLE,
00378 CPL_FRAME_GROUP_PRODUCT,
00379 CPL_FRAME_LEVEL_TEMPORARY));
00380
00381
00382 cleanup:
00383
00384 if (cpl_error_get_code() != CPL_ERROR_NONE){
00385 xsh_free_frame(&result);
00386 }
00387 XSH_TABLE_FREE( table);
00388 XSH_TABLE_FREE( sel);
00389 return result ;
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00418
00419 cpl_table *
00420 xsh_resid_order_2tab( xsh_resid_order_tab * resid,
00421 xsh_instrument * instrument,
00422 ORDERPOS_QC_PARAM* ord_qc_param)
00423 {
00424 cpl_table *table = NULL;
00425 int i = 0;
00426
00427
00428 cpl_table* sel=NULL;
00429 int* porder=NULL;
00430 double* pposx=NULL;
00431 double* pposy=NULL;
00432 double* presx=NULL;
00433 double* ppolx=NULL;
00434
00435 XSH_ASSURE_NOT_NULL( resid);
00436 XSH_ASSURE_NOT_NULL( instrument);
00437
00438 xsh_msg( " xsh_resid_order_save, size = %d", resid->size ) ;
00439
00440
00441 check( table = cpl_table_new( XSH_RESID_ORDER_TABLE_NB_COL));
00442
00443
00444 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00445 XSH_RESID_ORDER_TABLE_UNIT_ORDER, CPL_TYPE_INT);
00446
00447 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00448 XSH_RESID_ORDER_TABLE_UNIT_POSX, CPL_TYPE_DOUBLE);
00449
00450 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00451 XSH_RESID_ORDER_TABLE_UNIT_POSY, CPL_TYPE_DOUBLE);
00452
00453 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00454 XSH_RESID_ORDER_TABLE_UNIT_RESX, CPL_TYPE_DOUBLE);
00455
00456 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00457 XSH_RESID_ORDER_TABLE_UNIT_POLX, CPL_TYPE_DOUBLE);
00458
00459 check(cpl_table_set_size( table, resid->size));
00460
00461
00462
00463
00464 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00465 0,resid->size,-1);
00466
00467 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00468 0,resid->size,-1);
00469
00470 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00471 0,resid->size,-1);
00472
00473 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00474 0,resid->size,-1);
00475
00476 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00477 0,resid->size,-1);
00478 porder=cpl_table_get_data_int(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER);
00479 pposx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX);
00480 pposy=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY);
00481 presx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX);
00482 ppolx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX);
00483
00484
00485
00486 for (i=0; i<resid->size; i++) {
00487 check(porder[i]=resid->order[i]);
00488 check(pposx[i]=resid->pos_x[i]);
00489 check(pposy[i]=resid->pos_y[i]);
00490 check(presx[i]=resid->res_x[i]);
00491 check(ppolx[i]=resid->pol_x[i]);
00492 }
00493
00494
00495
00496
00497
00498
00499
00500
00501 ord_qc_param->nposall = resid->size ;
00502
00503 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00504 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,XSH_RESIDX_THRESHOLD));
00505
00506
00507 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00508 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,2*resid->residrms));
00509 check(sel=cpl_table_extract_selected(table));
00510
00511
00512 check(resid->residavg_sel =
00513 cpl_table_get_column_mean(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00514
00515 check(resid->residmax_sel =
00516 cpl_table_get_column_max(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00517
00518 check(resid->residmin_sel =
00519 cpl_table_get_column_min(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00520
00521 check(resid->residrms_sel =
00522 cpl_table_get_column_stdev(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00523
00524 xsh_msg("after selection: avg=%g med=%g rms=%g min=%g max=%g",
00525 resid->residavg_sel,
00526 cpl_table_get_column_median(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX),
00527 resid->residrms_sel,
00528 resid->residmin_sel,
00529 resid->residmax_sel);
00530
00531
00532 cleanup:
00533
00534 XSH_TABLE_FREE( sel);
00535 return table ;
00536 }
00537
00538
00539
00540
00541
00542
00543
00544