35 #include "irplib_utils.h"
52 #ifndef CPL_SIZE_FORMAT
53 #define CPL_SIZE_FORMAT "d"
70 inline static double irplib_data_get_double(
const void *, cpl_type,
int)
71 #ifdef CPL_HAVE_GNUC_NONNULL
72 __attribute__((nonnull))
76 inline static void irplib_data_set_double(
void *, cpl_type,
int,
double)
77 #ifdef CPL_HAVE_GNUC_NONNULL
78 __attribute__((nonnull))
84 void irplib_errorstate_dump_one_level(
void (*)(
const char *,
87 __attribute__((format (printf, 2, 3)))
89 ,
unsigned,
unsigned,
unsigned);
90 static double frame_get_exptime(
const cpl_frame * pframe);
91 static void quicksort(
int* index,
double* exptime,
int left,
int right);
95 const cpl_parameterlist *,
98 const cpl_imagelist *,
102 const cpl_propertylist *,
104 const cpl_propertylist *,
134 irplib_errorstate_dump_one_level(&cpl_msg_warning,
self, first, last);
138 static cpl_polynomial * irplib_polynomial_fit_1d_create_common(
139 const cpl_vector * x_pos,
140 const cpl_vector * values,
161 irplib_errorstate_dump_one_level(&cpl_msg_info,
self, first, last);
181 irplib_errorstate_dump_one_level(&cpl_msg_debug,
self, first, last);
209 const cpl_parameterlist * parlist,
210 const cpl_frameset * usedframes,
211 const cpl_image * image,
215 const cpl_propertylist * applist,
216 const char * remregexp,
217 const char * pipe_id,
218 const char * filename)
220 cpl_errorstate prestate = cpl_errorstate_get();
221 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
222 : cpl_propertylist_new();
224 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
227 bpp, recipe, prolist, remregexp, pipe_id, filename);
229 cpl_propertylist_delete(prolist);
231 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
233 return CPL_ERROR_NONE;
257 const cpl_parameterlist * parlist,
258 const cpl_frameset * usedframes,
261 const cpl_propertylist * applist,
262 const char * remregexp,
263 const char * pipe_id,
264 const char * filename)
266 cpl_errorstate prestate = cpl_errorstate_get();
267 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
268 : cpl_propertylist_new();
270 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
272 cpl_dfs_save_propertylist(allframes, NULL, parlist, usedframes, NULL,
273 recipe, prolist, remregexp, pipe_id, filename);
275 cpl_propertylist_delete(prolist);
277 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
279 return CPL_ERROR_NONE;
304 const cpl_parameterlist * parlist,
305 const cpl_frameset * usedframes,
306 const cpl_imagelist * imagelist,
310 const cpl_propertylist * applist,
311 const char * remregexp,
312 const char * pipe_id,
313 const char * filename)
315 cpl_errorstate prestate = cpl_errorstate_get();
316 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
317 : cpl_propertylist_new();
319 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
321 cpl_dfs_save_imagelist(allframes, NULL, parlist, usedframes, NULL,
322 imagelist, bpp, recipe, prolist, remregexp, pipe_id,
325 cpl_propertylist_delete(prolist);
327 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
329 return CPL_ERROR_NONE;
352 const cpl_parameterlist * parlist,
353 const cpl_frameset * usedframes,
354 const cpl_table * table,
355 const cpl_propertylist * tablelist,
358 const cpl_propertylist * applist,
359 const char * remregexp,
360 const char * pipe_id,
361 const char * filename)
364 cpl_errorstate prestate = cpl_errorstate_get();
365 cpl_propertylist * prolist = applist ? cpl_propertylist_duplicate(applist)
366 : cpl_propertylist_new();
368 cpl_propertylist_update_string(prolist, CPL_DFS_PRO_CATG, procat);
370 cpl_dfs_save_table(allframes, NULL, parlist, usedframes, NULL,
371 table, tablelist, recipe, prolist, remregexp,
374 cpl_propertylist_delete(prolist);
376 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
378 return CPL_ERROR_NONE;
412 cpl_propertylist * header,
413 const cpl_parameterlist * parlist,
414 const cpl_frameset * usedframes,
415 const cpl_frame * inherit,
416 const cpl_image * image,
419 const cpl_propertylist * applist,
420 const char * remregexp,
421 const char * pipe_id,
422 const char * filename)
426 NULL, image, type, NULL, NULL, recipe,
427 applist, remregexp, pipe_id, filename)
428 ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
461 cpl_propertylist * header,
462 const cpl_parameterlist * parlist,
463 const cpl_frameset * usedframes,
464 const cpl_frame * inherit,
465 const cpl_imagelist * imagelist,
466 const cpl_image * image,
468 const cpl_table * table,
469 const cpl_propertylist * tablelist,
471 const cpl_propertylist * applist,
472 const char * remregexp,
473 const char * pipe_id,
474 const char * filename) {
477 cpl_propertylist * plist;
478 cpl_frame * product_frame;
485 const unsigned pronum
486 = imagelist != NULL ? 0 : table != NULL ? 1 : (image != NULL ? 2 : 3);
487 const char * proname[] = {
"imagelist",
"table",
"image",
490 const int protype[] = {CPL_FRAME_TYPE_ANY, CPL_FRAME_TYPE_TABLE,
491 CPL_FRAME_TYPE_IMAGE, CPL_FRAME_TYPE_ANY};
492 cpl_error_code error = CPL_ERROR_NONE;
497 if (imagelist != NULL) {
499 assert(image == NULL);
500 assert(table == NULL);
501 assert(tablelist == NULL);
502 }
else if (table != NULL) {
504 assert(imagelist == NULL);
505 assert(image == NULL);
506 }
else if (image != NULL) {
508 assert(imagelist == NULL);
509 assert(table == NULL);
510 assert(tablelist == NULL);
513 assert(imagelist == NULL);
514 assert(table == NULL);
515 assert(tablelist == NULL);
516 assert(image == NULL);
519 cpl_ensure_code(allframes != NULL, CPL_ERROR_NULL_INPUT);
520 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
521 cpl_ensure_code(usedframes != NULL, CPL_ERROR_NULL_INPUT);
522 cpl_ensure_code(recipe != NULL, CPL_ERROR_NULL_INPUT);
523 cpl_ensure_code(applist != NULL, CPL_ERROR_NULL_INPUT);
524 cpl_ensure_code(pipe_id != NULL, CPL_ERROR_NULL_INPUT);
525 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
527 procat = cpl_propertylist_get_string(applist, CPL_DFS_PRO_CATG);
529 cpl_ensure_code(procat != NULL, cpl_error_get_code());
531 cpl_msg_info(cpl_func,
"Writing FITS %s product(%s): %s", proname[pronum],
534 product_frame = cpl_frame_new();
537 error |= cpl_frame_set_filename(product_frame, filename);
538 error |= cpl_frame_set_tag(product_frame, procat);
539 error |= cpl_frame_set_type(product_frame, protype[pronum]);
540 error |= cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
541 error |= cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
544 cpl_frame_delete(product_frame);
545 return cpl_error_set_where(cpl_func);
548 if (header != NULL) {
549 cpl_propertylist_empty(header);
552 plist = cpl_propertylist_new();
556 if (applist != NULL) error = cpl_propertylist_copy_property_regexp(plist,
562 error = cpl_dfs_setup_product_header(plist, product_frame, usedframes,
563 parlist, recipe, pipe_id,
564 "PRO-1.15", inherit);
566 if (remregexp != NULL && !error) {
567 cpl_errorstate prestate = cpl_errorstate_get();
568 (void)cpl_propertylist_erase_regexp(plist, remregexp, 0);
569 if (!cpl_errorstate_is_equal(prestate)) error = cpl_error_get_code();
575 error = cpl_imagelist_save(imagelist, filename, type, plist,
579 error = cpl_table_save(table, plist, tablelist, filename,
583 error = cpl_image_save(image, filename, type, plist,
588 error = cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
594 error = cpl_frameset_insert(allframes, product_frame);
597 cpl_frame_delete(product_frame);
600 if (plist != header) cpl_propertylist_delete(plist);
602 cpl_ensure_code(!error, error);
604 return CPL_ERROR_NONE;
670 cpl_boolean isleq_low,
672 cpl_boolean isgeq_high,
675 cpl_boolean isbad_low,
676 cpl_boolean isbad_mid,
677 cpl_boolean isbad_high)
680 const void * selfdata = cpl_image_get_data_const(
self);
684 const cpl_boolean hasbpm
685 = cpl_image_count_rejected(
self) ? CPL_TRUE : CPL_FALSE;
686 const cpl_binary * selfbpm = hasbpm
687 ? cpl_mask_get_data_const(cpl_image_get_bpm_const(
self)) : NULL;
688 const cpl_type selftype = cpl_image_get_type(
self);
689 const int nx = cpl_image_get_size_x(
self);
690 const int ny = cpl_image_get_size_y(
self);
691 const int npix = nx * ny;
692 const cpl_boolean do_low = im_low != NULL;
693 const cpl_boolean do_mid = im_mid != NULL;
694 const cpl_boolean do_high = im_high != NULL;
695 void * lowdata = NULL;
696 void * middata = NULL;
697 void * highdata = NULL;
698 cpl_binary * lowbpm = NULL;
699 cpl_binary * midbpm = NULL;
700 cpl_binary * highbpm = NULL;
701 const cpl_type lowtype
702 = do_low ? cpl_image_get_type(im_low) : CPL_TYPE_INVALID;
703 const cpl_type midtype
704 = do_mid ? cpl_image_get_type(im_mid) : CPL_TYPE_INVALID;
705 const cpl_type hightype
706 = do_high ? cpl_image_get_type(im_high) : CPL_TYPE_INVALID;
710 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
711 cpl_ensure_code(do_low || do_mid || do_high, CPL_ERROR_NULL_INPUT);
712 cpl_ensure_code(th_low <= th_high, CPL_ERROR_ILLEGAL_INPUT);
715 cpl_ensure_code(cpl_image_get_size_x(im_low) == nx,
716 CPL_ERROR_INCOMPATIBLE_INPUT);
717 cpl_ensure_code(cpl_image_get_size_y(im_low) == ny,
718 CPL_ERROR_INCOMPATIBLE_INPUT);
719 lowdata = cpl_image_get_data(im_low);
723 cpl_ensure_code(cpl_image_get_size_x(im_mid) == nx,
724 CPL_ERROR_INCOMPATIBLE_INPUT);
725 cpl_ensure_code(cpl_image_get_size_y(im_mid) == ny,
726 CPL_ERROR_INCOMPATIBLE_INPUT);
727 middata = cpl_image_get_data(im_mid);
731 cpl_ensure_code(cpl_image_get_size_x(im_high) == nx,
732 CPL_ERROR_INCOMPATIBLE_INPUT);
733 cpl_ensure_code(cpl_image_get_size_y(im_high) == ny,
734 CPL_ERROR_INCOMPATIBLE_INPUT);
735 highdata = cpl_image_get_data(im_high);
740 for (i = 0; i < npix; i++) {
741 const double value = irplib_data_get_double(selfdata, selftype, i);
742 cpl_boolean isalt_low = do_low;
743 cpl_boolean isalt_mid = do_mid;
744 cpl_boolean isalt_high = do_high;
745 cpl_boolean setbad_low = do_low;
746 cpl_boolean setbad_mid = do_mid;
747 cpl_boolean setbad_high = do_high;
748 const void * setdata = NULL;
749 double alt_mid = 0.0;
751 if (isleq_low ? value <= th_low : value < th_low) {
753 isalt_low = CPL_FALSE;
754 irplib_data_set_double(lowdata, lowtype, i, value);
755 setbad_low = hasbpm && selfbpm[i];
759 }
else if (isgeq_high ? value >= th_high : value > th_high) {
761 isalt_high = CPL_FALSE;
762 irplib_data_set_double(highdata, hightype, i, value);
763 setbad_high = hasbpm && selfbpm[i];
768 isalt_mid = CPL_FALSE;
769 irplib_data_set_double(middata, midtype, i, value);
770 setbad_mid = hasbpm && selfbpm[i];
774 if (isalt_low && lowdata != setdata) {
775 irplib_data_set_double(lowdata, lowtype, i, alt_low);
776 setbad_low = isbad_low;
778 if (isalt_mid && middata != setdata) {
779 irplib_data_set_double(middata, midtype, i, alt_mid);
780 setbad_mid = isbad_mid;
782 if (isalt_high && highdata != setdata) {
783 irplib_data_set_double(highdata, hightype, i, alt_high);
784 setbad_high = isbad_high;
788 if (lowbpm == NULL) lowbpm
789 = cpl_mask_get_data(cpl_image_get_bpm(im_low));
790 lowbpm[i] = CPL_BINARY_1;
793 if (midbpm == NULL) midbpm
794 = cpl_mask_get_data(cpl_image_get_bpm(im_mid));
795 midbpm[i] = CPL_BINARY_1;
798 if (highbpm == NULL) highbpm
799 = cpl_mask_get_data(cpl_image_get_bpm(im_high));
800 highbpm[i] = CPL_BINARY_1;
804 return CPL_ERROR_NONE;
861 cpl_frameset * allframes,
862 const cpl_frameset * useframes,
865 const char * product_name,
866 const char * procatg,
867 const cpl_parameterlist * parlist,
868 const char * recipe_name,
869 const cpl_propertylist * mainlist,
870 const cpl_propertylist * extlist,
871 const char * remregexp,
872 const char * instrume,
873 const char * pipe_id,
874 cpl_boolean (*table_set_row)
875 (cpl_table *,
const char *,
int,
877 const cpl_parameterlist *),
878 cpl_error_code (*table_check)
880 const cpl_frameset *,
881 const cpl_parameterlist *))
884 const char * filename;
885 cpl_propertylist * applist = NULL;
886 cpl_errorstate prestate = cpl_errorstate_get();
887 cpl_error_code error;
888 char * fallback_filename = NULL;
890 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
891 cpl_ensure_code(allframes != NULL, CPL_ERROR_NULL_INPUT);
892 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
893 cpl_ensure_code(procatg != NULL, CPL_ERROR_NULL_INPUT);
894 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
895 cpl_ensure_code(recipe_name != NULL, CPL_ERROR_NULL_INPUT);
896 cpl_ensure_code(instrume != NULL, CPL_ERROR_NULL_INPUT);
897 cpl_ensure_code(pipe_id != NULL, CPL_ERROR_NULL_INPUT);
904 cpl_error_get_code());
906 if (table_check != NULL && (table_check(
self, useframes, parlist) ||
907 !cpl_errorstate_is_equal(prestate))) {
908 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
909 "Consistency check of table failed");
912 fallback_filename = cpl_sprintf(
"%s" CPL_DFS_FITS, recipe_name);
913 filename = product_name != NULL ? product_name : fallback_filename;
915 applist = mainlist == NULL
916 ? cpl_propertylist_new() : cpl_propertylist_duplicate(mainlist);
918 error = cpl_propertylist_update_string(applist,
"INSTRUME", instrume);
922 extlist, recipe_name, procatg, applist,
923 remregexp, pipe_id, filename);
925 cpl_propertylist_delete(applist);
926 cpl_free(fallback_filename);
929 cpl_ensure_code(!error, error);
931 return CPL_ERROR_NONE;
990 const cpl_frameset * useframes,
993 const cpl_parameterlist * parlist,
994 cpl_boolean (*table_set_row)
995 (cpl_table *,
const char *,
int,
997 const cpl_parameterlist *))
1000 const cpl_frame * rawframe;
1001 char * linebuffer = NULL;
1002 FILE * stream = NULL;
1004 int nrow = cpl_table_get_nrow(
self);
1006 cpl_errorstate prestate = cpl_errorstate_get();
1007 cpl_frameset_iterator * iterator = NULL;
1009 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
1010 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
1011 cpl_ensure_code(maxlinelen > 0, CPL_ERROR_ILLEGAL_INPUT);
1012 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
1013 cpl_ensure_code(table_set_row != NULL, CPL_ERROR_NULL_INPUT);
1015 linebuffer = cpl_malloc(maxlinelen);
1017 for (rawframe = irplib_frameset_get_first_const(&iterator, useframes);
1019 rawframe = irplib_frameset_get_next_const(iterator), nfiles++) {
1021 const char * rawfile = cpl_frame_get_filename(rawframe);
1023 const int irowpre = irow;
1027 if (rawfile == NULL)
break;
1029 stream = fopen(rawfile,
"r");
1031 if (stream == NULL) {
1032 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1033 cpl_error_set_message(cpl_func, CPL_ERROR_FILE_IO,
"Could not "
1034 "open %s for reading", rawfile);
1036 cpl_error_set_message(cpl_func, CPL_ERROR_FILE_IO,
"Could not "
1037 "open file for reading");
1042 for (;(done = fgets(linebuffer, maxlinelen, stream)) != NULL; iirow++) {
1044 if (linebuffer[0] != commentchar) {
1046 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1047 const int prerow = irow;
1051 nrow += nrow ? nrow : 1;
1052 if (cpl_table_set_size(
self, nrow))
break;
1055 didset = table_set_row(
self, linebuffer, irow, rawframe,
1059 if (!cpl_errorstate_is_equal(prestate)) {
1061 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1062 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1063 "Failed to set table row %d "
1064 "using line %d from %d. file %s",
1068 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1069 "Failure with line %d from %d. "
1073 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1074 "Failed to set table row"
1075 "using catalogue line");
1077 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1078 "Failure with catalogue line");
1085 if (done != NULL)
break;
1087 ierror = fclose(stream);
1092 if (irow == irowpre)
1093 cpl_msg_warning(cpl_func,
"No usable lines in the %d. file: %s",
1097 cpl_frameset_iterator_delete(iterator);
1098 cpl_free(linebuffer);
1099 if (stream != NULL) fclose(stream);
1102 cpl_ensure_code(rawframe == NULL, cpl_error_get_code());
1105 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1106 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1107 "No usable lines in the %d input "
1108 "frame(s)", nfiles);
1110 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1111 "No usable lines in the input frame(s)");
1116 cpl_ensure_code(!cpl_table_set_size(
self, irow), cpl_error_get_code());
1118 return CPL_ERROR_NONE;
1157 if (frame1==NULL || frame2==NULL)
return -1 ;
1160 if ((v1 = cpl_frame_get_tag(frame1)) == NULL)
return -1 ;
1161 if ((v2 = cpl_frame_get_tag(frame2)) == NULL)
return -1 ;
1164 if (strcmp(v1, v2))
return 0 ;
1188 const cpl_frame * frame = cpl_frameset_find_const(
self, tag);
1191 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1193 if (frame == NULL)
return NULL;
1195 if (cpl_frameset_find_const(
self, NULL))
1196 cpl_msg_warning(cpl_func,
1197 "Frameset has more than one file with tag: %s",
1200 return cpl_frame_get_filename(frame);
1217 cpl_frame_group group)
1219 const cpl_frame * frame;
1220 cpl_frameset_iterator * iterator = NULL;
1222 cpl_ensure(
self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1224 for (frame = irplib_frameset_get_first_const(&iterator,
self);
1226 frame = irplib_frameset_get_next_const(iterator)) {
1227 if (cpl_frame_get_group(frame) == group)
break;
1229 cpl_frameset_iterator_delete(iterator);
1254 int * ind,
int nfind)
1256 const int nsize = cpl_apertures_get_size(
self);
1260 cpl_ensure_code(nsize > 0, cpl_error_get_code());
1261 cpl_ensure_code(ind, CPL_ERROR_NULL_INPUT);
1262 cpl_ensure_code(nfind > 0, CPL_ERROR_ILLEGAL_INPUT);
1263 cpl_ensure_code(nfind <= nsize, CPL_ERROR_ILLEGAL_INPUT);
1265 for (ifind=0; ifind < nfind; ifind++) {
1266 double maxflux = -1;
1269 for (i=1; i <= nsize; i++) {
1273 for (k=0; k < ifind; k++)
if (ind[k] == i)
break;
1277 const double flux = cpl_apertures_get_flux(
self, i);
1279 if (maxind < 0 || flux > maxflux) {
1285 ind[ifind] = maxind;
1288 return CPL_ERROR_NONE;
1309 double irplib_data_get_double(
const void *
self, cpl_type type,
int i)
1316 case CPL_TYPE_FLOAT:
1318 const float * pself = (
const float*)
self;
1319 value = (double)pself[i];
1324 const int * pself = (
const int*)
self;
1325 value = (double)pself[i];
1330 const double * pself = (
const double*)
self;
1354 void irplib_data_set_double(
void *
self, cpl_type type,
int i,
double value)
1358 case CPL_TYPE_FLOAT:
1360 float * pself = (
float*)
self;
1361 pself[i] = (float)value;
1366 int * pself = (
int*)
self;
1367 pself[i] = (int)value;
1372 double * pself = (
double*)
self;
1396 void irplib_errorstate_dump_one_level(
void (*messenger)(
const char *,
1398 unsigned self,
unsigned first,
1402 const cpl_boolean is_reverse = first > last ? CPL_TRUE : CPL_FALSE;
1403 const unsigned newest = is_reverse ? first : last;
1404 const unsigned oldest = is_reverse ? last : first;
1405 const char * revmsg = is_reverse ?
" in reverse order" :
"";
1415 messenger(cpl_func,
"No error(s) to dump");
1422 if (
self == first) {
1424 messenger(cpl_func,
"Dumping all %u error(s)%s:", newest,
1427 messenger(cpl_func,
"Dumping the %u most recent error(s) "
1428 "out of a total of %u errors%s:",
1429 newest - oldest + 1, newest, revmsg);
1431 cpl_msg_indent_more();
1434 messenger(cpl_func,
"[%u/%u] '%s' (%u) at %s",
self, newest,
1435 cpl_error_get_message(), cpl_error_get_code(),
1436 cpl_error_get_where());
1438 if (
self == last) cpl_msg_indent_less();
1442 cpl_polynomial * irplib_polynomial_fit_1d_create_chiq(
1443 const cpl_vector * x_pos,
1444 const cpl_vector * values,
1449 return irplib_polynomial_fit_1d_create_common(x_pos, values, degree, NULL, rechisq);
1451 cpl_polynomial * irplib_polynomial_fit_1d_create(
1452 const cpl_vector * x_pos,
1453 const cpl_vector * values,
1459 return irplib_polynomial_fit_1d_create_common(x_pos, values, degree, mse, NULL);
1461 static cpl_polynomial * irplib_polynomial_fit_1d_create_common(
1462 const cpl_vector * x_pos,
1463 const cpl_vector * values,
1469 cpl_polynomial * fit1d = NULL;
1470 cpl_size loc_degree = (cpl_size)degree ;
1472 fit1d = cpl_polynomial_new(1);
1473 x_size = cpl_vector_get_size(x_pos);
1474 if(fit1d != NULL && x_size > 1)
1476 cpl_matrix * samppos = NULL;
1477 cpl_vector * fitresidual = NULL;
1478 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1479 samppos = cpl_matrix_wrap(1, x_size,
1480 (
double*)cpl_vector_get_data_const(x_pos));
1481 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1482 fitresidual = cpl_vector_new(x_size);
1483 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1484 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
1485 CPL_FALSE, NULL, &loc_degree);
1486 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1487 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL,
1488 fit1d, samppos, rechisq);
1489 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
1492 *mse = cpl_vector_product(fitresidual, fitresidual)
1493 / cpl_vector_get_size(fitresidual);
1495 cpl_matrix_unwrap(samppos);
1496 cpl_vector_delete(fitresidual);
1501 static void quicksort(
int* iindex,
double* exptime,
int left,
int right)
1505 int pivot = (i + j) / 2;
1506 double index_value = exptime[pivot];
1509 while(exptime[i] < index_value) i++;
1510 while(exptime[j] > index_value) j--;
1515 int tmp = iindex[i];
1516 double dtmp = exptime[i];
1517 iindex[i]=iindex[j];
1519 exptime[i] = exptime[j];
1529 quicksort(iindex, exptime, i, right);
1533 quicksort(iindex, exptime,left, j);
1536 cpl_error_code irplib_frameset_sort(
const cpl_frameset *
self,
int* iindex,
double* exptime)
1539 const cpl_frame* tmp_frame = 0;
1540 cpl_error_code error = CPL_ERROR_NONE;
1541 int sz = cpl_frameset_get_size(
self);
1542 cpl_frameset_iterator* iterator = NULL;
1545 tmp_frame = irplib_frameset_get_first_const(&iterator,
self);
1548 exptime[i] = frame_get_exptime(tmp_frame);
1550 tmp_frame = irplib_frameset_get_next_const(iterator);
1553 cpl_frameset_iterator_delete(iterator);
1555 quicksort(iindex, exptime, 0, sz - 1);
1560 static double frame_get_exptime(
const cpl_frame * pframe)
1563 cpl_propertylist * plist =
1564 cpl_propertylist_load_regexp(cpl_frame_get_filename(pframe), 0,
1565 "EXPTIME", CPL_FALSE);
1567 dval = cpl_propertylist_get_double(plist,
"EXPTIME");
1568 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1569 cpl_msg_error(cpl_func,
"error during reading EXPTIME key from "
1570 "the frame [%s]", cpl_frame_get_filename(pframe));
1574 cpl_propertylist_delete(plist);
1594 void * irplib_aligned_malloc(
size_t alignment,
size_t size)
1596 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
1597 return aligned_alloc(alignment, size);
1598 #elif defined HAVE_POSIX_MEMALIGN && defined HAVE_DECL_POSIX_MEMALIGN
1601 return malloc (size);
1602 if (alignment == 2 || (
sizeof (
void *) == 8 && alignment == 4))
1603 alignment =
sizeof (
void *);
1604 if (posix_memalign (&ptr, alignment, size) == 0)
1614 if (alignment & (alignment - 1)) {
1626 if (alignment < 2 *
sizeof (
void *))
1627 alignment = 2 *
sizeof (
void *);
1629 malloc_ptr = malloc (size + alignment);
1634 aligned_ptr = (
void *) (((
size_t) malloc_ptr + alignment)
1635 & ~((size_t) (alignment) - 1));
1638 *(((
void **) aligned_ptr) - 1) = malloc_ptr;
1656 void * irplib_aligned_calloc(
size_t alignment,
size_t nelem,
size_t nbytes)
1658 void * buffer = irplib_aligned_malloc(alignment, nelem * nbytes);
1662 memset((
size_t *)buffer, 0, nelem * nbytes);
1676 void irplib_aligned_free (
void * aligned_ptr)
1678 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
1680 #elif defined HAVE_POSIX_MEMALIGN && defined HAVE_DECL_POSIX_MEMALIGN
1684 free (*(((
void **) aligned_ptr) - 1));
1704 irplib_frameset_get_first_const(cpl_frameset_iterator **iterator,
1705 const cpl_frameset *frameset)
1707 cpl_ensure(iterator != NULL, CPL_ERROR_NULL_INPUT, NULL);
1708 *iterator = cpl_frameset_iterator_new(frameset);
1709 return cpl_frameset_iterator_get_const(*iterator);
1723 irplib_frameset_get_next_const(cpl_frameset_iterator *iterator)
1725 cpl_errorstate prestate = cpl_errorstate_get();
1726 cpl_error_code error = cpl_frameset_iterator_advance(iterator, 1);
1727 if (error == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
1728 cpl_errorstate_set(prestate);
1730 }
else if (error != CPL_ERROR_NONE) {
1733 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.