vircam_paf.c

00001 /* $Id: vircam_paf.c,v 1.11 2010/09/09 12:11:09 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jim $
00023  * $Date: 2010/09/09 12:11:09 $
00024  * $Revision: 1.11 $
00025  * $Name: v1-1-0 $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <string.h>
00035 #include <cpl.h>
00036 #include "vircam_paf.h"
00037 #include "vircam_utils.h"
00038 
00039 
00040 static char *vircam_paf_keyname(char *name);
00041 static void vircam_paf_write_char(char *keyname, char val, char *comment,
00042                                   FILE *paf);
00043 static void vircam_paf_write_int(char *keyname, int val, char *comment,
00044                                  FILE *paf);
00045 static void vircam_paf_write_long(char *keyname, long val, char *comment,
00046                                   FILE *paf);
00047 static void vircam_paf_write_float(char *keyname, float val, char *comment,
00048                                    FILE *paf);
00049 static void vircam_paf_write_double(char *keyname, double val, char *comment,
00050                                     FILE *paf);
00051 static void vircam_paf_write_string(char *keyname, char *val, char *comment,
00052                                     FILE *paf);
00053 
00054 
00055 /*---------------------------------------------------------------------------*/
00080 /*---------------------------------------------------------------------------*/
00081 
00082 extern int vircam_paf_print(char *ftemp, const char *paf_id, 
00083                             const char *paf_desc, cpl_propertylist *incl) {
00084     FILE *paf;
00085     char filename[BUFSIZ],*comment,*name,*keyname;
00086     const char *fctid="vircam_paf_print";
00087     int i,ichip;
00088     cpl_property *p;
00089 
00090     /* Check the propertylist even exists... */
00091 
00092     if (incl == NULL) {
00093         cpl_msg_warning(fctid,"NULL propertylist. No PAF file written");
00094         return(VIR_FATAL);
00095     }
00096 
00097     /* Find the number of the detector */
00098 
00099     ichip = cpl_propertylist_get_int(incl,"ESO DET CHIP NO");
00100     if (ichip == 0) {
00101         cpl_error_reset();
00102         cpl_msg_warning(fctid,"No entry DET CHIP NO in property list");
00103         return(VIR_FATAL);
00104     }
00105 
00106     /* Create a file name from the template and try to open it. If it won't
00107        open, then get out of here */
00108 
00109     (void)snprintf(filename,BUFSIZ,"%s_%02d.paf",ftemp,ichip);
00110     if ((paf = fopen(filename,"w")) == NULL) {
00111         cpl_msg_warning(fctid,"Unable to open %s.",filename);
00112         return(VIR_FATAL);
00113     }
00114 
00115     /* Write the header */
00116 
00117     fprintf(paf,"PAF.HDR.START         ;# start of header\n");
00118     fprintf(paf,"PAF.TYPE              \"pipeline product\" ;\n");
00119     fprintf(paf,"PAF.ID                \"%s\"\n", paf_id);
00120     fprintf(paf,"PAF.NAME              \"%s\"\n", filename);
00121     fprintf(paf,"PAF.DESC              \"%s\"\n", paf_desc);
00122     fprintf(paf,"PAF.CHCK.CHECKSUM     \"\"\n");
00123     fprintf(paf,"PAF.HDR.END           ;# end of header\n");
00124     fprintf(paf,"\n");
00125 
00126     /* Now loop through the propertylist and create a keyname that is standard
00127        format for a paf file */
00128 
00129     for (i = 0; i < cpl_propertylist_get_size(incl); i++) {
00130         p = cpl_propertylist_get(incl,i);
00131         name = (char *)cpl_property_get_name(p);
00132         keyname = vircam_paf_keyname(name);
00133         comment = (char *)cpl_property_get_comment(p);
00134         
00135         /* Now switch for each acceptable data type */
00136 
00137         switch (cpl_property_get_type(p)) {
00138         case CPL_TYPE_CHAR:
00139             vircam_paf_write_char(keyname,cpl_property_get_char(p),
00140                                   comment,paf);
00141             break;
00142         case CPL_TYPE_INT:
00143             vircam_paf_write_int(keyname,cpl_property_get_int(p),
00144                                  comment,paf);
00145             break;
00146         case CPL_TYPE_LONG:
00147             vircam_paf_write_long(keyname,cpl_property_get_long(p),
00148                                   comment,paf);
00149             break;
00150         case CPL_TYPE_FLOAT:
00151             vircam_paf_write_float(keyname,cpl_property_get_float(p),
00152                                    comment,paf);
00153             break;
00154         case CPL_TYPE_DOUBLE:
00155             vircam_paf_write_double(keyname,cpl_property_get_double(p),
00156                                     comment,paf);
00157             break;
00158         case CPL_TYPE_STRING:
00159             vircam_paf_write_string(keyname,(char *)cpl_property_get_string(p),
00160                                     comment,paf);
00161             break;
00162         default:
00163             break;
00164         }
00165         cpl_free(keyname);
00166     }
00167 
00168     /* Close things up */
00169 
00170     fclose(paf);
00171     return(VIR_OK);
00172 }
00173 
00174 /*---------------------------------------------------------------------------*/
00195 /*---------------------------------------------------------------------------*/
00196 
00197 extern cpl_propertylist *vircam_paf_req_items(cpl_propertylist *src) {
00198     cpl_propertylist *dest;
00199     int nreq=2,i;
00200     const char *req_items[] = {"ESO DET CHIP NO","EXTNAME"};
00201     const char *req_reg = "^ESO QC";
00202     const char *fctid = "vircam_paf_req_items";
00203 
00204     /* Get a new propertylist */
00205 
00206     dest = cpl_propertylist_new();
00207 
00208     /* Copy the items over */
00209 
00210     for (i = 0; i < nreq; i++) {
00211         if (cpl_propertylist_copy_property(dest,src,req_items[i]) != CPL_ERROR_NONE) {
00212             cpl_msg_warning(fctid,"Can't find property %s in source header",
00213                             req_items[i]);
00214             cpl_error_reset();
00215             return(dest);
00216         }
00217     }
00218         
00219     /* Now the QC stuff */
00220 
00221     cpl_propertylist_copy_property_regexp(dest,src,req_reg,0);
00222     if (cpl_error_get_code() != CPL_ERROR_NONE) {
00223         cpl_msg_warning(fctid,"Can't find regexp %s in source header",
00224                         req_reg);
00225         cpl_error_reset();
00226         return(dest);
00227     }
00228 
00229     /* Otherwise get out of here */
00230 
00231     return(dest);
00232 }
00233 
00234 /*---------------------------------------------------------------------------*/
00253 /*---------------------------------------------------------------------------*/
00254 
00255 extern cpl_propertylist *vircam_paf_phu_items(cpl_propertylist *src) {
00256     cpl_propertylist *dest;
00257     int nreq=5,i;
00258     const char *req_items[] = {"ESO TPL ID","DATE-OBS","MJD-OBS",
00259                                "ESO DET DIT","ARCFILE"};
00260     const char *fctid = "vircam_paf_req_items";
00261 
00262     /* Get a new propertylist */
00263 
00264     dest = cpl_propertylist_new();
00265 
00266     /* Copy the items over */
00267 
00268     for (i = 0; i < nreq; i++) {
00269         if (cpl_propertylist_copy_property(dest,src,req_items[i]) != CPL_ERROR_NONE) {
00270             cpl_error_reset();
00271             cpl_msg_warning(fctid,"Can't find property %s in source header",
00272                             req_items[i]);
00273             cpl_propertylist_update_string(dest,req_items[i],"");
00274             cpl_propertylist_set_comment(dest,req_items[i],"Not available");
00275         }
00276     }
00277         
00278     /* Now get out of here */
00279 
00280     return(dest);
00281 }
00282 
00283 /*---------------------------------------------------------------------------*/
00305 /*---------------------------------------------------------------------------*/
00306 
00307 extern void vircam_paf_append(cpl_propertylist *dest, cpl_propertylist *src,
00308                               const char *prop) {
00309 
00310     /* Check to see if the property is even available */
00311 
00312     if (cpl_propertylist_has(src,prop)) {
00313         cpl_propertylist_copy_property(dest,src,prop);
00314     } else {
00315         cpl_propertylist_update_string(dest,prop,"");
00316         cpl_propertylist_set_comment(dest,prop,"Not available");
00317     }
00318 }
00319 
00320 static void vircam_paf_write_char(char *keyname, char val, char *comment,
00321                                   FILE *paf) {
00322 
00323     /* Write a the value out */
00324 
00325     if (comment != NULL) 
00326         fprintf(paf,KEYFMT "\"%c\" ; # %s\n",keyname,val,comment);
00327     else
00328         fprintf(paf,KEYFMT "\"%c\"\n",keyname,val);
00329 }
00330 
00331 static void vircam_paf_write_int(char *keyname, int val, char *comment,
00332                                  FILE *paf) {
00333 
00334     /* Write a the value out */
00335 
00336     if (comment != NULL) 
00337         fprintf(paf,KEYFMT "%d ; # %s\n",keyname,val,comment);
00338     else
00339         fprintf(paf,KEYFMT "%d\n",keyname,val);
00340 }
00341 
00342 static void vircam_paf_write_long(char *keyname, long val, char *comment,
00343                                   FILE *paf) {
00344 
00345     /* Write a the value out */
00346 
00347     if (comment != NULL) 
00348         fprintf(paf,KEYFMT "%ld ; # %s\n",keyname,val,comment);
00349     else
00350         fprintf(paf,KEYFMT "%ld\n",keyname,val);
00351 }
00352 
00353 static void vircam_paf_write_float(char *keyname, float val, char *comment,
00354                                    FILE *paf) {
00355 
00356     /* Write a the value out */
00357 
00358     if (comment != NULL) 
00359         fprintf(paf,KEYFMT "%.10g ; # %s\n",keyname,val,comment);
00360     else
00361         fprintf(paf,KEYFMT "%.10g\n",keyname,val);
00362 }
00363 
00364 static void vircam_paf_write_double(char *keyname, double val, char *comment,
00365                                     FILE *paf) {
00366 
00367     /* Write a the value out */
00368 
00369     if (comment != NULL) 
00370         fprintf(paf,KEYFMT "%.10g ; # %s\n",keyname,val,comment);
00371     else
00372         fprintf(paf,KEYFMT "%.10g\n",keyname,val);
00373 }
00374 
00375 static void vircam_paf_write_string(char *keyname, char *val, char *comment,
00376                                     FILE *paf) {
00377 
00378     /* Write a the value out */
00379 
00380     if (comment != NULL) 
00381         fprintf(paf,KEYFMT "\"%s\" ; # %s\n",keyname,val,comment);
00382     else
00383         fprintf(paf,KEYFMT "\"%s\"\n",keyname,val);
00384 }
00385 
00386 
00387 static char *vircam_paf_keyname(char *name) {
00388     char *keyname,*t;
00389 
00390     /* Locate the ESO part of the keyword and ditch it */
00391 
00392     keyname = cpl_malloc(SZKEY+1);
00393     t = strstr(name,"ESO");
00394     if (t == NULL) 
00395         (void)strncpy(keyname,name,SZKEY);
00396     else
00397         strncpy(keyname,t+4,SZKEY);
00398     keyname[SZKEY] = '\0';
00399 
00400     /* Now replace spaces with dots */
00401 
00402     for (t = keyname; *t != '\0'; t++) {
00403         if (*t == ' ')
00404             *t = '.';
00405     }
00406     return(keyname);
00407 }
00408 
00409         
00410 /*
00411 
00412 $Log: vircam_paf.c,v $
00413 Revision 1.11  2010/09/09 12:11:09  jim
00414 Fixed problems with docs that make doxygen barf
00415 
00416 Revision 1.10  2010/06/07 12:42:40  jim
00417 Modifications to get rid of compiler gripes
00418 
00419 Revision 1.9  2008/09/30 11:34:50  jim
00420 Rearranged which items come from the primary and extension headers
00421 
00422 Revision 1.8  2008/09/29 11:20:51  jim
00423 Now write PRO.CATG to the paf file
00424 
00425 Revision 1.7  2007/10/25 17:34:01  jim
00426 Modified to remove lint warnings
00427 
00428 Revision 1.6  2007/10/15 12:50:28  jim
00429 Modified for compatibility with cpl_4.0
00430 
00431 Revision 1.5  2007/04/30 09:40:01  jim
00432 Added vircam_paf_append
00433 
00434 Revision 1.4  2007/04/04 10:33:05  jim
00435 Modified to add vircam_paf_phu_items
00436 
00437 Revision 1.3  2007/03/01 12:42:42  jim
00438 Modified slightly after code checking
00439 
00440 Revision 1.2  2007/02/14 14:00:40  jim
00441 Added vircam_paf_req_items
00442 
00443 Revision 1.1  2007/02/14 12:53:51  jim
00444 New entry
00445 
00446 
00447 */

Generated on 7 Feb 2011 for VIRCAM Pipeline by  doxygen 1.6.1