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 #include <cxstring.h>
00033 #include <cxstrutils.h>
00034
00035 #include <cpl_error.h>
00036
00037 #include "gialias.h"
00038 #include "giutils.h"
00039 #include "giframe.h"
00040 #include "gierror.h"
00041
00042
00052 inline static cxint
00053 _giraffe_frame_update_product(cpl_propertylist* properties,
00054 const cxchar* name, const cxchar* tag,
00055 const cxchar* type, const cxchar* tech)
00056 {
00057
00058 cxchar* dpr_tech = NULL;
00059
00060 cxint science = -1;
00061
00062
00063 if (properties == NULL) {
00064 return -1;
00065 }
00066
00067
00068
00069
00070
00071
00072 if (type == NULL) {
00073 type = "REDUCED";
00074 }
00075
00076 if (tech == NULL) {
00077
00078 if (cpl_propertylist_has(properties, GIALIAS_DPRTECH) == TRUE) {
00079
00080 const cxchar* s = cpl_propertylist_get_string(properties,
00081 GIALIAS_DPRTECH);
00082
00083 dpr_tech = cx_strdup(s);
00084
00085 }
00086 else if (cpl_propertylist_has(properties, GIALIAS_PROTECH) == TRUE) {
00087
00088 const cxchar* s = cpl_propertylist_get_string(properties,
00089 GIALIAS_PROTECH);
00090
00091 dpr_tech = cx_strdup(s);
00092
00093 }
00094
00095 if ((dpr_tech == NULL) || (dpr_tech[0] == '\0')) {
00096 tech = "UNDEFINED";
00097 }
00098 else {
00099 tech = dpr_tech;
00100 }
00101
00102 }
00103
00104
00105
00106
00107
00108
00109 if (cpl_propertylist_has(properties, GIALIAS_PROSCIENCE) == FALSE) {
00110
00111 if (cpl_propertylist_has(properties, GIALIAS_DPRCATG) == TRUE) {
00112
00113 const cxchar* s = cpl_propertylist_get_string(properties,
00114 GIALIAS_DPRCATG);
00115
00116 if (s != NULL) {
00117 science = (strncmp(s, "SCIENCE", 7) == 0) ? 1 : 0;
00118 }
00119
00120 }
00121
00122 }
00123
00124
00125
00126
00127
00128
00129 cpl_propertylist_erase(properties, GIALIAS_ORIGIN);
00130 cpl_propertylist_erase(properties, GIALIAS_DATE);
00131 cpl_propertylist_erase(properties, GIALIAS_DATAMD5);
00132 cpl_propertylist_erase(properties, GIALIAS_ORIGFILE);
00133 cpl_propertylist_erase(properties, GIALIAS_ARCFILE);
00134 cpl_propertylist_erase(properties, GIALIAS_CHECKSUM);
00135 cpl_propertylist_erase(properties, GIALIAS_DATASUM);
00136
00137 cpl_propertylist_erase_regexp(properties, "ESO DPR.*", 0);
00138
00139
00140 giraffe_error_push();
00141
00142 cpl_propertylist_update_string(properties, GIALIAS_INSTRUMENT,
00143 "GIRAFFE");
00144 cpl_propertylist_set_comment(properties, GIALIAS_INSTRUMENT,
00145 "Name of the Instrument.");
00146
00147 cpl_propertylist_update_string(properties, GIALIAS_DATAMD5,
00148 "Not computed");
00149 cpl_propertylist_set_comment(properties, GIALIAS_DATAMD5,
00150 "MD5 checksum");
00151
00152 if (name != NULL) {
00153 cpl_propertylist_update_string(properties, GIALIAS_PIPEFILE, name);
00154 cpl_propertylist_set_comment(properties, GIALIAS_PIPEFILE,
00155 "Filename of data product");
00156 }
00157
00158 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00159
00160 cx_free(dpr_tech);
00161 return 1;
00162
00163 }
00164
00165 giraffe_error_pop();
00166
00167
00168
00169
00170
00171
00172 giraffe_error_push();
00173
00174 cpl_propertylist_update_string(properties, GIALIAS_PRODID, PRODUCT_DID);
00175 cpl_propertylist_set_comment(properties, GIALIAS_PRODID,
00176 "Data dictionary for PRO");
00177
00178 if (tag != NULL) {
00179
00180 cpl_propertylist_update_string(properties, GIALIAS_PROCATG, tag);
00181 cpl_propertylist_set_comment(properties, GIALIAS_PROCATG,
00182 "Pipeline product category");
00183
00184 }
00185
00186 cpl_propertylist_update_string(properties, GIALIAS_PROTYPE, type);
00187 cpl_propertylist_set_comment(properties, GIALIAS_PROTYPE,
00188 "Product type");
00189
00190 cpl_propertylist_update_string(properties, GIALIAS_PROTECH, tech);
00191 cpl_propertylist_set_comment(properties, GIALIAS_PROTECH,
00192 "Observation technique");
00193 cx_free(dpr_tech);
00194 dpr_tech = NULL;
00195
00196 if (science != -1) {
00197 cpl_propertylist_update_bool(properties, GIALIAS_PROSCIENCE, science);
00198 cpl_propertylist_set_comment(properties, GIALIAS_PROSCIENCE,
00199 "Scientific product if T");
00200 }
00201
00202 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00203 return 1;
00204 }
00205
00206 giraffe_error_pop();
00207
00208 return 0;
00209
00210 }
00211
00212
00244 cpl_frame *
00245 giraffe_frame_create(const cxchar *tag, cpl_frame_level level,
00246 const cpl_propertylist *properties, cxcptr object,
00247 cxcptr data, GiFrameCreator creator)
00248 {
00249
00250 const cxchar *const fctid = "giraffe_frame_create";
00251
00252 cxint status = 0;
00253
00254 cx_string *name = NULL;
00255
00256 cpl_propertylist *p = NULL;
00257
00258 cpl_frame *frame = NULL;
00259
00260
00261 if (properties == NULL || creator == NULL) {
00262 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00263 return NULL;
00264 }
00265
00266 if (tag == NULL) {
00267 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00268 return NULL;
00269 }
00270
00271 p = cpl_propertylist_duplicate(properties);
00272 cx_assert(p != NULL);
00273
00274
00275
00276
00277
00278 name = cx_string_create(tag);
00279 cx_assert(name != NULL);
00280
00281 cx_string_lower(name);
00282 cx_string_append(name, ".fits");
00283
00284 _giraffe_frame_update_product(p, cx_string_get(name), tag, "REDUCED",
00285 NULL);
00286
00287 frame = cpl_frame_new();
00288 cpl_frame_set_filename(frame, cx_string_get(name));
00289 cpl_frame_set_tag(frame, tag);
00290 cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
00291 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
00292 cpl_frame_set_level(frame, level);
00293
00294
00295
00296
00297
00298 status = creator(object, p, cx_string_get(name), data);
00299
00300 if (status != 0) {
00301 cpl_frame_delete(frame);
00302 frame = NULL;
00303 }
00304
00305 cx_string_delete(name);
00306 cpl_propertylist_delete(p);
00307
00308 return frame;
00309
00310 }
00311
00331 cxint
00332 giraffe_frame_save(const cpl_frame* frame,
00333 const cpl_propertylist* properties,
00334 cxcptr object, cxcptr data,
00335 GiFrameCreator creator)
00336 {
00337
00338 const cxchar* const fctid = "giraffe_frame_save";
00339
00340 cxint status = 0;
00341
00342 cpl_propertylist* p = NULL;
00343
00344
00345 if (properties == NULL || creator == NULL) {
00346 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00347 return 127;
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357 p = cpl_propertylist_duplicate(properties);
00358 cx_assert(p != NULL);
00359
00360
00361
00362
00363
00364
00365 status = creator(object, p, cpl_frame_get_filename(frame), data);
00366
00367 cpl_propertylist_delete(p);
00368 p = NULL;
00369
00370 return status;
00371
00372 }
00373
00374
00400 cpl_frame *
00401 giraffe_frame_create_image(GiImage *image, const cxchar *tag,
00402 cpl_frame_level level, cxbool save,
00403 cxbool update)
00404 {
00405 const cxchar *fctid = "giraffe_frame_create_image";
00406
00407 cx_string *name;
00408
00409 cpl_frame *frame;
00410
00411 cpl_propertylist *properties;
00412
00413
00414 if (image == NULL || tag == NULL) {
00415 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00416 return NULL;
00417 }
00418
00419 properties = giraffe_image_get_properties(image);
00420
00421 if (properties == NULL) {
00422 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00423 return NULL;
00424 }
00425
00426 if (update == TRUE) {
00427
00428 cpl_image *_image = giraffe_image_get(image);
00429
00430
00431 if (_image == NULL) {
00432 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00433 return NULL;
00434 }
00435
00436 cpl_propertylist_update_double(properties, GIALIAS_DATAMIN,
00437 cpl_image_get_min(_image));
00438 cpl_propertylist_set_comment(properties, GIALIAS_DATAMIN,
00439 "Minimal pixel value");
00440
00441 cpl_propertylist_update_double(properties, GIALIAS_DATAMAX,
00442 cpl_image_get_max(_image));
00443 cpl_propertylist_set_comment(properties, GIALIAS_DATAMAX,
00444 "Maximum pixel value");
00445
00446 cpl_propertylist_update_double(properties, GIALIAS_DATAMEAN,
00447 cpl_image_get_mean(_image));
00448 cpl_propertylist_set_comment(properties, GIALIAS_DATAMEAN,
00449 "Mean of pixel values");
00450
00451 cpl_propertylist_update_double(properties, GIALIAS_DATASIG,
00452 cpl_image_get_stdev(_image));
00453 cpl_propertylist_set_comment(properties, GIALIAS_DATASIG,
00454 "Standard deviation of pixel values");
00455
00456 cpl_propertylist_update_double(properties, GIALIAS_DATAMEDI,
00457 cpl_image_get_median(_image));
00458 cpl_propertylist_set_comment(properties, GIALIAS_DATAMEDI,
00459 "Median of pixel values");
00460
00461 cpl_propertylist_update_int(properties, GIALIAS_NAXIS1,
00462 cpl_image_get_size_x(_image));
00463 cpl_propertylist_update_int(properties, GIALIAS_NAXIS2,
00464 cpl_image_get_size_y(_image));
00465
00466 }
00467
00468
00469
00470
00471
00472
00473 name = cx_string_create(tag);
00474 cx_string_lower(name);
00475 cx_string_append(name, ".fits");
00476
00477 _giraffe_frame_update_product(properties, cx_string_get(name), tag,
00478 "REDUCED", NULL);
00479
00480 frame = cpl_frame_new();
00481 cpl_frame_set_filename(frame, cx_string_get(name));
00482 cpl_frame_set_tag(frame, tag);
00483 cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
00484 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
00485 cpl_frame_set_level(frame, level);
00486
00487 cx_string_delete(name);
00488
00489
00490
00491
00492
00493
00494 if (save == TRUE) {
00495
00496 cxint status = 0;
00497
00498 status = giraffe_image_save(image, cpl_frame_get_filename(frame));
00499
00500 if (status) {
00501 cpl_error_set(fctid, CPL_ERROR_FILE_IO);
00502 cpl_frame_delete(frame);
00503
00504 return NULL;
00505 }
00506
00507 }
00508
00509 return frame;
00510
00511 }
00512
00513
00539 cpl_frame *
00540 giraffe_frame_create_table(GiTable *table, const cxchar *tag,
00541 cpl_frame_level level, cxbool save,
00542 cxbool update)
00543 {
00544
00545 const cxchar *fctid = "giraffe_frame_create_table";
00546
00547 cx_string *name;
00548
00549 cpl_frame *frame;
00550
00551 cpl_propertylist *properties;
00552
00553
00554 if (table == NULL || tag == NULL) {
00555 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00556 return NULL;
00557 }
00558
00559 properties = giraffe_table_get_properties(table);
00560
00561 if (properties == NULL) {
00562 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00563 return NULL;
00564 }
00565
00566 if (update == TRUE) {
00567
00568 cpl_propertylist_update_string(properties, GIALIAS_EXTNAME, tag);
00569 cpl_propertylist_set_comment(properties, GIALIAS_EXTNAME,
00570 "FITS Extension name");
00571
00572 }
00573
00574
00575
00576
00577
00578
00579 name = cx_string_create(tag);
00580 cx_string_lower(name);
00581 cx_string_append(name, ".fits");
00582
00583 _giraffe_frame_update_product(properties, cx_string_get(name), tag,
00584 "REDUCED", NULL);
00585
00586 frame = cpl_frame_new();
00587 cpl_frame_set_filename(frame, cx_string_get(name));
00588 cpl_frame_set_tag(frame, tag);
00589 cpl_frame_set_type(frame, CPL_FRAME_TYPE_TABLE);
00590 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
00591 cpl_frame_set_level(frame, level);
00592
00593 cx_string_delete(name);
00594
00595
00596
00597
00598
00599
00600 if (save == TRUE) {
00601
00602 cxint status = 0;
00603
00604 status = giraffe_table_save(table, cpl_frame_get_filename(frame));
00605
00606 if (status) {
00607 cpl_error_set(fctid, CPL_ERROR_FILE_IO);
00608 cpl_frame_delete(frame);
00609
00610 return NULL;
00611 }
00612
00613 }
00614
00615 return frame;
00616
00617 }
00618
00619
00641 cxint
00642 giraffe_frame_attach_table(cpl_frame *frame, GiTable *table,
00643 const cxchar *tag, cxbool update)
00644 {
00645
00646 const cxchar *fctid = "giraffe_frame_attach_table";
00647
00648
00649 cxchar *name;
00650
00651 cxint status = 0;
00652
00653 cpl_propertylist *properties;
00654
00655 cpl_table *data;
00656
00657
00658
00659 if (frame == NULL || table == NULL || tag == NULL) {
00660 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00661 return 1;
00662 }
00663
00664 name = (cxchar *)cpl_frame_get_filename(frame);
00665
00666 if (name == NULL) {
00667 cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00668 return 1;
00669 }
00670
00671 properties = giraffe_table_get_properties(table);
00672
00673 if (properties == NULL) {
00674 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00675 return 1;
00676 }
00677
00678 data = giraffe_table_get(table);
00679
00680 if (data == NULL) {
00681 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00682 return 1;
00683 }
00684
00685
00686 if (update == TRUE) {
00687
00688
00689 ;
00690
00691 }
00692
00693
00694
00695
00696
00697
00698 cpl_propertylist_update_string(properties, GIALIAS_EXTNAME, tag);
00699 cpl_propertylist_set_comment(properties, GIALIAS_EXTNAME,
00700 "FITS Extension name");
00701
00702
00703
00704
00705
00706
00707 status = cpl_table_save(data, NULL, properties, name, CPL_IO_EXTEND);
00708
00709 if (status != CPL_ERROR_NONE) {
00710 cpl_error_set(fctid, CPL_ERROR_FILE_IO);
00711 return 1;
00712 }
00713
00714 return 0;
00715
00716 }
00717
00718
00735 cpl_frame *
00736 giraffe_get_frame(const cpl_frameset *set, const cxchar *tag,
00737 cpl_frame_group group)
00738 {
00739
00740 cpl_frameset* _set = (cpl_frameset*)set;
00741
00742 cpl_frame *frame = NULL;
00743
00744
00745 if (_set == NULL || tag == NULL) {
00746 return NULL;
00747 }
00748
00749 frame = cpl_frameset_find(_set, tag);
00750
00751 if (group != CPL_FRAME_GROUP_NONE) {
00752
00753 while (frame != NULL && cpl_frame_get_group(frame) != group) {
00754 frame = cpl_frameset_find(_set, NULL);
00755 }
00756
00757 }
00758
00759 return frame;
00760
00761 }
00762
00763
00782 cpl_frame *
00783 giraffe_get_slitgeometry(const cpl_frameset *set)
00784 {
00785
00786 cpl_frameset* _set = (cpl_frameset*)set;
00787
00788 cpl_frame *frame = NULL;
00789
00790
00791 if (_set == 0) {
00792 return 0;
00793 }
00794
00795 frame = cpl_frameset_find(_set, GIFRAME_SLITSETUP);
00796 frame = frame == 0 ? cpl_frameset_find(_set, GIFRAME_SLITMASTER) : frame;
00797 frame = frame == 0 ? cpl_frameset_find(_set, GIFRAME_SLITGEOMETRY) : frame;
00798
00799 return frame;
00800
00801 }