VIRCAM Pipeline  2.3.12
casu_tfits.c
1 /* $Id: casu_tfits.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_tfits.h"
41 
55 /*---------------------------------------------------------------------------*/
76 /*---------------------------------------------------------------------------*/
77 
78 extern casu_tfits *casu_tfits_load(cpl_frame *table, int nexten) {
79  casu_tfits *p;
80  cpl_table *tab;
81  int nf;
82  const char *fctid = "casu_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",
94  cpl_frame_get_filename(table),cpl_error_get_message());
95  cpl_error_reset();
96  return(NULL);
97  }
98 
99  /* Get the casu_tfits structure */
100 
101  p = cpl_malloc(sizeof(casu_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 = CASU_OK;
111 
112  /* Get the extension header and the extension name */
113 
114  (void)casu_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 casu_tfits *casu_tfits_extract(casu_tfits *in) {
153  casu_tfits *p;
154 
155  /* Check for nonsense input */
156 
157  if (in == NULL)
158  return(NULL);
159 
160  /* Get the casu_tfits structure */
161 
162  p = cpl_malloc(sizeof(casu_tfits));
163 
164  /* Load stuff in */
165 
166  p->table = cpl_table_extract_selected(casu_tfits_get_table(in));
167  p->nexten = casu_tfits_get_nexten(in);
168  p->phu = NULL;
169  p->ehu = NULL;
170  p->fname = cpl_strdup(casu_tfits_get_filename(in));
171 
172  /* Get out of here */
173 
174  return(p);
175 }
176 
177 /*---------------------------------------------------------------------------*/
194 /*---------------------------------------------------------------------------*/
195 
196 extern casu_tfits *casu_tfits_duplicate(casu_tfits *in) {
197  casu_tfits *p;
198 
199  /* Check for nonsense input */
200 
201  if (in == NULL)
202  return(NULL);
203 
204  /* Get the casu_tfits structure */
205 
206  p = cpl_malloc(sizeof(casu_tfits));
207 
208  /* Now copy everything over */
209 
210  p->table = cpl_table_duplicate(in->table);
211  p->phu = cpl_propertylist_duplicate(casu_tfits_get_phu(in));
212  p->ehu = cpl_propertylist_duplicate(casu_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 casu_tfits **casu_tfits_load_list(cpl_frameset *f, int exten) {
248  int i;
249  casu_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(casu_tfits *));
259 
260  /* Now load each of the frames... */
261 
262  for (i = 0; i < cpl_frameset_get_size(f); i++) {
263  p[i] = casu_tfits_load(cpl_frameset_get_position(f,i),exten);
264  if (p[i] == NULL) {
265  casu_tfits_delete_list(p,i-1);
266  return(NULL);
267  }
268  }
269 
270  /* Now return the array */
271 
272  return(p);
273 }
274 
275 /*---------------------------------------------------------------------------*/
290 /*---------------------------------------------------------------------------*/
291 
292 extern void casu_tfits_delete(casu_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 casu_tfits_delete_list(casu_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  casu_tfits_delete(p[i]);
341  cpl_free(p);
342 }
343 
344 /*---------------------------------------------------------------------------*/
362 /*---------------------------------------------------------------------------*/
363 
364 extern cpl_table *casu_tfits_get_table(casu_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 casu_tfits_get_nexten(casu_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 *casu_tfits_get_phu(casu_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 *casu_tfits_get_ehu(casu_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 *casu_tfits_get_filename(casu_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 *casu_tfits_get_fullname(casu_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 casu_tfits_get_status(casu_tfits *p) {
576 
577  /* Check for nonsense input */
578 
579  if (p == NULL)
580  return(CASU_FATAL);
581 
582  /* Return it */
583 
584  return(p->status);
585 }
586 
587 /*---------------------------------------------------------------------------*/
609 /*---------------------------------------------------------------------------*/
610 
611 extern int casu_tfits_set_error(casu_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 == CASU_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 == CASU_FATAL)
631  return(1);
632  else
633  return(0);
634 }
635 
636 /*---------------------------------------------------------------------------*/
657 /*---------------------------------------------------------------------------*/
658 
659 extern void casu_tfits_set_status(casu_tfits *p, int status) {
660 
661  /* Check for nonsense input */
662 
663  if (p == NULL)
664  return;
665 
666  /* Update the status */
667 
668  p->status = status;
669 
670 }
671 
672 /*---------------------------------------------------------------------------*/
695 /*---------------------------------------------------------------------------*/
696 
697 extern void casu_tfits_set_filename(casu_tfits *p, char *fname) {
698 
699  /* Check for nonsense input */
700 
701  if (p == NULL || fname == NULL)
702  return;
703 
704  /* Set the name up and get out of here */
705 
706  freespace(p->fname);
707  p->fname = cpl_strdup(fname);
708 }
709 
710 /*---------------------------------------------------------------------------*/
737 /*---------------------------------------------------------------------------*/
738 
739 extern casu_tfits *casu_tfits_wrap(cpl_table *tab, casu_tfits *model,
740  cpl_propertylist *phu,
741  cpl_propertylist *ehu) {
742  casu_tfits *p;
743 
744  /* Check for nonsense input */
745 
746  if (tab == NULL)
747  return(NULL);
748 
749  /* Get the casu_tfits structure */
750 
751  p = cpl_malloc(sizeof(casu_tfits));
752 
753  /* Load stuff in */
754 
755  p->table = tab;
756  p->nexten = -1;
757  if (phu != NULL)
758  p->phu = phu;
759  else if (model != NULL)
760  p->phu = cpl_propertylist_duplicate(casu_tfits_get_phu(model));
761  else
762  p->phu = cpl_propertylist_new();
763  if (ehu != NULL)
764  p->ehu = ehu;
765  else if (model != NULL)
766  p->ehu = cpl_propertylist_duplicate(casu_tfits_get_ehu(model));
767  else
768  p->ehu = cpl_propertylist_new();
769  p->fname = NULL;
770  p->status = CASU_OK;
771  p->extname = NULL;
772  p->fullname = NULL;
773 
774  /* Get out of here */
775 
776  return(p);
777 }
778 
781 /*
782 
783 $Log: casu_tfits.c,v $
784 Revision 1.4 2015/09/14 18:45:51 jim
785 replaced a free by cpl_free
786 
787 Revision 1.3 2015/08/07 13:06:54 jim
788 Fixed copyright to ESO
789 
790 Revision 1.2 2015/08/06 05:34:02 jim
791 Fixes to get rid of compiler moans
792 
793 Revision 1.1.1.1 2015/06/12 10:44:32 jim
794 Initial import
795 
796 Revision 1.7 2015/01/29 11:56:27 jim
797 modified comments
798 
799 Revision 1.6 2015/01/09 12:13:15 jim
800 *** empty log message ***
801 
802 Revision 1.5 2014/12/11 12:23:33 jim
803 new version
804 
805 Revision 1.4 2014/03/26 15:59:47 jim
806 Removed calls to deprecated cpl routine
807 
808 Revision 1.3 2013/11/21 09:38:14 jim
809 detabbed
810 
811 Revision 1.2 2013-10-24 09:27:01 jim
812 added casu_tfits_set_status
813 
814 Revision 1.1.1.1 2013-08-27 12:07:48 jim
815 Imported
816 
817 
818 
819 */
int casu_tfits_get_status(casu_tfits *p)
Definition: casu_tfits.c:575
void casu_tfits_set_filename(casu_tfits *p, char *fname)
Definition: casu_tfits.c:697
void casu_tfits_set_status(casu_tfits *p, int status)
Definition: casu_tfits.c:659
casu_tfits ** casu_tfits_load_list(cpl_frameset *f, int exten)
Definition: casu_tfits.c:247
void casu_tfits_delete(casu_tfits *p)
Definition: casu_tfits.c:292
casu_tfits * casu_tfits_extract(casu_tfits *in)
Definition: casu_tfits.c:152
void casu_tfits_delete_list(casu_tfits **p, int n)
Definition: casu_tfits.c:329
cpl_table * casu_tfits_get_table(casu_tfits *p)
Definition: casu_tfits.c:364
cpl_propertylist * casu_tfits_get_phu(casu_tfits *p)
Definition: casu_tfits.c:432
int casu_tfits_get_nexten(casu_tfits *p)
Definition: casu_tfits.c:397
cpl_propertylist * casu_tfits_get_ehu(casu_tfits *p)
Definition: casu_tfits.c:473
char * casu_tfits_get_fullname(casu_tfits *p)
Definition: casu_tfits.c:544
casu_tfits * casu_tfits_duplicate(casu_tfits *in)
Definition: casu_tfits.c:196
int casu_tfits_set_error(casu_tfits *p, int status)
Definition: casu_tfits.c:611
casu_tfits * casu_tfits_load(cpl_frame *table, int nexten)
Definition: casu_tfits.c:78
casu_tfits * casu_tfits_wrap(cpl_table *tab, casu_tfits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: casu_tfits.c:739
char * casu_tfits_get_filename(casu_tfits *p)
Definition: casu_tfits.c:510