OMEGA Pipeline Reference Manual  1.0.5
omega_fits.c
1 /* $Id: omega_fits.c,v 1.2 2012-01-31 13:38:50 agabasch Exp $
2  *
3  * This file is part of the OMEGA 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: agabasch $
23  * $Date: 2012-01-31 13:38:50 $
24  * $Revision: 1.2 $
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 
39 #include "omega_utils.h"
40 #include "omega_fits.h"
41 
59 /*---------------------------------------------------------------------------*/
82 /*---------------------------------------------------------------------------*/
83 
84 extern omega_fits *omega_fits_load(const cpl_frame *inframe, cpl_type type,
85  int extnum)
86 {
87  omega_fits *p;
88  cpl_image *im;
89  cpl_frame *fr;
90  int nf;
91 
92  /* Check for nonsense input */
93 
94  if (inframe == NULL)
95  return(NULL);
96 
97  /* Duplicate the frame */
98  fr = cpl_frame_duplicate(inframe);
99 
100  /* See if you can load the image */
101  im = cpl_image_load(cpl_frame_get_filename(inframe),type,0,extnum);
102  if (im == NULL) {
103  cpl_msg_error(cpl_func,"Unable to load %s -- %s\n",
104  cpl_frame_get_filename(inframe),cpl_error_get_message());
105  return(NULL);
106  }
107 
108  /* Get the omega_fits structure */
109 
110  p = cpl_malloc(sizeof(omega_fits));
111 
112  /* Load stuff in */
113 
114  p->frame = fr;
115  p->image = im;
116  p->extnum = extnum;
117  p->phu = NULL;
118  p->ehu = NULL;
119  p->fname = cpl_strdup(cpl_frame_get_filename(inframe));
120  p->status = 0;
121 
122  /* Get the extension header and the extension name */
123 
124  (void)omega_fits_get_ehu(p);
125  if (p->ehu == NULL)
126  return(NULL);
127  if (cpl_propertylist_has(p->ehu,"EXTNAME")) {
128  p->extname = cpl_strdup(cpl_propertylist_get_string(p->ehu,"EXTNAME"));
129  }
130  else {
131  nf = 11 + log10(extnum);
132  p->extname = cpl_malloc(nf);
133  (void)snprintf(p->extname,nf,"DET1.CHIP%d",extnum);
134  }
135  nf = strlen(p->extname) + strlen(p->fname) + 3;
136  p->fullname = cpl_malloc(nf);
137  (void)snprintf(p->fullname,nf,"%s[%s]",p->fname,p->extname);
138 
139  /* Get out of here */
140 
141  return(p);
142 }
143 
144 /*---------------------------------------------------------------------------*/
161 /*---------------------------------------------------------------------------*/
162 
163 extern omega_fits *omega_fits_duplicate(omega_fits *in) {
164  omega_fits *p;
165 
166  /* Check for nonsense input */
167 
168  if (in == NULL)
169  return(NULL);
170 
171  /* Get the omega_fits structure */
172 
173  p = cpl_malloc(sizeof(omega_fits));
174 
175  /*Initialize the structure*/
176  p->frame = NULL;
177  p->image = NULL;
178  p->phu = NULL;
179  p->ehu = NULL;
180  p->fname = NULL;
181  p->extname = NULL;
182  p->fullname = NULL;
183  p->extnum = 0;
184  p->status = 0;
185 
186  /* Now copy everything over */
187 
188  if (in->frame !=NULL) p->frame = cpl_frame_duplicate(in->frame);
189  if (in->image != NULL) p->image = cpl_image_duplicate(in->image);
190  if (in->phu != NULL) p->phu = cpl_propertylist_duplicate(in->phu);
191  if (in->ehu != NULL) p->ehu = cpl_propertylist_duplicate(in->ehu);
192  if (in->fname != NULL) p->fname = cpl_strdup(in->fname);
193  if (in->extname != NULL) p->extname = cpl_strdup(in->extname);
194  if (in->fullname != NULL) p->fullname = cpl_strdup(in->fullname);
195  p->extnum = in->extnum;
196  p->status = in->status;
197 
198  /* Get out of here */
199 
200  return(p);
201 }
202 
203 
204 /*---------------------------------------------------------------------------*/
227 /*---------------------------------------------------------------------------*/
228 
229 extern omega_fits **omega_fits_load_list(const cpl_frameset *f, cpl_type type,
230  int exten) {
231  int i;
232  omega_fits **p;
233 
234  /* Check for nonsense input */
235 
236  if (f == NULL)
237  return(NULL);
238 
239  /* Get some workspace */
240 
241  p = cpl_malloc(cpl_frameset_get_size(f)*sizeof(omega_fits *));
242 
243  /* Now load each of the frames... */
244 
245  for (i = 0; i < cpl_frameset_get_size(f); i++) {
246  p[i] = omega_fits_load(cpl_frameset_get_frame_const(f,i),type,exten);
247  if (p[i] == NULL) {
248  omega_fits_delete_list(p,i-1);
249  return(NULL);
250  }
251  }
252 
253  /* Now return the array */
254 
255  return(p);
256 }
257 
258 /*---------------------------------------------------------------------------*/
273 /*---------------------------------------------------------------------------*/
274 
275 extern void omega_fits_delete(omega_fits *p) {
276 
277  /* Check for nonsense input */
278 
279  if (p == NULL)
280  return;
281 
282  /* Free up workspace if it's been used */
283 
284  freeframe(p->frame);
285  freeimage(p->image);
286  freeplist(p->phu);
287  freeplist(p->ehu);
288  freespace(p->fname);
289  freespace(p->extname);
290  freespace(p->fullname);
291  cpl_free(p);
292 }
293 
294 /*---------------------------------------------------------------------------*/
311 /*---------------------------------------------------------------------------*/
312 
313 extern void omega_fits_delete_list(omega_fits **p, int n) {
314  int i;
315 
316  /* Check for nonsense input */
317 
318  if (p == NULL)
319  return;
320 
321  /* Free up workspace if it's been used */
322 
323  for (i = 0; i < n; i++)
324  omega_fits_delete(p[i]);
325 
326  freespace(p);
327 }
328 
329 /*---------------------------------------------------------------------------*/
347 /*---------------------------------------------------------------------------*/
348 
349 extern cpl_frame *omega_fits_get_frame(omega_fits *p) {
350 
351  /* Check for nonsense input */
352 
353  if (p == NULL)
354  return(NULL);
355 
356  /* Return it */
357 
358  return(p->frame);
359 }
360 
361 /*---------------------------------------------------------------------------*/
379 /*---------------------------------------------------------------------------*/
380 
381 extern cpl_image *omega_fits_get_image(omega_fits *p) {
382 
383  /* Check for nonsense input */
384 
385  if (p == NULL)
386  return(NULL);
387 
388  /* Return it */
389 
390  return(p->image);
391 }
392 
393 /*---------------------------------------------------------------------------*/
412 /*---------------------------------------------------------------------------*/
413 
414 extern int omega_fits_get_extnum(omega_fits *p) {
415 
416  /* Check for nonsense input */
417 
418  if (p == NULL)
419  return(-1);
420 
421  /* Return it */
422 
423  return(p->extnum);
424 }
425 
426 /*---------------------------------------------------------------------------*/
446 /*---------------------------------------------------------------------------*/
447 
448 extern cpl_propertylist *omega_fits_get_phu(omega_fits *p) {
449 
450  /* Check for nonsense input */
451 
452  if (p == NULL)
453  return(NULL);
454 
455  /* If the propertylist hasn't already been loaded, then do it now */
456 
457  if (p->phu == NULL)
458  p->phu = cpl_propertylist_load(p->fname,0);
459 
460  /* Return it */
461 
462  return(p->phu);
463 }
464 
465 /*---------------------------------------------------------------------------*/
487 /*---------------------------------------------------------------------------*/
488 
489 extern cpl_propertylist *omega_fits_get_ehu(omega_fits *p)
490 {
491 
492  /* Check for nonsense input */
493 
494  if (p == NULL){
495  return(NULL);
496  }
497 
498  /* If the propertylist hasn't already been loaded, then do it now */
499 
500  if (p->ehu == NULL) {
501  p->ehu = cpl_propertylist_load(p->fname,p->extnum);
502  }
503 
504  /* Return it */
505 
506  return(p->ehu);
507 }
508 
509 /*---------------------------------------------------------------------------*/
527 /*---------------------------------------------------------------------------*/
528 
529 extern char *omega_fits_get_extname(omega_fits *p) {
530 
531  /* Check for nonsense input */
532 
533  if (p == NULL)
534  return(NULL);
535 
536  /* Return it */
537 
538  return(p->extname);
539 }
540 
541 /*---------------------------------------------------------------------------*/
559 /*---------------------------------------------------------------------------*/
560 
561 extern char *omega_fits_get_filename(omega_fits *p) {
562 
563  /* Check for nonsense input */
564 
565  if (p == NULL)
566  return(NULL);
567 
568  /* Return it */
569 
570  return(p->fname);
571 }
572 
573 /*---------------------------------------------------------------------------*/
593 /*---------------------------------------------------------------------------*/
594 
595 extern char *omega_fits_get_fullname(omega_fits *p) {
596 
597  /* Check for nonsense input */
598 
599  if (p == NULL)
600  return(NULL);
601 
602  /* Return it */
603 
604  return(p->fullname);
605 }
606 
607 /*---------------------------------------------------------------------------*/
624 /*---------------------------------------------------------------------------*/
625 
626 extern int omega_fits_get_status(omega_fits *p) {
627 
628  /* Check for nonsense input */
629 
630  if (p == NULL)
631  return(-1);
632 
633  /* Return it */
634 
635  return(p->status);
636 }
637 
638 
639 /*---------------------------------------------------------------------------*/
661 /*---------------------------------------------------------------------------*/
662 
663 extern int omega_fits_set_error(omega_fits *p, int status) {
664 
665  /* Check for nonsense input */
666 
667  if (p == NULL)
668  return(0);
669 
670  /* Get out of here if the status is OK */
671 
672  if (status == 0)
673  return(0);
674 
675  /* Set the error status if there was an error */
676 
677  p->status = status;
678 
679  /* If there was a cpl error then spell that out now */
680 
681  if (cpl_error_get_code() != CPL_ERROR_NONE) {
682  cpl_msg_error("","%s",cpl_error_get_message());
683  cpl_error_reset();
684  }
685 
686  /* Get out of here */
687 
688  if (status == -1)
689  return(-1);
690  else
691  return(0);
692 }
693 
694 /*---------------------------------------------------------------------------*/
717 /*---------------------------------------------------------------------------*/
718 
719 extern void omega_fits_set_filename(omega_fits *p, char *fname) {
720 
721  /* Check for nonsense input */
722 
723  if (p == NULL || fname == NULL)
724  return;
725 
726  /* Set the name up and get out of here */
727 
728  freespace(p->fname);
729  p->fname = cpl_strdup(fname);
730 }
731 
732 /*---------------------------------------------------------------------------*/
759 /*---------------------------------------------------------------------------*/
760 
761 extern omega_fits *omega_fits_wrap(cpl_image *im, omega_fits *model,
762  cpl_propertylist *phu,
763  cpl_propertylist *ehu) {
764  omega_fits *p;
765 
766  /* Check for nonsense input */
767 
768  if (im == NULL)
769  return(NULL);
770 
771  /* Get the omega_fits structure */
772 
773  p = cpl_malloc(sizeof(omega_fits));
774 
775  /* Load stuff in */
776 
777  p->frame = NULL;
778  p->image = im;
779  p->extnum = -1;
780  if (phu != NULL)
781  p->phu = cpl_propertylist_duplicate(phu);
782  else if (model != NULL)
783  p->phu = cpl_propertylist_duplicate(omega_fits_get_phu(model));
784  else
785  p->phu = NULL;
786  if (ehu != NULL)
787  p->ehu = cpl_propertylist_duplicate(ehu);
788  else if (model != NULL)
789  p->ehu = cpl_propertylist_duplicate(omega_fits_get_ehu(model));
790  else
791  p->ehu = NULL;
792  p->fname = NULL;
793  p->status = 0;
794  p->extname = NULL;
795  p->fullname = NULL;
796 
797  /* Get out of here */
798 
799  return(p);
800 }
801