IIINSTRUMENT Pipeline Reference Manual 4.5.1
visir_utils.h
1/* $Id: visir_utils.h,v 1.109 2013-05-13 16:07:06 jtaylor Exp $
2 *
3 * This file is part of the VISIR Pipeline
4 * Copyright (C) 2002,2003,2013,2014 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19 */
20
21#ifndef VISIR_UTILS_H
22#define VISIR_UTILS_H
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28
29#include "irplib_utils.h"
30
31#include "irplib_plugin.h"
32
33#include "visir_spectro.h"
34#include "visir_cpl_compat.h"
35#include <cpl.h>
36#include <string.h> /* for strlen */
37#include <sys/types.h> /* for off_t */
38
39/*-----------------------------------------------------------------------------
40 Defines
41 -----------------------------------------------------------------------------*/
42
43#ifndef VISIR_STAR_MAX_RADIUS
44 /* 2 arcminutes in degrees */
45# define VISIR_STAR_MAX_RADIUS (2.0/60.0)
46#endif
47
48#define VISIR_STR_PAR_EMPTY "NONE"
49
50/* Plotting errors will be ignored */
51#define visir_vector_plot(ARG1, ARG2, ARG3, ARG4) \
52 do { \
53 cpl_errorstate prestate = cpl_errorstate_get(); \
54 if (cpl_plot_vector(ARG1, ARG2, ARG3, ARG4)) \
55 irplib_error_recover(prestate, "Could not plot vector"); \
56 } while (0)
57
58#define visir_bivector_plot(ARG1, ARG2, ARG3, ARG4) \
59 do { \
60 cpl_errorstate prestate = cpl_errorstate_get(); \
61 if (cpl_plot_bivector(ARG1, ARG2, ARG3, ARG4)) \
62 irplib_error_recover(prestate, "Could not plot bivector"); \
63 } while (0)
64
65#define visir_table_plot(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) \
66 do { \
67 cpl_errorstate prestate = cpl_errorstate_get(); \
68 if (cpl_plot_column(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6)) \
69 irplib_error_recover(prestate, "Could not plot table"); \
70 } while (0)
71
72#define visir_image_plot(ARG1, ARG2, ARG3, ARG4) \
73 do { \
74 cpl_errorstate prestate = cpl_errorstate_get(); \
75 if (cpl_plot_image(ARG1, ARG2, ARG3, ARG4)) \
76 irplib_error_recover(prestate, "Could not plot image"); \
77 } while (0)
78
79#define visir_image_col_plot(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) \
80 do { \
81 cpl_errorstate prestate = cpl_errorstate_get(); \
82 if (cpl_plot_image_col(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7)) \
83 irplib_error_recover(prestate, "Could not plot image columns"); \
84 } while (0)
85
86#define visir_image_row_plot(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) \
87 do { \
88 cpl_errorstate prestate = cpl_errorstate_get(); \
89 if (cpl_plot_image_row(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7)) \
90 irplib_error_recover(prestate, "Could not plot image raws"); \
91 } while (0)
92
93
94#define visir_error_set(ERRCODE) cpl_error_set(cpl_func, ERRCODE)
95
96/* FIXME: Copy to visir_error_reset() */
97#define visir_error_reset(...) \
98 irplib_error_recover(cleanstate, __VA_ARGS__)
99
100#define FOR_EACH(name, l) \
101 for(cx_list_iterator name = cx_list_begin(l); \
102 name != cx_list_end(l); name = cx_list_next(l, name))
103
104/* provides typed variable at expense of some namespace clobber,
105 * must not be nested due to the shadowing of __end
106 * usage FOR_EACH_T(type varname, list) */
107#define FOR_EACH_T(var, list) \
108 for (cx_list_iterator __it = cx_list_begin(list), __cond = NULL, \
109 __end = cx_list_end(list); \
110 __it != __end; __it = cx_list_next(list, __it)) \
111 if ((__cond = NULL) != NULL) {} else \
112 for (var = cx_list_get(list, __it); __cond == NULL; \
113 __cond = (cx_list_iterator)1)
114
115/* cpl 6 iterators, deprecated
116#define FOR_EACH_FRAMESET(name, fs) \
117 for (cpl_frame * name = cpl_frameset_get_first(fs); \
118 name != NULL; \
119 name = cpl_frameset_get_next(fs))
120
121#define FOR_EACH_FRAMESET_C(name, fs) \
122 for (const cpl_frame * name = cpl_frameset_get_first_const(fs); \
123 name != NULL; \
124 name = cpl_frameset_get_next_const(fs))
125*/
126
127#define FOR_EACH_FRAMESET(name, fs) \
128 for (int __ind = 0, __cond = 0; __ind < cpl_frameset_get_size(fs); \
129 __ind++) \
130 if ((__cond = 0) != 0) {} else \
131 for (cpl_frame * name = cpl_frameset_get_position(fs, __ind); \
132 __cond == 0; __cond = 1)
133
134#define FOR_EACH_FRAMESET_C(name, fs) \
135 for (int __ind = 0, __cond = 0; __ind < cpl_frameset_get_size(fs); \
136 __ind++) \
137 if ((__cond = 0) != 0) {} else \
138 for (const cpl_frame * name = cpl_frameset_get_position_const(fs, __ind); \
139 __cond == 0; __cond = 1)
140
141#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0]))
142
143/* must not be followed by a semicolon! */
144#ifdef _OPENMP
145 #if _OPENMP >= 200805
146 #define OMP_PRAGMA(x) _Pragma (#x)
147 #define OMP3_PRAGMA(x) _Pragma (#x)
148 #define VISIR_OMP3
149 #define VISIR_OMP
150 #else
151 #define OMP_PRAGMA(x) _Pragma (#x)
152 #define OMP3_PRAGMA(x)
153 #define VISIR_OMP
154 #endif
155#else
156 #define OMP_PRAGMA(x)
157 #define OMP3_PRAGMA(x)
158#endif
159
160#ifndef VISIR_ATTR_UNUSED
161 #if defined(__GNUC__) || defined(__ICC) || defined(__clang__)
162 #define VISIR_ATTR_UNUSED __attribute__ ((__unused__))
163 #else
164 #define VISIR_ATTR_UNUSED
165 #endif
166#endif
167
168
169static inline cpl_boolean visir_str_par_is_empty(const char * str)
170{
171 return (str == NULL || strlen(str) == 0 ||
172 strcmp(str, VISIR_STR_PAR_EMPTY) == 0);
173}
174
175static inline int visir_round_to_int(const double x)
176{
177 return x >= 0 ? (int)(x + 0.5) : (int)(x - 0.5);
178}
179
180/*-----------------------------------------------------------------------------
181 Prototypes
182 -----------------------------------------------------------------------------*/
183
184size_t visir_get_num_threads(cpl_boolean force);
185int visir_cmp_frm_fn(const cpl_frame * a, const cpl_frame * b);
186cpl_error_code visir_image_multiply_fast(cpl_image *, const cpl_image *);
187double visir_image_get_mean_fast(const cpl_image * im);
188cpl_error_code visir_move_products(cpl_frameset *frames,
189 const char * dest_, const char * src);
190double visir_utils_get_exptime(const int, const cpl_propertylist *);
191double * visir_utils_get_wls(const irplib_framelist *);
192cpl_image * visir_create_disk_intimage(int, int, int, int, int);
193cpl_image * visir_create_ring_intimage(int, int, int, int, int, int);
194double visir_image_sigma_clip(const cpl_image *, double *);
195double visir_img_phot_sigma_clip(const cpl_image *);
196cpl_error_code visir_get_subpixel_maxpos(const cpl_image *,
197 cpl_size, cpl_size,
198 double *, double *);
199
200typedef struct _visir_fftx_cache visir_fftx_cache;
201visir_fftx_cache * visir_new_fftx_cache(void);
202void visir_delete_fftx_cache(visir_fftx_cache *);
203cpl_error_code visir_fftxcorrelate(const cpl_image *, const cpl_image *,
204 cpl_boolean, double *, double *,
205 double *, visir_fftx_cache *);
206cpl_error_code
207visir_interpolate_rejected(cpl_image * img, size_t ** ppoints, size_t * n);
208
209cpl_image *
210visir_parallel_median_collapse(const cpl_imagelist * l);
211
212typedef struct {
213 /* main propertylist (e.g. from extension 0), must be deleted explicitly*/
214 cpl_propertylist * mplist;
215 /* arbitrary data associated with the list, must be deleted explicitly */
216 void ** auxdata;
217
218 /* images */
219 cpl_imagelist * imgs;
220 /* per image property lists */
221 cpl_propertylist ** plists;
222
223 cpl_size _size;
224 cpl_size _capacity;
225} visir_imglist;
226
227typedef void(*visir_free)(void *);
228
229visir_imglist *
230visir_imglist_new(const cpl_size capacity, cpl_propertylist * mplist);
231
232void
233visir_imglist_delete(visir_imglist * l, visir_free aux_destructor);
234
235void
236visir_imglist_unwrap(visir_imglist * l, visir_free aux_destructor);
237
238void
239visir_imglist_append(visir_imglist * l, cpl_image * img,
240 void * auxdata);
241
242void
243visir_imglist_append_imglist(visir_imglist * l, cpl_imagelist * imgs,
244 void ** auxdata);
245
246cpl_size
247visir_imglist_get_size(const visir_imglist * imgs);
248
249cpl_image *
250visir_imglist_get_img(const visir_imglist * imgs, const cpl_size index);
251
252void *
253visir_imglist_get_data(const visir_imglist * imgs, const cpl_size index);
254
255cpl_error_code
256visir_imglist_get(const visir_imglist * imgs, const cpl_size index,
257 cpl_image ** img, void ** auxdata);
258
259cpl_error_code
260visir_imglist_set_data(visir_imglist * imgs,
261 const cpl_size index, void * auxdata);
262
263void
264visir_imglist_set_mplist(visir_imglist * imgs, cpl_propertylist * mplist);
265
266cpl_propertylist *
267visir_imglist_get_mplist(const visir_imglist * imgs);
268
269cpl_imagelist *
270visir_imglist_get_imglist(const visir_imglist * imgs);
271
272
273int visir_star_find(const cpl_vector *, const cpl_vector *, double, double,
274 double, double *);
275
276cpl_error_code visir_star_convert(const char *, int, int, double, char, int,
277 int, double, const double *, int, double *,
278 double *);
279
280double visir_star_dist_min(const double *, const double *, int, int *, int *);
281
282cpl_table * visir_table_new_xypos(const cpl_imagelist * images, const char *);
283
284int visir_vector_minpos(const cpl_vector *);
285
286
287char * visir_frameset_serialize(const cpl_frameset * frames, size_t * size);
288cpl_frameset * visir_frameset_deserialize(char * block, size_t * size);
289cpl_error_code visir_send_frameset(FILE * , const cpl_frameset * );
290
291void visir_drop_cache(const char * filename, off_t offset, off_t length);
292void visir_readahead(const char * filename, off_t offset, off_t length);
293
294size_t visir_get_nbytes(const cpl_image * img);
295size_t visir_get_nbytes_plist(const cpl_propertylist * plist);
296
297/* FIXME: warn_unused_result */
298cpl_boolean visir_get_tempdir(char * tmpdir_);
299char * visir_get_cwd(void);
300
301cpl_parameter * visir_parameter_duplicate(const cpl_parameter * p);
302
303cpl_error_code visir_copy_parameters(cpl_parameterlist *,
304 const cpl_parameterlist *);
305
306cpl_recipe *
307visir_init_recipe(const char * name, int (*get_info)(cpl_pluginlist *),
308 cpl_pluginlist * plugins);
309
310cpl_error_code
311visir_run_recipe(cpl_recipe * recipe,
312 cpl_frameset * framelist, const cpl_parameterlist * parlist,
313 cpl_error_code (*set_parlist)(cpl_parameterlist *,
314 const cpl_parameterlist *));
315
316cpl_error_code
317visir_tmpdir_exec(const char * recipename, cpl_plugin * plugin,
318 int (*function)(cpl_frameset *, const cpl_parameterlist *));
319cpl_error_code
320visir_forking_exec(const char * recipename, cpl_plugin * plugin,
321 int (*function)(cpl_frameset *, const cpl_parameterlist *));
322
323cpl_frameset *
324visir_prepare_frameset(const cpl_frameset * frameset,
325 const char ** tagmap, size_t ntags,
326 cpl_boolean reverse);
327cpl_frameset * visir_remove_modified_calib(cpl_frameset * set);
328
329cpl_error_code visir_bivector_load(cpl_bivector *, FILE *);
330
331const char ** visir_framelist_set_tag(irplib_framelist *,
332 char * (*)(const cpl_frame *,
333 const cpl_propertylist *, int),
334 int *);
335
336int visir_get_ncombine(const irplib_framelist *);
337
338cpl_error_code visir_qc_append_background(cpl_propertylist *,
339 const irplib_framelist *, int, int);
340
341cpl_error_code visir_qc_append_capa(cpl_propertylist *,
342 const irplib_framelist *);
343
344cpl_error_code visir_qc_append_filter(cpl_propertylist *,
345 const irplib_framelist *);
346cpl_error_code visir_qc_append_exptime(cpl_propertylist *,
347 const irplib_framelist *);
348
349#ifdef _OPENMP
350struct visir_queue;
351typedef struct visir_queue visir_queue;
352visir_queue * visir_queue_init(const int max_size);
353void visir_queue_set_error(visir_queue * q, cpl_error_code error);
354void visir_queue_delete(visir_queue * q);
355cpl_error_code visir_queue_put(visir_queue * q, void * data);
356void * visir_queue_pop(visir_queue * q);
357#endif
358
359size_t visir_upper_bound(const cpl_vector * vec, double val);
360size_t visir_lower_bound(const cpl_vector * vec, double val);
361cpl_image * visir_linintp_values(const cpl_image * inp, const cpl_bivector * ref);
362cpl_error_code
363fit_2d_gauss(const cpl_image * img_, const cpl_image * weights, cpl_size x, cpl_size y,
364 double est_fwhmx, double est_fwhmy,
365 double * peak, double * peak_err,
366 double * major, double * major_err,
367 double * minor, double * minor_err,
368 double * angle, double * angle_err);
369cpl_error_code
370fit_1d_gauss(const cpl_vector * xv, const cpl_vector * y, cpl_vector * dyv,
371 double * x0, double * x0_err,
372 double * peak, double * peak_err,
373 double * sigma, double * sigma_err);
374
375#endif