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