VIRCAM Pipeline  1.3.3
vircam_tfits.c
1 /* $Id: vircam_tfits.c,v 1.16 2013-10-15 16:53:17 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: 2013-10-15 16:53:17 $
24  * $Revision: 1.16 $
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 <stdio.h>
35 #include <math.h>
36 #include <string.h>
37 
38 #include <cpl.h>
39 #include "vircam_utils.h"
40 #include "vircam_tfits.h"
41 
55 /*---------------------------------------------------------------------------*/
76 /*---------------------------------------------------------------------------*/
77 
78 extern vir_tfits *vircam_tfits_load(cpl_frame *table, int nexten) {
79  vir_tfits *p;
80  cpl_table *tab;
81  int nf;
82  const char *fctid = "vircam_tfits_load";
83 
84  /* Check for nonsense input */
85 
86  if (table == NULL)
87  return(NULL);
88 
89  /* See if you can load the table */
90 
91  tab = cpl_table_load(cpl_frame_get_filename(table),nexten,0);
92  if (tab == NULL) {
93  cpl_msg_error(fctid,"Unable to load %s -- %s\n",
94  cpl_frame_get_filename(table),cpl_error_get_message());
95  cpl_error_reset();
96  return(NULL);
97  }
98 
99  /* Get the vir_tfits structure */
100 
101  p = cpl_malloc(sizeof(vir_tfits));
102 
103  /* Load stuff in */
104 
105  p->table = tab;
106  p->nexten = nexten;
107  p->phu = NULL;
108  p->ehu = NULL;
109  p->fname = cpl_strdup(cpl_frame_get_filename(table));
110  p->status = VIR_OK;
111 
112  /* Get the extension header and the extension name */
113 
114  (void)vircam_tfits_get_ehu(p);
115  if (cpl_propertylist_has(p->ehu,"EXTNAME")) {
116  p->extname = cpl_strdup(cpl_propertylist_get_string(p->ehu,"EXTNAME"));
117  } else {
118  nf = 11 + (int)log10((double)nexten);
119  p->extname = cpl_malloc(nf);
120  (void)snprintf(p->extname,nf,"DET1.CHIP%d",nexten);
121  }
122  nf = strlen(p->extname) + strlen(p->fname) + 3;
123  p->fullname = cpl_malloc(nf);
124  (void)snprintf(p->fullname,nf,"%s[%s]",p->fname,p->extname);
125 
126  /* Get out of here */
127 
128  return(p);
129 }
130 
131 /*---------------------------------------------------------------------------*/
150 /*---------------------------------------------------------------------------*/
151 
152 extern vir_tfits *vircam_tfits_extract(vir_tfits *in) {
153  vir_tfits *p;
154 
155  /* Check for nonsense input */
156 
157  if (in == NULL)
158  return(NULL);
159 
160  /* Get the vir_tfits structure */
161 
162  p = cpl_malloc(sizeof(vir_tfits));
163 
164  /* Load stuff in */
165 
166  p->table = cpl_table_extract_selected(vircam_tfits_get_table(in));
167  p->nexten = vircam_tfits_get_nexten(in);
168  p->phu = NULL;
169  p->ehu = NULL;
170  p->fname = cpl_strdup(vircam_tfits_get_filename(in));
171 
172  /* Get out of here */
173 
174  return(p);
175 }
176 
177 /*---------------------------------------------------------------------------*/
194 /*---------------------------------------------------------------------------*/
195 
196 extern vir_tfits *vircam_tfits_duplicate(vir_tfits *in) {
197  vir_tfits *p;
198 
199  /* Check for nonsense input */
200 
201  if (in == NULL)
202  return(NULL);
203 
204  /* Get the vir_tfits structure */
205 
206  p = cpl_malloc(sizeof(vir_tfits));
207 
208  /* Now copy everything over */
209 
210  p->table = cpl_table_duplicate(in->table);
211  p->phu = cpl_propertylist_duplicate(vircam_tfits_get_phu(in));
212  p->ehu = cpl_propertylist_duplicate(vircam_tfits_get_ehu(in));
213  p->fname = cpl_strdup(in->fname);
214  p->extname = cpl_strdup(in->extname);
215  p->fullname = cpl_strdup(in->fullname);
216  p->nexten = in->nexten;
217  p->status = in->status;
218 
219  /* Get out of here */
220 
221  return(p);
222 }
223 
224 /*---------------------------------------------------------------------------*/
245 /*---------------------------------------------------------------------------*/
246 
247 extern vir_tfits **vircam_tfits_load_list(cpl_frameset *f, int exten) {
248  int i;
249  vir_tfits **p;
250 
251  /* Check for nonsense input */
252 
253  if (f == NULL)
254  return(NULL);
255 
256  /* Get some workspace */
257 
258  p = cpl_malloc(cpl_frameset_get_size(f)*sizeof(vir_tfits *));
259 
260  /* Now load each of the frames... */
261 
262  for (i = 0; i < cpl_frameset_get_size(f); i++) {
263  p[i] = vircam_tfits_load(cpl_frameset_get_frame(f,i),exten);
264  if (p[i] == NULL) {
266  return(NULL);
267  }
268  }
269 
270  /* Now return the array */
271 
272  return(p);
273 }
274 
275 /*---------------------------------------------------------------------------*/
290 /*---------------------------------------------------------------------------*/
291 
292 extern void vircam_tfits_delete(vir_tfits *p) {
293 
294  /* Check for nonsense input */
295 
296  if (p == NULL)
297  return;
298 
299  /* Free up workspace if it's been used */
300 
301  freetable(p->table);
302  freepropertylist(p->phu);
303  freepropertylist(p->ehu);
304  freespace(p->fname);
305  freespace(p->extname);
306  freespace(p->fullname);
307  cpl_free(p);
308 }
309 
310 /*---------------------------------------------------------------------------*/
327 /*---------------------------------------------------------------------------*/
328 
329 extern void vircam_tfits_delete_list(vir_tfits **p, int n) {
330  int i;
331 
332  /* Check for nonsense input */
333 
334  if (p == NULL)
335  return;
336 
337  /* Free up workspace if it's been used */
338 
339  for (i = 0; i < n; i++)
340  vircam_tfits_delete(p[i]);
341  freespace(p);
342 }
343 
344 /*---------------------------------------------------------------------------*/
362 /*---------------------------------------------------------------------------*/
363 
364 extern cpl_table *vircam_tfits_get_table(vir_tfits *p) {
365 
366  /* Check for nonsense input */
367 
368  if (p == NULL)
369  return(NULL);
370 
371  /* Return it */
372 
373  return(p->table);
374 }
375 
376 /*---------------------------------------------------------------------------*/
395 /*---------------------------------------------------------------------------*/
396 
397 extern int vircam_tfits_get_nexten(vir_tfits *p) {
398 
399  /* Check for nonsense input */
400 
401  if (p == NULL)
402  return(-1);
403 
404  /* Return it */
405 
406  return(p->nexten);
407 }
408 
409 /*---------------------------------------------------------------------------*/
430 /*---------------------------------------------------------------------------*/
431 
432 extern cpl_propertylist *vircam_tfits_get_phu(vir_tfits *p) {
433 
434  /* Check for nonsense input */
435 
436  if (p == NULL)
437  return(NULL);
438 
439  /* If the propertylist hasn't already been loaded, then do it now */
440 
441  if (p->phu == NULL)
442  p->phu = cpl_propertylist_load(p->fname,0);
443 
444  /* Return it */
445 
446  return(p->phu);
447 }
448 
449 /*---------------------------------------------------------------------------*/
471 /*---------------------------------------------------------------------------*/
472 
473 extern cpl_propertylist *vircam_tfits_get_ehu(vir_tfits *p) {
474 
475  /* Check for nonsense input */
476 
477  if (p == NULL)
478  return(NULL);
479 
480  /* If the propertylist hasn't already been loaded, then do it now */
481 
482  if (p->ehu == NULL)
483  p->ehu = cpl_propertylist_load(p->fname,(cpl_size)(p->nexten));
484 
485  /* Return it */
486 
487  return(p->ehu);
488 }
489 
490 /*---------------------------------------------------------------------------*/
508 /*---------------------------------------------------------------------------*/
509 
510 extern char *vircam_tfits_get_filename(vir_tfits *p) {
511 
512  /* Check for nonsense input */
513 
514  if (p == NULL)
515  return(NULL);
516 
517  /* Return it */
518 
519  return(p->fname);
520 }
521 
522 /*---------------------------------------------------------------------------*/
542 /*---------------------------------------------------------------------------*/
543 
544 extern char *vircam_tfits_get_fullname(vir_tfits *p) {
545 
546  /* Check for nonsense input */
547 
548  if (p == NULL)
549  return(NULL);
550 
551  /* Return it */
552 
553  return(p->fullname);
554 }
555 
556 /*---------------------------------------------------------------------------*/
573 /*---------------------------------------------------------------------------*/
574 
575 extern int vircam_tfits_get_status(vir_tfits *p) {
576 
577  /* Check for nonsense input */
578 
579  if (p == NULL)
580  return(VIR_FATAL);
581 
582  /* Return it */
583 
584  return(p->status);
585 }
586 
587 /*---------------------------------------------------------------------------*/
609 /*---------------------------------------------------------------------------*/
610 
611 extern int vircam_tfits_set_error(vir_tfits *p, int status) {
612 
613  /* Check for nonsense input */
614 
615  if (p == NULL)
616  return(0);
617 
618  /* Get out of here if the status is OK */
619 
620  if (status == VIR_OK)
621  return(0);
622 
623  /* Set the error message if there was an error */
624 
625  p->status = status;
626 
627  /* Reset the cpl error flag */
628 
629  cpl_error_reset();
630  if (status == VIR_FATAL)
631  return(1);
632  else
633  return(0);
634 }
635 
636 /*---------------------------------------------------------------------------*/
659 /*---------------------------------------------------------------------------*/
660 
661 extern void vircam_tfits_set_filename(vir_tfits *p, char *fname) {
662 
663  /* Check for nonsense input */
664 
665  if (p == NULL || fname == NULL)
666  return;
667 
668  /* Set the name up and get out of here */
669 
670  freespace(p->fname);
671  p->fname = cpl_strdup(fname);
672 }
673 
674 /*---------------------------------------------------------------------------*/
701 /*---------------------------------------------------------------------------*/
702 
703 extern vir_tfits *vircam_tfits_wrap(cpl_table *tab, vir_tfits *model,
704  cpl_propertylist *phu,
705  cpl_propertylist *ehu) {
706  vir_tfits *p;
707 
708  /* Check for nonsense input */
709 
710  if (tab == NULL)
711  return(NULL);
712 
713  /* Get the vir_fits structure */
714 
715  p = cpl_malloc(sizeof(vir_tfits));
716 
717  /* Load stuff in */
718 
719  p->table = tab;
720  p->nexten = -1;
721  if (phu != NULL)
722  p->phu = phu;
723  else if (model != NULL)
724  p->phu = cpl_propertylist_duplicate(vircam_tfits_get_phu(model));
725  else
726  p->phu = NULL;
727  if (ehu != NULL)
728  p->ehu = ehu;
729  else if (model != NULL)
730  p->ehu = cpl_propertylist_duplicate(vircam_tfits_get_ehu(model));
731  else
732  p->ehu = NULL;
733  p->fname = NULL;
734  p->status = VIR_OK;
735  p->extname = NULL;
736  p->fullname = NULL;
737 
738  /* Get out of here */
739 
740  return(p);
741 }
742 
745 /*
746 
747 $Log: not supported by cvs2svn $
748 Revision 1.15 2012/01/15 17:40:09 jim
749 Minor modifications to take into accout the changes in cpl API for v6
750 
751 Revision 1.14 2008/10/21 08:43:35 jim
752 added vircam_tfits_set_filename
753 
754 Revision 1.13 2007/10/25 17:34:01 jim
755 Modified to remove lint warnings
756 
757 Revision 1.12 2007/10/19 09:25:10 jim
758 Fixed problems with missing includes
759 
760 Revision 1.11 2007/10/15 12:50:28 jim
761 Modified for compatibility with cpl_4.0
762 
763 Revision 1.10 2007/03/01 12:42:42 jim
764 Modified slightly after code checking
765 
766 Revision 1.9 2007/02/25 06:32:40 jim
767 Fixed typo
768 
769 Revision 1.8 2007/02/20 21:13:13 jim
770 Better error reporting in case of load failure
771 
772 Revision 1.7 2006/07/04 09:19:05 jim
773 replaced all sprintf statements with snprintf
774 
775 Revision 1.6 2006/06/19 11:17:58 jim
776 Fixed bug causing a memory overwrite
777 
778 Revision 1.5 2006/06/09 11:26:26 jim
779 Small changes to keep lint happy
780 
781 Revision 1.4 2006/05/24 13:34:36 jim
782 Added vircam_tfits_wrap
783 
784 Revision 1.3 2006/04/20 11:21:21 jim
785 Added _fullname method
786 
787 Revision 1.2 2006/03/22 13:58:32 jim
788 Cosmetic fixes to keep lint happy
789 
790 Revision 1.1 2006/03/15 14:35:17 jim
791 new file
792 
793 
794 */