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 }
00216 *resid = NULL;
00217 }
00218
00219
00220
00238
00239 cpl_frame * xsh_resid_order_save( xsh_resid_order_tab * resid,
00240 const char* filename,
00241 xsh_instrument * instrument,
00242 ORDERPOS_QC_PARAM* ord_qc_param,
00243 const char* tag)
00244 {
00245 cpl_frame *result = NULL ;
00246 cpl_table *table = NULL;
00247 cpl_propertylist *header = NULL;
00248 int i = 0;
00249 int ord_min=0;
00250 int ord_max=0;
00251 cpl_table* sel=NULL;
00252
00253 XSH_ASSURE_NOT_NULL( resid);
00254 XSH_ASSURE_NOT_NULL( filename);
00255 XSH_ASSURE_NOT_NULL( instrument);
00256
00257 xsh_msg_dbg_high( " xsh_resid_order_save, size = %d", resid->size ) ;
00258
00259
00260 check( table = cpl_table_new( XSH_RESID_ORDER_TABLE_NB_COL));
00261 header = resid->header;
00262
00263
00264
00265
00266 check( xsh_pfits_set_qc( header, &resid->residmin,
00267 QC_ORD_ORDERPOS_RESIDMIN,
00268 instrument ) ) ;
00269 check( xsh_pfits_set_qc( header, &resid->residmax,
00270 QC_ORD_ORDERPOS_RESIDMAX,
00271 instrument ) ) ;
00272 check( xsh_pfits_set_qc( header, &resid->residavg,
00273 QC_ORD_ORDERPOS_RESIDAVG,
00274 instrument ) ) ;
00275 check( xsh_pfits_set_qc( header, &resid->residrms,
00276 QC_ORD_ORDERPOS_RESIDRMS,
00277 instrument ) ) ;
00278
00279
00280 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00281 XSH_RESID_ORDER_TABLE_UNIT_ORDER, CPL_TYPE_INT);
00282
00283 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00284 XSH_RESID_ORDER_TABLE_UNIT_POSX, CPL_TYPE_DOUBLE);
00285
00286 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00287 XSH_RESID_ORDER_TABLE_UNIT_POSY, CPL_TYPE_DOUBLE);
00288
00289 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00290 XSH_RESID_ORDER_TABLE_UNIT_RESX, CPL_TYPE_DOUBLE);
00291
00292 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00293 XSH_RESID_ORDER_TABLE_UNIT_POLX, CPL_TYPE_DOUBLE);
00294
00295 check(cpl_table_set_size( table, resid->size));
00296
00297
00298 for (i=0; i<resid->size; i++) {
00299 check(cpl_table_set_int(table, XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00300 i, resid->order[i]));
00301 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00302 i, resid->pos_x[i]));
00303 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00304 i, resid->pos_y[i]));
00305 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00306 i, resid->res_x[i]));
00307 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00308 i, resid->pol_x[i]));
00309 }
00310
00311
00312
00313
00314
00315 check(ord_max=cpl_table_get_column_max(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
00316 check(ord_min=cpl_table_get_column_min(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
00317
00318 ord_qc_param->nposall = resid->size ;
00319
00320 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00321 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,XSH_RESIDX_THRESHOLD));
00322
00323 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00324 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_GREATER_THAN,-XSH_RESIDX_THRESHOLD));
00325
00326
00327 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00328 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,2*resid->residrms));
00329 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00330 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_GREATER_THAN,-2*resid->residrms));
00331 check(sel=cpl_table_extract_selected(table));
00332
00333
00334 check(resid->residavg_sel =
00335 cpl_table_get_column_mean(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00336
00337 check(resid->residmax_sel =
00338 cpl_table_get_column_max(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00339
00340 check(resid->residmin_sel =
00341 cpl_table_get_column_min(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00342
00343 check(resid->residrms_sel =
00344 cpl_table_get_column_stdev(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00345
00346 xsh_msg_dbg_high("after selection: avg=%g med=%g rms=%g min=%g max=%g",
00347 resid->residavg_sel,
00348 cpl_table_get_column_median(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX),
00349 resid->residrms_sel,
00350 resid->residmin_sel,
00351 resid->residmax_sel);
00352
00353
00354 check( xsh_pfits_set_qc( header, &resid->residmin_sel,
00355 QC_ORD_ORDERPOS_RESIDMIN_SEL,
00356 instrument ) ) ;
00357 check( xsh_pfits_set_qc( header, &resid->residmax_sel,
00358 QC_ORD_ORDERPOS_RESIDMAX_SEL,
00359 instrument ) ) ;
00360 check( xsh_pfits_set_qc( header, &resid->residavg_sel,
00361 QC_ORD_ORDERPOS_RESIDAVG_SEL,
00362 instrument ) ) ;
00363 check( xsh_pfits_set_qc( header, &resid->residrms_sel,
00364 QC_ORD_ORDERPOS_RESIDRMS_SEL,
00365 instrument ) ) ;
00366
00367
00368
00369 check( xsh_pfits_set_pcatg(header, tag));
00370 check( cpl_table_save(table, header,NULL,filename, CPL_IO_DEFAULT));
00371
00372
00373
00374 check(result=xsh_frame_product(filename,
00375 tag,
00376 CPL_FRAME_TYPE_TABLE,
00377 CPL_FRAME_GROUP_PRODUCT,
00378 CPL_FRAME_LEVEL_TEMPORARY));
00379 check (xsh_add_temporary_file( filename));
00380
00381 cleanup:
00382
00383 if (cpl_error_get_code() != CPL_ERROR_NONE){
00384 xsh_free_frame(&result);
00385 }
00386 XSH_TABLE_FREE( table);
00387 XSH_TABLE_FREE( sel);
00388 return result ;
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00417
00418 cpl_table *
00419 xsh_resid_order_2tab( xsh_resid_order_tab * resid,
00420 xsh_instrument * instrument,
00421 ORDERPOS_QC_PARAM* ord_qc_param)
00422 {
00423 cpl_table *table = NULL;
00424 int i = 0;
00425 int ord_min=0;
00426 int ord_max=0;
00427 cpl_table* sel=NULL;
00428 int* porder=NULL;
00429 double* pposx=NULL;
00430 double* pposy=NULL;
00431 double* presx=NULL;
00432 double* ppolx=NULL;
00433
00434 XSH_ASSURE_NOT_NULL( resid);
00435 XSH_ASSURE_NOT_NULL( instrument);
00436
00437 xsh_msg( " xsh_resid_order_save, size = %d", resid->size ) ;
00438
00439
00440 check( table = cpl_table_new( XSH_RESID_ORDER_TABLE_NB_COL));
00441
00442
00443 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00444 XSH_RESID_ORDER_TABLE_UNIT_ORDER, CPL_TYPE_INT);
00445
00446 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00447 XSH_RESID_ORDER_TABLE_UNIT_POSX, CPL_TYPE_DOUBLE);
00448
00449 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00450 XSH_RESID_ORDER_TABLE_UNIT_POSY, CPL_TYPE_DOUBLE);
00451
00452 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00453 XSH_RESID_ORDER_TABLE_UNIT_RESX, CPL_TYPE_DOUBLE);
00454
00455 XSH_TABLE_NEW_COL(table, XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00456 XSH_RESID_ORDER_TABLE_UNIT_POLX, CPL_TYPE_DOUBLE);
00457
00458 check(cpl_table_set_size( table, resid->size));
00459
00460
00461
00462
00463 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
00464 0,resid->size,-1);
00465
00466 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX,
00467 0,resid->size,-1);
00468
00469 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY,
00470 0,resid->size,-1);
00471
00472 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX,
00473 0,resid->size,-1);
00474
00475 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX,
00476 0,resid->size,-1);
00477 porder=cpl_table_get_data_int(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER);
00478 pposx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX);
00479 pposy=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY);
00480 presx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX);
00481 ppolx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX);
00482
00483
00484
00485 for (i=0; i<resid->size; i++) {
00486 check(porder[i]=resid->order[i]);
00487 check(pposx[i]=resid->pos_x[i]);
00488 check(pposy[i]=resid->pos_y[i]);
00489 check(presx[i]=resid->res_x[i]);
00490 check(ppolx[i]=resid->pol_x[i]);
00491 }
00492
00493
00494
00495
00496
00497 check(ord_max=cpl_table_get_column_max(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
00498 check(ord_min=cpl_table_get_column_min(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
00499
00500 ord_qc_param->nposall = resid->size ;
00501
00502 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00503 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,XSH_RESIDX_THRESHOLD));
00504
00505
00506 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
00507 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,2*resid->residrms));
00508 check(sel=cpl_table_extract_selected(table));
00509
00510
00511 check(resid->residavg_sel =
00512 cpl_table_get_column_mean(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00513
00514 check(resid->residmax_sel =
00515 cpl_table_get_column_max(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00516
00517 check(resid->residmin_sel =
00518 cpl_table_get_column_min(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00519
00520 check(resid->residrms_sel =
00521 cpl_table_get_column_stdev(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
00522
00523 xsh_msg("after selection: avg=%g med=%g rms=%g min=%g max=%g",
00524 resid->residavg_sel,
00525 cpl_table_get_column_median(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX),
00526 resid->residrms_sel,
00527 resid->residmin_sel,
00528 resid->residmax_sel);
00529
00530
00531 cleanup:
00532
00533 XSH_TABLE_FREE( sel);
00534 return table ;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543