GIRAFFE Pipeline Reference Manual

giframe.c

00001 /* $Id: giframe.c,v 1.10.2.1 2008/02/22 08:26:35 rpalsa Exp $
00002  *
00003  * This file is part of the GIRAFFE Pipeline
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author: rpalsa $
00023  * $Date: 2008/02/22 08:26:35 $
00024  * $Revision: 1.10.2.1 $
00025  * $Name: giraffe-2_5_1 $
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      * Set default values
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      * Check whether a scientific observation was processed.
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      * Clean frame properties
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      * Set product keywords
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 || object == 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      * Update the property list
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      * Write object to file
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 
00312 
00338 cpl_frame *
00339 giraffe_frame_create_image(GiImage *image, const cxchar *tag,
00340                            cpl_frame_level level, cxbool save,
00341                            cxbool update)
00342 {
00343     const cxchar *fctid = "giraffe_frame_create_image";
00344 
00345     cx_string *name;
00346 
00347     cpl_frame *frame;
00348 
00349     cpl_propertylist *properties;
00350 
00351 
00352     if (image == NULL || tag == NULL) {
00353         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00354         return NULL;
00355     }
00356 
00357     properties = giraffe_image_get_properties(image);
00358 
00359     if (properties == NULL) {
00360         cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00361         return NULL;
00362     }
00363 
00364     if (update == TRUE) {
00365 
00366         cpl_image *_image = giraffe_image_get(image);
00367 
00368 
00369         if (_image == NULL) {
00370             cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00371             return NULL;
00372         }
00373 
00374         cpl_propertylist_update_double(properties, GIALIAS_DATAMIN,
00375                                        cpl_image_get_min(_image));
00376         cpl_propertylist_set_comment(properties, GIALIAS_DATAMIN,
00377                                      "Minimal pixel value");
00378 
00379         cpl_propertylist_update_double(properties, GIALIAS_DATAMAX,
00380                                        cpl_image_get_max(_image));
00381         cpl_propertylist_set_comment(properties, GIALIAS_DATAMAX,
00382                                      "Maximum pixel value");
00383 
00384         cpl_propertylist_update_double(properties, GIALIAS_DATAMEAN,
00385                                        cpl_image_get_mean(_image));
00386         cpl_propertylist_set_comment(properties, GIALIAS_DATAMEAN,
00387                                      "Mean of pixel values");
00388 
00389         cpl_propertylist_update_double(properties, GIALIAS_DATASIG,
00390                                        cpl_image_get_stdev(_image));
00391         cpl_propertylist_set_comment(properties, GIALIAS_DATASIG,
00392                                      "Standard deviation of pixel values");
00393 
00394         cpl_propertylist_update_double(properties, GIALIAS_DATAMEDI,
00395                                        cpl_image_get_median(_image));
00396         cpl_propertylist_set_comment(properties, GIALIAS_DATAMEDI,
00397                                      "Median of pixel values");
00398 
00399         cpl_propertylist_update_int(properties, GIALIAS_NAXIS1,
00400                                     cpl_image_get_size_x(_image));
00401         cpl_propertylist_update_int(properties, GIALIAS_NAXIS2,
00402                                     cpl_image_get_size_y(_image));
00403 
00404     }
00405 
00406 
00407     /*
00408      * Update the property list
00409      */
00410 
00411     name = cx_string_create(tag);
00412     cx_string_lower(name);
00413     cx_string_append(name, ".fits");
00414 
00415     _giraffe_frame_update_product(properties, cx_string_get(name), tag,
00416                                   "REDUCED", NULL);
00417 
00418     frame = cpl_frame_new();
00419     cpl_frame_set_filename(frame, cx_string_get(name));
00420     cpl_frame_set_tag(frame, tag);
00421     cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
00422     cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
00423     cpl_frame_set_level(frame, level);
00424 
00425     cx_string_delete(name);
00426 
00427 
00428     /*
00429      * Write image to disk
00430      */
00431 
00432     if (save == TRUE) {
00433 
00434         cxint status = 0;
00435 
00436         status = giraffe_image_save(image, cpl_frame_get_filename(frame));
00437 
00438         if (status) {
00439             cpl_error_set(fctid, CPL_ERROR_FILE_IO);
00440             cpl_frame_delete(frame);
00441 
00442             return NULL;
00443         }
00444 
00445     }
00446 
00447     return frame;
00448 
00449 }
00450 
00451 
00477 cpl_frame *
00478 giraffe_frame_create_table(GiTable *table, const cxchar *tag,
00479                            cpl_frame_level level, cxbool save,
00480                            cxbool update)
00481 {
00482 
00483     const cxchar *fctid = "giraffe_frame_create_table";
00484 
00485     cx_string *name;
00486 
00487     cpl_frame *frame;
00488 
00489     cpl_propertylist *properties;
00490 
00491 
00492     if (table == NULL || tag == NULL) {
00493         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00494         return NULL;
00495     }
00496 
00497     properties = giraffe_table_get_properties(table);
00498 
00499     if (properties == NULL) {
00500         cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00501         return NULL;
00502     }
00503 
00504     if (update == TRUE) {
00505 
00506         cpl_propertylist_update_string(properties, GIALIAS_EXTNAME, tag);
00507         cpl_propertylist_set_comment(properties, GIALIAS_EXTNAME,
00508                                      "FITS Extension name");
00509 
00510     }
00511 
00512 
00513     /*
00514      * Update the property list
00515      */
00516 
00517     name = cx_string_create(tag);
00518     cx_string_lower(name);
00519     cx_string_append(name, ".fits");
00520 
00521     _giraffe_frame_update_product(properties, cx_string_get(name), tag,
00522                                   "REDUCED", NULL);
00523 
00524     frame = cpl_frame_new();
00525     cpl_frame_set_filename(frame, cx_string_get(name));
00526     cpl_frame_set_tag(frame, tag);
00527     cpl_frame_set_type(frame, CPL_FRAME_TYPE_TABLE);
00528     cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
00529     cpl_frame_set_level(frame, level);
00530 
00531     cx_string_delete(name);
00532 
00533 
00534     /*
00535      * Write table to disk
00536      */
00537 
00538     if (save == TRUE) {
00539 
00540         cxint status = 0;
00541 
00542         status = giraffe_table_save(table, cpl_frame_get_filename(frame));
00543 
00544         if (status) {
00545             cpl_error_set(fctid, CPL_ERROR_FILE_IO);
00546             cpl_frame_delete(frame);
00547 
00548             return NULL;
00549         }
00550 
00551     }
00552 
00553     return frame;
00554 
00555 }
00556 
00557 
00579 cxint
00580 giraffe_frame_attach_table(cpl_frame *frame, GiTable *table,
00581                            const cxchar *tag, cxbool update)
00582 {
00583 
00584     const cxchar *fctid = "giraffe_frame_attach_table";
00585 
00586 
00587     cxchar *name;
00588 
00589     cxint status = 0;
00590 
00591     cpl_propertylist *properties;
00592 
00593     cpl_table *data;
00594 
00595 
00596 
00597     if (frame == NULL || table == NULL || tag == NULL) {
00598         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00599         return 1;
00600     }
00601 
00602     name = (cxchar *)cpl_frame_get_filename(frame);
00603 
00604     if (name == NULL) {
00605         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00606         return 1;
00607     }
00608 
00609     properties = giraffe_table_get_properties(table);
00610 
00611     if (properties == NULL) {
00612         cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00613         return 1;
00614     }
00615 
00616     data = giraffe_table_get(table);
00617 
00618     if (data == NULL) {
00619         cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00620         return 1;
00621     }
00622 
00623 
00624     if (update == TRUE) {
00625 
00626         /* For future extensions */
00627         ;
00628 
00629     }
00630 
00631 
00632     /*
00633      * Update the property list
00634      */
00635 
00636     cpl_propertylist_update_string(properties, GIALIAS_EXTNAME, tag);
00637     cpl_propertylist_set_comment(properties, GIALIAS_EXTNAME,
00638                                  "FITS Extension name");
00639 
00640 
00641     /*
00642      * Write table to disk
00643      */
00644 
00645     status = cpl_table_save(data, NULL, properties, name, 1);
00646 
00647     if (status != CPL_ERROR_NONE) {
00648         cpl_error_set(fctid, CPL_ERROR_FILE_IO);
00649         return 1;
00650     }
00651 
00652     return 0;
00653 
00654 }
00655 
00656 
00672 cpl_frame *
00673 giraffe_get_frame(const cpl_frameset *set, const cxchar *tag,
00674                   cpl_frame_group group)
00675 {
00676 
00677     cpl_frameset* _set = (cpl_frameset*)set;
00678 
00679     cpl_frame *frame = NULL;
00680 
00681 
00682     if (_set == NULL || tag == NULL) {
00683         return NULL;
00684     }
00685 
00686     frame = cpl_frameset_find(_set, tag);
00687 
00688     if (group != CPL_FRAME_GROUP_NONE) {
00689 
00690         while (frame != NULL &&
00691                cpl_frame_get_group(frame) != CPL_FRAME_GROUP_PRODUCT) {
00692 
00693             frame = cpl_frameset_find(_set, NULL);
00694 
00695         }
00696 
00697     }
00698 
00699     return frame;
00700 
00701 }
00702 
00703 
00722 cpl_frame *
00723 giraffe_get_slitgeometry(const cpl_frameset *set)
00724 {
00725 
00726     cpl_frameset* _set = (cpl_frameset*)set;
00727 
00728     cpl_frame *frame = NULL;
00729 
00730 
00731     if (_set == 0) {
00732         return 0;
00733     }
00734 
00735     frame = cpl_frameset_find(_set, GIFRAME_SLITSETUP);
00736     frame = frame == 0 ? cpl_frameset_find(_set, GIFRAME_SLITMASTER) : frame;
00737     frame = frame == 0 ? cpl_frameset_find(_set, GIFRAME_SLITGEOMETRY) : frame;
00738 
00739     return frame;
00740 
00741 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.5.1.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Tue Mar 18 10:47:42 2008 by doxygen 1.4.6 written by Dimitri van Heesch, © 1997-2004