00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <math.h>
00037
00038 #include <string.h>
00039 #include <assert.h>
00040
00041 #include <cpl.h>
00042
00043 #include "irplib_utils.h"
00044
00045
00046
00047
00048
00049 #if defined HAVE_ISNAN && HAVE_ISNAN != 0
00050 #if !defined isnan && defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 0
00051
00052
00053 int isnan(double);
00054 #endif
00055 #endif
00056
00057
00061
00064
00112
00113
00114 cpl_error_code
00115 irplib_dfs_table_convert(cpl_table * self,
00116 cpl_frameset * allframes,
00117 const cpl_frameset * useframes,
00118 int maxlinelen,
00119 char commentchar,
00120 const char * product_name,
00121 const char * procatg,
00122 const cpl_parameterlist * parlist,
00123 const char * recipe_name,
00124 const cpl_propertylist * mainlist,
00125 const cpl_propertylist * extlist,
00126 const char * remregexp,
00127 const char * instrume,
00128 const char * pipe_id,
00129 cpl_boolean (*table_set_row)
00130 (cpl_table *, const char *, int,
00131 const cpl_frame *,
00132 const cpl_parameterlist *),
00133 cpl_error_code (*table_check)
00134 (cpl_table *,
00135 const cpl_frameset *,
00136 const cpl_parameterlist *))
00137 {
00138
00139 const char * filename;
00140 cpl_propertylist * applist = NULL;
00141 cpl_errorstate prestate = cpl_errorstate_get();
00142 cpl_error_code error;
00143
00144 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
00145 cpl_ensure_code(allframes != NULL, CPL_ERROR_NULL_INPUT);
00146 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
00147 cpl_ensure_code(procatg != NULL, CPL_ERROR_NULL_INPUT);
00148 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
00149 cpl_ensure_code(recipe_name != NULL, CPL_ERROR_NULL_INPUT);
00150 cpl_ensure_code(instrume != NULL, CPL_ERROR_NULL_INPUT);
00151 cpl_ensure_code(pipe_id != NULL, CPL_ERROR_NULL_INPUT);
00152
00153 cpl_ensure_code(!irplib_table_read_from_frameset(self, useframes,
00154 maxlinelen,
00155 commentchar,
00156 parlist,
00157 table_set_row),
00158 cpl_error_get_code());
00159
00160 if (table_check != NULL && (table_check(self, useframes, parlist) ||
00161 !cpl_errorstate_is_equal(prestate))) {
00162 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
00163 "Consistency check of table failed");
00164 }
00165
00166 filename = product_name != NULL
00167 ? product_name : cpl_sprintf("%s" CPL_DFS_FITS, recipe_name);
00168
00169 applist = mainlist == NULL
00170 ? cpl_propertylist_new() : cpl_propertylist_duplicate(mainlist);
00171
00172 error = cpl_propertylist_append_string(applist, "INSTRUME", instrume);
00173
00174 if (!error)
00175 error = cpl_dfs_save_table(allframes, parlist, useframes, self,
00176 extlist, recipe_name, procatg, applist,
00177 remregexp, pipe_id, filename);
00178
00179 cpl_propertylist_delete(applist);
00180 if (filename != product_name) cpl_free((char*)filename);
00181
00182
00183 cpl_ensure_code(!error, error);
00184
00185 return CPL_ERROR_NONE;
00186
00187 }
00188
00189
00190
00191
00240
00241
00242 cpl_error_code
00243 irplib_table_read_from_frameset(cpl_table * self,
00244 const cpl_frameset * useframes,
00245 int maxlinelen,
00246 char commentchar,
00247 const cpl_parameterlist * parlist,
00248 cpl_boolean (*table_set_row)
00249 (cpl_table *, const char *, int,
00250 const cpl_frame *,
00251 const cpl_parameterlist *))
00252 {
00253
00254 const cpl_frame * rawframe;
00255 char * linebuffer = NULL;
00256 FILE * stream = NULL;
00257 int nfiles = 0;
00258 int nrow = cpl_table_get_nrow(self);
00259 int irow = 0;
00260 cpl_errorstate prestate = cpl_errorstate_get();
00261
00262 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
00263 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
00264 cpl_ensure_code(maxlinelen > 0, CPL_ERROR_ILLEGAL_INPUT);
00265 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
00266 cpl_ensure_code(table_set_row != NULL, CPL_ERROR_NULL_INPUT);
00267
00268 linebuffer = cpl_malloc(maxlinelen);
00269
00270 for (rawframe = cpl_frameset_get_first_const(useframes);
00271 rawframe != NULL;
00272 rawframe = cpl_frameset_get_next_const(useframes), nfiles++) {
00273
00274 const char * rawfile = cpl_frame_get_filename(rawframe);
00275 const char * done;
00276 const int irowpre = irow;
00277 int iirow = 0;
00278 int ierror;
00279
00280 if (rawfile == NULL) break;
00281
00282 stream = fopen(rawfile, "r");
00283
00284 if (stream == NULL) {
00285 cpl_error_set_message(cpl_func, CPL_ERROR_FILE_IO, "Could not "
00286 "open %s for reading", rawfile);
00287 break;
00288 }
00289
00290 for (;(done = fgets(linebuffer, maxlinelen, stream)) != NULL; iirow++) {
00291
00292 if (linebuffer[0] != commentchar) {
00293 cpl_boolean didset;
00294 const int prerow = irow;
00295
00296 if (irow == nrow) {
00297 nrow += nrow ? nrow : 1;
00298 if (cpl_table_set_size(self, nrow)) break;
00299 }
00300
00301 didset = table_set_row(self, linebuffer, irow, rawframe,
00302 parlist);
00303 if (didset) irow++;
00304
00305 if (!cpl_errorstate_is_equal(prestate)) {
00306 if (didset)
00307 cpl_error_set_message(cpl_func, cpl_error_get_code(),
00308 "Failed to set table row %d "
00309 "using line %d from %d. file %s",
00310 1+prerow, iirow+1,
00311 nfiles+1, rawfile);
00312 else
00313 cpl_error_set_message(cpl_func, cpl_error_get_code(),
00314 "Failure with line %d from %d. "
00315 "file %s", iirow+1,
00316 nfiles+1, rawfile);
00317
00318 break;
00319 }
00320 }
00321 }
00322 if (done != NULL) break;
00323
00324 ierror = fclose(stream);
00325 stream = NULL;
00326 if (ierror) break;
00327
00328
00329 if (irow == irowpre)
00330 cpl_msg_warning(cpl_func, "No usable lines in the %d. file: %s",
00331 1+nfiles, rawfile);
00332 }
00333
00334 cpl_free(linebuffer);
00335 if (stream != NULL) fclose(stream);
00336
00337
00338 cpl_ensure_code(rawframe == NULL, cpl_error_get_code());
00339
00340 if (irow == 0) {
00341 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00342 "No usable lines in the %d input "
00343 "frame(s)", nfiles);
00344 }
00345
00346
00347 cpl_ensure_code(!cpl_table_set_size(self, irow), cpl_error_get_code());
00348
00349 return CPL_ERROR_NONE;
00350 }
00351
00352
00353
00354
00366
00367 void irplib_reset(void)
00368 {
00369 return;
00370 }
00371
00372
00379
00380 int irplib_compare_tags(
00381 cpl_frame * frame1,
00382 cpl_frame * frame2)
00383 {
00384 char * v1 ;
00385 char * v2 ;
00386
00387
00388 if (frame1==NULL || frame2==NULL) return -1 ;
00389
00390
00391 if ((v1 = (char*)cpl_frame_get_tag(frame1)) == NULL) return -1 ;
00392 if ((v2 = (char*)cpl_frame_get_tag(frame2)) == NULL) return -1 ;
00393
00394
00395 if (strcmp(v1, v2)) return 0 ;
00396 else return 1 ;
00397 }
00398
00399
00415
00416 const char * irplib_frameset_find_file(const cpl_frameset * self,
00417 const char * tag)
00418 {
00419 const cpl_frame * frame = cpl_frameset_find_const(self, tag);
00420
00421
00422 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
00423
00424 if (frame == NULL) return NULL;
00425
00426 if (cpl_frameset_find_const(self, NULL))
00427 cpl_msg_warning(cpl_func,
00428 "Frameset has more than one file with tag: %s",
00429 tag);
00430
00431 return cpl_frame_get_filename(frame);
00432
00433 }
00434
00435
00445
00446 cpl_frame * irplib_frameset_get_first_from_group(
00447 const cpl_frameset * self,
00448 cpl_frame_group group)
00449 {
00450 const cpl_frame * frame;
00451 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
00452
00453 for (frame = cpl_frameset_get_first_const(self); frame != NULL ;
00454 frame = cpl_frameset_get_next_const(self)) {
00455 if (cpl_frame_get_group(frame) == group) break;
00456 }
00457 return (cpl_frame*)frame;
00458 }
00459
00460
00479
00480 cpl_error_code irplib_apertures_find_max_flux(const cpl_apertures * self,
00481 int * ind, int nfind)
00482 {
00483 const int nsize = cpl_apertures_get_size(self);
00484 int ifind;
00485
00486
00487 cpl_ensure_code(nsize > 0, cpl_error_get_code());
00488 cpl_ensure_code(ind, CPL_ERROR_NULL_INPUT);
00489 cpl_ensure_code(nfind > 0, CPL_ERROR_ILLEGAL_INPUT);
00490 cpl_ensure_code(nfind <= nsize, CPL_ERROR_ILLEGAL_INPUT);
00491
00492 for (ifind=0; ifind < nfind; ifind++) {
00493 double maxflux = -1;
00494 int maxind = -1;
00495 int i;
00496 for (i=1; i <= nsize; i++) {
00497 int k;
00498
00499
00500 for (k=0; k < ifind; k++) if (ind[k] == i) break;
00501
00502 if (k == ifind) {
00503
00504 const double flux = cpl_apertures_get_flux(self, i);
00505
00506 if (maxind < 0 || flux > maxflux) {
00507 maxind = i;
00508 maxflux = flux;
00509 }
00510 }
00511 }
00512 ind[ifind] = maxind;
00513 }
00514
00515 return CPL_ERROR_NONE;
00516
00517 }
00518
00519
00523
00524 int irplib_isinf(double value)
00525 {
00526 #if defined HAVE_ISINF && HAVE_ISINF
00527 return isinf(value);
00528 #else
00529 return value != 0 && value == 2 * value;
00530 #endif
00531 }
00532
00533
00537
00538 int irplib_isnan(double value)
00539 {
00540 #if defined HAVE_ISNAN && HAVE_ISNAN
00541 return isnan(value);
00542 #else
00543 return value != value;
00544 #endif
00545 }
00546
00547
00548
00549
00560
00561 void irplib_errorstate_warning(unsigned self, unsigned first, unsigned last)
00562 {
00563
00564 const cpl_boolean is_reverse = first > last ? CPL_TRUE : CPL_FALSE;
00565 const unsigned newest = is_reverse ? first : last;
00566 const unsigned oldest = is_reverse ? last : first;
00567 const char * revmsg = is_reverse ? " in reverse order" : "";
00568
00569
00570 assert( oldest <= self );
00571 assert( newest >= self );
00572
00573 if (newest == 0) {
00574 cpl_msg_info(cpl_func, "No error(s) to dump");
00575 assert( oldest == 0);
00576 } else {
00577 assert( oldest > 0);
00578 assert( newest >= oldest);
00579 if (self == first) {
00580 if (oldest == 1) {
00581 cpl_msg_warning(cpl_func, "Dumping all %u error(s)%s:", newest,
00582 revmsg);
00583 } else {
00584 cpl_msg_warning(cpl_func, "Dumping the %u most recent error(s) "
00585 "out of a total of %u errors%s:",
00586 newest - oldest + 1, newest, revmsg);
00587 }
00588 cpl_msg_indent_more();
00589 }
00590
00591 cpl_msg_warning(cpl_func, "[%u/%u] '%s' (%u) at %s", self, newest,
00592 cpl_error_get_message(), cpl_error_get_code(),
00593 cpl_error_get_where());
00594
00595 if (self == last) cpl_msg_indent_less();
00596 }
00597 }
00598
00599