28 #include "irplib_utils.h"
54 inline static double irplib_data_get_double(
const void *, cpl_type,
int)
55 #ifdef CPL_HAVE_GNUC_NONNULL
56 __attribute__((nonnull))
60 inline static void irplib_data_set_double(
void *, cpl_type,
int,
double)
61 #ifdef CPL_HAVE_GNUC_NONNULL
62 __attribute__((nonnull))
68 void irplib_errorstate_dump_one_level(
void (*)(
const char *,
71 __attribute__((format (printf, 2, 3)))
73 ,
unsigned,
unsigned,
unsigned);
74 static double frame_get_exptime(
const cpl_frame * pframe);
75 static void quicksort(
int* index,
double* exptime,
int left,
int right);
79 const cpl_parameterlist *,
82 const cpl_imagelist *,
86 const cpl_propertylist *,
88 const cpl_propertylist *,
118 irplib_errorstate_dump_one_level(&cpl_msg_warning,
self, first, last);
122 static cpl_polynomial * irplib_polynomial_fit_1d_create_common(
123 const cpl_vector * x_pos,
124 const cpl_vector * values,
145 irplib_errorstate_dump_one_level(&cpl_msg_info,
self, first, last);
165 irplib_errorstate_dump_one_level(&cpl_msg_debug,
self, first, last);
193 const cpl_parameterlist * parlist,
194 const cpl_frameset * usedframes,
195 const cpl_image * image,
199 const cpl_propertylist * applist,
200 const char * remregexp,
201 const char * pipe_id,
202 const char * filename)
204 cpl_errorstate prestate = cpl_errorstate_get();
205 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
206 : cpl_propertylist_new();
208 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
211 bpp, recipe, prolist, remregexp, pipe_id, filename);
213 cpl_propertylist_delete(prolist);
215 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
217 return CPL_ERROR_NONE;
241 const cpl_parameterlist * parlist,
242 const cpl_frameset * usedframes,
245 const cpl_propertylist * applist,
246 const char * remregexp,
247 const char * pipe_id,
248 const char * filename)
250 cpl_errorstate prestate = cpl_errorstate_get();
251 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
252 : cpl_propertylist_new();
254 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
256 cpl_dfs_save_propertylist(allframes, NULL, parlist, usedframes, NULL,
257 recipe, prolist, remregexp, pipe_id, filename);
259 cpl_propertylist_delete(prolist);
261 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
263 return CPL_ERROR_NONE;
288 const cpl_parameterlist * parlist,
289 const cpl_frameset * usedframes,
290 const cpl_imagelist * imagelist,
294 const cpl_propertylist * applist,
295 const char * remregexp,
296 const char * pipe_id,
297 const char * filename)
299 cpl_errorstate prestate = cpl_errorstate_get();
300 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
301 : cpl_propertylist_new();
303 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
305 cpl_dfs_save_imagelist(allframes, NULL, parlist, usedframes, NULL,
306 imagelist, bpp, recipe, prolist, remregexp, pipe_id,
309 cpl_propertylist_delete(prolist);
311 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
313 return CPL_ERROR_NONE;
336 const cpl_parameterlist * parlist,
337 const cpl_frameset * usedframes,
338 const cpl_table * table,
339 const cpl_propertylist * tablelist,
342 const cpl_propertylist * applist,
343 const char * remregexp,
344 const char * pipe_id,
345 const char * filename)
348 cpl_errorstate prestate = cpl_errorstate_get();
349 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
350 : cpl_propertylist_new();
352 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
354 cpl_dfs_save_table(allframes, NULL, parlist, usedframes, NULL,
355 table, tablelist, recipe, prolist, remregexp,
358 cpl_propertylist_delete(prolist);
360 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
362 return CPL_ERROR_NONE;
396 cpl_propertylist * header,
397 const cpl_parameterlist * parlist,
398 const cpl_frameset * usedframes,
399 const cpl_frame * inherit,
400 const cpl_image * image,
403 const cpl_propertylist * applist,
404 const char * remregexp,
405 const char * pipe_id,
406 const char * filename)
410 NULL, image, type, NULL, NULL, recipe,
411 applist, remregexp, pipe_id, filename)
412 ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
445 cpl_propertylist * header,
446 const cpl_parameterlist * parlist,
447 const cpl_frameset * usedframes,
448 const cpl_frame * inherit,
449 const cpl_imagelist * imagelist,
450 const cpl_image * image,
452 const cpl_table * table,
453 const cpl_propertylist * tablelist,
455 const cpl_propertylist * applist,
456 const char * remregexp,
457 const char * pipe_id,
458 const char * filename) {
461 cpl_propertylist * plist;
462 cpl_frame * product_frame;
469 const unsigned pronum
470 = imagelist != NULL ? 0 : table != NULL ? 1 : (image != NULL ? 2 : 3);
471 const char * proname[] = {
"imagelist",
"table",
"image",
474 const int protype[] = {CPL_FRAME_TYPE_ANY, CPL_FRAME_TYPE_TABLE,
475 CPL_FRAME_TYPE_IMAGE, CPL_FRAME_TYPE_ANY};
476 cpl_error_code error = CPL_ERROR_NONE;
481 if (imagelist != NULL) {
483 assert(image == NULL);
484 assert(table == NULL);
485 assert(tablelist == NULL);
486 }
else if (table != NULL) {
488 assert(imagelist == NULL);
489 assert(image == NULL);
490 }
else if (image != NULL) {
492 assert(imagelist == NULL);
493 assert(table == NULL);
494 assert(tablelist == NULL);
497 assert(imagelist == NULL);
498 assert(table == NULL);
499 assert(tablelist == NULL);
500 assert(image == NULL);
503 cpl_ensure_code(allframes != NULL, CPL_ERROR_NULL_INPUT);
504 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
505 cpl_ensure_code(usedframes != NULL, CPL_ERROR_NULL_INPUT);
506 cpl_ensure_code(recipe != NULL, CPL_ERROR_NULL_INPUT);
507 cpl_ensure_code(applist != NULL, CPL_ERROR_NULL_INPUT);
508 cpl_ensure_code(pipe_id != NULL, CPL_ERROR_NULL_INPUT);
509 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
511 procat = cpl_propertylist_get_string(applist, CPL_DFS_PRO_CATG);
513 cpl_ensure_code(procat != NULL, cpl_error_get_code());
515 cpl_msg_info(cpl_func,
"Writing FITS %s product(%s): %s", proname[pronum],
518 product_frame = cpl_frame_new();
521 error |= cpl_frame_set_filename(product_frame, filename);
522 error |= cpl_frame_set_tag(product_frame, procat);
523 error |= cpl_frame_set_type(product_frame, protype[pronum]);
524 error |= cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
525 error |= cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
528 cpl_frame_delete(product_frame);
529 return cpl_error_set_where(cpl_func);
532 if (header != NULL) {
533 cpl_propertylist_empty(header);
536 plist = cpl_propertylist_new();
540 if (applist != NULL) error = cpl_propertylist_copy_property_regexp(plist,
546 error = cpl_dfs_setup_product_header(plist, product_frame, usedframes,
547 parlist, recipe, pipe_id,
548 "PRO-1.15", inherit);
550 if (remregexp != NULL && !error) {
551 cpl_errorstate prestate = cpl_errorstate_get();
552 (void)cpl_propertylist_erase_regexp(plist, remregexp, 0);
553 if (!cpl_errorstate_is_equal(prestate)) error = cpl_error_get_code();
559 error = cpl_imagelist_save(imagelist, filename, type, plist,
563 error = cpl_table_save(table, plist, tablelist, filename,
567 error = cpl_image_save(image, filename, type, plist,
572 error = cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
578 error = cpl_frameset_insert(allframes, product_frame);
581 cpl_frame_delete(product_frame);
584 if (plist != header) cpl_propertylist_delete(plist);
586 cpl_ensure_code(!error, error);
588 return CPL_ERROR_NONE;
654 cpl_boolean isleq_low,
656 cpl_boolean isgeq_high,
659 cpl_boolean isbad_low,
660 cpl_boolean isbad_mid,
661 cpl_boolean isbad_high)
664 const void * selfdata = cpl_image_get_data_const(
self);
668 const cpl_boolean hasbpm
669 = cpl_image_count_rejected(
self) ? CPL_TRUE : CPL_FALSE;
670 const cpl_binary * selfbpm = hasbpm
671 ? cpl_mask_get_data_const(cpl_image_get_bpm_const(
self)) : NULL;
672 const cpl_type selftype = cpl_image_get_type(
self);
673 const int nx = cpl_image_get_size_x(
self);
674 const int ny = cpl_image_get_size_y(
self);
675 const int npix = nx * ny;
676 const cpl_boolean do_low = im_low != NULL;
677 const cpl_boolean do_mid = im_mid != NULL;
678 const cpl_boolean do_high = im_high != NULL;
679 void * lowdata = NULL;
680 void * middata = NULL;
681 void * highdata = NULL;
682 cpl_binary * lowbpm = NULL;
683 cpl_binary * midbpm = NULL;
684 cpl_binary * highbpm = NULL;
685 const cpl_type lowtype
686 = do_low ? cpl_image_get_type(im_low) : CPL_TYPE_INVALID;
687 const cpl_type midtype
688 = do_mid ? cpl_image_get_type(im_mid) : CPL_TYPE_INVALID;
689 const cpl_type hightype
690 = do_high ? cpl_image_get_type(im_high) : CPL_TYPE_INVALID;
694 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
695 cpl_ensure_code(do_low || do_mid || do_high, CPL_ERROR_NULL_INPUT);
696 cpl_ensure_code(th_low <= th_high, CPL_ERROR_ILLEGAL_INPUT);
699 cpl_ensure_code(cpl_image_get_size_x(im_low) == nx,
700 CPL_ERROR_INCOMPATIBLE_INPUT);
701 cpl_ensure_code(cpl_image_get_size_y(im_low) == ny,
702 CPL_ERROR_INCOMPATIBLE_INPUT);
703 lowdata = cpl_image_get_data(im_low);
707 cpl_ensure_code(cpl_image_get_size_x(im_mid) == nx,
708 CPL_ERROR_INCOMPATIBLE_INPUT);
709 cpl_ensure_code(cpl_image_get_size_y(im_mid) == ny,
710 CPL_ERROR_INCOMPATIBLE_INPUT);
711 middata = cpl_image_get_data(im_mid);
715 cpl_ensure_code(cpl_image_get_size_x(im_high) == nx,
716 CPL_ERROR_INCOMPATIBLE_INPUT);
717 cpl_ensure_code(cpl_image_get_size_y(im_high) == ny,
718 CPL_ERROR_INCOMPATIBLE_INPUT);
719 highdata = cpl_image_get_data(im_high);
724 for (i = 0; i < npix; i++) {
725 const double value = irplib_data_get_double(selfdata, selftype, i);
726 cpl_boolean isalt_low = do_low;
727 cpl_boolean isalt_mid = do_mid;
728 cpl_boolean isalt_high = do_high;
729 cpl_boolean setbad_low = do_low;
730 cpl_boolean setbad_mid = do_mid;
731 cpl_boolean setbad_high = do_high;
732 const void * setdata = NULL;
733 double alt_mid = 0.0;
735 if (isleq_low ? value <= th_low : value < th_low) {
737 isalt_low = CPL_FALSE;
738 irplib_data_set_double(lowdata, lowtype, i, value);
739 setbad_low = hasbpm && selfbpm[i];
743 }
else if (isgeq_high ? value >= th_high : value > th_high) {
745 isalt_high = CPL_FALSE;
746 irplib_data_set_double(highdata, hightype, i, value);
747 setbad_high = hasbpm && selfbpm[i];
752 isalt_mid = CPL_FALSE;
753 irplib_data_set_double(middata, midtype, i, value);
754 setbad_mid = hasbpm && selfbpm[i];
758 if (isalt_low && lowdata != setdata) {
759 irplib_data_set_double(lowdata, lowtype, i, alt_low);
760 setbad_low = isbad_low;
762 if (isalt_mid && middata != setdata) {
763 irplib_data_set_double(middata, midtype, i, alt_mid);
764 setbad_mid = isbad_mid;
766 if (isalt_high && highdata != setdata) {
767 irplib_data_set_double(highdata, hightype, i, alt_high);
768 setbad_high = isbad_high;
772 if (lowbpm == NULL) lowbpm
773 = cpl_mask_get_data(cpl_image_get_bpm(im_low));
774 lowbpm[i] = CPL_BINARY_1;
777 if (midbpm == NULL) midbpm
778 = cpl_mask_get_data(cpl_image_get_bpm(im_mid));
779 midbpm[i] = CPL_BINARY_1;
782 if (highbpm == NULL) highbpm
783 = cpl_mask_get_data(cpl_image_get_bpm(im_high));
784 highbpm[i] = CPL_BINARY_1;
788 return CPL_ERROR_NONE;
845 cpl_frameset * allframes,
846 const cpl_frameset * useframes,
849 const char * product_name,
850 const char * procatg,
851 const cpl_parameterlist * parlist,
852 const char * recipe_name,
853 const cpl_propertylist * mainlist,
854 const cpl_propertylist * extlist,
855 const char * remregexp,
856 const char * instrume,
857 const char * pipe_id,
858 cpl_boolean (*table_set_row)
859 (cpl_table *,
const char *,
int,
861 const cpl_parameterlist *),
862 cpl_error_code (*table_check)
864 const cpl_frameset *,
865 const cpl_parameterlist *))
868 const char * filename;
869 cpl_propertylist * applist = NULL;
870 cpl_errorstate prestate = cpl_errorstate_get();
871 cpl_error_code error;
872 char * fallback_filename = NULL;
874 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
875 cpl_ensure_code(allframes != NULL, CPL_ERROR_NULL_INPUT);
876 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
877 cpl_ensure_code(procatg != NULL, CPL_ERROR_NULL_INPUT);
878 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
879 cpl_ensure_code(recipe_name != NULL, CPL_ERROR_NULL_INPUT);
880 cpl_ensure_code(instrume != NULL, CPL_ERROR_NULL_INPUT);
881 cpl_ensure_code(pipe_id != NULL, CPL_ERROR_NULL_INPUT);
888 cpl_error_get_code());
890 if (table_check != NULL && (table_check(
self, useframes, parlist) ||
891 !cpl_errorstate_is_equal(prestate))) {
892 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
893 "Consistency check of table failed");
896 fallback_filename = cpl_sprintf(
"%s" CPL_DFS_FITS, recipe_name);
897 filename = product_name != NULL ? product_name : fallback_filename;
899 applist = mainlist == NULL
900 ? cpl_propertylist_new() : cpl_propertylist_duplicate(mainlist);
902 error = cpl_propertylist_update_string(applist,
"INSTRUME", instrume);
906 extlist, recipe_name, procatg, applist,
907 remregexp, pipe_id, filename);
909 cpl_propertylist_delete(applist);
910 cpl_free(fallback_filename);
913 cpl_ensure_code(!error, error);
915 return CPL_ERROR_NONE;
974 const cpl_frameset * useframes,
977 const cpl_parameterlist * parlist,
978 cpl_boolean (*table_set_row)
979 (cpl_table *,
const char *,
int,
981 const cpl_parameterlist *))
984 const cpl_frame * rawframe;
985 char * linebuffer = NULL;
986 FILE * stream = NULL;
988 int nrow = cpl_table_get_nrow(
self);
990 cpl_errorstate prestate = cpl_errorstate_get();
991 cpl_frameset_iterator * iterator = NULL;
993 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
994 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
995 cpl_ensure_code(maxlinelen > 0, CPL_ERROR_ILLEGAL_INPUT);
996 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
997 cpl_ensure_code(table_set_row != NULL, CPL_ERROR_NULL_INPUT);
999 linebuffer = cpl_malloc(maxlinelen);
1001 for (rawframe = irplib_frameset_get_first_const(&iterator, useframes);
1003 rawframe = irplib_frameset_get_next_const(iterator), nfiles++) {
1005 const char * rawfile = cpl_frame_get_filename(rawframe);
1007 const int irowpre = irow;
1011 if (rawfile == NULL)
break;
1013 stream = fopen(rawfile,
"r");
1015 if (stream == NULL) {
1016 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1017 cpl_error_set_message(cpl_func, CPL_ERROR_FILE_IO,
"Could not "
1018 "open %s for reading", rawfile);
1020 cpl_error_set_message(cpl_func, CPL_ERROR_FILE_IO,
"Could not "
1021 "open file for reading");
1026 for (;(done = fgets(linebuffer, maxlinelen, stream)) != NULL; iirow++) {
1028 if (linebuffer[0] != commentchar) {
1030 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1031 const int prerow = irow;
1035 nrow += nrow ? nrow : 1;
1036 if (cpl_table_set_size(
self, nrow))
break;
1039 didset = table_set_row(
self, linebuffer, irow, rawframe,
1043 if (!cpl_errorstate_is_equal(prestate)) {
1045 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1046 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1047 "Failed to set table row %d "
1048 "using line %d from %d. file %s",
1052 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1053 "Failure with line %d from %d. "
1057 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1058 "Failed to set table row"
1059 "using catalogue line");
1061 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1062 "Failure with catalogue line");
1069 if (done != NULL)
break;
1071 ierror = fclose(stream);
1076 if (irow == irowpre)
1077 cpl_msg_warning(cpl_func,
"No usable lines in the %d. file: %s",
1081 cpl_frameset_iterator_delete(iterator);
1082 cpl_free(linebuffer);
1083 if (stream != NULL) fclose(stream);
1086 cpl_ensure_code(rawframe == NULL, cpl_error_get_code());
1089 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1090 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1091 "No usable lines in the %d input "
1092 "frame(s)", nfiles);
1094 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1095 "No usable lines in the input frame(s)");
1100 cpl_ensure_code(!cpl_table_set_size(
self, irow), cpl_error_get_code());
1102 return CPL_ERROR_NONE;
1141 if (frame1==NULL || frame2==NULL)
return -1 ;
1144 if ((v1 = cpl_frame_get_tag(frame1)) == NULL)
return -1 ;
1145 if ((v2 = cpl_frame_get_tag(frame2)) == NULL)
return -1 ;
1148 if (strcmp(v1, v2))
return 0 ;
1172 const cpl_frame * frame = cpl_frameset_find_const(
self, tag);
1175 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1177 if (frame == NULL)
return NULL;
1179 if (cpl_frameset_find_const(
self, NULL))
1180 cpl_msg_warning(cpl_func,
1181 "Frameset has more than one file with tag: %s",
1184 return cpl_frame_get_filename(frame);
1201 cpl_frame_group group)
1203 const cpl_frame * frame;
1204 cpl_frameset_iterator * iterator = NULL;
1206 cpl_ensure(
self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1208 for (frame = irplib_frameset_get_first_const(&iterator,
self);
1210 frame = irplib_frameset_get_next_const(iterator)) {
1211 if (cpl_frame_get_group(frame) == group)
break;
1213 cpl_frameset_iterator_delete(iterator);
1238 int * ind,
int nfind)
1240 const int nsize = cpl_apertures_get_size(
self);
1244 cpl_ensure_code(nsize > 0, cpl_error_get_code());
1245 cpl_ensure_code(ind, CPL_ERROR_NULL_INPUT);
1246 cpl_ensure_code(nfind > 0, CPL_ERROR_ILLEGAL_INPUT);
1247 cpl_ensure_code(nfind <= nsize, CPL_ERROR_ILLEGAL_INPUT);
1249 for (ifind=0; ifind < nfind; ifind++) {
1250 double maxflux = -1;
1253 for (i=1; i <= nsize; i++) {
1257 for (k=0; k < ifind; k++)
if (ind[k] == i)
break;
1261 const double flux = cpl_apertures_get_flux(
self, i);
1263 if (maxind < 0 || flux > maxflux) {
1269 ind[ifind] = maxind;
1272 return CPL_ERROR_NONE;
1293 double irplib_data_get_double(
const void *
self, cpl_type type,
int i)
1300 case CPL_TYPE_FLOAT:
1302 const float * pself = (
const float*)
self;
1303 value = (double)pself[i];
1308 const int * pself = (
const int*)
self;
1309 value = (double)pself[i];
1314 const double * pself = (
const double*)
self;
1338 void irplib_data_set_double(
void *
self, cpl_type type,
int i,
double value)
1342 case CPL_TYPE_FLOAT:
1344 float * pself = (
float*)
self;
1345 pself[i] = (float)value;
1350 int * pself = (
int*)
self;
1351 pself[i] = (int)value;
1356 double * pself = (
double*)
self;
1380 void irplib_errorstate_dump_one_level(
void (*messenger)(
const char *,
1382 unsigned self,
unsigned first,
1386 const cpl_boolean is_reverse = first > last ? CPL_TRUE : CPL_FALSE;
1387 const unsigned newest = is_reverse ? first : last;
1388 const unsigned oldest = is_reverse ? last : first;
1389 const char * revmsg = is_reverse ?
" in reverse order" :
"";
1399 messenger(cpl_func,
"No error(s) to dump");
1406 if (
self == first) {
1408 messenger(cpl_func,
"Dumping all %u error(s)%s:", newest,
1411 messenger(cpl_func,
"Dumping the %u most recent error(s) "
1412 "out of a total of %u errors%s:",
1413 newest - oldest + 1, newest, revmsg);
1415 cpl_msg_indent_more();
1418 messenger(cpl_func,
"[%u/%u] '%s' (%u) at %s",
self, newest,
1419 cpl_error_get_message(), cpl_error_get_code(),
1420 cpl_error_get_where());
1422 if (
self == last) cpl_msg_indent_less();
1426 cpl_polynomial * irplib_polynomial_fit_1d_create_chiq(
1427 const cpl_vector * x_pos,
1428 const cpl_vector * values,
1433 return irplib_polynomial_fit_1d_create_common(x_pos, values, degree, NULL, rechisq);
1435 cpl_polynomial * irplib_polynomial_fit_1d_create(
1436 const cpl_vector * x_pos,
1437 const cpl_vector * values,
1443 return irplib_polynomial_fit_1d_create_common(x_pos, values, degree, mse, NULL);
1445 static cpl_polynomial * irplib_polynomial_fit_1d_create_common(
1446 const cpl_vector * x_pos,
1447 const cpl_vector * values,
1453 cpl_polynomial * fit1d = NULL;
1454 cpl_size loc_degree = (cpl_size)degree ;
1456 fit1d = cpl_polynomial_new(1);
1457 x_size = cpl_vector_get_size(x_pos);
1458 if(fit1d != NULL && x_size > 1)
1460 cpl_matrix * samppos = NULL;
1461 cpl_vector * fitresidual = NULL;
1462 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1463 samppos = cpl_matrix_wrap(1, x_size,
1464 (
double*)cpl_vector_get_data_const(x_pos));
1465 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1466 fitresidual = cpl_vector_new(x_size);
1467 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1468 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
1469 CPL_FALSE, NULL, &loc_degree);
1470 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1471 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL,
1472 fit1d, samppos, rechisq);
1473 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1476 *mse = cpl_vector_product(fitresidual, fitresidual)
1477 / cpl_vector_get_size(fitresidual);
1479 cpl_matrix_unwrap(samppos);
1480 cpl_vector_delete(fitresidual);
1485 static void quicksort(
int* iindex,
double* exptime,
int left,
int right)
1489 int pivot = (i + j) / 2;
1490 double index_value = exptime[pivot];
1493 while(exptime[i] < index_value) i++;
1494 while(exptime[j] > index_value) j--;
1499 int tmp = iindex[i];
1500 double dtmp = exptime[i];
1501 iindex[i]=iindex[j];
1503 exptime[i] = exptime[j];
1513 quicksort(iindex, exptime, i, right);
1517 quicksort(iindex, exptime,left, j);
1520 cpl_error_code irplib_frameset_sort(
const cpl_frameset *
self,
int* iindex,
double* exptime)
1523 const cpl_frame* tmp_frame = 0;
1524 cpl_error_code error = CPL_ERROR_NONE;
1525 int sz = cpl_frameset_get_size(
self);
1526 cpl_frameset_iterator* iterator = NULL;
1529 tmp_frame = irplib_frameset_get_first_const(&iterator,
self);
1532 exptime[i] = frame_get_exptime(tmp_frame);
1534 tmp_frame = irplib_frameset_get_next_const(iterator);
1537 cpl_frameset_iterator_delete(iterator);
1539 quicksort(iindex, exptime, 0, sz - 1);
1544 static double frame_get_exptime(
const cpl_frame * pframe)
1547 cpl_propertylist * plist =
1548 cpl_propertylist_load_regexp(cpl_frame_get_filename(pframe), 0,
1549 "EXPTIME", CPL_FALSE);
1551 dval = cpl_propertylist_get_double(plist,
"EXPTIME");
1552 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1553 cpl_msg_error(cpl_func,
"error during reading EXPTIME key from "
1554 "the frame [%s]", cpl_frame_get_filename(pframe));
1558 cpl_propertylist_delete(plist);
1578 void * irplib_aligned_malloc(
size_t alignment,
size_t size)
1583 if (alignment & (alignment - 1)) {
1588 if ((size % alignment) != 0) {
1589 size += alignment - (size % alignment);
1592 #if defined HAVE_DECL_ALIGNED_ALLOC && defined HAVE_ALIGNED_ALLOC
1593 return aligned_alloc(alignment, size);
1594 #elif defined HAVE_POSIX_MEMALIGN && defined HAVE_DECL_POSIX_MEMALIGN
1598 return malloc (size);
1599 if (alignment == 2 || (
sizeof (
void *) == 8 && alignment == 4))
1600 alignment =
sizeof (
void *);
1601 if (posix_memalign (&ptr, alignment, size) == 0)
1619 if (alignment < 2 *
sizeof (
void *))
1620 alignment = 2 *
sizeof (
void *);
1622 malloc_ptr = malloc (size + alignment);
1627 aligned_ptr = (
void *) (((
size_t) malloc_ptr + alignment)
1628 & ~((size_t) (alignment) - 1));
1631 *(((
void **) aligned_ptr) - 1) = malloc_ptr;
1650 void * irplib_aligned_calloc(
size_t alignment,
size_t nelem,
size_t nbytes)
1652 void * buffer = irplib_aligned_malloc(alignment, nelem * nbytes);
1656 memset((
size_t *)buffer, 0, nelem * nbytes);
1670 void irplib_aligned_free (
void * aligned_ptr)
1672 #if defined HAVE_DECL_ALIGNED_ALLOC && defined HAVE_ALIGNED_ALLOC
1674 #elif defined HAVE_POSIX_MEMALIGN && defined HAVE_DECL_POSIX_MEMALIGN
1678 free (*(((
void **) aligned_ptr) - 1));
1698 irplib_frameset_get_first_const(cpl_frameset_iterator **iterator,
1699 const cpl_frameset *frameset)
1701 cpl_ensure(iterator != NULL, CPL_ERROR_NULL_INPUT, NULL);
1702 *iterator = cpl_frameset_iterator_new(frameset);
1703 return cpl_frameset_iterator_get_const(*iterator);
1717 irplib_frameset_get_next_const(cpl_frameset_iterator *iterator)
1719 cpl_errorstate prestate = cpl_errorstate_get();
1720 cpl_error_code error = cpl_frameset_iterator_advance(iterator, 1);
1721 if (error == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
1722 cpl_errorstate_set(prestate);
1724 }
else if (error != CPL_ERROR_NONE) {
1727 return cpl_frameset_iterator_get_const(iterator);
cpl_error_code irplib_dfs_save_table(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_table *table, const cpl_propertylist *tablelist, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a table as a DFS-compliant pipeline product.
static cpl_error_code irplib_dfs_product_save(cpl_frameset *, cpl_propertylist *, const cpl_parameterlist *, const cpl_frameset *, const cpl_frame *, const cpl_imagelist *, const cpl_image *, cpl_type, const cpl_table *, const cpl_propertylist *, const char *, const cpl_propertylist *, const char *, const char *, const char *)
Save either an image or table as a pipeline product.
void irplib_errorstate_dump_info(unsigned self, unsigned first, unsigned last)
Dump a single CPL error at the CPL info level.
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.
void irplib_reset(void)
Reset IRPLIB state.
cpl_error_code irplib_apertures_find_max_flux(const cpl_apertures *self, int *ind, int nfind)
Find the aperture(s) with the greatest flux.
cpl_error_code irplib_dfs_save_propertylist(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a propertylist as a DFS-compliant pipeline product.
cpl_error_code irplib_dfs_save_image_(cpl_frameset *allframes, cpl_propertylist *header, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_frame *inherit, const cpl_image *image, cpl_type type, const char *recipe, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
const cpl_frame * irplib_frameset_get_first_from_group(const cpl_frameset *self, cpl_frame_group group)
Find the first frame belonging to the given group.
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)
void irplib_errorstate_dump_warning(unsigned self, unsigned first, unsigned last)
Dump a single CPL error at the CPL warning level.
void irplib_errorstate_dump_debug(unsigned self, unsigned first, unsigned last)
Dump a single CPL error at the CPL debug level.
cpl_error_code irplib_dfs_save_image(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_image *image, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
int irplib_compare_tags(cpl_frame *frame1, cpl_frame *frame2)
Comparison function to identify different input frames.
const char * irplib_frameset_find_file(const cpl_frameset *self, const char *tag)
Find the filename with the given tag in a frame set.
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.
cpl_error_code irplib_dfs_save_imagelist(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_imagelist *imagelist, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an imagelist as a DFS-compliant pipeline product.