00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef VISIR_UTILS_H
00022 #define VISIR_UTILS_H
00023
00024
00025
00026
00027
00028
00029 #include "irplib_utils.h"
00030
00031 #include "irplib_plugin.h"
00032
00033 #include "visir_spectro.h"
00034 #include "visir_cpl_compat.h"
00035 #include <cpl.h>
00036 #include <string.h>
00037 #include <sys/types.h>
00038
00039
00040
00041
00042
00043 #ifndef VISIR_STAR_MAX_RADIUS
00044
00045 # define VISIR_STAR_MAX_RADIUS (2.0/60.0)
00046 #endif
00047
00048 #define VISIR_STR_PAR_EMPTY "NONE"
00049
00050
00051 #define visir_vector_plot(ARG1, ARG2, ARG3, ARG4) \
00052 do { \
00053 cpl_errorstate prestate = cpl_errorstate_get(); \
00054 if (cpl_plot_vector(ARG1, ARG2, ARG3, ARG4)) \
00055 irplib_error_recover(prestate, "Could not plot vector"); \
00056 } while (0)
00057
00058 #define visir_bivector_plot(ARG1, ARG2, ARG3, ARG4) \
00059 do { \
00060 cpl_errorstate prestate = cpl_errorstate_get(); \
00061 if (cpl_plot_bivector(ARG1, ARG2, ARG3, ARG4)) \
00062 irplib_error_recover(prestate, "Could not plot bivector"); \
00063 } while (0)
00064
00065 #define visir_table_plot(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) \
00066 do { \
00067 cpl_errorstate prestate = cpl_errorstate_get(); \
00068 if (cpl_plot_column(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6)) \
00069 irplib_error_recover(prestate, "Could not plot table"); \
00070 } while (0)
00071
00072 #define visir_image_plot(ARG1, ARG2, ARG3, ARG4) \
00073 do { \
00074 cpl_errorstate prestate = cpl_errorstate_get(); \
00075 if (cpl_plot_image(ARG1, ARG2, ARG3, ARG4)) \
00076 irplib_error_recover(prestate, "Could not plot image"); \
00077 } while (0)
00078
00079 #define visir_image_col_plot(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) \
00080 do { \
00081 cpl_errorstate prestate = cpl_errorstate_get(); \
00082 if (cpl_plot_image_col(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7)) \
00083 irplib_error_recover(prestate, "Could not plot image columns"); \
00084 } while (0)
00085
00086 #define visir_image_row_plot(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) \
00087 do { \
00088 cpl_errorstate prestate = cpl_errorstate_get(); \
00089 if (cpl_plot_image_row(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7)) \
00090 irplib_error_recover(prestate, "Could not plot image raws"); \
00091 } while (0)
00092
00093
00094 #define visir_error_set(ERRCODE) cpl_error_set(cpl_func, ERRCODE)
00095
00096
00097 #define visir_error_reset(...) \
00098 irplib_error_recover(cleanstate, __VA_ARGS__)
00099
00100 #define FOR_EACH(name, l) \
00101 for(cx_list_iterator name = cx_list_begin(l); \
00102 name != cx_list_end(l); name = cx_list_next(l, name))
00103
00104
00105
00106
00107 #define FOR_EACH_T(var, list) \
00108 for (cx_list_iterator __it = cx_list_begin(list), __cond = NULL, \
00109 __end = cx_list_end(list); \
00110 __it != __end; __it = cx_list_next(list, __it)) \
00111 if ((__cond = NULL) != NULL) {} else \
00112 for (var = cx_list_get(list, __it); __cond == NULL; \
00113 __cond = (cx_list_iterator)1)
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 #define FOR_EACH_FRAMESET(name, fs) \
00128 for (int __ind = 0, __cond = 0; __ind < cpl_frameset_get_size(fs); \
00129 __ind++) \
00130 if ((__cond = 0) != 0) {} else \
00131 for (cpl_frame * name = cpl_frameset_get_position(fs, __ind); \
00132 __cond == 0; __cond = 1)
00133
00134 #define FOR_EACH_FRAMESET_C(name, fs) \
00135 for (int __ind = 0, __cond = 0; __ind < cpl_frameset_get_size(fs); \
00136 __ind++) \
00137 if ((__cond = 0) != 0) {} else \
00138 for (const cpl_frame * name = cpl_frameset_get_position_const(fs, __ind); \
00139 __cond == 0; __cond = 1)
00140
00141 #define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0]))
00142
00143
00144 #ifdef _OPENMP
00145 #if _OPENMP >= 200805
00146 #define OMP_PRAGMA(x) _Pragma (#x)
00147 #define OMP3_PRAGMA(x) _Pragma (#x)
00148 #define VISIR_OMP3
00149 #define VISIR_OMP
00150 #else
00151 #define OMP_PRAGMA(x) _Pragma (#x)
00152 #define OMP3_PRAGMA(x)
00153 #define VISIR_OMP
00154 #endif
00155 #else
00156 #define OMP_PRAGMA(x)
00157 #define OMP3_PRAGMA(x)
00158 #endif
00159
00160 #ifndef VISIR_ATTR_UNUSED
00161 #if defined(__GNUC__) || defined(__ICC) || defined(__clang__)
00162 #define VISIR_ATTR_UNUSED __attribute__ ((__unused__))
00163 #else
00164 #define VISIR_ATTR_UNUSED
00165 #endif
00166 #endif
00167
00168
00169 static inline cpl_boolean visir_str_par_is_empty(const char * str)
00170 {
00171 return (str == NULL || strlen(str) == 0 ||
00172 strcmp(str, VISIR_STR_PAR_EMPTY) == 0);
00173 }
00174
00175 static inline int visir_round_to_int(const double x)
00176 {
00177 return x >= 0 ? (int)(x + 0.5) : (int)(x - 0.5);
00178 }
00179
00180
00181
00182
00183
00184 size_t visir_get_num_threads(cpl_boolean force);
00185 int visir_cmp_frm_fn(const cpl_frame * a, const cpl_frame * b);
00186 cpl_error_code visir_image_multiply_fast(cpl_image *, const cpl_image *);
00187 double visir_image_get_mean_fast(const cpl_image * im);
00188 cpl_error_code visir_move_products(cpl_frameset *frames,
00189 const char * dest_, const char * src);
00190 double visir_utils_get_exptime(const int, const cpl_propertylist *);
00191 double * visir_utils_get_wls(const irplib_framelist *);
00192 cpl_image * visir_create_disk_intimage(int, int, int, int, int);
00193 cpl_image * visir_create_ring_intimage(int, int, int, int, int, int);
00194 double visir_image_sigma_clip(const cpl_image *, double *);
00195 double visir_img_phot_sigma_clip(const cpl_image *);
00196 cpl_error_code visir_get_subpixel_maxpos(const cpl_image *,
00197 cpl_size, cpl_size,
00198 double *, double *);
00199
00200 typedef struct _visir_fftx_cache visir_fftx_cache;
00201 visir_fftx_cache * visir_new_fftx_cache(void);
00202 void visir_delete_fftx_cache(visir_fftx_cache *);
00203 cpl_error_code visir_fftxcorrelate(const cpl_image *, const cpl_image *,
00204 cpl_boolean, double *, double *,
00205 double *, visir_fftx_cache *);
00206 cpl_error_code
00207 visir_interpolate_rejected(cpl_image * img, size_t ** ppoints, size_t * n);
00208
00209 cpl_image *
00210 visir_parallel_median_collapse(const cpl_imagelist * l);
00211
00212 typedef struct {
00213
00214 cpl_propertylist * mplist;
00215
00216 void ** auxdata;
00217
00218
00219 cpl_imagelist * imgs;
00220
00221 cpl_propertylist ** plists;
00222
00223 cpl_size _size;
00224 cpl_size _capacity;
00225 } visir_imglist;
00226
00227 typedef void(*visir_free)(void *);
00228
00229 visir_imglist *
00230 visir_imglist_new(const cpl_size capacity, cpl_propertylist * mplist);
00231
00232 void
00233 visir_imglist_delete(visir_imglist * l, visir_free aux_destructor);
00234
00235 void
00236 visir_imglist_unwrap(visir_imglist * l, visir_free aux_destructor);
00237
00238 void
00239 visir_imglist_append(visir_imglist * l, cpl_image * img,
00240 void * auxdata);
00241
00242 void
00243 visir_imglist_append_imglist(visir_imglist * l, cpl_imagelist * imgs,
00244 void ** auxdata);
00245
00246 cpl_size
00247 visir_imglist_get_size(const visir_imglist * imgs);
00248
00249 cpl_image *
00250 visir_imglist_get_img(const visir_imglist * imgs, const cpl_size index);
00251
00252 void *
00253 visir_imglist_get_data(const visir_imglist * imgs, const cpl_size index);
00254
00255 cpl_error_code
00256 visir_imglist_get(const visir_imglist * imgs, const cpl_size index,
00257 cpl_image ** img, void ** auxdata);
00258
00259 cpl_error_code
00260 visir_imglist_set_data(visir_imglist * imgs,
00261 const cpl_size index, void * auxdata);
00262
00263 void
00264 visir_imglist_set_mplist(visir_imglist * imgs, cpl_propertylist * mplist);
00265
00266 cpl_propertylist *
00267 visir_imglist_get_mplist(const visir_imglist * imgs);
00268
00269 cpl_imagelist *
00270 visir_imglist_get_imglist(const visir_imglist * imgs);
00271
00272
00273 int visir_star_find(const cpl_vector *, const cpl_vector *, double, double,
00274 double, double *);
00275
00276 cpl_error_code visir_star_convert(const char *, int, int, double, char, int,
00277 int, double, const double *, int, double *,
00278 double *);
00279
00280 double visir_star_dist_min(const double *, const double *, int, int *, int *);
00281
00282 cpl_table * visir_table_new_xypos(const cpl_imagelist * images, const char *);
00283
00284 int visir_vector_minpos(const cpl_vector *);
00285
00286
00287 char * visir_frameset_serialize(const cpl_frameset * frames, size_t * size);
00288 cpl_frameset * visir_frameset_deserialize(char * block, size_t * size);
00289 cpl_error_code visir_send_frameset(FILE * , const cpl_frameset * );
00290
00291 void visir_drop_cache(const char * filename, off_t offset, off_t length);
00292 void visir_readahead(const char * filename, off_t offset, off_t length);
00293
00294 size_t visir_get_nbytes(const cpl_image * img);
00295 size_t visir_get_nbytes_plist(const cpl_propertylist * plist);
00296
00297
00298 cpl_boolean visir_get_tempdir(char * tmpdir_);
00299 char * visir_get_cwd(void);
00300
00301 cpl_parameter * visir_parameter_duplicate(const cpl_parameter * p);
00302
00303 cpl_error_code visir_copy_parameters(cpl_parameterlist *,
00304 const cpl_parameterlist *);
00305
00306 cpl_recipe *
00307 visir_init_recipe(const char * name, int (*get_info)(cpl_pluginlist *),
00308 cpl_pluginlist * plugins);
00309
00310 cpl_error_code
00311 visir_run_recipe(cpl_recipe * recipe,
00312 cpl_frameset * framelist, const cpl_parameterlist * parlist,
00313 cpl_error_code (*set_parlist)(cpl_parameterlist *,
00314 const cpl_parameterlist *));
00315
00316 cpl_error_code
00317 visir_tmpdir_exec(const char * recipename, cpl_plugin * plugin,
00318 int (*function)(cpl_frameset *, const cpl_parameterlist *));
00319 cpl_error_code
00320 visir_forking_exec(const char * recipename, cpl_plugin * plugin,
00321 int (*function)(cpl_frameset *, const cpl_parameterlist *));
00322
00323 cpl_frameset *
00324 visir_prepare_frameset(const cpl_frameset * frameset,
00325 const char ** tagmap, size_t ntags,
00326 cpl_boolean reverse);
00327 cpl_frameset * visir_remove_modified_calib(cpl_frameset * set);
00328
00329 cpl_error_code visir_bivector_load(cpl_bivector *, FILE *);
00330
00331 const char ** visir_framelist_set_tag(irplib_framelist *,
00332 char * (*)(const cpl_frame *,
00333 const cpl_propertylist *, int),
00334 int *);
00335
00336 cpl_error_code visir_qc_append_background(cpl_propertylist *,
00337 const irplib_framelist *, int, int);
00338
00339 cpl_error_code visir_qc_append_capa(cpl_propertylist *,
00340 const irplib_framelist *);
00341
00342 cpl_error_code visir_qc_append_filter(cpl_propertylist *,
00343 const irplib_framelist *);
00344 cpl_error_code visir_qc_append_exptime(cpl_propertylist *,
00345 const irplib_framelist *);
00346
00347 #ifdef _OPENMP
00348 struct visir_queue;
00349 typedef struct visir_queue visir_queue;
00350 visir_queue * visir_queue_init(const int max_size);
00351 void visir_queue_set_error(visir_queue * q, cpl_error_code error);
00352 void visir_queue_delete(visir_queue * q);
00353 cpl_error_code visir_queue_put(visir_queue * q, void * data);
00354 void * visir_queue_pop(visir_queue * q);
00355 #endif
00356
00357 size_t visir_upper_bound(const cpl_vector * vec, double val);
00358 size_t visir_lower_bound(const cpl_vector * vec, double val);
00359 cpl_image * visir_linintp_values(const cpl_image * inp, const cpl_bivector * ref);
00360 cpl_error_code
00361 fit_2d_gauss(const cpl_image * img_, const cpl_image * weights, cpl_size x, cpl_size y,
00362 double est_fwhmx, double est_fwhmy,
00363 double * peak, double * peak_err,
00364 double * major, double * major_err,
00365 double * minor, double * minor_err,
00366 double * angle, double * angle_err);
00367 cpl_error_code
00368 fit_1d_gauss(const cpl_vector * xv, const cpl_vector * y, cpl_vector * dyv,
00369 double * x0, double * x0_err,
00370 double * peak, double * peak_err,
00371 double * sigma, double * sigma_err);
00372
00373 #endif