VISIR Pipeline Reference Manual  4.1.0
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 static inline cpl_boolean visir_str_par_is_empty(const char * str)
161 {
162  return (str == NULL || strlen(str) == 0 ||
163  strcmp(str, VISIR_STR_PAR_EMPTY) == 0);
164 }
165 
166 static inline int visir_round_to_int(const double x)
167 {
168  return x >= 0 ? (int)(x + 0.5) : (int)(x - 0.5);
169 }
170 
171 /*-----------------------------------------------------------------------------
172  Prototypes
173  -----------------------------------------------------------------------------*/
174 
175 size_t visir_get_num_threads(cpl_boolean force);
176 int visir_cmp_frm_fn(const cpl_frame * a, const cpl_frame * b);
177 cpl_error_code visir_image_multiply_fast(cpl_image *, const cpl_image *);
178 double visir_image_get_mean_fast(const cpl_image * im);
179 cpl_error_code visir_move_products(cpl_frameset *frames,
180  const char * dest_, const char * src);
181 double visir_utils_get_exptime(const int, const cpl_propertylist *);
182 double * visir_utils_get_wls(const irplib_framelist *);
183 cpl_image * visir_create_disk_intimage(int, int, int, int, int);
184 cpl_image * visir_create_ring_intimage(int, int, int, int, int, int);
185 double visir_image_sigma_clip(const cpl_image *, double *);
186 double visir_img_phot_sigma_clip(const cpl_image *);
187 cpl_error_code visir_get_subpixel_maxpos(const cpl_image *,
188  cpl_size, cpl_size,
189  double *, double *);
190 
191 typedef struct _visir_fftx_cache visir_fftx_cache;
192 visir_fftx_cache * visir_new_fftx_cache(void);
193 void visir_delete_fftx_cache(visir_fftx_cache *);
194 cpl_error_code visir_fftxcorrelate(const cpl_image *, const cpl_image *,
195  cpl_boolean, double *, double *,
196  double *, visir_fftx_cache *);
197 cpl_error_code
198 visir_interpolate_rejected(cpl_image * img, size_t ** ppoints, size_t * n);
199 
200 cpl_image *
201 visir_parallel_median_collapse(const cpl_imagelist * l);
202 
203 typedef struct {
204  /* main propertylist (e.g. from extension 0), must be deleted explicitly*/
205  cpl_propertylist * mplist;
206  /* arbitrary data associated with the list, must be deleted explicitly */
207  void ** auxdata;
208 
209  /* images */
210  cpl_imagelist * imgs;
211  /* per image property lists */
212  cpl_propertylist ** plists;
213 
214  cpl_size _size;
215  cpl_size _capacity;
216 } visir_imglist;
217 
218 typedef void(*visir_free)(void *);
219 
221 visir_imglist_new(const cpl_size capacity, cpl_propertylist * mplist);
222 
223 void
224 visir_imglist_delete(visir_imglist * l, visir_free aux_destructor);
225 
226 void
227 visir_imglist_unwrap(visir_imglist * l, visir_free aux_destructor);
228 
229 void
230 visir_imglist_append(visir_imglist * l, cpl_image * img,
231  void * auxdata);
232 
233 void
234 visir_imglist_append_imglist(visir_imglist * l, cpl_imagelist * imgs,
235  void ** auxdata);
236 
237 cpl_size
238 visir_imglist_get_size(const visir_imglist * imgs);
239 
240 cpl_image *
241 visir_imglist_get_img(const visir_imglist * imgs, const cpl_size index);
242 
243 void *
244 visir_imglist_get_data(const visir_imglist * imgs, const cpl_size index);
245 
246 cpl_error_code
247 visir_imglist_get(const visir_imglist * imgs, const cpl_size index,
248  cpl_image ** img, void ** auxdata);
249 
250 cpl_error_code
251 visir_imglist_set_data(visir_imglist * imgs,
252  const cpl_size index, void * auxdata);
253 
254 void
255 visir_imglist_set_mplist(visir_imglist * imgs, cpl_propertylist * mplist);
256 
257 cpl_propertylist *
258 visir_imglist_get_mplist(const visir_imglist * imgs);
259 
260 cpl_imagelist *
261 visir_imglist_get_imglist(const visir_imglist * imgs);
262 
263 
264 int visir_star_find(const cpl_vector *, const cpl_vector *, double, double,
265  double, double *);
266 
267 cpl_error_code visir_star_convert(const char *, int, int, double, char, int,
268  int, double, const double *, int, double *,
269  double *);
270 
271 double visir_star_dist_min(const double *, const double *, int, int *, int *);
272 
273 cpl_table * visir_table_new_xypos(const cpl_imagelist * images, const char *);
274 
275 int visir_vector_minpos(const cpl_vector *);
276 
277 
278 char * visir_frameset_serialize(const cpl_frameset * frames, size_t * size);
279 cpl_frameset * visir_frameset_deserialize(char * block, size_t * size);
280 cpl_error_code visir_send_frameset(FILE * , const cpl_frameset * );
281 
282 void visir_drop_cache(const char * filename, off_t offset, off_t length);
283 void visir_readahead(const char * filename, off_t offset, off_t length);
284 
285 size_t visir_get_nbytes(const cpl_image * img);
286 size_t visir_get_nbytes_plist(const cpl_propertylist * plist);
287 
288 /* FIXME: warn_unused_result */
289 cpl_boolean visir_get_tempdir(char * tmpdir_);
290 
291 cpl_parameter * visir_parameter_duplicate(const cpl_parameter * p);
292 
293 cpl_error_code visir_copy_parameters(cpl_parameterlist *,
294  const cpl_parameterlist *);
295 
296 cpl_recipe *
297 visir_init_recipe(const char * name, int (*get_info)(cpl_pluginlist *),
298  cpl_pluginlist * plugins);
299 
300 cpl_error_code
301 visir_run_recipe(cpl_recipe * recipe,
302  cpl_frameset * framelist, const cpl_parameterlist * parlist,
303  cpl_error_code (*set_parlist)(cpl_parameterlist *,
304  const cpl_parameterlist *));
305 
306 cpl_error_code
307 visir_tmpdir_exec(const char * recipename, cpl_plugin * plugin,
308  int (*function)(cpl_frameset *, const cpl_parameterlist *));
309 cpl_error_code
310 visir_forking_exec(const char * recipename, cpl_plugin * plugin,
311  int (*function)(cpl_frameset *, const cpl_parameterlist *));
312 
313 cpl_frameset *
314 visir_prepare_frameset(const cpl_frameset * frameset,
315  const char ** tagmap, size_t ntags,
316  cpl_boolean reverse);
317 cpl_frameset * visir_remove_modified_calib(cpl_frameset * set);
318 
319 cpl_error_code visir_bivector_load(cpl_bivector *, FILE *);
320 
321 const char ** visir_framelist_set_tag(irplib_framelist *,
322  char * (*)(const cpl_frame *,
323  const cpl_propertylist *, int),
324  int *);
325 
326 cpl_error_code visir_qc_append_background(cpl_propertylist *,
327  const irplib_framelist *, int, int);
328 
329 cpl_error_code visir_qc_append_capa(cpl_propertylist *,
330  const irplib_framelist *);
331 
332 cpl_error_code visir_qc_append_filter(cpl_propertylist *,
333  const irplib_framelist *);
334 cpl_error_code visir_qc_append_exptime(cpl_propertylist *,
335  const irplib_framelist *);
336 
337 #ifdef _OPENMP
338 struct visir_queue;
339 typedef struct visir_queue visir_queue;
340 visir_queue * visir_queue_init(const int max_size);
341 void visir_queue_set_error(visir_queue * q, cpl_error_code error);
342 void visir_queue_delete(visir_queue * q);
343 cpl_error_code visir_queue_put(visir_queue * q, void * data);
344 void * visir_queue_pop(visir_queue * q);
345 #endif
346 
347 size_t visir_upper_bound(const cpl_vector * vec, double val);
348 size_t visir_lower_bound(const cpl_vector * vec, double val);
349 cpl_image * visir_linintp_values(const cpl_image * inp, const cpl_bivector * ref);
350 cpl_error_code
351 fit_2d_gauss(const cpl_image * img_, const cpl_image * weights, cpl_size x, cpl_size y,
352  double est_fwhmx, double est_fwhmy,
353  double * peak, double * peak_err,
354  double * major, double * major_err,
355  double * minor, double * minor_err,
356  double * angle, double * angle_err);
357 cpl_error_code
358 fit_1d_gauss(const cpl_vector * xv, const cpl_vector * y, cpl_vector * dyv,
359  double * x0, double * x0_err,
360  double * peak, double * peak_err,
361  double * sigma, double * sigma_err);
362 
363 #endif