28#include <irplib_utils.h>
37static IRPLIB_UTIL_SET_ROW(my_table_set_row);
38static IRPLIB_UTIL_CHECK(my_table_check);
40static void test_irplib_image_split(
void);
41static void test_irplib_dfs_table_convert(
void);
42static void test_irplib_table_read_from_frameset(
void);
43static void test_irplib_isnaninf(
void);
44static void bench_irplib_image_split(
int,
int);
45static void frameset_sort_test(
int sz);
46static void test_irplib_aligned_alloc(
void);
64 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
68 test_irplib_isnaninf();
70 test_irplib_dfs_table_convert();
71 test_irplib_table_read_from_frameset();
73 test_irplib_image_split();
75 frameset_sort_test(122);
76 frameset_sort_test(127);
78 test_irplib_aligned_alloc();
80 if (cpl_msg_get_level() <= CPL_MSG_INFO) {
81 bench_irplib_image_split(1024, 100);
83 bench_irplib_image_split(64, 1);
86 return cpl_test_end(0);
96static void test_irplib_isnaninf(
void)
98 double infinity = DBL_MAX * DBL_MAX;
99 double number[] = {17, 0};
112 cpl_test_zero(irplib_isnan(infinity) );
114 cpl_test_zero(irplib_isnan(number[0]) );
115 cpl_test_zero(irplib_isnan(number[1]) );
117 cpl_test( irplib_isinf(infinity) );
119 cpl_test_zero(irplib_isinf(number[0]) );
120 cpl_test_zero(irplib_isinf(number[1]) );
126static void test_irplib_aligned_alloc(
void)
129 size_t alignment[] = {2, 4, 8, 16, 32, 64, 128, 4096};
130 char zero[100] = {0};
133 for (i = 0; i <
sizeof(alignment)/
sizeof(alignment[0]); i++) {
134 ptr = irplib_aligned_malloc(alignment[i], 100);
135 cpl_test_nonnull(ptr);
136 cpl_test_error(CPL_ERROR_NONE);
137 cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
138 irplib_aligned_free(ptr);
139 cpl_test_error(CPL_ERROR_NONE);
142 ptr = irplib_aligned_malloc(5, 100);
144 irplib_aligned_free(NULL);
146 for (i = 0; i <
sizeof(alignment)/
sizeof(alignment[0]); i++) {
147 ptr = irplib_aligned_calloc(alignment[i], 100, 1);
148 cpl_test_nonnull(ptr);
149 cpl_test_error(CPL_ERROR_NONE);
150 cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
151 cpl_test_eq(memcmp(ptr, zero, 100), 0);
152 irplib_aligned_free(ptr);
153 cpl_test_error(CPL_ERROR_NONE);
156 ptr = irplib_aligned_calloc(5, 100, 1);
158 irplib_aligned_free(NULL);
162static cpl_boolean my_table_set_row(cpl_table * self,
165 const cpl_frame * rawframe,
166 const cpl_parameterlist * parlist)
171 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
172 cpl_ensure(line != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
173 cpl_ensure(irow >= 0, CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
174 cpl_ensure(rawframe != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
175 cpl_ensure(parlist != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
177 cpl_test_assert(sscanf(line,
"%31s %16lf", &str[0], &val) != EOF);
179 cpl_test_assert(cpl_table_set_string(self,
"MYLABEL1", (cpl_size)irow, str)
181 cpl_test_assert(cpl_table_set_double(self,
"MYLABEL2", (cpl_size)irow, val)
188static cpl_error_code my_table_check(cpl_table * self,
189 const cpl_frameset * useframes,
190 const cpl_parameterlist * parlist)
193 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
194 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
195 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
197 return CPL_ERROR_NONE;
209static void test_irplib_dfs_table_convert(
void)
215 NULL, NULL, NULL, NULL, NULL, NULL,
216 NULL, NULL, NULL, my_table_set_row,
219 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
230static void test_irplib_table_read_from_frameset(
void)
232 cpl_size initial_failed = cpl_test_get_failed();
233 cpl_error_code error;
235 const char * filename1 =
"dummy_input_file_for_irplib_utils_test_1.txt";
236 const char * filename2 =
"dummy_input_file_for_irplib_utils_test_2.txt";
237 const int expected_rows = 5;
239 cpl_parameterlist * parlist = cpl_parameterlist_new();
240 cpl_frameset * useframes = cpl_frameset_new();
243 cpl_test_nonnull(parlist);
244 cpl_test_nonnull(useframes);
250 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
256 file = fopen(filename1,
"w");
257 cpl_test_nonnull(file);
258 cpl_test(fprintf(file,
"abc 1.2\nde 4.3\nfhij 5.6\n") >= 0);
260 file = fopen(filename2,
"w");
261 cpl_test_nonnull(file);
262 cpl_test(fprintf(file,
"klm -7.8\nnopq 9\n") >= 0);
265 frame = cpl_frame_new();
266 cpl_test_nonnull(frame);
267 cpl_test_eq_error(cpl_frame_set_filename(frame, filename1), CPL_ERROR_NONE);
268 cpl_test_eq_error(cpl_frame_set_tag(frame,
"TEXT"), CPL_ERROR_NONE);
269 cpl_test_eq_error(cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY),
271 cpl_test_eq_error(cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW),
273 cpl_test_eq_error(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_TEMPORARY),
275 cpl_test_eq_error(cpl_frameset_insert(useframes, frame), CPL_ERROR_NONE);
276 frame = cpl_frame_new();
277 cpl_test_nonnull(frame);
278 cpl_test_eq_error(cpl_frame_set_filename(frame, filename2), CPL_ERROR_NONE);
279 cpl_test_eq_error(cpl_frame_set_tag(frame,
"TEXT"), CPL_ERROR_NONE);
280 cpl_test_eq_error(cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY),
282 cpl_test_eq_error(cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW),
284 cpl_test_eq_error(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_TEMPORARY),
286 cpl_test_eq_error(cpl_frameset_insert(useframes, frame), CPL_ERROR_NONE);
288 self = cpl_table_new(expected_rows);
289 cpl_test_nonnull(self);
290 cpl_table_new_column(self,
"MYLABEL1", CPL_TYPE_STRING);
291 cpl_table_new_column(self,
"MYLABEL2", CPL_TYPE_DOUBLE);
292 cpl_table_set_column_unit(self,
"MYLABEL2",
"Some_SI_Unit");
296 cpl_test_eq_error(error, CPL_ERROR_NONE);
299 cpl_test_eq(cpl_table_get_nrow(self), expected_rows);
300 cpl_test_eq_string(cpl_table_get_string(self,
"MYLABEL1", 0),
"abc");
301 cpl_test_eq_string(cpl_table_get_string(self,
"MYLABEL1", 1),
"de");
302 cpl_test_eq_string(cpl_table_get_string(self,
"MYLABEL1", 2),
"fhij");
303 cpl_test_eq_string(cpl_table_get_string(self,
"MYLABEL1", 3),
"klm");
304 cpl_test_eq_string(cpl_table_get_string(self,
"MYLABEL1", 4),
"nopq");
305 cpl_test_abs(cpl_table_get_double(self,
"MYLABEL2", 0, NULL),
307 cpl_test_abs(cpl_table_get_double(self,
"MYLABEL2", 1, NULL),
309 cpl_test_abs(cpl_table_get_double(self,
"MYLABEL2", 2, NULL),
311 cpl_test_abs(cpl_table_get_double(self,
"MYLABEL2", 3, NULL),
313 cpl_test_abs(cpl_table_get_double(self,
"MYLABEL2", 4, NULL),
316 cpl_table_delete(self);
317 cpl_parameterlist_delete(parlist);
318 cpl_frameset_delete(useframes);
321 if (cpl_test_get_failed() == initial_failed) {
322 (void) remove(filename1);
323 (void) remove(filename2);
335static void bench_irplib_image_split(
int nxy,
int nsplit) {
337 const double th_low = 0.0;
338 const double th_high = 50.0;
339 const double alt_low = th_low - 1.0;
340 const double alt_high = th_high + 1.0;
341 cpl_image * test = cpl_image_new(nxy, nxy, CPL_TYPE_FLOAT);
345 for (i = 0; i < nsplit; i++) {
347 const double time0 = cpl_test_get_cputime();
348 const cpl_error_code error =
350 th_low, CPL_TRUE, th_high, CPL_TRUE,
352 CPL_TRUE, CPL_FALSE, CPL_TRUE);
353 time1 = cpl_test_get_cputime();
355 cpl_test_eq_error(error, CPL_ERROR_NONE);
357 if (time1 > time0) tsum += time1 - time0;
360 cpl_msg_info(cpl_func,
"Time to split with image size %d [ms]: %g", nxy,
363 cpl_image_delete(test);
375static void test_irplib_image_split(
void) {
377 const double th_low = 0.0;
378 const double th_high = 50.0;
379 const double alt_low = th_low - 1.0;
380 const double alt_high = th_high + 1.0;
382 cpl_image * test = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
383 cpl_image * result = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
388 0.0, CPL_FALSE, 0.0, CPL_FALSE,
390 CPL_FALSE, CPL_FALSE, CPL_FALSE);
391 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
395 th_low, CPL_TRUE, th_high, CPL_TRUE,
397 CPL_TRUE, CPL_FALSE, CPL_TRUE);
398 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
401 th_low, CPL_TRUE, alt_low, CPL_TRUE,
403 CPL_TRUE, CPL_FALSE, CPL_TRUE);
405 cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
408 error = cpl_image_fill_noise_uniform(test, -100.0, 100.0);
409 cpl_test_eq_error(error, CPL_ERROR_NONE);
412 th_low, CPL_TRUE, th_high, CPL_TRUE,
414 CPL_TRUE, CPL_FALSE, CPL_TRUE);
415 cpl_test_eq_error(error, CPL_ERROR_NONE);
417 error = cpl_image_threshold(test, th_low, th_high, alt_low, alt_high);
418 cpl_test_eq_error(error, CPL_ERROR_NONE);
420 error = cpl_image_subtract(result, test);
421 cpl_test_eq_error(error, CPL_ERROR_NONE);
423 cpl_test_leq(cpl_image_get_absflux(result), DBL_EPSILON);
425 cpl_image_delete(test);
426 cpl_image_delete(result);
430static void frameset_sort_test(
int sz)
433 cpl_frameset * pframeset = cpl_frameset_new();
434 int * idx = cpl_malloc(sz *
sizeof(*idx));
435 double * exptime = cpl_malloc(sz *
sizeof(*exptime));
436 cpl_error_code error;
439 cpl_test_nonnull(pframeset);
441 for (i = 0; i < sz; i++) {
442 cpl_frame * pframe = cpl_frame_new();
443 cpl_propertylist * plist = cpl_propertylist_new();
444 char * filename = cpl_sprintf(
"dummyon%d.fits", i);
445 const double value = (i % 2) > 0 ? i : sz - i - 1;
448 cpl_test_nonnull(pframe);
450 error = cpl_frame_set_filename(pframe, filename);
451 cpl_test_eq_error(error, CPL_ERROR_NONE);
452 error = cpl_frame_set_tag(pframe,
"ON");
453 cpl_test_eq_error(error, CPL_ERROR_NONE);
454 error = cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE);
455 cpl_test_eq_error(error, CPL_ERROR_NONE);
456 error = cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW);
457 cpl_test_eq_error(error, CPL_ERROR_NONE);
459 error = cpl_frameset_insert(pframeset, pframe);
460 cpl_test_eq_error(error, CPL_ERROR_NONE);
461 error = cpl_propertylist_append_double(plist,
"EXPTIME", value);
462 cpl_test_eq_error(error, CPL_ERROR_NONE);
463 error = cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
464 cpl_test_eq_error(error, CPL_ERROR_NONE);
466 cpl_propertylist_delete(plist);
470 error = irplib_frameset_sort(pframeset, idx, exptime);
471 cpl_test_eq_error(error, CPL_ERROR_NONE);
473 for (i = 0; i < sz; i++) {
474 int k = i + 1 - (sz % 2);
476 cpl_test_eq(idx[i], (((i + (sz % 2)) % 2) == 0 ? k : j));
481 cpl_frameset_delete(pframeset);
482 cpl_test_zero(system(
"rm dummyon*.fits"));
cpl_error_code irplib_dfs_table_convert(cpl_table *self, cpl_frameset *allframes, const cpl_frameset *useframes, int maxlinelen, char commentchar, const char *product_name, const char *procatg, const cpl_parameterlist *parlist, const char *recipe_name, const cpl_propertylist *mainlist, const cpl_propertylist *extlist, const char *remregexp, const char *instrume, const char *pipe_id, cpl_boolean(*table_set_row)(cpl_table *, const char *, int, const cpl_frame *, const cpl_parameterlist *), cpl_error_code(*table_check)(cpl_table *, const cpl_frameset *, const cpl_parameterlist *))
Create a DFS product with one table from one or more (ASCII) file(s)
cpl_error_code irplib_table_read_from_frameset(cpl_table *self, const cpl_frameset *useframes, int maxlinelen, char commentchar, const cpl_parameterlist *parlist, cpl_boolean(*table_set_row)(cpl_table *, const char *, int, const cpl_frame *, const cpl_parameterlist *))
Set the rows of a table with data from one or more (ASCII) files.
cpl_error_code irplib_image_split(const cpl_image *self, cpl_image *im_low, cpl_image *im_mid, cpl_image *im_high, double th_low, cpl_boolean isleq_low, double th_high, cpl_boolean isgeq_high, double alt_low, double alt_high, cpl_boolean isbad_low, cpl_boolean isbad_mid, cpl_boolean isbad_high)
Split the values in an image in three according to two thresholds.