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 #ifndef XSH_UTILS_H
00027 #define XSH_UTILS_H
00028
00029
00030
00031
00032
00033 #include <cpl.h>
00034 #include <sys/time.h>
00035 #include <sys/resource.h>
00036 #include <xsh_data_grid.h>
00037 #include <xsh_utils_polynomial.h>
00038 #include <xsh_data_instrument.h>
00039 #include <xsh_parameters.h>
00040
00041 #ifndef M_PI
00042 #define M_PI 3.1415926535897932384626433832795
00043 #endif
00044
00045 #define XSH_MAX(A,B)\
00046 A > B ? A : B
00047
00048 #define XSH_MALLOC( POINTER, TYPE, SIZE) \
00049 assure(POINTER == NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00050 "Try to allocate non NULL pointer");\
00051 POINTER = (TYPE*)(cpl_malloc(SIZE*sizeof(TYPE)));\
00052 assure (POINTER != NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00053 "Memory allocation failed!")
00054
00055 #define XSH_CALLOC( POINTER, TYPE, SIZE) \
00056 assure(POINTER == NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00057 "Try to allocate non NULL pointer");\
00058 POINTER = (TYPE*)(cpl_calloc(SIZE,sizeof(TYPE)));\
00059 assure (POINTER != NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00060 "Memory allocation failed!")
00061
00062 #define XSH_REALLOC( POINTER, TYPE, SIZE ) \
00063 assure(POINTER != NULL, CPL_ERROR_ILLEGAL_INPUT,\
00064 "Try to re-allocate NULL pointer") ;\
00065 POINTER = (TYPE *)cpl_realloc(POINTER,SIZE*sizeof(TYPE)));\
00066 assure( POINTER != NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00067 "Memory re-allocation failed!")
00068
00069 #define XSH_NEW_PROPERTYLIST( POINTER) \
00070 assure(POINTER == NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00071 "Try to allocate non NULL pointer");\
00072 POINTER = cpl_propertylist_new();\
00073 assure (POINTER != NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00074 "Memory allocation for propertylist failed!")
00075
00076 #define XSH_NEW_FRAME( POINTER) \
00077 assure(POINTER == NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00078 "Try to allocate non NULL pointer");\
00079 POINTER = cpl_frame_new();\
00080 assure (POINTER != NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00081 "Memory allocation for frame failed!")
00082
00083 #define XSH_NEW_FRAMESET( POINTER) \
00084 assure(POINTER == NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00085 "Try to allocate non NULL pointer");\
00086 POINTER = cpl_frameset_new();\
00087 assure (POINTER != NULL, CPL_ERROR_ILLEGAL_OUTPUT,\
00088 "Memory allocation for frameset failed!")
00089
00090
00091 #define XSH_FREE(POINTER)\
00092 if(POINTER!=NULL) cpl_free(POINTER);\
00093 POINTER = NULL
00094
00095 #define XSH_PREFIX(prefix,name,instr) \
00096 XSH_FREE(prefix);\
00097 prefix = xsh_stringcat_any(name,"_",\
00098 xsh_instrument_arm_tostring(instr ),\
00099 "" ) ;\
00100 XSH_ASSURE_NOT_NULL(prefix)
00101
00102 #define XSH_MODE_PREFIX(prefix,name,instr) \
00103 XSH_FREE(prefix);\
00104 prefix = xsh_stringcat_any(name,"_",\
00105 xsh_instrument_mode_tostring(instr ),\
00106 "_",\
00107 xsh_instrument_arm_tostring(instr ),\
00108 "" ) ;\
00109 XSH_ASSURE_NOT_NULL(prefix)
00110
00111
00112 #define XSH_TABLE_NEW_COL(TABLE, NAME, UNIT, TYPE) \
00113 check( cpl_table_new_column(TABLE, NAME, TYPE));\
00114 check( cpl_table_set_column_unit( TABLE, NAME, UNIT))
00115
00116 #define BOOLEAN_TO_STRING( boolean) \
00117 boolean == 0 ? "false" : "true"
00118 typedef struct{
00119 void* data;
00120 int idx;
00121 }xsh_sort_data;
00122
00123
00124
00125
00126
00127 typedef struct {
00128 double peakpos,
00129 sigma,
00130 area,
00131 offset,
00132 mse ;
00133 } XSH_GAUSSIAN_FIT ;
00134
00135 enum {
00136 XSH_DEBUG_LEVEL_NONE, XSH_DEBUG_LEVEL_LOW,
00137 XSH_DEBUG_LEVEL_MEDIUM, XSH_DEBUG_LEVEL_HIGH
00138 } ;
00139
00140
00141
00142
00143
00144 long xsh_round_double(double x);
00145 double xsh_max_double(double x, double y);
00146 double xsh_pow_int(double x, int y);
00147
00148
00149
00150
00151
00152
00153 cpl_frame* xsh_frame_inv( cpl_frame* in, const char *filename,
00154 xsh_instrument* instr);
00155
00156 cpl_frame* xsh_frame_abs( cpl_frame* in, xsh_instrument* instr,
00157 cpl_frame** sign);
00158
00159 cpl_frame* xsh_frame_mult( cpl_frame* in, xsh_instrument* instr,
00160 cpl_frame* sign);
00161
00162 cpl_parameterlist*
00163 xsh_parameterlist_duplicate(const cpl_parameterlist* pin);
00164
00165 void
00166 xsh_plist_dump(cpl_propertylist *plist);
00167
00168 cpl_error_code
00169 xsh_frameset_dump(cpl_frameset* set);
00170
00171 char * xsh_get_basename(const char *filename);
00172
00173 const char * xsh_get_license(void) ;
00174 void xsh_init(void);
00175
00176 int xsh_max_int(int x, int y);
00177
00178 void xsh_free(const void *mem);
00179
00180 void xsh_free_temporary_files(void);
00181 cpl_error_code xsh_end(const char *recipe_id, cpl_frameset *frames,
00182 cpl_parameterlist * list );
00183 cpl_error_code xsh_begin(cpl_frameset *frames,
00184 const cpl_parameterlist *parameters,
00185 xsh_instrument ** instr,
00186 cpl_frameset ** raws, cpl_frameset ** calib,
00187 const char * tag_list[],
00188 int tag_list_size,
00189 const char *recipe_id,
00190 unsigned int binary_version, const char *short_descr);
00191 void xsh_add_temporary_file( const char *name ) ;
00192
00193 cpl_error_code xsh_get_property_value(const cpl_propertylist *plist,
00194 const char *keyword, cpl_type keywordtype, void *result);
00195
00196 char *xsh_sdate_utc( time_t * t ) ;
00197
00198 char *xsh_stringdup (const char *s1);
00199 char *xsh_stringcat (const char *s1, const char *s2);
00200 char *xsh_stringcat_3(const char *s1, const char *s2, const char *s3);
00201 char *xsh_stringcat_4(const char *s1, const char *s2, const char *s3,
00202 const char *s4);
00203 char *xsh_stringcat_5(const char *s1, const char *s2, const char *s3,
00204 const char *s4, const char *s5);
00205 char *xsh_stringcat_6(const char *s1, const char *s2, const char *s3,
00206 const char *s4, const char *s5, const char *s6);
00207 char *xsh_stringcat_any( const char *s, ...) ;
00208 void xsh_reindex(double* data, int* idx, int size);
00209 void xsh_reindex_float( float * data, int* idx, int size);
00210 void xsh_reindex_int( int * data, int* idx, int size);
00211 int* xsh_sort(void* base, size_t nmemb, size_t size,
00212 int (*compar)(const void *, const void *));
00213 void xsh_tools_min_max(int size, double *tab, double* min, double* max);
00214 void xsh_tools_get_statistics(double* tab, int size, double* median,
00215 double* mean, double* stdev);
00216
00217 void xsh_free_table(cpl_table **t);
00218 void xsh_free_image(cpl_image **i);
00219 void xsh_free_mask(cpl_mask **m);
00220 void xsh_free_imagelist(cpl_imagelist **i);
00221 void xsh_free_propertylist(cpl_propertylist **p);
00222 void xsh_free_polynomial(cpl_polynomial **p);
00223 void xsh_free_matrix(cpl_matrix **m);
00224 void xsh_free_array(cpl_array **v);
00225 void xsh_free_vector(cpl_vector **v);
00226 void xsh_free_stats(cpl_stats **s);
00227 void xsh_unwrap_image( cpl_image **i);
00228 void xsh_unwrap_vector(cpl_vector **v);
00229 void xsh_unwrap_array(cpl_array **a);
00230 void xsh_unwrap_bivector_vectors(cpl_bivector **b);
00231 void xsh_free_parameterlist(cpl_parameterlist **p);
00232 void xsh_free_parameter(cpl_parameter **p);
00233 void xsh_free_frameset(cpl_frameset **f);
00234 void xsh_free_frame(cpl_frame **f);
00235
00236 void xsh_show_time( const char * comment ) ;
00237
00238 cpl_error_code xsh_tools_sort_double( double * pix_arr, int size ) ;
00239 cpl_error_code xsh_tools_sort_float( float * pix_arr, int size ) ;
00240 cpl_error_code xsh_tools_sort_int( int * pix_arr, int size ) ;
00241 void xsh_tools_tchebitchev_transform_tab(int size, double* pos, double min,
00242 double max, double* tcheb_pos);
00243 double xsh_tools_tchebitchev_transform(double pos, double min,
00244 double max);
00245 double xsh_tools_tchebitchev_reverse_transform(double pos, double min,
00246 double max);
00247
00248 cpl_vector* xsh_tools_tchebitchev_poly_eval( int n, double X);
00249 double xsh_tools_get_median_double( double *array, int size ) ;
00250 int xsh_tools_running_median_1d_get_max( double * tab, int size, int wsize ) ;
00251
00252 void xsh_image_fit_spline(cpl_image* img, xsh_grid* grid);
00253
00254 void xsh_vector_fit_gaussian( cpl_vector * x, cpl_vector * y,
00255 XSH_GAUSSIAN_FIT * result ) ;
00256 double xsh_vector_get_err_median( cpl_vector *vect);
00257 double xsh_vector_get_err_mean( cpl_vector *vect);
00258
00259 int xsh_debug_level_set( int level ) ;
00260 int xsh_debug_level_get( void ) ;
00261 const char * xsh_debug_level_tostring( void ) ;
00262
00263 int xsh_time_stamp_set( int ts ) ;
00264 int xsh_time_stamp_get( void ) ;
00265
00266 void xsh_mem_dump( const char * prompt) ;
00267 cpl_image * xsh_imagelist_collapse_sigclip_iter_create(
00268 const cpl_imagelist * imlist,
00269 double sigma_low,
00270 double sigma_upp,
00271 const int niter);
00272
00273 double convert_bin_to_data( double bin_data, int binning);
00274 double convert_data_to_bin( double data, int binning);
00275
00276 cpl_frameset * xsh_order_frameset_by_date( cpl_frameset * frameset ) ;
00277
00278 cpl_error_code xsh_set_cd_matrix(cpl_propertylist* plist);
00279 cpl_error_code xsh_set_cd_matrix1d(cpl_propertylist* plist);
00280 cpl_error_code xsh_set_cd_matrix2d(cpl_propertylist* plist);
00281 cpl_error_code xsh_set_cd_matrix3d(cpl_propertylist* plist);
00282 int xsh_erase_table_rows(cpl_table *t, const char *column,
00283 cpl_table_select_operator operator,
00284 double value);
00285 int xsh_select_table_rows(cpl_table *t, const char *column,
00286 cpl_table_select_operator operator,
00287 double value);
00288
00289
00290 polynomial *
00291 xsh_polynomial_regression_2d(cpl_table *t,
00292 const char *X1, const char *X2, const char *Y,
00293 const char *sigmaY,
00294 int degree1, int degree2,
00295 const char *polynomial_fit, const char *residual_square,
00296 const char *variance_fit,
00297 double *mse, double *red_chisq,
00298 polynomial **variance, double kappa,
00299 double min_reject);
00300 cpl_error_code
00301 xsh_check_input_is_unbinned(cpl_frame* in);
00302
00303 cpl_error_code
00304 xsh_update_pheader_in_image_multi(cpl_frame *frame,
00305 const cpl_propertylist* pheader);
00306
00307
00308 cpl_error_code
00309 xsh_monitor_flux(cpl_frame* frm_ima,const cpl_frame* frm_tab,
00310 xsh_instrument* instrument);
00311 cpl_error_code
00312 xsh_frameset_dump_nod_info(cpl_frameset* set);
00313 void
00314 xsh_frame_image_save(cpl_frame* frm,const char* name_o);
00315
00316 void
00317 xsh_frame_table_save(cpl_frame* frm,const char* name_o);
00318
00319 char*
00320 xsh_set_recipe_file_prefix(cpl_frameset* raw,const char* recipe);
00321 const char*
00322 xsh_set_recipe_sky_file_prefix(char* rec_prefix);
00323 cpl_frame*
00324 xsh_frameset_average(cpl_frameset *set, const char* tag);
00325 cpl_frame* xsh_frameset_add( cpl_frameset *set, xsh_instrument *instr,const int decode_bp);
00326
00327 int xsh_fileutils_move (const char *srcpath, const char *dstpath);
00328 int xsh_fileutils_copy (const char * srcpath, const char * dstpath);
00329 void xsh_add_product_file( const char *name);
00330 void xsh_free_product_files( void);
00331 const char* xsh_string_tolower(char* s);
00332 const char* xsh_string_toupper(char* s);
00333 double
00334 xsh_spline_hermite_table( double xp, const cpl_table *t, const char *column_x,
00335 const char *column_y, int *istart );
00336
00337 cpl_frame*
00338 xsh_util_multiply_by_response(cpl_frame* merged_sci, cpl_frame* response,
00339 const char* tag);
00340
00341 cpl_frame*
00342 xsh_util_multiply_by_response_ord(cpl_frame* merged_sci, cpl_frame* response,
00343 const char* tag);
00344
00345 cpl_frame*
00346 xsh_util_frameset_collapse_mean(cpl_frameset* set,
00347 xsh_instrument* instrument);
00348
00349 cpl_frame*
00350 xsh_spectrum_resample(cpl_frame* frame_inp,
00351 const double wstep,
00352 const double wmin,
00353 const double wmax,
00354 xsh_instrument* instr);
00355
00356 cpl_frame*
00357 xsh_spectrum_resample2(cpl_frame* frame_inp,
00358 const double wstep,
00359 const double wmin,
00360 const double wmax,
00361 xsh_instrument* instr);
00362
00363
00364 cpl_frame*
00365 xsh_spectrum_interpolate(cpl_frame* table_frame,
00366 const double wstep,
00367 const double wmin,
00368 const double wmax);
00369
00370 cpl_frame*
00371 xsh_spectrum_interpolate_linear(cpl_frame* table_frame,
00372 const double wstep,
00373 const double wmin,
00374 const double wmax);
00375
00376 cpl_image*
00377 xsh_vector_to_image(const cpl_vector* vector,cpl_type type);
00378 cpl_vector *
00379 xsh_image_to_vector( cpl_image * spectrum );
00380
00381 cpl_image *
00382 xsh_normalize_spectrum_image(const cpl_image *spectrum,
00383 const cpl_image *spectrum_error,
00384 const cpl_propertylist *spectrum_header,
00385 const int binx,
00386 const double gain,
00387 const double exptime,
00388 const double airmass,
00389 const int n_traces,
00390 const cpl_table *atm_extinction,
00391 cpl_image **scaled_error);
00392
00393 cpl_frame *
00394 xsh_normalize_spectrum(const cpl_frame *obj_frame,
00395 const cpl_frame *atm_ext_frame,
00396 cpl_boolean correct_binning,
00397 xsh_instrument* instrument,
00398 const char* tag);
00399
00400 cpl_frame *
00401 xsh_normalize_spectrum_ord(const cpl_frame *obj_frame,
00402 const cpl_frame *atm_ext_frame,
00403 cpl_boolean correct_binning,
00404 xsh_instrument* instrument,
00405 const char* tag);
00406
00407 void xsh_array_clip_mean( cpl_array *array, double kappa, int niter,
00408 double frac_min, double *mean, double *stdev);
00409
00410 void xsh_array_clip_median( cpl_array *array, double kappa, int niter,
00411 double frac_min, double *median, double *stdev);
00412
00413 void xsh_array_clip_poly1d( cpl_vector *pos_array, cpl_vector *val_array,
00414 double kappa, int niter, double frac_min, int deg, cpl_polynomial **poly,
00415 double *chisq, int **flags);
00416
00417 cpl_error_code
00418 xsh_rectify_params_set_defaults(cpl_parameterlist* pars,
00419 const char* rec_id,
00420 xsh_instrument* inst,
00421 xsh_rectify_param * rectify_par);
00422
00423 void xsh_gsl_init_gaussian_fit( cpl_vector *xpos_vect, cpl_vector *ypos_vect,
00424 double *init_par);
00425 void xsh_gsl_fit_gaussian( cpl_vector *xpos_vect, cpl_vector *ypos_vect, int deg,
00426 double *params, double *errs, int *status);
00427
00428 double xsh_hms2deg(const double hms);
00429 double xsh_sess2deg(const double sess);
00430
00431 #endif