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