VIRCAM Pipeline  2.3.10
casu_fits.c
1 /* $Id: casu_fits.c,v 1.4 2015/09/14 18:45:51 jim Exp $
2  *
3  * This file is part of the CASU Pipeline utilities
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: 2015/09/14 18:45:51 $
24  * $Revision: 1.4 $
25  * $Name: $
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 "casu_utils.h"
40 #include "casu_fits.h"
41 
55 /*---------------------------------------------------------------------------*/
78 /*---------------------------------------------------------------------------*/
79 
80 extern casu_fits *casu_fits_load(cpl_frame *frame, cpl_type type, int nexten) {
81  casu_fits *p;
82  cpl_image *im,*im2;
83  cpl_propertylist *ph,*eh;
84  int nf;
85  char extname[81] = "";
86  const char *fctid = "casu_fits_load";
87 
88  /* Check for nonsense input */
89 
90  if (frame == NULL)
91  return(NULL);
92 
93  /* See if you can load the image */
94 
95  im = cpl_image_load(cpl_frame_get_filename(frame),type,0,(cpl_size)nexten);
96  if (im == NULL) {
97  cpl_msg_error(fctid,"Unable to load %s[%" CPL_SIZE_FORMAT "] -- %s",
98  cpl_frame_get_filename(frame),(cpl_size)nexten,
99  cpl_error_get_message());
100  cpl_error_reset();
101  return(NULL);
102  }
103 
104  /* If this is an unspecified type, the force it to be float */
105 
106  if (type == CPL_TYPE_UNSPECIFIED &&
107  cpl_image_get_type(im) != CPL_TYPE_FLOAT) {
108  im2 = cpl_image_cast(im,CPL_TYPE_FLOAT);
109  cpl_image_delete(im);
110  im = im2;
111  }
112 
113  /* Get the casu_fits structure */
114 
115  p = cpl_malloc(sizeof(casu_fits));
116 
117  /* Load stuff in */
118 
119  p->image = im;
120  p->nexten = nexten;
121  p->phu = NULL;
122  p->ehu = NULL;
123  p->fname = cpl_strdup(cpl_frame_get_filename(frame));
124  p->type = type;
125  p->status = CASU_OK;
126 
127  /* What type of file do we have? Also extract the extension name if
128  it exists. */
129 
130  p->extname = NULL;
131  if (cpl_frame_get_nextensions(frame) == 0) {
132  p->casufitstype = CASU_FITS_SIMPLE;
133  p->extname = cpl_strdup("0");
134  } else {
135  ph = cpl_propertylist_load(p->fname,0);
136  eh = cpl_propertylist_load(p->fname,nexten);
137  if (cpl_propertylist_get_int(ph,"NAXIS") != 0) {
138  p->casufitstype = CASU_FITS_MEF_NOPHU;
139  if (cpl_propertylist_has(eh,"EXTNAME")) {
140  strcpy(extname,cpl_propertylist_get_string(eh,"EXTNAME"));
141  if (strcmp(extname,"COMPRESSED_IMAGE")) {
142  p->extname = cpl_strdup(extname);
143  } else {
144  (void)sprintf(extname,"%d",nexten);
145  p->extname = cpl_strdup(extname);
146  }
147  }
148  } else if (cpl_propertylist_has(eh,"ZSIMPLE")) {
149  p->casufitstype = CASU_FITS_SIMPLE_CMP;
150  p->extname = cpl_strdup("0");
151  } else {
152  p->casufitstype = CASU_FITS_MEF;
153  if (cpl_propertylist_has(eh,"EXTNAME")) {
154  strcpy(extname,cpl_propertylist_get_string(eh,"EXTNAME"));
155  if (strcmp(extname,"COMPRESSED_IMAGE")) {
156  p->extname = cpl_strdup(extname);
157  } else {
158  (void)sprintf(extname,"%d",nexten);
159  p->extname = cpl_strdup(extname);
160  }
161  }
162  }
163  cpl_propertylist_delete(eh);
164  cpl_propertylist_delete(ph);
165  }
166  if (p->extname == NULL) {
167  (void)sprintf(extname,"%d",nexten);
168  p->extname = cpl_strdup(extname);
169  }
170 
171  /* Create full name string */
172 
173  nf = strlen(p->extname) + strlen(p->fname) + 3;
174  p->fullname = cpl_malloc(nf);
175  (void)snprintf(p->fullname,nf,"%s[%s]",p->fname,p->extname);
176 
177  /* Get out of here */
178 
179  return(p);
180 }
181 
182 /*---------------------------------------------------------------------------*/
198 /*---------------------------------------------------------------------------*/
199 
200 extern void casu_fits_unload_im(casu_fits *in) {
201 
202  freeimage(in->image);
203 }
204 
205 
206 /*---------------------------------------------------------------------------*/
223 /*---------------------------------------------------------------------------*/
224 
225 extern casu_fits *casu_fits_duplicate(casu_fits *in) {
226  casu_fits *p;
227 
228  /* Check for nonsense input */
229 
230  if (in == NULL)
231  return(NULL);
232 
233  /* Get the casu_fits structure */
234 
235  p = cpl_malloc(sizeof(casu_fits));
236 
237  /* Now copy everything over */
238 
239  if (in->image != NULL)
240  p->image = cpl_image_duplicate(in->image);
241  else
242  p->image = NULL;
243  p->phu = cpl_propertylist_duplicate(casu_fits_get_phu(in));
244  p->ehu = cpl_propertylist_duplicate(casu_fits_get_ehu(in));
245  p->fname = cpl_strdup(in->fname);
246  p->extname = cpl_strdup(in->extname);
247  p->fullname = cpl_strdup(in->fullname);
248  p->nexten = in->nexten;
249  p->status = in->status;
250  p->casufitstype = in->casufitstype;
251  p->type = in->type;
252 
253  /* Get out of here */
254 
255  return(p);
256 }
257 
258 /*---------------------------------------------------------------------------*/
277 /*---------------------------------------------------------------------------*/
278 
279 extern void casu_fits_replace_image(casu_fits *in, cpl_image *image) {
280 
281  /* Check for nonsense input */
282 
283  if (in == NULL || image == NULL)
284  return;
285 
286  /* Delete the old image and replace it with the new one */
287 
288  if (in->image != NULL)
289  cpl_image_delete(in->image);
290  in->image = image;
291 }
292 
293 /*---------------------------------------------------------------------------*/
316 /*---------------------------------------------------------------------------*/
317 
318 extern casu_fits **casu_fits_load_list(cpl_frameset *f, cpl_type type,
319  int exten) {
320  int i;
321  casu_fits **p;
322 
323  /* Check for nonsense input */
324 
325  if (f == NULL)
326  return(NULL);
327 
328  /* Get some workspace */
329 
330  p = cpl_malloc(cpl_frameset_get_size(f)*sizeof(casu_fits *));
331 
332  /* Now load each of the frames... */
333 
334  for (i = 0; i < cpl_frameset_get_size(f); i++) {
335  p[i] = casu_fits_load(cpl_frameset_get_position(f,i),type,exten);
336  if (p[i] == NULL) {
337  casu_fits_delete_list(p,i-1);
338  return(NULL);
339  }
340  }
341 
342  /* Now return the array */
343 
344  return(p);
345 }
346 
347 /*---------------------------------------------------------------------------*/
362 /*---------------------------------------------------------------------------*/
363 
364 extern void casu_fits_delete(casu_fits *p) {
365 
366  /* Check for nonsense input */
367 
368  if (p == NULL)
369  return;
370 
371  /* Free up workspace if it's been used */
372 
373  freeimage(p->image);
374  freepropertylist(p->phu);
375  freepropertylist(p->ehu);
376  freespace(p->fname);
377  freespace(p->extname);
378  freespace(p->fullname);
379  cpl_free(p);
380 }
381 
382 /*---------------------------------------------------------------------------*/
399 /*---------------------------------------------------------------------------*/
400 
401 extern void casu_fits_delete_list(casu_fits **p, int n) {
402  int i;
403 
404  /* Check for nonsense input */
405 
406  if (p == NULL)
407  return;
408 
409  /* Free up workspace if it's been used */
410 
411  for (i = 0; i < n; i++)
412  casu_fits_delete(p[i]);
413  cpl_free(p);
414 }
415 
416 /*---------------------------------------------------------------------------*/
434 /*---------------------------------------------------------------------------*/
435 
436 extern cpl_image *casu_fits_get_image(casu_fits *p) {
437  const char *fctid = "casu_fits_get_image";
438  cpl_image *im2;
439 
440  /* Check for nonsense input */
441 
442  if (p == NULL)
443  return(NULL);
444 
445  /* If the image is already loaded, then return it now */
446 
447  if (p->image != NULL)
448  return(p->image);
449 
450  /* If the image hasn't been loaded, then do that now */
451 
452  if (p->image == NULL)
453  p->image = cpl_image_load(p->fname,p->type,0,(cpl_size)(p->nexten));
454  if (p->image == NULL) {
455  cpl_msg_error(fctid,"Unable to load %s[%" CPL_SIZE_FORMAT "] -- %s\n",
456  p->fname,(cpl_size)(p->nexten),
457  cpl_error_get_message());
458  cpl_error_reset();
459  return(NULL);
460  }
461 
462  /* If this is an unspecified type, the force it to be float */
463 
464  if (p->type == CPL_TYPE_UNSPECIFIED &&
465  cpl_image_get_type(p->image) != CPL_TYPE_FLOAT) {
466  im2 = cpl_image_cast(p->image,CPL_TYPE_FLOAT);
467  cpl_image_delete(p->image);
468  p->image = im2;
469  }
470 
471  /* Return it */
472 
473  return(p->image);
474 }
475 
476 /*---------------------------------------------------------------------------*/
495 /*---------------------------------------------------------------------------*/
496 
497 extern int casu_fits_get_nexten(casu_fits *p) {
498 
499  /* Check for nonsense input */
500 
501  if (p == NULL)
502  return(-1);
503 
504  /* Return it */
505 
506  return(p->nexten);
507 }
508 
509 /*---------------------------------------------------------------------------*/
529 /*---------------------------------------------------------------------------*/
530 
531 extern cpl_propertylist *casu_fits_get_phu(casu_fits *p) {
532 
533  /* Check for nonsense input */
534 
535  if (p == NULL)
536  return(NULL);
537 
538  /* If the propertylist hasn't already been loaded, then do it now */
539 
540  if (p->phu == NULL) {
541  if (p->casufitstype == CASU_FITS_MEF)
542  p->phu = cpl_propertylist_load(p->fname,0);
543  else
544  p->phu = cpl_propertylist_load(p->fname,p->nexten);
545  }
546 
547  /* Return it */
548 
549  return(p->phu);
550 }
551 
552 /*---------------------------------------------------------------------------*/
574 /*---------------------------------------------------------------------------*/
575 
576 extern cpl_propertylist *casu_fits_get_ehu(casu_fits *p) {
577 
578  /* Check for nonsense input */
579 
580  if (p == NULL)
581  return(NULL);
582 
583  /* If the propertylist hasn't already been loaded, then do it now */
584 
585  if (p->ehu == NULL)
586  p->ehu = cpl_propertylist_load(p->fname,(cpl_size)(p->nexten));
587 
588  /* Return it */
589 
590  return(p->ehu);
591 }
592 
593 /*---------------------------------------------------------------------------*/
611 /*---------------------------------------------------------------------------*/
612 
613 extern char *casu_fits_get_extname(casu_fits *p) {
614 
615  /* Check for nonsense input */
616 
617  if (p == NULL)
618  return(NULL);
619 
620  /* Return it */
621 
622  return(p->extname);
623 }
624 
625 /*---------------------------------------------------------------------------*/
644 /*---------------------------------------------------------------------------*/
645 
646 extern char *casu_fits_get_filename(casu_fits *p) {
647 
648  /* Check for nonsense input */
649 
650  if (p == NULL)
651  return(NULL);
652 
653  /* Return it */
654 
655  return(p->fname);
656 }
657 
658 /*---------------------------------------------------------------------------*/
678 /*---------------------------------------------------------------------------*/
679 
680 extern char *casu_fits_get_fullname(casu_fits *p) {
681 
682  /* Check for nonsense input */
683 
684  if (p == NULL)
685  return(NULL);
686 
687  /* Return it */
688 
689  return(p->fullname);
690 }
691 
692 /*---------------------------------------------------------------------------*/
709 /*---------------------------------------------------------------------------*/
710 
711 extern int casu_fits_get_status(casu_fits *p) {
712 
713  /* Check for nonsense input */
714 
715  if (p == NULL)
716  return(CASU_FATAL);
717 
718  /* Return it */
719 
720  return(p->status);
721 }
722 
723 /*---------------------------------------------------------------------------*/
745 /*---------------------------------------------------------------------------*/
746 
747 extern int casu_fits_set_error(casu_fits *p, int status) {
748 
749  /* Check for nonsense input */
750 
751  if (p == NULL)
752  return(0);
753 
754  /* Get out of here if the status is OK */
755 
756  if (status == CASU_OK)
757  return(0);
758 
759  /* Set the error status if there was an error */
760 
761  p->status = status;
762 
763  /* If there was a cpl error then spew that out now */
764 
765  if (cpl_error_get_code() != CPL_ERROR_NONE) {
766  cpl_msg_error("","%s",cpl_error_get_message());
767  cpl_error_reset();
768  }
769 
770  /* Get out of here */
771 
772  if (status == CASU_FATAL)
773  return(1);
774  else
775  return(0);
776 }
777 
778 /*---------------------------------------------------------------------------*/
799 /*---------------------------------------------------------------------------*/
800 
801 extern void casu_fits_set_status(casu_fits *p, int status) {
802 
803  /* Check for nonsense input */
804 
805  if (p == NULL)
806  return;
807 
808  /* Update the status */
809 
810  p->status = status;
811 
812 }
813 
814 /*---------------------------------------------------------------------------*/
837 /*---------------------------------------------------------------------------*/
838 
839 extern void casu_fits_set_filename(casu_fits *p, char *fname) {
840 
841  /* Check for nonsense input */
842 
843  if (p == NULL || fname == NULL)
844  return;
845 
846  /* Set the name up and get out of here */
847 
848  freespace(p->fname);
849  p->fname = cpl_strdup(fname);
850 }
851 
852 /*---------------------------------------------------------------------------*/
881 /*---------------------------------------------------------------------------*/
882 
883 extern casu_fits *casu_fits_wrap(cpl_image *im, casu_fits *model,
884  cpl_propertylist *phu, cpl_propertylist *ehu) {
885  casu_fits *p;
886 
887  /* Check for nonsense input */
888 
889  if (im == NULL)
890  return(NULL);
891 
892  /* Get the casu_fits structure */
893 
894  p = cpl_malloc(sizeof(casu_fits));
895 
896  /* Load stuff in */
897 
898  p->image = im;
899  p->nexten = -1;
900  if (phu != NULL)
901  p->phu = cpl_propertylist_duplicate(phu);
902  else if (model != NULL)
903  p->phu = cpl_propertylist_duplicate(casu_fits_get_phu(model));
904  else
905  p->phu = cpl_propertylist_new();
906  if (ehu != NULL)
907  p->ehu = cpl_propertylist_duplicate(ehu);
908  else if (model != NULL)
909  p->ehu = cpl_propertylist_duplicate(casu_fits_get_ehu(model));
910  else
911  p->ehu = cpl_propertylist_new();
912  p->fname = NULL;
913  p->status = CASU_OK;
914  p->extname = NULL;
915  p->fullname = NULL;
916  if (model != NULL)
917  p->casufitstype = model->casufitstype;
918  else
919  p->casufitstype = CASU_FITS_MEF;
920  p->type = cpl_image_get_type(im);
921 
922  /* Get out of here */
923 
924  return(p);
925 }
926 /*---------------------------------------------------------------------------*/
943 /*---------------------------------------------------------------------------*/
944 
945 extern void casu_fits_unwrap(casu_fits *p) {
946 
947  /* Check for nonsense input */
948 
949  if (p == NULL)
950  return;
951 
952  /* Free up workspace if it's been used */
953 
954  freepropertylist(p->phu);
955  freepropertylist(p->ehu);
956  freespace(p->fname);
957  freespace(p->extname);
958  freespace(p->fullname);
959  cpl_free(p);
960 }
961 
964 /*
965 
966 $Log: casu_fits.c,v $
967 Revision 1.4 2015/09/14 18:45:51 jim
968 replaced a free by cpl_free
969 
970 Revision 1.3 2015/08/07 13:06:54 jim
971 Fixed copyright to ESO
972 
973 Revision 1.2 2015/08/06 05:34:02 jim
974 Fixes to get rid of compiler moans
975 
976 Revision 1.1.1.1 2015/06/12 10:44:32 jim
977 Initial import
978 
979 Revision 1.10 2015/01/29 11:48:15 jim
980 modified comments
981 
982 Revision 1.9 2015/01/09 12:13:15 jim
983 *** empty log message ***
984 
985 Revision 1.8 2014/12/11 12:23:33 jim
986 new version
987 
988 Revision 1.7 2014/04/09 11:08:21 jim
989 Get rid of a couple of compiler moans
990 
991 Revision 1.6 2014/04/09 09:09:51 jim
992 Detabbed
993 
994 Revision 1.5 2014/03/26 15:38:01 jim
995 Modified so that using type CPL_TYPE_UNSPECIFIED will force a floating
996 point image. Also removed calls to deprecated cpl routine
997 
998 Revision 1.4 2013/11/21 09:38:13 jim
999 detabbed
1000 
1001 Revision 1.3 2013/11/08 06:51:39 jim
1002 added casu_fits_unload_im
1003 
1004 Revision 1.2 2013-10-24 09:18:37 jim
1005 Now tracks the type of fits file using casufitstype
1006 
1007 Revision 1.1.1.1 2013-08-27 12:07:48 jim
1008 Imported
1009 
1010 
1011 */
char * casu_fits_get_extname(casu_fits *p)
Definition: casu_fits.c:613
int casu_fits_get_status(casu_fits *p)
Definition: casu_fits.c:711
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
void casu_fits_set_filename(casu_fits *p, char *fname)
Definition: casu_fits.c:839
casu_fits * casu_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: casu_fits.c:80
casu_fits ** casu_fits_load_list(cpl_frameset *f, cpl_type type, int exten)
Definition: casu_fits.c:318
char * casu_fits_get_filename(casu_fits *p)
Definition: casu_fits.c:646
void casu_fits_delete_list(casu_fits **p, int n)
Definition: casu_fits.c:401
char * casu_fits_get_fullname(casu_fits *p)
Definition: casu_fits.c:680
void casu_fits_delete(casu_fits *p)
Definition: casu_fits.c:364
casu_fits * casu_fits_duplicate(casu_fits *in)
Definition: casu_fits.c:225
int casu_fits_set_error(casu_fits *p, int status)
Definition: casu_fits.c:747
casu_fits * casu_fits_wrap(cpl_image *im, casu_fits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: casu_fits.c:883
void casu_fits_unload_im(casu_fits *in)
Definition: casu_fits.c:200
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
Definition: casu_fits.c:531
void casu_fits_set_status(casu_fits *p, int status)
Definition: casu_fits.c:801
int casu_fits_get_nexten(casu_fits *p)
Definition: casu_fits.c:497
void casu_fits_unwrap(casu_fits *p)
Definition: casu_fits.c:945
void casu_fits_replace_image(casu_fits *in, cpl_image *image)
Definition: casu_fits.c:279
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
Definition: casu_fits.c:576