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 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00032
00033 #include <xsh_utils_table.h>
00034 #include <xsh_utils.h>
00035 #include <xsh_error.h>
00036 #include <xsh_msg.h>
00037
00038
00039
00040
00041
00049
00050
00053
00065
00066 cpl_error_code xsh_get_table_value (const cpl_table* table,
00067 const char *colname, cpl_type coltype, int i, void* result)
00068 {
00069 int flag = 0;
00070
00071 XSH_ASSURE_NOT_NULL( table);
00072 XSH_ASSURE_NOT_NULL( colname);
00073 XSH_ASSURE_NOT_NULL( result);
00074
00075
00076
00077 switch (coltype) {
00078 case CPL_TYPE_INT:
00079 check_msg (*((int *) result) = cpl_table_get_int(table,colname,i,&flag),
00080 "Could not get (integer) value of %s at row %d", colname,i);
00081 break;
00082 case CPL_TYPE_FLOAT:
00083 check_msg (*((float *) result) = cpl_table_get_float( table,
00084 colname,i,&flag),
00085 "Could not get (float) value of %s at row %d",colname,i);
00086 break;
00087 case CPL_TYPE_DOUBLE:
00088 check_msg (*((double *) result) = cpl_table_get_double (table,
00089 colname,i,&flag),
00090 "Could not get (double) value of %s at row %d",colname,i);
00091 break;
00092 case CPL_TYPE_STRING:
00093 check_msg (*((const char **) result) =
00094 cpl_table_get_string (table, colname,i),
00095 "Could not get (string) value of %s at row %d",colname,i);
00096 break;
00097 default:
00098 assure (false, CPL_ERROR_INVALID_TYPE, "Unknown type");
00099 }
00100
00101 cleanup:
00102 return cpl_error_get_code ();
00103 }
00104
00105 XSH_TABLE_GET_ARRAY(int)
00106 XSH_TABLE_GET_ARRAY(float)
00107 XSH_TABLE_GET_ARRAY(double)
00108
00109
00122
00123 cpl_error_code
00124 xsh_sort_table_1(cpl_table *t, const char *column, cpl_boolean reverse)
00125 {
00126 cpl_propertylist *plist = NULL;
00127
00128 assure(t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
00129 assure(cpl_table_has_column(t, column), CPL_ERROR_ILLEGAL_INPUT,
00130 "No column '%s'", column);
00131
00132 check_msg(( plist = cpl_propertylist_new(),
00133 cpl_propertylist_append_bool(plist, column, reverse)),
00134 "Could not create property list for sorting");
00135
00136 check_msg( cpl_table_sort(t, plist), "Could not sort table");
00137
00138 cleanup:
00139 xsh_free_propertylist(&plist);
00140 return cpl_error_get_code();
00141 }
00142
00143
00160
00161 cpl_error_code
00162 xsh_sort_table_2(cpl_table *t, const char *column1, const char *column2,
00163 cpl_boolean reverse1, cpl_boolean reverse2)
00164 {
00165 cpl_propertylist *plist = NULL;
00166
00167 assure(t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
00168 assure(cpl_table_has_column(t, column1), CPL_ERROR_ILLEGAL_INPUT,
00169 "No column '%s'", column1);
00170 assure(cpl_table_has_column(t, column2), CPL_ERROR_ILLEGAL_INPUT,
00171 "No column '%s'", column2);
00172
00173 check_msg(( plist = cpl_propertylist_new(),
00174 cpl_propertylist_append_bool(plist, column1, reverse1),
00175 cpl_propertylist_append_bool(plist, column2, reverse2)),
00176 "Could not create property list for sorting");
00177 check_msg( cpl_table_sort(t, plist), "Could not sort table");
00178
00179 cleanup:
00180 xsh_free_propertylist(&plist);
00181 return cpl_error_get_code();
00182 }
00183
00184
00193
00194 double
00195 xsh_data_interpolate(
00196 double wav,
00197 int nrow,
00198 double* pw,
00199 double* pe
00200 )
00201 {
00202 double y = 0;
00203 double w1=pw[0];
00204 double w2=pw[nrow-1];
00205 double y1_=pe[0];
00206 double y2=pe[nrow-1];
00207 if(wav < pw[0])
00208 {
00209 w1=pw[0];
00210 w2=pw[1];
00211 y1_=pe[0];
00212 y2=pe[1];
00213 }
00214 else if (wav > pw[nrow - 1])
00215 {
00216 w1=pw[nrow - 2];
00217 w2=pw[nrow - 1];
00218 y1_=pe[nrow - 2];
00219 y2=pe[nrow - 1];
00220 }
00221 else
00222 {
00223 int l = 0;
00224 int h = nrow - 1;
00225 int curr_row = 0;
00226 curr_row = (h-l) / 2;
00227 while( h-l >1 )
00228 {
00229 if(wav < pw[curr_row])
00230 {
00231 h = curr_row;
00232 }
00233 else
00234 {
00235 l = curr_row;
00236 }
00237 curr_row = (h-l) / 2 + l;
00238 }
00239 w1=pw[curr_row];
00240 w2=pw[curr_row + 1];
00241 y1_=pe[curr_row];
00242 y2=pe[curr_row + 1];
00243 }
00244 y=y1_+(y2-y1_)/(w2-w1)*(wav-w1);
00245 return y;
00246 }
00247
00248
00259
00260 double
00261 xsh_table_interpolate(cpl_table* tbl,
00262 double wav,
00263 const char* colx,
00264 const char* coly)
00265 {
00266
00267 double y=0;
00268 double* pe=NULL;
00269 double* pw=NULL;
00270 int nrow=0;
00271
00272 check(pw=cpl_table_get_data_double(tbl,colx));
00273 check(pe=cpl_table_get_data_double(tbl,coly));
00274 check(nrow=cpl_table_get_nrow(tbl));
00275
00276 y = xsh_data_interpolate(wav, nrow, pw, pe);
00277 cleanup:
00278 return y;
00279
00280 }
00281
00292 static cpl_error_code
00293 xsh_table_monitor_flux_qc(cpl_table* table,
00294 const double ws,
00295 const double we,
00296 const char* prefix,
00297 const int index,
00298 cpl_propertylist** header)
00299 {
00300
00301 double flux=0;
00302 cpl_table* ext=NULL;
00303 char comment[40];
00304 char qc_key[20];
00305 int next=0;
00306
00307 check(cpl_table_and_selected_double(table,"W",CPL_GREATER_THAN,ws));
00308 check(next=cpl_table_and_selected_double(table,"W",CPL_LESS_THAN,we));
00309 if(next>0) {
00310 check(ext=cpl_table_extract_selected(table));
00311 check(cpl_table_select_all(table));
00312 if(cpl_table_has_valid(ext,"F")) {
00313 check(flux=cpl_table_get_column_mean(ext,"F"));
00314 } else {
00315 flux=-999;
00316 }
00317 sprintf(qc_key,"ESO QC %s%d",prefix,index);
00318 sprintf(comment,"Mean value of %s in %4.0f-%4.0f nm",prefix,ws,we);
00319 check(cpl_propertylist_append_double(*header,qc_key,flux));
00320 check(cpl_propertylist_set_comment(*header,qc_key,comment));
00321 }
00322
00323
00324 cleanup:
00325 xsh_free_table(&ext);
00326
00327 return cpl_error_get_code();
00328
00329 }
00330
00331
00332
00342 static cpl_propertylist*
00343 xsh_frame_table_monitor_flux_qc_ext(cpl_table* table,
00344 const char* colw,
00345 const char* colf,
00346 const char* prefix,
00347 xsh_instrument* instrument)
00348 {
00349 cpl_propertylist* header=NULL;
00350
00351 XSH_ASSURE_NOT_NULL_MSG(table,"Null input table");
00352 header=cpl_propertylist_new();
00353 check(cpl_table_cast_column(table,colw,"W",CPL_TYPE_DOUBLE));
00354 check(cpl_table_cast_column(table,colf,"F",CPL_TYPE_DOUBLE));
00355
00356 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_UVB){
00357
00358 check( xsh_table_monitor_flux_qc(table,450,470,prefix,1,&header));
00359 check( xsh_table_monitor_flux_qc(table,510,530,prefix,2,&header));
00360
00361 }
00362 else if ( xsh_instrument_get_arm(instrument) == XSH_ARM_VIS){
00363
00364 check( xsh_table_monitor_flux_qc(table,672,680,prefix,1,&header));
00365 check( xsh_table_monitor_flux_qc(table,745,756,prefix,2,&header));
00366 check( xsh_table_monitor_flux_qc(table,992,999,prefix,3,&header));
00367
00368 }
00369 else if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR){
00370
00371 check( xsh_table_monitor_flux_qc(table,1514,1548,prefix,1,&header));
00372 check( xsh_table_monitor_flux_qc(table,2214,2243,prefix,2,&header));
00373
00374 }
00375
00376 cpl_table_erase_column(table,"W");
00377 cpl_table_erase_column(table,"F");
00378
00379 cleanup:
00380 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00381 return NULL;
00382 } else {
00383 return header;
00384 }
00385 }
00386
00387
00388
00398
00399 cpl_error_code
00400 xsh_frame_table_monitor_flux_qc(cpl_frame* frm,
00401 const char* colw,
00402 const char* colf,
00403 const char* prefix,
00404 xsh_instrument* instrument)
00405 {
00406
00407 const char* name=NULL;
00408 cpl_table* table=NULL;
00409 cpl_propertylist* header=NULL;
00410 cpl_propertylist* qc_header=NULL;
00411
00412 int next=0;
00413 int ext=0;
00414
00415 XSH_ASSURE_NOT_NULL_MSG(frm,"Null input spectrum frame");
00416 next=cpl_frame_get_nextensions(frm);
00417 name=cpl_frame_get_filename(frm);
00418
00419
00420
00421 if (next) {
00422 for(ext=1;ext<=next;ext++) {
00423
00424 table=cpl_table_load(name,1,ext);
00425 check(header=cpl_propertylist_load(name,0));
00426 check(qc_header=xsh_frame_table_monitor_flux_qc_ext(table,colw,colf,
00427 prefix,instrument));
00428 cpl_propertylist_append(header,qc_header);
00429 if(next==1) {
00430 check(cpl_table_save(table,header,NULL,name,CPL_IO_DEFAULT));
00431 } else {
00432 check(cpl_table_save(table,header,NULL,name,CPL_IO_EXTEND));
00433 }
00434 xsh_free_table(&table);
00435 xsh_free_propertylist(&qc_header);
00436 xsh_free_propertylist(&header);
00437 }
00438 } else {
00439 table=cpl_table_load(name,1,0);
00440 check(header=cpl_propertylist_load(name,0));
00441 check(qc_header=xsh_frame_table_monitor_flux_qc_ext(table,colw,colf,
00442 prefix,instrument));
00443 cpl_propertylist_append(header,qc_header);
00444 check(cpl_table_save(table,header,NULL,name,CPL_IO_DEFAULT));
00445 xsh_free_propertylist(&qc_header);
00446 xsh_free_propertylist(&header);
00447 }
00448
00449 cleanup:
00450 xsh_free_table(&table);
00451 xsh_free_propertylist(&qc_header);
00452 xsh_free_propertylist(&header);
00453
00454 return cpl_error_get_code();
00455
00456 }