VIRCAM Pipeline  1.3.3
vircam_fits.c
1 /* $Id: vircam_fits.c,v 1.24 2013-10-15 16:38: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:38:17 $
24  * $Revision: 1.24 $
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_fits.h"
41 
55 /*---------------------------------------------------------------------------*/
78 /*---------------------------------------------------------------------------*/
79 
80 extern vir_fits *vircam_fits_load(cpl_frame *frame, cpl_type type,
81  int nexten) {
82  vir_fits *p;
83  cpl_image *im;
84  int nf;
85  const char *fctid = "vircam_fits_load";
86 
87  /* Check for nonsense input */
88 
89  if (frame == NULL)
90  return(NULL);
91 
92  /* See if you can load the image */
93 
94  im = cpl_image_load(cpl_frame_get_filename(frame),type,0,(cpl_size)nexten);
95  if (im == NULL) {
96  cpl_msg_error(fctid,"Unable to load %s[%" CPL_SIZE_FORMAT "] -- %s\n",
97  cpl_frame_get_filename(frame),(cpl_size)nexten,
98  cpl_error_get_message());
99  cpl_error_reset();
100  return(NULL);
101  }
102 
103  /* Get the vir_fits structure */
104 
105  p = cpl_malloc(sizeof(vir_fits));
106 
107  /* Load stuff in */
108 
109  p->image = im;
110  p->nexten = nexten;
111  p->phu = NULL;
112  p->ehu = NULL;
113  p->fname = cpl_strdup(cpl_frame_get_filename(frame));
114  p->status = VIR_OK;
115 
116  /* Get the extension header and the extension name */
117 
118  (void)vircam_fits_get_ehu(p);
119  if (p->ehu == NULL)
120  return(NULL);
121  if (cpl_propertylist_has(p->ehu,"EXTNAME")) {
122  p->extname = cpl_strdup(cpl_propertylist_get_string(p->ehu,"EXTNAME"));
123  } else {
124  if (nexten == 0)
125  nf = 12;
126  else
127  nf = 11 + (int)log10((double)nexten);
128  p->extname = cpl_malloc(nf);
129  (void)snprintf(p->extname,nf,"DET1.CHIP%d",nexten);
130  }
131  nf = strlen(p->extname) + strlen(p->fname) + 3;
132  p->fullname = cpl_malloc(nf);
133  (void)snprintf(p->fullname,nf,"%s[%s]",p->fname,p->extname);
134 
135  /* Get out of here */
136 
137  return(p);
138 }
139 
140 /*---------------------------------------------------------------------------*/
157 /*---------------------------------------------------------------------------*/
158 
159 extern vir_fits *vircam_fits_duplicate(vir_fits *in) {
160  vir_fits *p;
161 
162  /* Check for nonsense input */
163 
164  if (in == NULL)
165  return(NULL);
166 
167  /* Get the vir_fits structure */
168 
169  p = cpl_malloc(sizeof(vir_fits));
170 
171  /*Initialize the structure*/
172 
173  p->image = NULL;
174  p->phu = NULL;
175  p->ehu = NULL;
176  p->fname = NULL;
177  p->extname = NULL;
178  p->fullname = NULL;
179  p->nexten = 0;
180  p->status = 0;
181 
182 
183  /* Now copy everything over */
184 
185  if (in->image != NULL)
186  p->image = cpl_image_duplicate(in->image);
187  if (vircam_fits_get_phu(in) != NULL)
188  p->phu = cpl_propertylist_duplicate(vircam_fits_get_phu(in));
189  if (vircam_fits_get_ehu(in) != NULL)
190  p->ehu = cpl_propertylist_duplicate(vircam_fits_get_ehu(in));
191  if (in->fname != NULL)
192  p->fname = cpl_strdup(in->fname);
193  if (in->extname != NULL)
194  p->extname = cpl_strdup(in->extname);
195  if (in->fullname != NULL)
196  p->fullname = cpl_strdup(in->fullname);
197  p->nexten = in->nexten;
198  p->status = in->status;
199 
200  /* Get out of here */
201 
202  return(p);
203 }
204 
205 
206 /*---------------------------------------------------------------------------*/
229 /*---------------------------------------------------------------------------*/
230 
231 extern vir_fits **vircam_fits_load_list(cpl_frameset *f, cpl_type type,
232  int exten) {
233  int i;
234  vir_fits **p;
235 
236  /* Check for nonsense input */
237 
238  if (f == NULL)
239  return(NULL);
240 
241  /* Get some workspace */
242 
243  p = cpl_malloc(cpl_frameset_get_size(f)*sizeof(vir_fits *));
244 
245  /* Now load each of the frames... */
246 
247  for (i = 0; i < cpl_frameset_get_size(f); i++) {
248  p[i] = vircam_fits_load(cpl_frameset_get_frame(f,i),type,exten);
249  if (p[i] == NULL) {
251  return(NULL);
252  }
253  }
254 
255  /* Now return the array */
256 
257  return(p);
258 }
259 
260 /*---------------------------------------------------------------------------*/
275 /*---------------------------------------------------------------------------*/
276 
277 extern void vircam_fits_delete(vir_fits *p) {
278 
279  /* Check for nonsense input */
280 
281  if (p == NULL)
282  return;
283 
284  /* Free up workspace if it's been used */
285 
286  freeimage(p->image);
287  freepropertylist(p->phu);
288  freepropertylist(p->ehu);
289  freespace(p->fname);
290  freespace(p->extname);
291  freespace(p->fullname);
292  cpl_free(p);
293 }
294 
295 /*---------------------------------------------------------------------------*/
312 /*---------------------------------------------------------------------------*/
313 
314 extern void vircam_fits_delete_list(vir_fits **p, int n) {
315  int i;
316 
317  /* Check for nonsense input */
318 
319  if (p == NULL)
320  return;
321 
322  /* Free up workspace if it's been used */
323 
324  for (i = 0; i < n; i++)
325  vircam_fits_delete(p[i]);
326  freespace(p);
327 }
328 
329 /*---------------------------------------------------------------------------*/
347 /*---------------------------------------------------------------------------*/
348 
349 extern cpl_image *vircam_fits_get_image(vir_fits *p) {
350 
351  /* Check for nonsense input */
352 
353  if (p == NULL)
354  return(NULL);
355 
356  /* Return it */
357 
358  return(p->image);
359 }
360 
361 /*---------------------------------------------------------------------------*/
380 /*---------------------------------------------------------------------------*/
381 
382 extern int vircam_fits_get_nexten(vir_fits *p) {
383 
384  /* Check for nonsense input */
385 
386  if (p == NULL)
387  return(-1);
388 
389  /* Return it */
390 
391  return(p->nexten);
392 }
393 
394 /*---------------------------------------------------------------------------*/
414 /*---------------------------------------------------------------------------*/
415 
416 extern cpl_propertylist *vircam_fits_get_phu(vir_fits *p) {
417 
418  /* Check for nonsense input */
419 
420  if (p == NULL)
421  return(NULL);
422 
423  /* If the propertylist hasn't already been loaded, then do it now */
424 
425  if (p->phu == NULL)
426  p->phu = cpl_propertylist_load(p->fname,0);
427 
428  /* Return it */
429 
430  return(p->phu);
431 }
432 
433 /*---------------------------------------------------------------------------*/
455 /*---------------------------------------------------------------------------*/
456 
457 extern cpl_propertylist *vircam_fits_get_ehu(vir_fits *p) {
458 
459  /* Check for nonsense input */
460 
461  if (p == NULL)
462  return(NULL);
463 
464  /* If the propertylist hasn't already been loaded, then do it now */
465 
466  if (p->ehu == NULL)
467  p->ehu = cpl_propertylist_load(p->fname,(cpl_size)(p->nexten));
468 
469  /* Return it */
470 
471  return(p->ehu);
472 }
473 
474 /*---------------------------------------------------------------------------*/
492 /*---------------------------------------------------------------------------*/
493 
494 extern char *vircam_fits_get_extname(vir_fits *p) {
495 
496  /* Check for nonsense input */
497 
498  if (p == NULL)
499  return(NULL);
500 
501  /* Return it */
502 
503  return(p->extname);
504 }
505 
506 /*---------------------------------------------------------------------------*/
524 /*---------------------------------------------------------------------------*/
525 
526 extern char *vircam_fits_get_filename(vir_fits *p) {
527 
528  /* Check for nonsense input */
529 
530  if (p == NULL)
531  return(NULL);
532 
533  /* Return it */
534 
535  return(p->fname);
536 }
537 
538 /*---------------------------------------------------------------------------*/
558 /*---------------------------------------------------------------------------*/
559 
560 extern char *vircam_fits_get_fullname(vir_fits *p) {
561 
562  /* Check for nonsense input */
563 
564  if (p == NULL)
565  return(NULL);
566 
567  /* Return it */
568 
569  return(p->fullname);
570 }
571 
572 /*---------------------------------------------------------------------------*/
589 /*---------------------------------------------------------------------------*/
590 
591 extern int vircam_fits_get_status(vir_fits *p) {
592 
593  /* Check for nonsense input */
594 
595  if (p == NULL)
596  return(VIR_FATAL);
597 
598  /* Return it */
599 
600  return(p->status);
601 }
602 
603 /*---------------------------------------------------------------------------*/
625 /*---------------------------------------------------------------------------*/
626 
627 extern int vircam_fits_set_error(vir_fits *p, int status) {
628 
629  /* Check for nonsense input */
630 
631  if (p == NULL)
632  return(0);
633 
634  /* Get out of here if the status is OK */
635 
636  if (status == VIR_OK)
637  return(0);
638 
639  /* Set the error status if there was an error */
640 
641  p->status = status;
642 
643  /* If there was a cpl error then spew that out now */
644 
645  if (cpl_error_get_code() != CPL_ERROR_NONE) {
646  cpl_msg_error("","%s",cpl_error_get_message());
647  cpl_error_reset();
648  }
649 
650  /* Get out of here */
651 
652  if (status == VIR_FATAL)
653  return(1);
654  else
655  return(0);
656 }
657 
658 /*---------------------------------------------------------------------------*/
681 /*---------------------------------------------------------------------------*/
682 
683 extern void vircam_fits_set_filename(vir_fits *p, char *fname) {
684 
685  /* Check for nonsense input */
686 
687  if (p == NULL || fname == NULL)
688  return;
689 
690  /* Set the name up and get out of here */
691 
692  freespace(p->fname);
693  p->fname = cpl_strdup(fname);
694 }
695 
696 /*---------------------------------------------------------------------------*/
723 /*---------------------------------------------------------------------------*/
724 
725 extern vir_fits *vircam_fits_wrap(cpl_image *im, vir_fits *model,
726  cpl_propertylist *phu,
727  cpl_propertylist *ehu) {
728  vir_fits *p;
729 
730  /* Check for nonsense input */
731 
732  if (im == NULL)
733  return(NULL);
734 
735  /* Get the vir_fits structure */
736 
737  p = cpl_malloc(sizeof(vir_fits));
738 
739  /* Load stuff in */
740 
741  p->image = im;
742  p->nexten = -1;
743  if (phu != NULL)
744  p->phu = cpl_propertylist_duplicate(phu);
745  else if (model != NULL)
746  p->phu = cpl_propertylist_duplicate(vircam_fits_get_phu(model));
747  else
748  p->phu = NULL;
749  if (ehu != NULL)
750  p->ehu = cpl_propertylist_duplicate(ehu);
751  else if (model != NULL)
752  p->ehu = cpl_propertylist_duplicate(vircam_fits_get_ehu(model));
753  else
754  p->ehu = NULL;
755  p->fname = NULL;
756  p->status = VIR_OK;
757  p->extname = NULL;
758  p->fullname = NULL;
759 
760  /* Get out of here */
761 
762  return(p);
763 }
764 /*---------------------------------------------------------------------------*/
779 /*---------------------------------------------------------------------------*/
780 
781 extern void vircam_fits_unwrap(vir_fits *p) {
782 
783  /* Check for nonsense input */
784 
785  if (p == NULL)
786  return;
787 
788  /* Free up workspace if it's been used */
789 
790  freepropertylist(p->phu);
791  freepropertylist(p->ehu);
792  freespace(p->fname);
793  freespace(p->extname);
794  freespace(p->fullname);
795  cpl_free(p);
796 }
797 
800 /*
801 
802 $Log: not supported by cvs2svn $
803 Revision 1.23 2012/01/15 17:40:09 jim
804 Minor modifications to take into accout the changes in cpl API for v6
805 
806 Revision 1.22 2010/06/03 12:15:31 jim
807 A few mods to get rid of compiler warnings
808 
809 Revision 1.21 2009/05/20 12:19:29 jim
810 Added vircam_fits_unwrap
811 
812 Revision 1.20 2008/10/21 08:37:59 jim
813 Added vircam_fits_set_filename
814 
815 Revision 1.19 2007/11/14 10:47:08 jim
816 Modified vircam_fits_duplicate so that in case the pointers to the
817 input headers are NULL, then this isn't an error
818 
819 Revision 1.18 2007/10/25 17:34:00 jim
820 Modified to remove lint warnings
821 
822 Revision 1.17 2007/10/19 09:25:09 jim
823 Fixed problems with missing includes
824 
825 Revision 1.16 2007/10/15 12:50:28 jim
826 Modified for compatibility with cpl_4.0
827 
828 Revision 1.15 2007/07/18 15:33:06 jim
829 Modified error message in vircam_fits_load to include extension number
830 
831 Revision 1.14 2007/03/01 12:42:41 jim
832 Modified slightly after code checking
833 
834 Revision 1.13 2007/02/20 21:13:13 jim
835 Better error reporting in case of load failure
836 
837 Revision 1.12 2006/10/31 10:26:51 jim
838 Added vircam_fits_get_extname
839 
840 Revision 1.11 2006/08/07 14:20:14 jim
841 Added vircam_fits_duplicate
842 
843 Revision 1.10 2006/07/07 09:34:00 jim
844 wrap routine now duplicates input property lists rather than using the
845 input pointer
846 
847 Revision 1.9 2006/07/04 09:19:05 jim
848 replaced all sprintf statements with snprintf
849 
850 Revision 1.8 2006/05/26 15:02:21 jim
851 Fixed bad error message call
852 
853 Revision 1.7 2006/05/24 13:34:19 jim
854 Added vircam_fits_wrap
855 
856 Revision 1.6 2006/04/30 22:12:40 jim
857 Fixed memory bug
858 
859 Revision 1.5 2006/04/24 13:46:36 jim
860 A bit more error trapping in case fits structures can't be loaded
861 
862 Revision 1.4 2006/04/20 11:21:21 jim
863 Added _fullname method
864 
865 Revision 1.3 2006/03/22 13:32:25 jim
866 Cosmetic changes to keep lint happy
867 
868 Revision 1.2 2006/03/03 14:29:45 jim
869 Modified definition of vir_fits and channel table
870 
871 Revision 1.1 2006/03/01 10:31:03 jim
872 new files
873 
874 
875 */