fitsread.c 6.43 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 				fitsread.c

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	Part of:	The LDAC Tools
*
*	Author:		E.BERTIN, DeNIS/LDAC
*
*	Contents:	low-level functions for reading LDAC FITS catalogs.
*
*	Last modify:	26/09/2004
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

#ifdef	HAVE_CONFIG_H
#include "config.h"
#endif

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

#include	"fitscat_defs.h"
#include	"fitscat.h"

char	padbuf[FBSIZE];

/****** read_cat ***************************************************************
PROTO	catstruct read_cat(char *filename)
PURPOSE	``Read'' a FITS catalog with name filename.
INPUT	Filename,
OUTPUT	catstruct pointer.
NOTES	Returns NULL if no file with name \<filename\> is found.
AUTHOR	E. Bertin (IAP & Leiden observatory)
VERSION	07/05/2002
 ***/
catstruct	*read_cat(char *filename)

  {
   catstruct *cat;

  if (!(cat = new_cat(1)))
    error (EXIT_FAILURE, "Not enough memory to read ", filename);

  strcpy(cat->filename, filename);
  if (open_cat(cat, READ_ONLY) != RETURN_OK)
    {
    free_cat(&cat, 1);
    return NULL;
    }

  if (map_cat(cat) != RETURN_OK)
    {
    free_cat(&cat, 1);
    return NULL;
    }

  return cat;
  }


/****** read_cats **************************************************************
PROTO	read_cats(char **filenames, int ncat)
PURPOSE	``Read'' several FITS catalogs.
INPUT	A pointer to pointers of char,
	The number of catalogs.
OUTPUT	catstruct pointer.
NOTES	-.
AUTHOR	E. Bertin (IAP & Leiden observatory)
VERSION	25/04/97
 ***/
catstruct	*read_cats(char **filenames, int ncat)

  {
   catstruct	*cat, *ccat;
   int		i;

  if (!(cat = new_cat(ncat)))
    error (EXIT_FAILURE, "Not enough memory to read ", "catalogs");

  for (i=ncat, ccat = cat; i--; ccat++, filenames++)
    {
    strcpy(ccat->filename, *filenames);
    if (open_cat(ccat, READ_ONLY) != RETURN_OK)
      error (EXIT_FAILURE, "Cannot open ", *filenames);
    if (map_cat(ccat) != RETURN_OK)
      error (EXIT_FAILURE, "Cannot map ", *filenames);
    close_cat(ccat);
    }

  return cat;
  }


/****** init_readobj **********************************************************
PROTO	tabstruct *init_readobj(tabstruct *tab, char **pbuf)
PURPOSE	Prepare the reading of individual sources in a FITS table
INPUT	Table structure,
	pointer to an array pointer to be used as a temporary buffer.
OUTPUT	Pointer to the table structure from which the data will be read.
NOTES	-.
AUTHOR	E. Bertin (IAP & Leiden observatory)
VERSION	26/09/2004
 ***/
tabstruct	*init_readobj(tabstruct *tab, char **pbuf)

  {
   catstruct	*tabcat;
   tabstruct	*keytab;
   keystruct	*key;
   int		k;

/* Scan keys to find the reference tab and other things*/
  keytab = NULL;
  tabcat = NULL;
  key = tab->key;
  for (k=tab->nkey; k--; key = key->nextkey)
    if (!key->ptr)
      {
      keytab = key->tab;
      tabcat = keytab->cat;
      QMALLOC(key->ptr, char, key->nbytes);
      }
    else
      key->pos = -1;

  if (!keytab)
    error(EXIT_FAILURE,"*Error*: no original table found among keys in table ",
	tab->extname);

  if (open_cat(tabcat, READ_ONLY) != RETURN_OK)
    error(EXIT_FAILURE, "*Error*: Cannot access ", tabcat->filename);
  QFSEEK(tabcat->file, keytab->bodypos, SEEK_SET, tabcat->filename);

/* Allocate memory for the input buffer */
  QMALLOC(*pbuf, char, tab->naxisn[0]);

  return keytab;
  }


/****** read_obj **************************************************************
PROTO	int read_obj(tabstruct *keytab, tabstruct *tab, char *buf)
PURPOSE	Read one individual source at the current position in a FITS table.
INPUT	Table which will be accessed from disk (provided by init_readobj()),
	table containing the keys that will be read,
	pointer to the temporary buffer.
OUTPUT	The number of table lines that remain to be read.
NOTES	-.
AUTHOR	E. Bertin (IAP & Leiden observatory)
VERSION	26/09/2004
 ***/
int	read_obj(tabstruct *keytab, tabstruct *tab, char *buf)

  {
   keystruct	*key;
   char		*pin, *pout;
   int		b,k;
   int		esize;

  QFREAD(buf,keytab->naxisn[0],keytab->cat->file,keytab->cat->filename);
  key = tab->key;
  for (k=tab->nkey; k--; key = key->nextkey)
    if (key->pos>=0)
      {
      pin = buf+key->pos;
      pout = key->ptr;
      if (bswapflag)
        {
        esize = t_size[key->ttype];
        swapbytes(pin, esize, key->nbytes/esize);
        }
      for (b=key->nbytes; b--;)
        *(pout++) = *(pin++);
      }

  return --keytab->naxisn[1];
  }


/****** read_obj_at ***********************************************************
PROTO	int read_obj_at(tabstruct *keytab, tabstruct *tab, char *buf, long pos)
PURPOSE Get one source at a specific position in a FITS table.
INPUT	Table which will be accessed from disk (provided by init_readobj()),
	table containing the keys that will be read.
	pointer to the temporary buffer,
	position number in table.
OUTPUT	RETURN_OK if the object has been accessed, RETURN_ERROR otherwise.
NOTES	-.
AUTHOR	E. Bertin (IAP & Leiden observatory)
VERSION	26/09/2004
 ***/
int	read_obj_at(tabstruct *keytab, tabstruct *tab, char *buf, long pos)

  {
   keystruct	*key;
   char		*pin, *pout;
   size_t	n;
   int		b,k;
   int		esize;

  if ((n=keytab->naxisn[0]*pos) >= keytab->tabsize)
    return RETURN_ERROR;
  QFSEEK(keytab->cat->file,keytab->bodypos+n, SEEK_SET, keytab->cat->filename);
  QFREAD(buf,keytab->naxisn[0],keytab->cat->file,keytab->cat->filename);
  key = tab->key;
  for (k=tab->nkey; k--; key = key->nextkey)
    if (key->pos>=0)
      {
      pin = buf+key->pos;
      pout = key->ptr;
      if (bswapflag)
        {
        esize = t_size[key->ttype];
        swapbytes(pin, esize, key->nbytes/esize);
        }
      for (b=key->nbytes; b--;)
        *(pout++) = *(pin++);
      }

  return RETURN_OK;
  }


/****** end_readobj **********************************************************
PROTO	void end_readobj(tabstruct *keytab, tabstruct *tab, char *buf)
PURPOSE	End the writing of individual sources in a FITS table
INPUT	Table which will be accessed from disk (provided by init_readobj()),
	table containing the keys that have been read,
	pointer to the temporary buffer.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP & Leiden observatory)
VERSION	26/09/2004
 ***/
void	end_readobj(tabstruct *keytab, tabstruct *tab, char *buf)

  {

  if (close_cat(keytab->cat) != RETURN_OK)
    error(EXIT_FAILURE,"*Error*: Problem while closing",keytab->cat->filename);

/* Recover the original state of the original table */
  update_tab(keytab);
  free(buf);

  return;
  }