VIRCAM Pipeline  1.3.4
vircam_paf.c
1 /* $Id: vircam_paf.c,v 1.11 2010-09-09 12:11:09 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2010-09-09 12:11:09 $
24  * $Revision: 1.11 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <string.h>
35 #include <cpl.h>
36 #include "vircam_paf.h"
37 #include "vircam_utils.h"
38 
39 
40 static char *vircam_paf_keyname(char *name);
41 static void vircam_paf_write_char(char *keyname, char val, char *comment,
42  FILE *paf);
43 static void vircam_paf_write_int(char *keyname, int val, char *comment,
44  FILE *paf);
45 static void vircam_paf_write_long(char *keyname, long val, char *comment,
46  FILE *paf);
47 static void vircam_paf_write_float(char *keyname, float val, char *comment,
48  FILE *paf);
49 static void vircam_paf_write_double(char *keyname, double val, char *comment,
50  FILE *paf);
51 static void vircam_paf_write_string(char *keyname, char *val, char *comment,
52  FILE *paf);
53 
54 
55 /*---------------------------------------------------------------------------*/
80 /*---------------------------------------------------------------------------*/
81 
82 extern int vircam_paf_print(char *ftemp, const char *paf_id,
83  const char *paf_desc, cpl_propertylist *incl) {
84  FILE *paf;
85  char filename[BUFSIZ],*comment,*name,*keyname;
86  const char *fctid="vircam_paf_print";
87  int i,ichip;
88  cpl_property *p;
89 
90  /* Check the propertylist even exists... */
91 
92  if (incl == NULL) {
93  cpl_msg_warning(fctid,"NULL propertylist. No PAF file written");
94  return(VIR_FATAL);
95  }
96 
97  /* Find the number of the detector */
98 
99  ichip = cpl_propertylist_get_int(incl,"ESO DET CHIP NO");
100  if (ichip == 0) {
101  cpl_error_reset();
102  cpl_msg_warning(fctid,"No entry DET CHIP NO in property list");
103  return(VIR_FATAL);
104  }
105 
106  /* Create a file name from the template and try to open it. If it won't
107  open, then get out of here */
108 
109  (void)snprintf(filename,BUFSIZ,"%s_%02d.paf",ftemp,ichip);
110  if ((paf = fopen(filename,"w")) == NULL) {
111  cpl_msg_warning(fctid,"Unable to open %s.",filename);
112  return(VIR_FATAL);
113  }
114 
115  /* Write the header */
116 
117  fprintf(paf,"PAF.HDR.START ;# start of header\n");
118  fprintf(paf,"PAF.TYPE \"pipeline product\" ;\n");
119  fprintf(paf,"PAF.ID \"%s\"\n", paf_id);
120  fprintf(paf,"PAF.NAME \"%s\"\n", filename);
121  fprintf(paf,"PAF.DESC \"%s\"\n", paf_desc);
122  fprintf(paf,"PAF.CHCK.CHECKSUM \"\"\n");
123  fprintf(paf,"PAF.HDR.END ;# end of header\n");
124  fprintf(paf,"\n");
125 
126  /* Now loop through the propertylist and create a keyname that is standard
127  format for a paf file */
128 
129  for (i = 0; i < cpl_propertylist_get_size(incl); i++) {
130  p = cpl_propertylist_get(incl,i);
131  name = (char *)cpl_property_get_name(p);
132  keyname = vircam_paf_keyname(name);
133  comment = (char *)cpl_property_get_comment(p);
134 
135  /* Now switch for each acceptable data type */
136 
137  switch (cpl_property_get_type(p)) {
138  case CPL_TYPE_CHAR:
139  vircam_paf_write_char(keyname,cpl_property_get_char(p),
140  comment,paf);
141  break;
142  case CPL_TYPE_INT:
143  vircam_paf_write_int(keyname,cpl_property_get_int(p),
144  comment,paf);
145  break;
146  case CPL_TYPE_LONG:
147  vircam_paf_write_long(keyname,cpl_property_get_long(p),
148  comment,paf);
149  break;
150  case CPL_TYPE_FLOAT:
151  vircam_paf_write_float(keyname,cpl_property_get_float(p),
152  comment,paf);
153  break;
154  case CPL_TYPE_DOUBLE:
155  vircam_paf_write_double(keyname,cpl_property_get_double(p),
156  comment,paf);
157  break;
158  case CPL_TYPE_STRING:
159  vircam_paf_write_string(keyname,(char *)cpl_property_get_string(p),
160  comment,paf);
161  break;
162  default:
163  break;
164  }
165  cpl_free(keyname);
166  }
167 
168  /* Close things up */
169 
170  fclose(paf);
171  return(VIR_OK);
172 }
173 
174 /*---------------------------------------------------------------------------*/
195 /*---------------------------------------------------------------------------*/
196 
197 extern cpl_propertylist *vircam_paf_req_items(cpl_propertylist *src) {
198  cpl_propertylist *dest;
199  int nreq=2,i;
200  const char *req_items[] = {"ESO DET CHIP NO","EXTNAME"};
201  const char *req_reg = "^ESO QC";
202  const char *fctid = "vircam_paf_req_items";
203 
204  /* Get a new propertylist */
205 
206  dest = cpl_propertylist_new();
207 
208  /* Copy the items over */
209 
210  for (i = 0; i < nreq; i++) {
211  if (cpl_propertylist_copy_property(dest,src,req_items[i]) != CPL_ERROR_NONE) {
212  cpl_msg_warning(fctid,"Can't find property %s in source header",
213  req_items[i]);
214  cpl_error_reset();
215  return(dest);
216  }
217  }
218 
219  /* Now the QC stuff */
220 
221  cpl_propertylist_copy_property_regexp(dest,src,req_reg,0);
222  if (cpl_error_get_code() != CPL_ERROR_NONE) {
223  cpl_msg_warning(fctid,"Can't find regexp %s in source header",
224  req_reg);
225  cpl_error_reset();
226  return(dest);
227  }
228 
229  /* Otherwise get out of here */
230 
231  return(dest);
232 }
233 
234 /*---------------------------------------------------------------------------*/
253 /*---------------------------------------------------------------------------*/
254 
255 extern cpl_propertylist *vircam_paf_phu_items(cpl_propertylist *src) {
256  cpl_propertylist *dest;
257  int nreq=5,i;
258  const char *req_items[] = {"ESO TPL ID","DATE-OBS","MJD-OBS",
259  "ESO DET DIT","ARCFILE"};
260  const char *fctid = "vircam_paf_req_items";
261 
262  /* Get a new propertylist */
263 
264  dest = cpl_propertylist_new();
265 
266  /* Copy the items over */
267 
268  for (i = 0; i < nreq; i++) {
269  if (cpl_propertylist_copy_property(dest,src,req_items[i]) != CPL_ERROR_NONE) {
270  cpl_error_reset();
271  cpl_msg_warning(fctid,"Can't find property %s in source header",
272  req_items[i]);
273  cpl_propertylist_update_string(dest,req_items[i],"");
274  cpl_propertylist_set_comment(dest,req_items[i],"Not available");
275  }
276  }
277 
278  /* Now get out of here */
279 
280  return(dest);
281 }
282 
283 /*---------------------------------------------------------------------------*/
305 /*---------------------------------------------------------------------------*/
306 
307 extern void vircam_paf_append(cpl_propertylist *dest, cpl_propertylist *src,
308  const char *prop) {
309 
310  /* Check to see if the property is even available */
311 
312  if (cpl_propertylist_has(src,prop)) {
313  cpl_propertylist_copy_property(dest,src,prop);
314  } else {
315  cpl_propertylist_update_string(dest,prop,"");
316  cpl_propertylist_set_comment(dest,prop,"Not available");
317  }
318 }
319 
320 static void vircam_paf_write_char(char *keyname, char val, char *comment,
321  FILE *paf) {
322 
323  /* Write a the value out */
324 
325  if (comment != NULL)
326  fprintf(paf,KEYFMT "\"%c\" ; # %s\n",keyname,val,comment);
327  else
328  fprintf(paf,KEYFMT "\"%c\"\n",keyname,val);
329 }
330 
331 static void vircam_paf_write_int(char *keyname, int val, char *comment,
332  FILE *paf) {
333 
334  /* Write a the value out */
335 
336  if (comment != NULL)
337  fprintf(paf,KEYFMT "%d ; # %s\n",keyname,val,comment);
338  else
339  fprintf(paf,KEYFMT "%d\n",keyname,val);
340 }
341 
342 static void vircam_paf_write_long(char *keyname, long val, char *comment,
343  FILE *paf) {
344 
345  /* Write a the value out */
346 
347  if (comment != NULL)
348  fprintf(paf,KEYFMT "%ld ; # %s\n",keyname,val,comment);
349  else
350  fprintf(paf,KEYFMT "%ld\n",keyname,val);
351 }
352 
353 static void vircam_paf_write_float(char *keyname, float val, char *comment,
354  FILE *paf) {
355 
356  /* Write a the value out */
357 
358  if (comment != NULL)
359  fprintf(paf,KEYFMT "%.10g ; # %s\n",keyname,val,comment);
360  else
361  fprintf(paf,KEYFMT "%.10g\n",keyname,val);
362 }
363 
364 static void vircam_paf_write_double(char *keyname, double val, char *comment,
365  FILE *paf) {
366 
367  /* Write a the value out */
368 
369  if (comment != NULL)
370  fprintf(paf,KEYFMT "%.10g ; # %s\n",keyname,val,comment);
371  else
372  fprintf(paf,KEYFMT "%.10g\n",keyname,val);
373 }
374 
375 static void vircam_paf_write_string(char *keyname, char *val, char *comment,
376  FILE *paf) {
377 
378  /* Write a the value out */
379 
380  if (comment != NULL)
381  fprintf(paf,KEYFMT "\"%s\" ; # %s\n",keyname,val,comment);
382  else
383  fprintf(paf,KEYFMT "\"%s\"\n",keyname,val);
384 }
385 
386 
387 static char *vircam_paf_keyname(char *name) {
388  char *keyname,*t;
389 
390  /* Locate the ESO part of the keyword and ditch it */
391 
392  keyname = cpl_malloc(SZKEY+1);
393  t = strstr(name,"ESO");
394  if (t == NULL)
395  (void)strncpy(keyname,name,SZKEY);
396  else
397  strncpy(keyname,t+4,SZKEY);
398  keyname[SZKEY] = '\0';
399 
400  /* Now replace spaces with dots */
401 
402  for (t = keyname; *t != '\0'; t++) {
403  if (*t == ' ')
404  *t = '.';
405  }
406  return(keyname);
407 }
408 
409 
410 /*
411 
412 $Log: not supported by cvs2svn $
413 Revision 1.10 2010/06/07 12:42:40 jim
414 Modifications to get rid of compiler gripes
415 
416 Revision 1.9 2008/09/30 11:34:50 jim
417 Rearranged which items come from the primary and extension headers
418 
419 Revision 1.8 2008/09/29 11:20:51 jim
420 Now write PRO.CATG to the paf file
421 
422 Revision 1.7 2007/10/25 17:34:01 jim
423 Modified to remove lint warnings
424 
425 Revision 1.6 2007/10/15 12:50:28 jim
426 Modified for compatibility with cpl_4.0
427 
428 Revision 1.5 2007/04/30 09:40:01 jim
429 Added vircam_paf_append
430 
431 Revision 1.4 2007/04/04 10:33:05 jim
432 Modified to add vircam_paf_phu_items
433 
434 Revision 1.3 2007/03/01 12:42:42 jim
435 Modified slightly after code checking
436 
437 Revision 1.2 2007/02/14 14:00:40 jim
438 Added vircam_paf_req_items
439 
440 Revision 1.1 2007/02/14 12:53:51 jim
441 New entry
442 
443 
444 */