public software.sextractor

[/] [trunk/] [src/] [fits/] [fitsconv.c] - Blame information for rev 235

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 bertin
/*
2 233 bertin
*                               fitsconv.c
3 2 bertin
*
4 233 bertin
* Functions for converting LDAC FITS catalogs.
5 2 bertin
*
6 233 bertin
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 2 bertin
*
8 233 bertin
*       This file part of:      AstrOmatic FITS/LDAC library
9 2 bertin
*
10 235 bertin
*       Copyright:              (C) 1995-2010 Emmanuel Bertin -- IAP/CNRS/UPMC
11 2 bertin
*
12 233 bertin
*       License:                GNU General Public License
13
*
14
*       AstrOmatic software is free software: you can redistribute it and/or
15
*       modify it under the terms of the GNU General Public License as
16
*       published by the Free Software Foundation, either version 3 of the
17
*       License, or (at your option) any later version.
18
*       AstrOmatic software is distributed in the hope that it will be useful,
19
*       but WITHOUT ANY WARRANTY; without even the implied warranty of
20
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
*       GNU General Public License for more details.
22
*       You should have received a copy of the GNU General Public License
23
*       along with AstrOmatic software.
24
*       If not, see <http://www.gnu.org/licenses/>.
25
*
26
*       Last modified:          09/10/2010
27
*
28
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
29 215 bertin
 
30
#ifdef HAVE_CONFIG_H
31
#include        "config.h"
32
#endif
33
 
34 2 bertin
#include        <stdio.h>
35
#include        <stdlib.h>
36
#include        <string.h>
37
 
38
#include        "fitscat_defs.h"
39
#include        "fitscat.h"
40
 
41
/****** asc2bin_tab ***********************************************************
42
PROTO   tabstruct *asc2bin_tab(catstruct *catin, char *tabinname,
43
        catstruct *catout, char *taboutname)
44
PURPOSE Convert an ASCII table to a BINARY table
45
        which is then stored in a destination catalog.
46
INPUT   Pointer to the input catalog,
47
        Name of the input ASCII table,
48
        Pointer to the output catalog,
49
        Name of the output binary table.
50
OUTPUT  RETURN_OK if the ASCII table was transformed, and RETURN_ERROR
51
        otherwise.
52
NOTES   This function can be used to stick the binary translation of
53
        similar ASCII tables.
54
AUTHOR  E. Bertin (IAP & Leiden observatory)
55
VERSION 25/09/2004
56
 ***/
57
tabstruct *asc2bin_tab(catstruct *catin, char *tabinname, catstruct *catout,
58
                char *taboutname)
59
 
60
  {
61
   catstruct    *tabcat;
62
   keystruct    *key;
63
   tabstruct    *tabin,*tabout;
64
   char         comment[82], keyword[16], ptr[82];
65
   h_type       htype;
66
   t_type       ttype;
67
   char         *buf, *lptr;
68
   int          i;
69
 
70
/*Convert the table name to a pointer*/
71
  if (!(tabin = name_to_tab(catin, tabinname, 0)))
72
    return NULL;
73
 
74
/*Get the original catalog*/
75
  tabcat = tabin->cat;
76
 
77
/*Create a new binary table*/
78
  tabout = new_tab(taboutname);
79
 
80
/*Alloc. mem. for the whole ASCII table at once (should not be very large)*/
81
  QMALLOC(buf, char, tabin->tabsize);
82
/*Now read all the elements from the original table*/
83
  if (open_cat(tabcat, READ_ONLY) != RETURN_OK)
84
    error(EXIT_FAILURE, "*Error*: Cannot access ", tabcat->filename);
85
  QFSEEK(tabcat->file, tabin->bodypos, SEEK_SET, tabcat->filename);
86
  QFREAD(buf, tabin->tabsize, tabcat->file, tabcat->filename);
87
  if (close_cat(tabcat) != RETURN_OK)
88
    error(EXIT_FAILURE, "*Error*: Problem while closing", tabcat->filename);
89
  lptr = buf;
90
  for (i=tabin->tabsize/80; i-- && strncmp(lptr, "END     ", 8);)
91
    {
92
/*Interprete the next FITS line */
93
    if (fitspick(lptr, keyword, ptr, &htype, &ttype, comment) != RETURN_OK) {
94
      char line[81];
95
      int  qflag=1;
96
      strncpy(line, lptr, 80);
97
      line[80] = '\0';
98
      QFPRINTF(OUTPUT, line);
99
      warning("*Warning*: incorrect FITS field will be ignored in ",
100
        tabcat->filename);
101
    }
102
    if (htype != H_COMMENT)
103
      {
104
/*----Create a new key and fill it with the right parameters*/
105
      key = new_key(keyword);
106
      strcpy(key->comment, comment+strspn(comment, " "));
107
      key->htype = htype;
108
      key->ttype = ttype;
109
      key->nbytes = t_size[ttype];
110
/*----!!Temporary (?)  solution for STRINGS*/
111
      if (htype==H_STRING)
112
        {
113
        key->naxis = 1;
114
        QMALLOC(key->naxisn, int, 1);
115
        key->naxisn[0] = 32;
116
        key->nbytes *= key->naxisn[0];
117
        }
118
      key->nobj = 1;
119
/*----Allocate memory and copy data in the same time*/
120
      QMEMCPY(ptr, key->ptr, char, key->nbytes);
121
      if (add_key(key, tabout, 0)==RETURN_ERROR)
122
        {
123
        sprintf(comment, "%s keyword found twice in ",
124
                keyword);
125
        warning(comment, tabcat->filename);
126
        }
127
      }
128
    lptr += 80;
129
    }
130
 
131
  free(buf);
132
 
133
  update_tab(tabout);
134
  return tabout;
135
  }
136
 
137
 
138
/****** ttypeconv ************************************************************
139
PROTO   void    ttypeconv(void *ptrin, void *ptrout,
140
                t_type ttypein, t_type ttypeout)
141
PURPOSE Convert data from one type to another.
142
INPUT   Pointer to element to convert,
143
        destination pointer to the converted element,
144
        t_type of the element to convert,
145
        t_type of the converted element.
146
OUTPUT  -.
147
NOTES   ttypeconv does not yet handle arrays.
148
AUTHOR  E. Bertin (IAP)
149 217 bertin
VERSION 19/11/2009
150 2 bertin
 ***/
151
 
152
void    ttypeconv(void *ptrin, void *ptrout, t_type ttypein, t_type ttypeout)
153
 
154
  {
155 217 bertin
//   union      {char tbyte; short tshort; int tlong; LONGLONG tlonglong;
156
//               float tfloat; double tdouble; char tstring;} ival;
157 2 bertin
 
158 215 bertin
#ifdef HAVE_LONG_LONG_INT
159 2 bertin
#define         OUTCONV(x, y)                   \
160
      switch(y)                                 \
161
         {                                      \
162
         case T_BYTE:                           \
163
         case T_STRING:                         \
164
           *((char *)ptrout) = (char)x;         \
165
           break;                               \
166
         case T_SHORT:                          \
167
           *((short *)ptrout) = (short)x;       \
168
           break;                               \
169
         case T_LONG:                           \
170
           *((int *)ptrout) = (int)x;           \
171
           break;                               \
172 215 bertin
         case T_LONGLONG:                       \
173
           *((LONGLONG *)ptrout) = (LONGLONG)x; \
174
           break;                               \
175 2 bertin
         case T_FLOAT:                          \
176
           *((float *)ptrout) = (float)x;       \
177
           break;                               \
178
         case T_DOUBLE:                         \
179
           *((double *)ptrout) = (double)x;     \
180
           break;                               \
181
         default:                               \
182
           break;                               \
183
         }
184 215 bertin
#else
185
#define         OUTCONV(x, y)                   \
186
      switch(y)                                 \
187
         {                                      \
188
         case T_BYTE:                           \
189
         case T_STRING:                         \
190
           *((char *)ptrout) = (char)x;         \
191
           break;                               \
192
         case T_SHORT:                          \
193
           *((short *)ptrout) = (short)x;       \
194
           break;                               \
195
         case T_LONG:                           \
196
           *((int *)ptrout) = (int)x;           \
197
           break;                               \
198
         case T_FLOAT:                          \
199
           *((float *)ptrout) = (float)x;       \
200
           break;                               \
201
         case T_DOUBLE:                         \
202
           *((double *)ptrout) = (double)x;     \
203
           break;                               \
204
         default:                               \
205
           break;                               \
206
         }
207
#endif
208 2 bertin
 
209
  switch(ttypein)
210
    {
211
    case T_BYTE:
212
    case T_STRING:
213 215 bertin
      OUTCONV(*(char *)ptrin, ttypeout);
214 2 bertin
      break;
215
    case T_SHORT:
216 215 bertin
      OUTCONV(*(short *)ptrin, ttypeout);
217 2 bertin
      break;
218
    case T_LONG:
219 215 bertin
      OUTCONV(*(int *)ptrin, ttypeout);
220 2 bertin
      break;
221 215 bertin
#ifdef HAVE_LONG_LONG_INT
222
    case T_LONGLONG:
223
      OUTCONV(*(LONGLONG *)ptrin, ttypeout);
224
      break;
225
#endif
226 2 bertin
    case T_FLOAT:
227 215 bertin
      OUTCONV(*(float *)ptrin, ttypeout);
228 2 bertin
      break;
229
    case T_DOUBLE:
230 215 bertin
      OUTCONV(*(double *)ptrin, ttypeout);
231 2 bertin
      break;
232
    default:
233
      break;
234
    }
235
 
236
  return;
237
  }
238