1 /*
2 fitsread.c
3
4 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 *
6 * Part of: The LDAC Tools
7 *
8 * Author: E.BERTIN, DeNIS/LDAC
9 *
10 * Contents: low-level functions for reading LDAC FITS catalogs.
11 *
12 * Last modify: 26/09/2004
13 *
14 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "fitscat_defs.h"
26 #include "fitscat.h"
27
28 char padbuf[FBSIZE];
29
30 /****** read_cat ***************************************************************
31 PROTO catstruct read_cat(char *filename)
32 PURPOSE ``Read'' a FITS catalog with name filename.
33 INPUT Filename,
34 OUTPUT catstruct pointer.
35 NOTES Returns NULL if no file with name \<filename\> is found.
36 AUTHOR E. Bertin (IAP & Leiden observatory)
37 VERSION 07/05/2002
38 ***/
read_cat(char * filename)39 catstruct *read_cat(char *filename)
40
41 {
42 catstruct *cat;
43
44 if (!(cat = new_cat(1)))
45 error (EXIT_FAILURE, "Not enough memory to read ", filename);
46
47 strcpy(cat->filename, filename);
48 if (open_cat(cat, READ_ONLY) != RETURN_OK)
49 {
50 free_cat(&cat, 1);
51 return NULL;
52 }
53
54 if (map_cat(cat) != RETURN_OK)
55 {
56 free_cat(&cat, 1);
57 return NULL;
58 }
59
60 return cat;
61 }
62
63
64 /****** read_cats **************************************************************
65 PROTO read_cats(char **filenames, int ncat)
66 PURPOSE ``Read'' several FITS catalogs.
67 INPUT A pointer to pointers of char,
68 The number of catalogs.
69 OUTPUT catstruct pointer.
70 NOTES -.
71 AUTHOR E. Bertin (IAP & Leiden observatory)
72 VERSION 25/04/97
73 ***/
read_cats(char ** filenames,int ncat)74 catstruct *read_cats(char **filenames, int ncat)
75
76 {
77 catstruct *cat, *ccat;
78 int i;
79
80 if (!(cat = new_cat(ncat)))
81 error (EXIT_FAILURE, "Not enough memory to read ", "catalogs");
82
83 for (i=ncat, ccat = cat; i--; ccat++, filenames++)
84 {
85 strcpy(ccat->filename, *filenames);
86 if (open_cat(ccat, READ_ONLY) != RETURN_OK)
87 error (EXIT_FAILURE, "Cannot open ", *filenames);
88 if (map_cat(ccat) != RETURN_OK)
89 error (EXIT_FAILURE, "Cannot map ", *filenames);
90 close_cat(ccat);
91 }
92
93 return cat;
94 }
95
96
97 /****** init_readobj **********************************************************
98 PROTO tabstruct *init_readobj(tabstruct *tab, char **pbuf)
99 PURPOSE Prepare the reading of individual sources in a FITS table
100 INPUT Table structure,
101 pointer to an array pointer to be used as a temporary buffer.
102 OUTPUT Pointer to the table structure from which the data will be read.
103 NOTES -.
104 AUTHOR E. Bertin (IAP & Leiden observatory)
105 VERSION 26/09/2004
106 ***/
init_readobj(tabstruct * tab,char ** pbuf)107 tabstruct *init_readobj(tabstruct *tab, char **pbuf)
108
109 {
110 catstruct *tabcat;
111 tabstruct *keytab;
112 keystruct *key;
113 int k;
114
115 /* Scan keys to find the reference tab and other things*/
116 keytab = NULL;
117 tabcat = NULL;
118 key = tab->key;
119 for (k=tab->nkey; k--; key = key->nextkey)
120 if (!key->ptr)
121 {
122 keytab = key->tab;
123 tabcat = keytab->cat;
124 QMALLOC(key->ptr, char, key->nbytes);
125 }
126 else
127 key->pos = -1;
128
129 if (!keytab)
130 error(EXIT_FAILURE,"*Error*: no original table found among keys in table ",
131 tab->extname);
132
133 if (open_cat(tabcat, READ_ONLY) != RETURN_OK)
134 error(EXIT_FAILURE, "*Error*: Cannot access ", tabcat->filename);
135 QFSEEK(tabcat->file, keytab->bodypos, SEEK_SET, tabcat->filename);
136
137 /* Allocate memory for the input buffer */
138 QMALLOC(*pbuf, char, tab->naxisn[0]);
139
140 return keytab;
141 }
142
143
144 /****** read_obj **************************************************************
145 PROTO int read_obj(tabstruct *keytab, tabstruct *tab, char *buf)
146 PURPOSE Read one individual source at the current position in a FITS table.
147 INPUT Table which will be accessed from disk (provided by init_readobj()),
148 table containing the keys that will be read,
149 pointer to the temporary buffer.
150 OUTPUT The number of table lines that remain to be read.
151 NOTES -.
152 AUTHOR E. Bertin (IAP & Leiden observatory)
153 VERSION 26/09/2004
154 ***/
read_obj(tabstruct * keytab,tabstruct * tab,char * buf)155 int read_obj(tabstruct *keytab, tabstruct *tab, char *buf)
156
157 {
158 keystruct *key;
159 char *pin, *pout;
160 int b,k;
161 int esize;
162
163 QFREAD(buf,keytab->naxisn[0],keytab->cat->file,keytab->cat->filename);
164 key = tab->key;
165 for (k=tab->nkey; k--; key = key->nextkey)
166 if (key->pos>=0)
167 {
168 pin = buf+key->pos;
169 pout = key->ptr;
170 if (bswapflag)
171 {
172 esize = t_size[key->ttype];
173 swapbytes(pin, esize, key->nbytes/esize);
174 }
175 for (b=key->nbytes; b--;)
176 *(pout++) = *(pin++);
177 }
178
179 return --keytab->naxisn[1];
180 }
181
182
183 /****** read_obj_at ***********************************************************
184 PROTO int read_obj_at(tabstruct *keytab, tabstruct *tab, char *buf, long pos)
185 PURPOSE Get one source at a specific position in a FITS table.
186 INPUT Table which will be accessed from disk (provided by init_readobj()),
187 table containing the keys that will be read.
188 pointer to the temporary buffer,
189 position number in table.
190 OUTPUT RETURN_OK if the object has been accessed, RETURN_ERROR otherwise.
191 NOTES -.
192 AUTHOR E. Bertin (IAP & Leiden observatory)
193 VERSION 26/09/2004
194 ***/
read_obj_at(tabstruct * keytab,tabstruct * tab,char * buf,long pos)195 int read_obj_at(tabstruct *keytab, tabstruct *tab, char *buf, long pos)
196
197 {
198 keystruct *key;
199 char *pin, *pout;
200 size_t n;
201 int b,k;
202 int esize;
203
204 if ((n=keytab->naxisn[0]*pos) >= keytab->tabsize)
205 return RETURN_ERROR;
206 QFSEEK(keytab->cat->file,keytab->bodypos+n, SEEK_SET, keytab->cat->filename);
207 QFREAD(buf,keytab->naxisn[0],keytab->cat->file,keytab->cat->filename);
208 key = tab->key;
209 for (k=tab->nkey; k--; key = key->nextkey)
210 if (key->pos>=0)
211 {
212 pin = buf+key->pos;
213 pout = key->ptr;
214 if (bswapflag)
215 {
216 esize = t_size[key->ttype];
217 swapbytes(pin, esize, key->nbytes/esize);
218 }
219 for (b=key->nbytes; b--;)
220 *(pout++) = *(pin++);
221 }
222
223 return RETURN_OK;
224 }
225
226
227 /****** end_readobj **********************************************************
228 PROTO void end_readobj(tabstruct *keytab, tabstruct *tab, char *buf)
229 PURPOSE End the writing of individual sources in a FITS table
230 INPUT Table which will be accessed from disk (provided by init_readobj()),
231 table containing the keys that have been read,
232 pointer to the temporary buffer.
233 OUTPUT -.
234 NOTES -.
235 AUTHOR E. Bertin (IAP & Leiden observatory)
236 VERSION 26/09/2004
237 ***/
end_readobj(tabstruct * keytab,tabstruct * tab,char * buf)238 void end_readobj(tabstruct *keytab, tabstruct *tab, char *buf)
239
240 {
241
242 if (close_cat(keytab->cat) != RETURN_OK)
243 error(EXIT_FAILURE,"*Error*: Problem while closing",keytab->cat->filename);
244
245 /* Recover the original state of the original table */
246 update_tab(keytab);
247 free(buf);
248
249 return;
250 }
251
252