vircam_fits.c

00001 /* $Id: vircam_fits.c,v 1.19 2007/11/14 10:47:08 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jim $
00023  * $Date: 2007/11/14 10:47:08 $
00024  * $Revision: 1.19 $
00025  * $Name:  $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <stdio.h>
00035 #include <math.h>
00036 #include <string.h>
00037 
00038 #include <cpl.h>
00039 #include "vircam_utils.h"
00040 #include "vircam_fits.h"
00041 
00055 /*---------------------------------------------------------------------------*/
00078 /*---------------------------------------------------------------------------*/
00079 
00080 extern vir_fits *vircam_fits_load(cpl_frame *frame, cpl_type type, 
00081                                   int nexten) {
00082     vir_fits *p;
00083     cpl_image *im;
00084     int nf;
00085     const char *fctid = "vircam_fits_load";
00086 
00087     /* Check for nonsense input */
00088 
00089     if (frame == NULL)
00090         return(NULL);
00091 
00092     /* See if you can load the image */
00093 
00094     im = cpl_image_load(cpl_frame_get_filename(frame),type,0,nexten);
00095     if (im == NULL) {
00096         cpl_msg_error(fctid,"Unable to load %s[%d] -- %s\n",
00097                       cpl_frame_get_filename(frame),nexten,
00098                       cpl_error_get_message());
00099         cpl_error_reset();
00100         return(NULL);
00101     }
00102 
00103     /* Get the vir_fits structure */
00104 
00105     p = cpl_malloc(sizeof(vir_fits));
00106 
00107     /* Load stuff in */
00108 
00109     p->image = im;
00110     p->nexten = nexten;
00111     p->phu = NULL;
00112     p->ehu = NULL;
00113     p->fname = cpl_strdup(cpl_frame_get_filename(frame));
00114     p->status = VIR_OK;
00115 
00116     /* Get the extension header and the extension name */
00117 
00118     (void)vircam_fits_get_ehu(p);
00119     if (p->ehu == NULL)
00120         return(NULL);
00121     if (cpl_propertylist_has(p->ehu,"EXTNAME")) {
00122         p->extname = cpl_strdup(cpl_propertylist_get_string(p->ehu,"EXTNAME"));
00123     } else {
00124         nf = 11 + (int)log10((double)nexten);
00125         p->extname = cpl_malloc(nf);
00126         (void)snprintf(p->extname,nf,"DET1.CHIP%d",nexten);
00127     }
00128     nf = strlen(p->extname) + strlen(p->fname) + 3;
00129     p->fullname = cpl_malloc(nf);
00130     (void)snprintf(p->fullname,nf,"%s[%s]",p->fname,p->extname);
00131    
00132     /* Get out of here */
00133 
00134     return(p);
00135 }
00136 
00137 /*---------------------------------------------------------------------------*/
00154 /*---------------------------------------------------------------------------*/
00155 
00156 extern vir_fits *vircam_fits_duplicate(vir_fits *in) {
00157     vir_fits *p;
00158 
00159     /* Check for nonsense input */
00160 
00161     if (in == NULL)
00162         return(NULL);
00163 
00164     /* Get the vir_fits structure */
00165 
00166     p = cpl_malloc(sizeof(vir_fits));
00167 
00168     /* Now copy everything over */
00169 
00170     p->image = cpl_image_duplicate(in->image);
00171     if (in->phu != NULL) 
00172         p->phu = cpl_propertylist_duplicate(in->phu);
00173     else
00174         p->phu = NULL;
00175     if (in->ehu != NULL) 
00176         p->ehu = cpl_propertylist_duplicate(in->ehu);
00177     else
00178         p->ehu = NULL;
00179     p->fname = cpl_strdup(in->fname);
00180     p->extname = cpl_strdup(in->extname);
00181     p->fullname = cpl_strdup(in->fullname);
00182     p->nexten = in->nexten;
00183     p->status = in->status;
00184    
00185     /* Get out of here */
00186 
00187     return(p);
00188 }
00189 
00190 
00191 /*---------------------------------------------------------------------------*/
00214 /*---------------------------------------------------------------------------*/
00215 
00216 extern vir_fits **vircam_fits_load_list(cpl_frameset *f, cpl_type type, 
00217                                         int exten) {
00218     int i;
00219     vir_fits **p;
00220 
00221     /* Check for nonsense input */
00222 
00223     if (f == NULL)
00224         return(NULL);
00225 
00226     /* Get some workspace */
00227 
00228     p = cpl_malloc(cpl_frameset_get_size(f)*sizeof(vir_fits *));
00229     
00230     /* Now load each of the frames... */
00231 
00232     for (i = 0; i < cpl_frameset_get_size(f); i++) {
00233         p[i] = vircam_fits_load(cpl_frameset_get_frame(f,i),type,exten);
00234         if (p[i] == NULL) {
00235             vircam_fits_delete_list(p,i-1);
00236             return(NULL);
00237         }
00238     }
00239 
00240     /* Now return the array */
00241 
00242     return(p);
00243 }
00244 
00245 /*---------------------------------------------------------------------------*/
00260 /*---------------------------------------------------------------------------*/
00261 
00262 extern void vircam_fits_delete(vir_fits *p) {
00263 
00264     /* Check for nonsense input */
00265 
00266     if (p == NULL)
00267         return;
00268 
00269     /* Free up workspace if it's been used */
00270 
00271     freeimage(p->image);
00272     freepropertylist(p->phu);
00273     freepropertylist(p->ehu);
00274     freespace(p->fname);
00275     freespace(p->extname);
00276     freespace(p->fullname);
00277     cpl_free(p);
00278 }
00279 
00280 /*---------------------------------------------------------------------------*/
00297 /*---------------------------------------------------------------------------*/
00298 
00299 extern void vircam_fits_delete_list(vir_fits **p, int n) {
00300     int i;
00301 
00302     /* Check for nonsense input */
00303 
00304     if (p == NULL)
00305         return;
00306 
00307     /* Free up workspace if it's been used */
00308 
00309     for (i = 0; i < n; i++)
00310         vircam_fits_delete(p[i]);
00311     freespace(p);
00312 }
00313 
00314 /*---------------------------------------------------------------------------*/
00332 /*---------------------------------------------------------------------------*/
00333 
00334 extern cpl_image *vircam_fits_get_image(vir_fits *p) {
00335     
00336     /* Check for nonsense input */
00337 
00338     if (p == NULL)
00339         return(NULL);
00340 
00341     /* Return it */
00342 
00343     return(p->image);
00344 }
00345 
00346 /*---------------------------------------------------------------------------*/
00365 /*---------------------------------------------------------------------------*/
00366 
00367 extern int vircam_fits_get_nexten(vir_fits *p) {
00368     
00369     /* Check for nonsense input */
00370 
00371     if (p == NULL)
00372         return(-1);
00373 
00374     /* Return it */
00375 
00376     return(p->nexten);
00377 }
00378 
00379 /*---------------------------------------------------------------------------*/
00399 /*---------------------------------------------------------------------------*/
00400 
00401 extern cpl_propertylist *vircam_fits_get_phu(vir_fits *p) {
00402 
00403     /* Check for nonsense input */
00404 
00405     if (p == NULL)
00406         return(NULL);
00407 
00408     /* If the propertylist hasn't already been loaded, then do it now */
00409 
00410     if (p->phu == NULL) 
00411         p->phu = cpl_propertylist_load(p->fname,0);
00412     
00413     /* Return it */
00414 
00415     return(p->phu);
00416 }
00417 
00418 /*---------------------------------------------------------------------------*/
00440 /*---------------------------------------------------------------------------*/
00441 
00442 extern cpl_propertylist *vircam_fits_get_ehu(vir_fits *p) {
00443 
00444     /* Check for nonsense input */
00445 
00446     if (p == NULL)
00447         return(NULL);
00448 
00449     /* If the propertylist hasn't already been loaded, then do it now */
00450 
00451     if (p->ehu == NULL) 
00452         p->ehu = cpl_propertylist_load(p->fname,p->nexten);
00453     
00454     /* Return it */
00455 
00456     return(p->ehu);
00457 }
00458 
00459 /*---------------------------------------------------------------------------*/
00477 /*---------------------------------------------------------------------------*/
00478 
00479 extern char *vircam_fits_get_extname(vir_fits *p) {
00480 
00481     /* Check for nonsense input */
00482 
00483     if (p == NULL)
00484         return(NULL);
00485 
00486     /* Return it */
00487 
00488     return(p->extname);
00489 }
00490 
00491 /*---------------------------------------------------------------------------*/
00509 /*---------------------------------------------------------------------------*/
00510 
00511 extern char *vircam_fits_get_filename(vir_fits *p) {
00512 
00513     /* Check for nonsense input */
00514 
00515     if (p == NULL)
00516         return(NULL);
00517 
00518     /* Return it */
00519 
00520     return(p->fname);
00521 }
00522 
00523 /*---------------------------------------------------------------------------*/
00543 /*---------------------------------------------------------------------------*/
00544 
00545 extern char *vircam_fits_get_fullname(vir_fits *p) {
00546 
00547     /* Check for nonsense input */
00548 
00549     if (p == NULL)
00550         return(NULL);
00551 
00552     /* Return it */
00553 
00554     return(p->fullname);
00555 }
00556 
00557 /*---------------------------------------------------------------------------*/
00574 /*---------------------------------------------------------------------------*/
00575 
00576 extern int vircam_fits_get_status(vir_fits *p) {
00577     
00578     /* Check for nonsense input */
00579 
00580     if (p == NULL)
00581         return(VIR_FATAL);
00582 
00583     /* Return it */
00584    
00585     return(p->status);
00586 }
00587 
00588   
00589 /*---------------------------------------------------------------------------*/
00611 /*---------------------------------------------------------------------------*/
00612 
00613 extern int vircam_fits_set_error(vir_fits *p, int status) {
00614 
00615     /* Check for nonsense input */
00616 
00617     if (p == NULL)
00618         return(0);
00619 
00620     /* Get out of here if the status is OK */
00621 
00622     if (status == VIR_OK)
00623         return(0);
00624 
00625     /* Set the error status if there was an error */
00626 
00627     p->status = status;
00628 
00629     /* If there was a cpl error then spew that out now */
00630 
00631     if (cpl_error_get_code() != CPL_ERROR_NONE) {
00632         cpl_msg_error("","%s",cpl_error_get_message());
00633         cpl_error_reset();
00634     }
00635     
00636     /* Get out of here */
00637 
00638     if (status == VIR_FATAL)
00639         return(1);
00640     else
00641         return(0);
00642 }
00643 
00644 /*---------------------------------------------------------------------------*/
00671 /*---------------------------------------------------------------------------*/
00672 
00673 extern vir_fits *vircam_fits_wrap(cpl_image *im, vir_fits *model, 
00674                                   cpl_propertylist *phu,
00675                                   cpl_propertylist *ehu) {
00676     vir_fits *p;
00677 
00678     /* Check for nonsense input */
00679 
00680     if (im == NULL)
00681         return(NULL);
00682 
00683     /* Get the vir_fits structure */
00684 
00685     p = cpl_malloc(sizeof(vir_fits));
00686 
00687     /* Load stuff in */
00688 
00689     p->image = im;
00690     p->nexten = -1;
00691     if (phu != NULL) 
00692         p->phu = cpl_propertylist_duplicate(phu);
00693     else if (model != NULL) 
00694         p->phu = cpl_propertylist_duplicate(vircam_fits_get_phu(model));
00695     else
00696         p->phu = NULL;
00697     if (ehu != NULL)
00698         p->ehu = cpl_propertylist_duplicate(ehu);
00699     else if (model != NULL) 
00700         p->ehu = cpl_propertylist_duplicate(vircam_fits_get_ehu(model));
00701     else 
00702         p->ehu = NULL;
00703     p->fname = NULL;
00704     p->status = VIR_OK;
00705     p->extname = NULL;
00706     p->fullname = NULL;
00707    
00708     /* Get out of here */
00709 
00710     return(p);
00711 }
00712     
00715 /*
00716 
00717 $Log: vircam_fits.c,v $
00718 Revision 1.19  2007/11/14 10:47:08  jim
00719 Modified vircam_fits_duplicate so that in case the pointers to the
00720 input headers are NULL, then this isn't an error
00721 
00722 Revision 1.18  2007/10/25 17:34:00  jim
00723 Modified to remove lint warnings
00724 
00725 Revision 1.17  2007/10/19 09:25:09  jim
00726 Fixed problems with missing includes
00727 
00728 Revision 1.16  2007/10/15 12:50:28  jim
00729 Modified for compatibility with cpl_4.0
00730 
00731 Revision 1.15  2007/07/18 15:33:06  jim
00732 Modified error message in vircam_fits_load to include extension number
00733 
00734 Revision 1.14  2007/03/01 12:42:41  jim
00735 Modified slightly after code checking
00736 
00737 Revision 1.13  2007/02/20 21:13:13  jim
00738 Better error reporting in case of load failure
00739 
00740 Revision 1.12  2006/10/31 10:26:51  jim
00741 Added vircam_fits_get_extname
00742 
00743 Revision 1.11  2006/08/07 14:20:14  jim
00744 Added vircam_fits_duplicate
00745 
00746 Revision 1.10  2006/07/07 09:34:00  jim
00747 wrap routine now duplicates input property lists rather than using the
00748 input pointer
00749 
00750 Revision 1.9  2006/07/04 09:19:05  jim
00751 replaced all sprintf statements with snprintf
00752 
00753 Revision 1.8  2006/05/26 15:02:21  jim
00754 Fixed bad error message call
00755 
00756 Revision 1.7  2006/05/24 13:34:19  jim
00757 Added vircam_fits_wrap
00758 
00759 Revision 1.6  2006/04/30 22:12:40  jim
00760 Fixed memory bug
00761 
00762 Revision 1.5  2006/04/24 13:46:36  jim
00763 A bit more error trapping in case fits structures can't be loaded
00764 
00765 Revision 1.4  2006/04/20 11:21:21  jim
00766 Added _fullname method
00767 
00768 Revision 1.3  2006/03/22 13:32:25  jim
00769 Cosmetic changes to keep lint happy
00770 
00771 Revision 1.2  2006/03/03 14:29:45  jim
00772 Modified definition of vir_fits and channel table
00773 
00774 Revision 1.1  2006/03/01 10:31:03  jim
00775 new files
00776 
00777 
00778 */

Generated on Wed Apr 10 04:01:55 2013 for VIRCAM Pipeline by  doxygen 1.5.1