vircam_tfits.c

00001 /* $Id: vircam_tfits.c,v 1.13 2007/10/25 17:34:01 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/10/25 17:34:01 $
00024  * $Revision: 1.13 $
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_tfits.h"
00041 
00055 /*---------------------------------------------------------------------------*/
00076 /*---------------------------------------------------------------------------*/
00077 
00078 extern vir_tfits *vircam_tfits_load(cpl_frame *table, int nexten) {
00079     vir_tfits *p;
00080     cpl_table *tab;
00081     int nf;
00082     const char *fctid = "vircam_tfits_load";
00083 
00084     /* Check for nonsense input */
00085 
00086     if (table == NULL)
00087         return(NULL);
00088 
00089     /* See if you can load the table */
00090 
00091     tab = cpl_table_load(cpl_frame_get_filename(table),nexten,0);
00092     if (tab == NULL) {
00093         cpl_msg_error(fctid,"Unable to load %s -- %s\n",
00094                       cpl_frame_get_filename(table),cpl_error_get_message());
00095         cpl_error_reset();
00096         return(NULL);
00097     }
00098 
00099     /* Get the vir_tfits structure */
00100 
00101     p = cpl_malloc(sizeof(vir_tfits));
00102 
00103     /* Load stuff in */
00104 
00105     p->table = tab;
00106     p->nexten = nexten;
00107     p->phu = NULL;
00108     p->ehu = NULL;
00109     p->fname = cpl_strdup(cpl_frame_get_filename(table));
00110     p->status = VIR_OK;
00111    
00112     /* Get the extension header and the extension name */
00113 
00114     (void)vircam_tfits_get_ehu(p);
00115     if (cpl_propertylist_has(p->ehu,"EXTNAME")) {
00116         p->extname = cpl_strdup(cpl_propertylist_get_string(p->ehu,"EXTNAME"));
00117     } else {
00118         nf = 11 + (int)log10((double)nexten);
00119         p->extname = cpl_malloc(nf);
00120         (void)snprintf(p->extname,nf,"DET1.CHIP%d",nexten);
00121     }
00122     nf = strlen(p->extname) + strlen(p->fname) + 3;
00123     p->fullname = cpl_malloc(nf);
00124     (void)snprintf(p->fullname,nf,"%s[%s]",p->fname,p->extname);
00125 
00126     /* Get out of here */
00127 
00128     return(p);
00129 }
00130 
00131 /*---------------------------------------------------------------------------*/
00150 /*---------------------------------------------------------------------------*/
00151 
00152 extern vir_tfits *vircam_tfits_extract(vir_tfits *in) {
00153     vir_tfits *p;
00154 
00155     /* Check for nonsense input */
00156 
00157     if (in == NULL)
00158         return(NULL);
00159 
00160     /* Get the vir_tfits structure */
00161 
00162     p = cpl_malloc(sizeof(vir_tfits));
00163 
00164     /* Load stuff in */
00165 
00166     p->table = cpl_table_extract_selected(vircam_tfits_get_table(in));
00167     p->nexten = vircam_tfits_get_nexten(in);
00168     p->phu = NULL;
00169     p->ehu = NULL;
00170     p->fname = cpl_strdup(vircam_tfits_get_filename(in));
00171    
00172     /* Get out of here */
00173 
00174     return(p);
00175 }
00176 
00177 /*---------------------------------------------------------------------------*/
00198 /*---------------------------------------------------------------------------*/
00199 
00200 extern vir_tfits **vircam_tfits_load_list(cpl_frameset *f, int exten) {
00201     int i;
00202     vir_tfits **p;
00203 
00204     /* Check for nonsense input */
00205 
00206     if (f == NULL)
00207         return(NULL);
00208 
00209     /* Get some workspace */
00210 
00211     p = cpl_malloc(cpl_frameset_get_size(f)*sizeof(vir_tfits *));
00212     
00213     /* Now load each of the frames... */
00214 
00215     for (i = 0; i < cpl_frameset_get_size(f); i++) {
00216         p[i] = vircam_tfits_load(cpl_frameset_get_frame(f,i),exten);
00217         if (p[i] == NULL) {
00218             vircam_tfits_delete_list(p,i-1);
00219             return(NULL);
00220         }
00221     }
00222 
00223     /* Now return the array */
00224 
00225     return(p);
00226 }
00227 
00228 /*---------------------------------------------------------------------------*/
00243 /*---------------------------------------------------------------------------*/
00244 
00245 extern void vircam_tfits_delete(vir_tfits *p) {
00246 
00247     /* Check for nonsense input */
00248 
00249     if (p == NULL)
00250         return;
00251 
00252     /* Free up workspace if it's been used */
00253 
00254     freetable(p->table);
00255     freepropertylist(p->phu);
00256     freepropertylist(p->ehu);
00257     freespace(p->fname);
00258     freespace(p->extname);
00259     freespace(p->fullname);
00260     cpl_free(p);
00261 }
00262 
00263 /*---------------------------------------------------------------------------*/
00280 /*---------------------------------------------------------------------------*/
00281 
00282 extern void vircam_tfits_delete_list(vir_tfits **p, int n) {
00283     int i;
00284 
00285     /* Check for nonsense input */
00286 
00287     if (p == NULL)
00288         return;
00289 
00290     /* Free up workspace if it's been used */
00291 
00292     for (i = 0; i < n; i++)
00293         vircam_tfits_delete(p[i]);
00294     freespace(p);
00295 }
00296 
00297 /*---------------------------------------------------------------------------*/
00315 /*---------------------------------------------------------------------------*/
00316 
00317 extern cpl_table *vircam_tfits_get_table(vir_tfits *p) {
00318     
00319     /* Check for nonsense input */
00320 
00321     if (p == NULL)
00322         return(NULL);
00323 
00324     /* Return it */
00325 
00326     return(p->table);
00327 }
00328 
00329 /*---------------------------------------------------------------------------*/
00348 /*---------------------------------------------------------------------------*/
00349 
00350 extern int vircam_tfits_get_nexten(vir_tfits *p) {
00351     
00352     /* Check for nonsense input */
00353 
00354     if (p == NULL)
00355         return(-1);
00356 
00357     /* Return it */
00358 
00359     return(p->nexten);
00360 }
00361 
00362 /*---------------------------------------------------------------------------*/
00383 /*---------------------------------------------------------------------------*/
00384 
00385 extern cpl_propertylist *vircam_tfits_get_phu(vir_tfits *p) {
00386 
00387     /* Check for nonsense input */
00388 
00389     if (p == NULL)
00390         return(NULL);
00391 
00392     /* If the propertylist hasn't already been loaded, then do it now */
00393 
00394     if (p->phu == NULL) 
00395         p->phu = cpl_propertylist_load(p->fname,0);
00396     
00397     /* Return it */
00398 
00399     return(p->phu);
00400 }
00401 
00402 /*---------------------------------------------------------------------------*/
00424 /*---------------------------------------------------------------------------*/
00425 
00426 extern cpl_propertylist *vircam_tfits_get_ehu(vir_tfits *p) {
00427 
00428     /* Check for nonsense input */
00429 
00430     if (p == NULL)
00431         return(NULL);
00432 
00433     /* If the propertylist hasn't already been loaded, then do it now */
00434 
00435     if (p->ehu == NULL) 
00436         p->ehu = cpl_propertylist_load(p->fname,p->nexten);
00437     
00438     /* Return it */
00439 
00440     return(p->ehu);
00441 }
00442 
00443 /*---------------------------------------------------------------------------*/
00461 /*---------------------------------------------------------------------------*/
00462 
00463 extern char *vircam_tfits_get_filename(vir_tfits *p) {
00464 
00465     /* Check for nonsense input */
00466 
00467     if (p == NULL)
00468         return(NULL);
00469 
00470     /* Return it */
00471 
00472     return(p->fname);
00473 }
00474   
00475 /*---------------------------------------------------------------------------*/
00495 /*---------------------------------------------------------------------------*/
00496 
00497 extern char *vircam_tfits_get_fullname(vir_tfits *p) {
00498 
00499     /* Check for nonsense input */
00500 
00501     if (p == NULL)
00502         return(NULL);
00503 
00504     /* Return it */
00505 
00506     return(p->fullname);
00507 }
00508 
00509 /*---------------------------------------------------------------------------*/
00526 /*---------------------------------------------------------------------------*/
00527 
00528 extern int vircam_tfits_get_status(vir_tfits *p) {
00529     
00530     /* Check for nonsense input */
00531 
00532     if (p == NULL)
00533         return(VIR_FATAL);
00534 
00535     /* Return it */
00536    
00537     return(p->status);
00538 }
00539   
00540 /*---------------------------------------------------------------------------*/
00562 /*---------------------------------------------------------------------------*/
00563 
00564 extern int vircam_tfits_set_error(vir_tfits *p, int status) {
00565 
00566     /* Check for nonsense input */
00567 
00568     if (p == NULL)
00569         return(0);
00570 
00571     /* Get out of here if the status is OK */
00572 
00573     if (status == VIR_OK)
00574         return(0);
00575 
00576     /* Set the error message if there was an error */
00577 
00578     p->status = status;
00579     
00580     /* Reset the cpl error flag */
00581 
00582     cpl_error_reset();
00583     if (status == VIR_FATAL) 
00584         return(1);
00585     else
00586         return(0);
00587 }
00588     
00589 /*---------------------------------------------------------------------------*/
00616 /*---------------------------------------------------------------------------*/
00617 
00618 extern vir_tfits *vircam_tfits_wrap(cpl_table *tab, vir_tfits *model,
00619                                     cpl_propertylist *phu,
00620                                     cpl_propertylist *ehu) {
00621     vir_tfits *p;
00622 
00623     /* Check for nonsense input */
00624 
00625     if (tab == NULL)
00626         return(NULL);
00627 
00628     /* Get the vir_fits structure */
00629 
00630     p = cpl_malloc(sizeof(vir_tfits));
00631 
00632     /* Load stuff in */
00633 
00634     p->table = tab;
00635     p->nexten = -1;
00636     if (phu != NULL)
00637         p->phu = phu;
00638     else if (model != NULL)
00639         p->phu = cpl_propertylist_duplicate(vircam_tfits_get_phu(model));
00640     else
00641         p->phu = NULL;
00642     if (ehu != NULL)
00643         p->ehu = ehu;
00644     else if (model != NULL)
00645         p->ehu = cpl_propertylist_duplicate(vircam_tfits_get_ehu(model));
00646     else
00647         p->ehu = NULL;
00648     p->fname = NULL;
00649     p->status = VIR_OK;
00650     p->extname = NULL;
00651     p->fullname = NULL;
00652 
00653     /* Get out of here */
00654 
00655     return(p);
00656 }
00657 
00660 /*
00661 
00662 $Log: vircam_tfits.c,v $
00663 Revision 1.13  2007/10/25 17:34:01  jim
00664 Modified to remove lint warnings
00665 
00666 Revision 1.12  2007/10/19 09:25:10  jim
00667 Fixed problems with missing includes
00668 
00669 Revision 1.11  2007/10/15 12:50:28  jim
00670 Modified for compatibility with cpl_4.0
00671 
00672 Revision 1.10  2007/03/01 12:42:42  jim
00673 Modified slightly after code checking
00674 
00675 Revision 1.9  2007/02/25 06:32:40  jim
00676 Fixed typo
00677 
00678 Revision 1.8  2007/02/20 21:13:13  jim
00679 Better error reporting in case of load failure
00680 
00681 Revision 1.7  2006/07/04 09:19:05  jim
00682 replaced all sprintf statements with snprintf
00683 
00684 Revision 1.6  2006/06/19 11:17:58  jim
00685 Fixed bug causing a memory overwrite
00686 
00687 Revision 1.5  2006/06/09 11:26:26  jim
00688 Small changes to keep lint happy
00689 
00690 Revision 1.4  2006/05/24 13:34:36  jim
00691 Added vircam_tfits_wrap
00692 
00693 Revision 1.3  2006/04/20 11:21:21  jim
00694 Added _fullname method
00695 
00696 Revision 1.2  2006/03/22 13:58:32  jim
00697 Cosmetic fixes to keep lint happy
00698 
00699 Revision 1.1  2006/03/15 14:35:17  jim
00700 new file
00701 
00702 
00703 */

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