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_min_int(int x, int y);
00177 int xsh_max_int(int x, int y);
00178
00179 void xsh_free(const void *mem);
00180
00181 void xsh_free_temporary_files(void);
00182 cpl_error_code xsh_end(const char *recipe_id, cpl_frameset *frames,
00183 cpl_parameterlist * list );
00184 cpl_error_code xsh_begin(cpl_frameset *frames,
00185 const cpl_parameterlist *parameters,
00186 xsh_instrument ** instr,
00187 cpl_frameset ** raws, cpl_frameset ** calib,
00188 const char * tag_list[],
00189 int tag_list_size,
00190 const char *recipe_id,
00191 unsigned int binary_version, const char *short_descr);
00192 void xsh_add_temporary_file( const char *name ) ;
00193
00194 cpl_error_code xsh_get_property_value(const cpl_propertylist *plist,
00195 const char *keyword, cpl_type keywordtype, void *result);
00196
00197 char *xsh_sdate_utc( time_t * t ) ;
00198
00199 char *xsh_stringdup (const char *s1);
00200 char *xsh_stringcat (const char *s1, const char *s2);
00201 char *xsh_stringcat_3(const char *s1, const char *s2, const char *s3);
00202 char *xsh_stringcat_4(const char *s1, const char *s2, const char *s3,
00203 const char *s4);
00204 char *xsh_stringcat_5(const char *s1, const char *s2, const char *s3,
00205 const char *s4, const char *s5);
00206 char *xsh_stringcat_6(const char *s1, const char *s2, const char *s3,
00207 const char *s4, const char *s5, const char *s6);
00208 char *xsh_stringcat_any( const char *s, ...) ;
00209 void xsh_reindex(double* data, int* idx, int size);
00210 void xsh_reindex_float( float * data, int* idx, int size);
00211 void xsh_reindex_int( int * data, int* idx, int size);
00212 int* xsh_sort(void* base, size_t nmemb, size_t size,
00213 int (*compar)(const void *, const void *));
00214 void xsh_tools_min_max(int size, double *tab, double* min, double* max);
00215 void xsh_tools_get_statistics(double* tab, int size, double* median,
00216 double* mean, double* stdev);
00217
00218 void xsh_free_table(cpl_table **t);
00219 void xsh_free_image(cpl_image **i);
00220 void xsh_free_mask(cpl_mask **m);
00221 void xsh_free_imagelist(cpl_imagelist **i);
00222 void xsh_free_propertylist(cpl_propertylist **p);
00223 void xsh_free_polynomial(cpl_polynomial **p);
00224 void xsh_free_matrix(cpl_matrix **m);
00225 void xsh_free_array(cpl_array **v);
00226 void xsh_free_vector(cpl_vector **v);
00227 void xsh_free_stats(cpl_stats **s);
00228 void xsh_unwrap_image( cpl_image **i);
00229 void xsh_unwrap_vector(cpl_vector **v);
00230 void xsh_unwrap_array(cpl_array **a);
00231 void xsh_unwrap_bivector_vectors(cpl_bivector **b);
00232 void xsh_free_parameterlist(cpl_parameterlist **p);
00233 void xsh_free_parameter(cpl_parameter **p);
00234 void xsh_free_frameset(cpl_frameset **f);
00235 void xsh_free_frame(cpl_frame **f);
00236
00237 void xsh_show_time( const char * comment ) ;
00238
00239 cpl_error_code xsh_tools_sort_double( double * pix_arr, int size ) ;
00240 cpl_error_code xsh_tools_sort_float( float * pix_arr, int size ) ;
00241 cpl_error_code xsh_tools_sort_int( int * pix_arr, int size ) ;
00242 void xsh_tools_tchebitchev_transform_tab(int size, double* pos, double min,
00243 double max, double* tcheb_pos);
00244 double xsh_tools_tchebitchev_transform(double pos, double min,
00245 double max);
00246 double xsh_tools_tchebitchev_reverse_transform(double pos, double min,
00247 double max);
00248
00249 cpl_vector* xsh_tools_tchebitchev_poly_eval( int n, double X);
00250 double xsh_tools_get_median_double( double *array, int size ) ;
00251 int xsh_tools_running_median_1d_get_max( double * tab, int size, int wsize ) ;
00252
00253 void xsh_image_fit_spline(cpl_image* img, xsh_grid* grid);
00254
00255 void xsh_vector_fit_gaussian( cpl_vector * x, cpl_vector * y,
00256 XSH_GAUSSIAN_FIT * result ) ;
00257 double xsh_vector_get_err_median( cpl_vector *vect);
00258 double xsh_vector_get_err_mean( cpl_vector *vect);
00259
00260 int xsh_debug_level_set( int level ) ;
00261 int xsh_debug_level_get( void ) ;
00262 const char * xsh_debug_level_tostring( void ) ;
00263
00264 int xsh_time_stamp_set( int ts ) ;
00265 int xsh_time_stamp_get( void ) ;
00266
00267 void xsh_mem_dump( const char * prompt) ;
00268 cpl_image * xsh_imagelist_collapse_sigclip_iter_create(
00269 const cpl_imagelist * imlist,
00270 double sigma_low,
00271 double sigma_upp,
00272 const int niter);
00273
00274 double convert_bin_to_data( double bin_data, int binning);
00275 double convert_data_to_bin( double data, int binning);
00276
00277 cpl_frameset * xsh_order_frameset_by_date( cpl_frameset * frameset ) ;
00278
00279 cpl_error_code xsh_set_cd_matrix(cpl_propertylist* plist);
00280 cpl_error_code xsh_set_cd_matrix1d(cpl_propertylist* plist);
00281 cpl_error_code xsh_set_cd_matrix2d(cpl_propertylist* plist);
00282 cpl_error_code xsh_set_cd_matrix3d(cpl_propertylist* plist);
00283 int xsh_erase_table_rows(cpl_table *t, const char *column,
00284 cpl_table_select_operator operator,
00285 double value);
00286 int xsh_select_table_rows(cpl_table *t, const char *column,
00287 cpl_table_select_operator operator,
00288 double value);
00289
00290
00291 polynomial *
00292 xsh_polynomial_regression_2d(cpl_table *t,
00293 const char *X1, const char *X2, const char *Y,
00294 const char *sigmaY,
00295 int degree1, int degree2,
00296 const char *polynomial_fit, const char *residual_square,
00297 const char *variance_fit,
00298 double *mse, double *red_chisq,
00299 polynomial **variance, double kappa,
00300 double min_reject);
00301 cpl_error_code
00302 xsh_check_input_is_unbinned(cpl_frame* in);
00303
00304 cpl_error_code
00305 xsh_update_pheader_in_image_multi(cpl_frame *frame,
00306 const cpl_propertylist* pheader);
00307
00308
00309 cpl_error_code
00310 xsh_monitor_flux(cpl_frame* frm_ima,const cpl_frame* frm_tab,
00311 xsh_instrument* instrument);
00312 cpl_error_code
00313 xsh_frameset_dump_nod_info(cpl_frameset* set);
00314 void
00315 xsh_frame_image_save(cpl_frame* frm,const char* name_o);
00316
00317 void
00318 xsh_frame_table_save(cpl_frame* frm,const char* name_o);
00319
00320 char*
00321 xsh_set_recipe_file_prefix(cpl_frameset* raw,const char* recipe);
00322 const char*
00323 xsh_set_recipe_sky_file_prefix(char* rec_prefix);
00324 cpl_frame*
00325 xsh_frameset_average(cpl_frameset *set, const char* tag);
00326 cpl_frame* xsh_frameset_add( cpl_frameset *set, xsh_instrument *instr,const int decode_bp);
00327
00328 int xsh_fileutils_move (const char *srcpath, const char *dstpath);
00329 int xsh_fileutils_copy (const char * srcpath, const char * dstpath);
00330 void xsh_add_product_file( const char *name);
00331 void xsh_free_product_files( void);
00332 const char* xsh_string_tolower(char* s);
00333 const char* xsh_string_toupper(char* s);
00334 double
00335 xsh_spline_hermite_table( double xp, const cpl_table *t, const char *column_x,
00336 const char *column_y, int *istart );
00337 double
00338 xsh_spline_hermite( double xp, const double *x, const double *y, int n, int *istart );
00339 cpl_frame*
00340 xsh_util_multiply_by_response(cpl_frame* merged_sci, cpl_frame* response,
00341 const char* tag);
00342
00343 cpl_frame*
00344 xsh_util_multiply_by_response_ord(cpl_frame* merged_sci, cpl_frame* response,
00345 const char* tag);
00346
00347 cpl_frame*
00348 xsh_util_frameset_collapse_mean(cpl_frameset* set,
00349 xsh_instrument* instrument);
00350
00351 cpl_frame*
00352 xsh_spectrum_resample(cpl_frame* frame_inp,
00353 const double wstep,
00354 const double wmin,
00355 const double wmax,
00356 xsh_instrument* instr);
00357
00358 cpl_frame*
00359 xsh_spectrum_resample2(cpl_frame* frame_inp,
00360 const double wstep,
00361 const double wmin,
00362 const double wmax,
00363 xsh_instrument* instr);
00364
00365
00366 cpl_frame*
00367 xsh_spectrum_interpolate(cpl_frame* table_frame,
00368 const double wstep,
00369 const double wmin,
00370 const double wmax);
00371
00372 cpl_frame*
00373 xsh_spectrum_interpolate_linear(cpl_frame* table_frame,
00374 const double wstep,
00375 const double wmin,
00376 const double wmax);
00377
00378 cpl_image*
00379 xsh_vector_to_image(const cpl_vector* vector,cpl_type type);
00380 cpl_vector *
00381 xsh_image_to_vector( cpl_image * spectrum );
00382
00383 cpl_image *
00384 xsh_normalize_spectrum_image(const cpl_image *spectrum,
00385 const cpl_image *spectrum_error,
00386 const cpl_propertylist *spectrum_header,
00387 const int binx,
00388 const double gain,
00389 const double exptime,
00390 const double airmass,
00391 const int n_traces,
00392 const cpl_table *atm_extinction,
00393 cpl_image **scaled_error);
00394
00395 cpl_frame *
00396 xsh_normalize_spectrum(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 cpl_frame *
00403 xsh_normalize_spectrum_ord(const cpl_frame *obj_frame,
00404 const cpl_frame *atm_ext_frame,
00405 cpl_boolean correct_binning,
00406 xsh_instrument* instrument,
00407 const char* tag);
00408
00409 void xsh_array_clip_mean( cpl_array *array, double kappa, int niter,
00410 double frac_min, double *mean, double *stdev);
00411
00412 void xsh_array_clip_median( cpl_array *array, double kappa, int niter,
00413 double frac_min, double *median, double *stdev);
00414
00415 void xsh_array_clip_poly1d( cpl_vector *pos_array, cpl_vector *val_array,
00416 double kappa, int niter, double frac_min, int deg, cpl_polynomial **poly,
00417 double *chisq, int **flags);
00418
00419 cpl_error_code
00420 xsh_rectify_params_set_defaults(cpl_parameterlist* pars,
00421 const char* rec_id,
00422 xsh_instrument* inst,
00423 xsh_rectify_param * rectify_par);
00424
00425 void xsh_gsl_init_gaussian_fit( cpl_vector *xpos_vect, cpl_vector *ypos_vect,
00426 double *init_par);
00427 void xsh_gsl_fit_gaussian( cpl_vector *xpos_vect, cpl_vector *ypos_vect, int deg,
00428 double *params, double *errs, int *status);
00429
00430 double xsh_hms2deg(const double hms);
00431 double xsh_sess2deg(const double sess);
00432
00433 double*
00434 xsh_function1d_xcorrelate(
00435 double * line_i,
00436 int width_i,
00437 double * line_t,
00438 int width_t,
00439 int half_search,
00440 int normalise,
00441 double * xcorr_max,
00442 double * delta
00443 );
00444
00445 #endif