GIRAFFE Pipeline Reference Manual

giutils.c

00001 /* $Id: giutils.c,v 1.26 2007/08/23 08:39:06 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: 2007/08/23 08:39:06 $
00024  * $Revision: 1.26 $
00025  * $Name: giraffe-2_5_2 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #ifdef HAVE_SYS_TYPES_H
00033 #  include <sys/types.h>
00034 #endif
00035 #include <time.h>
00036 #include <string.h>
00037 #include <regex.h>
00038 
00039 #include <cxstring.h>
00040 #include <cxstrutils.h>
00041 
00042 #include <cpl_error.h>
00043 #include <cpl_matrix.h>
00044 
00045 #include "gialias.h"
00046 #include "gimessages.h"
00047 #include "giutils.h"
00048 
00049 
00058 /*
00059  * Giraffe version and license
00060  */
00061 
00062 static const cxchar *_giraffe_license =
00063  " This file is part of the GIRAFFE Instrument Pipeline\n"
00064  " Copyright (C) 2002-2006 European Southern Observatory\n"
00065  "\n"
00066  " This program is free software; you can redistribute it and/or modify\n"
00067  " it under the terms of the GNU General Public License as published by\n"
00068  " the Free Software Foundation; either version 2 of the License, or\n"
00069  " (at your option) any later version.\n"
00070  "\n"
00071  " This program is distributed in the hope that it will be useful,\n"
00072  " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00073  " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
00074  " GNU General Public License for more details.\n"
00075  "\n"
00076  " You should have received a copy of the GNU General Public License\n"
00077  " along with this program; if not, write to the Free Software\n"
00078  " Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301"
00079  "  USA";
00080 
00081 
00082 inline static cxint
00083 _giraffe_plist_append(cpl_propertylist *self, cpl_property *p)
00084 {
00085 
00086     const cxchar *name = cpl_property_get_name(p);
00087     const cxchar *comment = cpl_property_get_comment(p);
00088 
00089 
00090     switch (cpl_property_get_type(p)) {
00091     case CPL_TYPE_BOOL:
00092     {
00093         cxbool value = cpl_property_get_bool(p);
00094 
00095         cpl_propertylist_append_bool(self, name, value);
00096         break;
00097     }
00098 
00099     case CPL_TYPE_CHAR:
00100     {
00101         cxchar value = cpl_property_get_char(p);
00102 
00103         cpl_propertylist_append_char(self, name, value);
00104         break;
00105     }
00106 
00107     case CPL_TYPE_INT:
00108     {
00109         cxint value = cpl_property_get_int(p);
00110 
00111         cpl_propertylist_append_int(self, name, value);
00112         break;
00113     }
00114 
00115     case CPL_TYPE_LONG:
00116     {
00117         cxlong value = cpl_property_get_long(p);
00118 
00119         cpl_propertylist_append_long(self, name, value);
00120         break;
00121     }
00122 
00123     case CPL_TYPE_FLOAT:
00124     {
00125         cxfloat value = cpl_property_get_float(p);
00126 
00127         cpl_propertylist_append_float(self, name, value);
00128         break;
00129     }
00130 
00131     case CPL_TYPE_DOUBLE:
00132     {
00133         cxdouble value = cpl_property_get_double(p);
00134 
00135         cpl_propertylist_append_double(self, name, value);
00136         break;
00137     }
00138 
00139     case CPL_TYPE_STRING:
00140     {
00141         const cxchar *value = cpl_property_get_string(p);
00142 
00143         cpl_propertylist_append_string(self, name, value);
00144         break;
00145     }
00146 
00147     default:
00148 
00149         /*
00150          * We should never reach this point! Since the property
00151          * was a valid property it has a valid type. But this
00152          * protects against addition of new types.
00153          */
00154 
00155         return 1;
00156         break;
00157     }
00158 
00159     if (comment != NULL) {
00160         cpl_propertylist_set_comment(self, name, comment);
00161     }
00162 
00163     return 0;
00164 
00165 }
00166 
00167 
00168 inline static cxint
00169 _giraffe_add_frame_info(cpl_propertylist *plist, const cxchar *name,
00170                         const cxchar *tag, cxint sequence, cxint frame_index,
00171                         cxint mode)
00172 {
00173 
00174     const cxchar *id = NULL;
00175     const cxchar *group = NULL;
00176 
00177     cxint status = 0;
00178 
00179     cx_string *key = NULL;
00180     cx_string *comment = NULL;
00181 
00182 
00183     if (plist == NULL) {
00184         return 1;
00185     }
00186 
00187     switch (mode) {
00188     case 0:
00189         id = "RAW";
00190         group = "raw";
00191         break;
00192 
00193     case 1:
00194         id = "CAL";
00195         group = "calibration";
00196         break;
00197 
00198     default:
00199         return 2;
00200         break;
00201     }
00202 
00203     key = cx_string_new();
00204     comment = cx_string_new();
00205 
00206 
00207     /*
00208      * Frame name
00209      */
00210 
00211     cx_string_sprintf(key, "%s%-d %s%-d %s", "ESO PRO REC", sequence, id,
00212                       frame_index, "NAME");
00213     cx_string_sprintf(comment, "%s %s %s", "File name of", group, "frame");
00214 
00215     status = cpl_propertylist_update_string(plist, cx_string_get(key), name);
00216 
00217     if (status != CPL_ERROR_NONE) {
00218         cx_string_delete(key);
00219         cx_string_delete(comment);
00220 
00221         return 3;
00222     }
00223 
00224     status = cpl_propertylist_set_comment(plist, cx_string_get(key),
00225                                    cx_string_get(comment));
00226 
00227     if (status != 0) {
00228         cx_string_delete(key);
00229         cx_string_delete(comment);
00230 
00231         return 3;
00232     }
00233 
00234 
00235     /*
00236      * Frame category
00237      */
00238 
00239     cx_string_sprintf(key, "%s%-d %s%-d %s", "ESO PRO REC", sequence, id,
00240                       frame_index, "CATG");
00241     cx_string_sprintf(comment, "%s %s %s", "Frame category of", group,
00242                       "frame");
00243 
00244     status = cpl_propertylist_update_string(plist, cx_string_get(key), tag);
00245 
00246     if (status != CPL_ERROR_NONE) {
00247         cx_string_delete(key);
00248         cx_string_delete(comment);
00249 
00250         return 4;
00251     }
00252 
00253     status = cpl_propertylist_set_comment(plist, cx_string_get(key),
00254                                    cx_string_get(comment));
00255 
00256     if (status != 0) {
00257         cx_string_delete(key);
00258         cx_string_delete(comment);
00259 
00260         return 4;
00261     }
00262 
00263     cx_string_delete(key);
00264     cx_string_delete(comment);
00265 
00266     return 0;
00267 
00268 }
00269 
00270 
00281 const cxchar *
00282 giraffe_get_license(void)
00283 {
00284 
00285     return _giraffe_license;
00286 
00287 }
00288 
00289 
00303 GiInstrumentMode
00304 giraffe_get_mode(cpl_propertylist *properties)
00305 {
00306 
00307     const cxchar *fctid = "giraffe_get_mode";
00308     const cxchar *mode;
00309 
00310     cx_string *s = NULL;
00311 
00312     GiInstrumentMode insmode;
00313 
00314 
00315     if (!properties) {
00316         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00317         return GIMODE_NONE;
00318     }
00319 
00320 
00321     if (!cpl_propertylist_has(properties, GIALIAS_INSMODE)) {
00322         gi_warning("%s: Property (%s) not found\n", fctid, GIALIAS_INSMODE);
00323 
00324         if (!cpl_propertylist_has(properties, GIALIAS_SLITNAME)) {
00325             cx_warning("%s: Property (%s) not found\n", fctid,
00326                        GIALIAS_SLITNAME);
00327             return GIMODE_NONE;
00328         }
00329         else {
00330             mode = cpl_propertylist_get_string(properties, GIALIAS_SLITNAME);
00331         }
00332     }
00333     else {
00334         mode = cpl_propertylist_get_string(properties, GIALIAS_SLITNAME);
00335     }
00336 
00337     if (!mode || strlen(mode) == 0) {
00338         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00339         return GIMODE_NONE;
00340     }
00341 
00342 
00343     s = cx_string_create(mode);
00344     cx_string_lower(s);
00345 
00346     if (strncmp(cx_string_get(s), "med", 3) == 0) {
00347         insmode = GIMODE_MEDUSA;
00348     }
00349     else if (strncmp(cx_string_get(s), "ifu", 3) == 0) {
00350         insmode = GIMODE_IFU;
00351     }
00352     else if (strncmp(cx_string_get(s), "arg", 3) == 0) {
00353         insmode = GIMODE_ARGUS;
00354     }
00355     else {
00356         cpl_error_set(fctid, CPL_ERROR_UNSUPPORTED_MODE);
00357         insmode = GIMODE_NONE;
00358     }
00359 
00360     cx_string_delete(s);
00361 
00362     return insmode;
00363 
00364 }
00365 
00381 cxchar *
00382 giraffe_path_get_basename(const cxchar *path)
00383 {
00384 
00385     register cxssize base;
00386     register cxssize last_nonslash;
00387 
00388     cxsize len;
00389 
00390     cxchar *result;
00391 
00392 
00393     if (path == NULL) {
00394         return NULL;
00395     }
00396 
00397     if (path[0] == '\0') {
00398         return cx_strdup(".");
00399     }
00400 
00401     last_nonslash = strlen(path) - 1;
00402 
00403     while (last_nonslash >= 0 && path[last_nonslash] == '/') {
00404         --last_nonslash;
00405     }
00406 
00407 
00408     /* String only containing slashes */
00409 
00410     if (last_nonslash == -1) {
00411         return cx_strdup("/");
00412     }
00413 
00414     base = last_nonslash;
00415 
00416     while (base >=0 && path[base] != '/') {
00417         --base;
00418     }
00419 
00420     len = last_nonslash - base;
00421 
00422     result = cx_malloc(len + 1);
00423     memcpy(result, path + base + 1, len);
00424     result[len] = '\0';
00425 
00426     return result;
00427 
00428 }
00429 
00430 
00443 cxchar *
00444 giraffe_localtime_iso8601(void)
00445 {
00446 
00447     struct tm *ts;
00448 
00449     time_t seconds = time(NULL);
00450 
00451     cxchar *sdate = NULL;
00452 
00453     cxulong milliseconds = 0;
00454 
00455     cx_string *self = cx_string_new();
00456 
00457 
00458     cx_assert(self != NULL);
00459 
00460     ts = localtime(&seconds);
00461 
00462     cx_string_sprintf(self, "%4d-%02d-%02dT%02d:%02d:%02d.%03ld",
00463                       ts->tm_year + 1900,
00464                       ts->tm_mon + 1,
00465                       ts->tm_mday,
00466                       ts->tm_hour,
00467                       ts->tm_min,
00468                       ts->tm_sec,
00469                       milliseconds);
00470 
00471     sdate = cx_strdup(cx_string_get(self));
00472     cx_string_delete(self);
00473 
00474     return sdate;
00475 
00476 }
00477 
00478 
00486 cxint
00487 giraffe_add_recipe_info(cpl_propertylist *plist, const GiRecipeInfo *info)
00488 {
00489 
00490     if (plist == NULL) {
00491         return -1;
00492     }
00493 
00494     if (info != NULL) {
00495 
00496         cxint status = 0;
00497 
00498         cx_string *name = cx_string_new();
00499         cx_string *value = cx_string_new();
00500 
00501 
00502         cx_string_sprintf(name, "%s%-d %s", "ESO PRO REC", info->sequence,
00503                           "PIPE ID");
00504         cx_string_sprintf(value, "%s/%s", PACKAGE, VERSION);
00505 
00506         status = cpl_propertylist_update_string(plist, cx_string_get(name),
00507                                                 cx_string_get(value));
00508 
00509 
00510         if (status != CPL_ERROR_NONE) {
00511             cx_string_delete(name);
00512             cx_string_delete(value);
00513 
00514             return 1;
00515         }
00516 
00517         status = cpl_propertylist_set_comment(plist, cx_string_get(name),
00518                                        "Pipeline (unique) identifier");
00519 
00520         if (status != 0) {
00521             cx_string_delete(name);
00522             cx_string_delete(value);
00523 
00524             return 1;
00525         }
00526 
00527         if (info->start != NULL) {
00528             cx_string_sprintf(name, "%s%-d %s", "ESO PRO REC",
00529                               info->sequence, "START");
00530             status = cpl_propertylist_update_string(plist,
00531                                                     cx_string_get(name),
00532                                                     info->start);
00533 
00534             if (status != CPL_ERROR_NONE) {
00535                 cx_string_delete(name);
00536                 cx_string_delete(value);
00537 
00538                 return 1;
00539             }
00540 
00541             status = cpl_propertylist_set_comment(plist, cx_string_get(name),
00542                                            "Date when recipe execution "
00543                                            "started.");
00544 
00545             if (status != 0) {
00546                 cx_string_delete(name);
00547                 cx_string_delete(value);
00548 
00549                 return 1;
00550             }
00551         }
00552 
00553         cx_string_delete(name);
00554         cx_string_delete(value);
00555 
00556     }
00557 
00558     return 0;
00559 
00560 }
00561 
00562 
00570 cxint
00571 giraffe_add_frameset_info(cpl_propertylist *plist, const cpl_frameset *set,
00572                           cxint sequence)
00573 {
00574 
00575     if (plist == NULL) {
00576         return -1;
00577     }
00578 
00579     if (set != NULL) {
00580 
00581         cxsize nraw = 0;
00582         cxsize ncalib = 0;
00583 
00584         cx_string *key = cx_string_new();
00585 
00586         const cpl_frame *frame = cpl_frameset_get_first_const(set);
00587 
00588         while (frame != NULL) {
00589 
00590             cxint status = 0;
00591 
00592             cpl_frame_group group = cpl_frame_get_group(frame);
00593 
00594             const cxchar *name = cpl_frame_get_filename(frame);
00595             const cxchar *tag = cpl_frame_get_tag(frame);
00596             const cxchar *base = giraffe_path_get_basename(name);
00597 
00598 
00599             cx_assert(base != NULL);
00600 
00601             switch (group) {
00602             case CPL_FRAME_GROUP_RAW:
00603             {
00604                 ++nraw;
00605                 status = _giraffe_add_frame_info(plist, base, tag, sequence,
00606                                                  nraw, 0);
00607 
00608                 if (status != 0) {
00609                     if (base != NULL) {
00610                         cx_free((cxchar *)base);
00611                     }
00612 
00613                     cx_string_delete(key);
00614                     return -2;
00615                 }
00616 
00617                 break;
00618             }
00619 
00620             case CPL_FRAME_GROUP_CALIB:
00621             {
00622 
00623                 cx_string *md5 = NULL;
00624 
00625                 cpl_propertylist *_plist = NULL;
00626 
00627 
00628                 ++ncalib;
00629                 status = _giraffe_add_frame_info(plist, base, tag, sequence,
00630                                                  ncalib, 1);
00631 
00632                 if (status != 0) {
00633                     if (base != NULL) {
00634                         cx_free((cxchar *)base);
00635                     }
00636 
00637                     cx_string_delete(key);
00638                     return -3;
00639                 }
00640 
00641                 _plist = cpl_propertylist_load(name, 0);
00642 
00643                 if (_plist == NULL) {
00644                     if (base != NULL) {
00645                         cx_free((cxchar *)base);
00646                     }
00647 
00648                     cx_string_delete(key);
00649                     return -3;
00650                 }
00651 
00652 
00653                 md5 = cx_string_new();
00654 
00655                 cx_string_sprintf(md5, "%s%d %s%d %s", "ESO PRO REC",
00656                                   sequence, "CAL", ncalib, "DATAMD5");
00657 
00658                 if (cpl_propertylist_has(_plist, cx_string_get(md5))) {
00659 
00660                     const cxchar *_md5 = cx_string_get(md5);
00661                     const cxchar *s = cpl_propertylist_get_string(_plist,
00662                                                                   _md5);
00663 
00664                     status = cpl_propertylist_update_string(plist, _md5, s);
00665 
00666                     if (status != CPL_ERROR_NONE) {
00667                         cx_string_delete(md5);
00668                         cpl_propertylist_delete(_plist);
00669 
00670                         if (base != NULL) {
00671                             cx_free((cxchar *)base);
00672                         }
00673 
00674                         cx_string_delete(key);
00675                         return -3;
00676                     }
00677 
00678                 }
00679 
00680                 cx_string_delete(md5);
00681                 cpl_propertylist_delete(_plist);
00682                 break;
00683             }
00684 
00685             default:
00686                 break;
00687             }
00688 
00689 
00690             if (base != NULL) {
00691                 cx_free((cxchar *)base);
00692             }
00693 
00694             frame = cpl_frameset_get_next_const(set);
00695 
00696         }
00697 
00698         cx_string_delete(key);
00699 
00700     }
00701 
00702     return 0;
00703 
00704 }
00705 
00706 
00727 cxint
00728 giraffe_propertylist_update(cpl_propertylist *self,
00729                             cpl_propertylist *properties,
00730                             const cxchar *regexp)
00731 {
00732 
00733     const cxchar *fctid = "giraffe_propertylist_update";
00734 
00735     cxlong i;
00736     cxlong sz = 0;
00737 
00738 
00739     cx_assert(self != NULL);
00740 
00741     if (properties == NULL) {
00742         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00743         return -1;
00744     }
00745 
00746     sz = cpl_propertylist_get_size(properties);
00747 
00748     if (regexp == NULL || regexp[0] == '\0') {
00749 
00750         for (i = 0; i < sz; i++) {
00751 
00752             cpl_property *p = cpl_propertylist_get(properties, i);
00753             const cxchar *name = cpl_property_get_name(p);
00754 
00755 
00756             if (!cpl_propertylist_has(self, name)) {
00757 
00758                 cxint status = _giraffe_plist_append(self, p);
00759 
00760                 if (status) {
00761                     cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
00762                     return 1;
00763                 }
00764             }
00765         }
00766     }
00767     else {
00768 
00769         cxint status = 0;
00770 
00771         regex_t re;
00772 
00773 
00774         status = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB);
00775 
00776         if (status) {
00777             cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00778             return 1;
00779         }
00780 
00781         for (i = 0; i < sz; i++) {
00782 
00783             cpl_property *p = cpl_propertylist_get(properties, i);
00784             const cxchar *name = cpl_property_get_name(p);
00785 
00786 
00787             if (regexec(&re, name, (size_t)0, NULL, 0) == REG_NOMATCH) {
00788                 continue;
00789             }
00790 
00791             if (!cpl_propertylist_has(self, name)) {
00792 
00793                 status = _giraffe_plist_append(self, p);
00794 
00795                 if (status) {
00796                     cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
00797                     return 1;
00798                 }
00799             }
00800         }
00801 
00802         regfree(&re);
00803 
00804     }
00805 
00806     return 0;
00807 
00808 }
00809 
00810 
00818 cxint
00819 giraffe_propertylist_copy(cpl_propertylist *self,
00820                           const cxchar *name,
00821                           const cpl_propertylist *other,
00822                           const cxchar *othername)
00823 {
00824 
00825     const cxchar *fctid = "giraffe_propertylist_copy";
00826 
00827 
00828     const cxchar *s;
00829     const cxchar *comment;
00830 
00831     cpl_type type;
00832 
00833 
00834 
00835     cx_assert(self != NULL);
00836 
00837     if (other == NULL) {
00838         return -1;
00839     }
00840 
00841     if (othername == NULL) {
00842         return -2;
00843     }
00844 
00845     if (!cpl_propertylist_has(other, othername)) {
00846         return 1;
00847     }
00848 
00849     type = cpl_propertylist_get_type(other, othername);
00850 
00851 
00852     /*
00853      * Determine the name the new property should have in self.
00854      */
00855 
00856     s = name == NULL ? othername : name;
00857 
00858 
00859     /*
00860      * Add the property to the destination list.
00861      */
00862 
00863     switch (type) {
00864         case CPL_TYPE_CHAR:
00865         {
00866             cxchar value = cpl_propertylist_get_char(other, othername);
00867 
00868             if (cpl_propertylist_has(self, s)) {
00869                 cpl_propertylist_set_char(self, s, value);
00870             }
00871             else {
00872                 cpl_propertylist_append_char(self, s, value);
00873             }
00874         }
00875         break;
00876 
00877         case CPL_TYPE_BOOL:
00878         {
00879             cxbool value = cpl_propertylist_get_bool(other, othername);
00880 
00881             if (cpl_propertylist_has(self, s)) {
00882                 cpl_propertylist_set_bool(self, s, value);
00883             }
00884             else {
00885                 cpl_propertylist_append_bool(self, s, value);
00886             }
00887         }
00888         break;
00889 
00890         case CPL_TYPE_INT:
00891         {
00892             cxint value = cpl_propertylist_get_int(other, othername);
00893 
00894             if (cpl_propertylist_has(self, s)) {
00895                 cpl_propertylist_set_int(self, s, value);
00896             }
00897             else {
00898                 cpl_propertylist_append_int(self, s, value);
00899             }
00900         }
00901         break;
00902 
00903         case CPL_TYPE_LONG:
00904         {
00905             cxlong value = cpl_propertylist_get_long(other, othername);
00906 
00907             if (cpl_propertylist_has(self, s)) {
00908                 cpl_propertylist_set_long(self, s, value);
00909             }
00910             else {
00911                 cpl_propertylist_append_long(self, s, value);
00912             }
00913         }
00914         break;
00915 
00916         case CPL_TYPE_FLOAT:
00917         {
00918             cxfloat value = cpl_propertylist_get_float(other, othername);
00919 
00920             if (cpl_propertylist_has(self, s)) {
00921                 cpl_propertylist_set_float(self, s, value);
00922             }
00923             else {
00924                 cpl_propertylist_append_float(self, s, value);
00925             }
00926         }
00927         break;
00928 
00929         case CPL_TYPE_DOUBLE:
00930         {
00931             cxdouble value = cpl_propertylist_get_double(other, othername);
00932 
00933             if (cpl_propertylist_has(self, s)) {
00934                 cpl_propertylist_set_double(self, s, value);
00935             }
00936             else {
00937                 cpl_propertylist_append_double(self, s, value);
00938             }
00939         }
00940         break;
00941 
00942         case CPL_TYPE_STRING:
00943         {
00944             const cxchar *value = cpl_propertylist_get_string(other,
00945                                                               othername);
00946 
00947             if (cpl_propertylist_has(self, s)) {
00948                 cpl_propertylist_set_string(self, s, value);
00949             }
00950             else {
00951                 cpl_propertylist_append_string(self, s, value);
00952             }
00953         }
00954         break;
00955 
00956         default:
00957             cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
00958             return 2;
00959             break;
00960     }
00961 
00962     comment = cpl_propertylist_get_comment(other, othername);
00963 
00964     if (comment != NULL) {
00965         cpl_propertylist_set_comment(self, s, comment);
00966     }
00967 
00968     return 0;
00969 
00970 }
00971 
00972 
00973 cxint
00974 giraffe_propertylist_update_wcs(cpl_propertylist* properties, cxsize naxis,
00975                                 const cxdouble* crpix, const cxdouble* crval,
00976                                 const cxchar** ctype, const cxchar** cunit,
00977                                 const cpl_matrix* cd)
00978 {
00979 
00980     if (properties == NULL) {
00981         return 0;
00982     }
00983 
00984     cpl_propertylist_erase_regexp(properties, "^CRPIX[0-9]", 0);
00985     cpl_propertylist_erase_regexp(properties, "^CRVAL[0-9]", 0);
00986     cpl_propertylist_erase_regexp(properties, "^CDELT[0-9]", 0);
00987     cpl_propertylist_erase_regexp(properties, "^CTYPE[0-9]", 0);
00988     cpl_propertylist_erase_regexp(properties, "^CUNIT[0-9]", 0);
00989     cpl_propertylist_erase_regexp(properties, "^CROTA[0-9]", 0);
00990     cpl_propertylist_erase_regexp(properties, "^CD[0-9]*_[0-9]", 0);
00991     cpl_propertylist_erase_regexp(properties, "^PC[0-9]*_[0-9]", 0);
00992 
00993 
00994     if (naxis > 0) {
00995 
00996 
00997         register cxsize i = 0;
00998 
00999         cx_string* keyword = cx_string_new();
01000         cx_string* comment = cx_string_new();
01001 
01002 
01003         cx_assert(cpl_matrix_get_nrow(cd) == cpl_matrix_get_ncol(cd));
01004 
01005         for (i = 0; i < naxis; i++) {
01006 
01007             cx_string_sprintf(keyword, "CTYPE%-d", i + 1);
01008             cx_string_sprintf(comment, "Coordinate system of axis %d", i + 1);
01009             cpl_propertylist_append_string(properties,
01010                                            cx_string_get(keyword), ctype[i]);
01011             cpl_propertylist_set_comment(properties, cx_string_get(keyword),
01012                                          cx_string_get(comment));
01013 
01014         }
01015 
01016         for (i = 0; i < naxis; i++) {
01017 
01018             cx_string_sprintf(keyword, "CRPIX%-d", i + 1);
01019             cx_string_sprintf(comment, "Reference pixel of axis %d", i + 1);
01020             cpl_propertylist_append_double(properties,
01021                                            cx_string_get(keyword), crpix[i]);
01022             cpl_propertylist_set_comment(properties, cx_string_get(keyword),
01023                                          cx_string_get(comment));
01024 
01025         }
01026 
01027         for (i = 0; i < naxis; i++) {
01028 
01029             cx_string_sprintf(keyword, "CRVAL%-d", i + 1);
01030             cx_string_sprintf(comment, "Coordinate of axis %d at reference "
01031                               "pixel", i + 1);
01032             cpl_propertylist_append_double(properties,
01033                                            cx_string_get(keyword), crval[i]);
01034             cpl_propertylist_set_comment(properties, cx_string_get(keyword),
01035                                          cx_string_get(comment));
01036 
01037         }
01038 
01039         for (i = 0; i < naxis; i++) {
01040 
01041             if (cunit[i] != NULL) {
01042                 cx_string_sprintf(keyword, "CUNIT%-d", i + 1);
01043                 cx_string_sprintf(comment, "Unit of coordinate axis %d",
01044                                   i + 1);
01045                 cpl_propertylist_append_string(properties,
01046                                                cx_string_get(keyword),
01047                                                cunit[i]);
01048                 cpl_propertylist_set_comment(properties,
01049                                              cx_string_get(keyword),
01050                                              cx_string_get(comment));
01051             }
01052 
01053         }
01054 
01055 
01056         /*
01057          * CD matrix
01058          */
01059 
01060         for (i = 0; i < naxis; i++) {
01061 
01062             register cxsize j = 0;
01063 
01064 
01065             for (j = 0; j < naxis; j++) {
01066 
01067                 cx_string_sprintf(keyword, "CD%-d_%-d", i + 1, j + 1);
01068                 cx_string_sprintf(comment, "Coordinate translation matrix "
01069                                   "element", i + 1);
01070                 cpl_propertylist_append_double(properties,
01071                                                cx_string_get(keyword),
01072                                                cpl_matrix_get(cd, i, j));
01073                 cpl_propertylist_set_comment(properties,
01074                                              cx_string_get(keyword),
01075                                              cx_string_get(comment));
01076             }
01077 
01078         }
01079 
01080         cx_string_delete(keyword);
01081         keyword = NULL;
01082 
01083         cx_string_delete(comment);
01084         comment = NULL;
01085 
01086     }
01087 
01088     return 0;
01089 
01090 }
01091 
01092 
01093 cxint
01094 giraffe_frameset_set_groups(cpl_frameset* set, GiGroupInfo *groups)
01095 {
01096 
01097     cpl_frame* frame = NULL;
01098 
01099 
01100     if (set == NULL) {
01101         return -1;
01102     }
01103 
01104     if (groups == NULL || groups->tag == NULL) {
01105         return 0;
01106     }
01107 
01108     frame = cpl_frameset_get_first(set);
01109 
01110     while (frame != NULL) {
01111 
01112         const cxchar* tag = cpl_frame_get_tag(frame);
01113 
01114         if (tag != NULL &&
01115             cpl_frame_get_group(frame) == CPL_FRAME_GROUP_NONE) {
01116 
01117             const GiGroupInfo* g = groups;
01118 
01119             while (g->tag != NULL) {
01120                 if (strcmp(tag, g->tag) == 0) {
01121                     cpl_frame_set_group(frame, g->group);
01122                     break;
01123                 }
01124                 ++g;
01125             }
01126 
01127         }
01128 
01129         frame = cpl_frameset_get_next(set);
01130 
01131     }
01132 
01133     return 0;
01134 
01135 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.5.2.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri Jun 13 14:36:24 2008 by doxygen 1.4.6 written by Dimitri van Heesch, © 1997-2004