Newer
Older
* fitscat.c
*
* Low-level functions for handling FITS images and tables.
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* This file part of: AstrOmatic FITS/LDAC library
*
Emmanuel Bertin
committed
* Copyright: (C) 1995-2012 Emmanuel Bertin -- IAP/CNRS/UPMC
* License: GNU General Public License
* AstrOmatic software is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* AstrOmatic software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with AstrOmatic software.
* If not, see <http://www.gnu.org/licenses/>.
Emmanuel Bertin
committed
* Last modified: 29/08/2012
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include "fitscat_defs.h"
#include "fitscat.h"
/****** about_cat **************************************************************
PROTO int about_cat(catstruct *cat, FILE *stream)
PURPOSE Print some info about a catalog.
INPUT Catalog structure,
output stream.
OUTPUT RETURN_OK if everything went as expected, RETURN_ERROR otherwise.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 19/03/2002
***/
int about_cat(catstruct *cat, FILE *stream)
{
tabstruct *tab;
int i;
fprintf(stream,"\n");
/*General info about the catalog itself*/
fprintf(stream,
"------------------Catalog information----------------\n");
fprintf(stream,
"Filename:..............%s\n", cat->filename);
fprintf(stream,
"Number of segments:....%d\n", cat->ntab);
fprintf(stream,"\n");
/*Now for each table*/
tab = cat->tab;
for (i=0; i<cat->ntab; i++)
{
fprintf(stream,
"****** Table #%d\n", i+1);
fprintf(stream,
" Extension type:.........%s\n",
tab->xtension[0]? tab->xtension: "(Primary HDU)");
fprintf(stream,
" Extension name:.........%s\n", tab->extname);
if (tab->naxis)
{
fprintf(stream,
" Number of dimensions:...%d\n", tab->naxis);
fprintf(stream,
" Number of elements:.....%d\n", tab->naxisn[1]);
if (tab->tfields)
fprintf(stream,
" Number of data fields...%d\n", tab->tfields);
fprintf(stream,
" Body size:..............%ld bytes\n",
(unsigned long)tab->tabsize);
}
fprintf(stream,"\n");
while (!(tab=tab->nexttab)->nseg);
}
fprintf(stream,"\n");
return RETURN_OK;
}
/****** addhistoryto_cat *******************************************************
PROTO int addhistoryto_cat(catstruct *cat, char *str)
PURPOSE Add a HISTORY line to a FITS catalog.
INPUT A pointer to catalog structure, and the character string to insert.
OUTPUT RETURN_OK if everything went as expected, RETURN_ERROR otherwise.
NOTES The pointer to the primary header might be reallocated if necessary.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 25/09/2004
***/
int addhistoryto_cat(catstruct *cat, char *str)
{
time_t thetime;
char str2[82];
tabstruct *tab;
int n, headpos;
tab = cat->tab;
n = tab->headnblock;
headpos = fitsfind(tab->headbuf, "END ");
if (headpos >= n*(FBSIZE/80) - 1)
{
QREALLOC(tab->headbuf, char, (n+1)*FBSIZE);
memset(&tab->headbuf[n*FBSIZE], ' ', FBSIZE);
tab->headnblock++;
}
if (time(&thetime)==-1)
warning("No time available for history","");
if (!strftime(str2, 16, "%d/%m/%Y %H:%M", localtime(&thetime)))
error(EXIT_FAILURE, "*Internal Error*: Time/date string too long in ",
"addhistoryto_cat()");
sprintf(str2, "%s %.65s", str2, str);
fitsadd(tab->headbuf, "HISTORY ", str2);
return RETURN_OK;
}
/****** close_cat **************************************************************
PROTO int close_cat(catstruct *cat)
PURPOSE Close a FITS catalog.
INPUT catalog structure.
OUTPUT RETURN_OK if everything went as expected, RETURN_ERROR otherwise.
NOTES the file structure member is set to NULL;
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 22/06/2001
***/
int close_cat(catstruct *cat)
{
if (cat->file && fclose(cat->file))
{
cat->file = NULL;
return RETURN_ERROR;
}
cat->file = NULL;
return RETURN_OK;
}
int open_cfitsio(catstruct *cat)
{
fitsfile *infptr;
int status;
status = 0;
fits_open_file(&infptr, cat->filename, READONLY, &status);
if (status != 0) {
fits_report_error(stderr, status);
printf("ERROR could not open FITS file with cfitsio: %s\n", cat->filename);
}
cat->tab->infptr = infptr;
return status;
#else
return 1;
#endif
}
int close_cfitsio(catstruct *cat)
{
#ifdef HAVE_CFITSIO
int status = 0;
; fits_close_file(cat->tab->infptr, &status);
if (status != 0) {
fits_report_error(stderr, status);
printf("ERROR could not close FITS file with cfitsio: %s\n", cat->filename);
}
else {
cat->tab->infptr == NULL;
}
}
return status;
#else
return 1;
#endif
void hdu_seek(tabstruct *tab) {
#ifdef HAVE_CFITSIO
if(!tab->infptr) {
tab->infptr = tab->prevtab->infptr;
}
int status = 0;
int hdutype;
fits_movabs_hdu(tab->infptr, tab->hdunum, &hdutype, &status);
if (status != 0)
printf("ERROR could not move to hdu %d in file %s\n", tab->hdunum, tab->cat->filename);
if (tab->tabsize)
// IMPORTANT: moving to start of next header using fseek and cfitsio position rather than table size, as done previously
fseek(tab->cat->file, tab->infptr->Fptr->headstart[tab->hdunum], SEEK_SET);
#else
if (tab->tabsize)
QFSEEK(tab->cat->file, PADTOTAL(tab->tabsize), SEEK_CUR, tab->cat->filename);
/****** free_cat ***************************************************************
PROTO void free_cat(catstruct **cat, int ncat)
PURPOSE Free all structures allocated for one or several FITS catalog.
INPUT Pointer to a catalog structure,
Number of catalogs.
OUTPUT -.
NOTES Unallocated pointers should have been put to NULL.
AUTHOR E. Bertin (IAP & Leiden observatory)
***/
void free_cat(catstruct **cat, int ncat)
{
catstruct **thecat;
int i;
/*--free memory allocated within each catalog */
thecat = cat;
for (i=ncat; i--;)
{
if ((*thecat)->file)
close_cat(*thecat);
remove_tabs(*thecat);
free(*(thecat++));
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
}
return;
}
/****** inherit_cat ************************************************************
PROTO int inherit_cat(catstruct *catin, catstruct *catout)
PURPOSE Copy the primary table, and all other informations from one catalog
to another, except those related to the associated file itself
(filename, etc...),
INPUT A pointer to both catalog structures.
OUTPUT RETURN_OK if at least one table was copied, RETURN_ERROR otherwise.
NOTES The output catalog should be ``cleaned'' before call.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 13/06/2002
***/
int inherit_cat(catstruct *catin, catstruct *catout)
{
tabstruct *tabin, *tabout, *prevtabout;
int j;
catout->ntab = 1;
tabin = catin->tab;
/*copy only one table: well it could be simpler, but let's stay general!*/
prevtabout = NULL;
for (j=tabin->nseg; j--;)
{
QCALLOC(tabout, tabstruct, 1);
*tabout = *tabin;
if (tabin->naxis)
QMEMCPY(tabin->naxisn, tabout->naxisn, int, (size_t)tabin->naxis);
if (tabin->headbuf)
QMEMCPY(tabin->headbuf, tabout->headbuf, char,
tabin->headnblock*FBSIZE);
if (tabin->bodybuf)
QMEMCPY(tabin->bodybuf, tabout->bodybuf, char, (size_t)tabin->tabsize);
if (prevtabout)
{
tabout->prevtab = prevtabout;
prevtabout->nexttab = tabout;
}
else
{
catout->tab = tabout;
}
prevtabout = tabout;
tabin = tabin->nexttab;
}
if (prevtabout)
{
prevtabout->nexttab = catout->tab;
catout->tab->prevtab = prevtabout;
}
else
return RETURN_ERROR;
return RETURN_OK;
}
/****** init_cat ***************************************************************
PROTO int init_cat(catstruct *cat)
PURPOSE Initialize a catalog, "cleaning" any content if present
and adding the primary header "table".
INPUT A pointer to the catalog structure.
OUTPUT RETURN_OK if everything went as expected, RETURN_ERROR otherwise.
NOTES The output catalog should be ``cleaned'' before call.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 28/05/2001
***/
int init_cat(catstruct *cat)
{
static char bintabtemplate[][80] = {
"SIMPLE = T / This is a FITS file",
"BITPIX = 8 / ",
"NAXIS = 0 / ",
"EXTEND = T / This file may contain FITS extensions",
"END "};
tabstruct *tab;
char *buf;
int i;
/* Initialize the primary header itself */
QCALLOC(tab, tabstruct, 1);
tab->naxis = 0;
tab->bitpix = 8;
tab->bytepix = 1;
tab->pcount = 0;
tab->gcount = 1;
tab->seg = 1;
tab->nseg = 1;
/* Provide a new header*/
QCALLOC(tab->headbuf, char, FBSIZE);
memcpy(tab->headbuf, bintabtemplate, sizeof(bintabtemplate));
for (buf = tab->headbuf, i=0; i<FBSIZE; i++, buf++)
if (!*buf)
*buf = ' ';
tab->headnblock = 1;
/* Clean catalog and add the table to it */
remove_tabs(cat);
cat->tab = tab->prevtab = tab->nexttab = tab;
cat->ntab = 1;
return RETURN_OK;
}
/****** map_cat ****************************************************************
PROTO int map_cat(catstruct *cat)
PURPOSE Explores the whole FITS file
and gets information for each of the FITS tables it contains.
INPUT catalog structure.
OUTPUT RETURN_OK if at least one table was found, RETURN_ERROR otherwise.
NOTES Memory space for the array of fits structures is reallocated.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 14/12/2002
***/
int map_cat(catstruct *cat)
{
int ntab;
tabstruct *tab, *prevtab;
/*scan through the file until we reach the end*/
prevtab = NULL;
QCALLOC(tab, tabstruct, 1);
tab->cat = cat;
QFTELL(cat->file, tab->headpos, cat->filename);
rhenders
committed
for (ntab=0; !get_head(tab); ntab++)
{
readbasic_head(tab);
readbintabparam_head(tab);
QFTELL(cat->file, tab->bodypos, cat->filename);
tab->nseg = tab->seg = 1;
if (prevtab)
{
tab->prevtab = prevtab;
prevtab->nexttab = tab;
}
prevtab = tab;
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
QCALLOC(tab, tabstruct, 1);
tab->cat = cat;
QFTELL(cat->file, tab->headpos, cat->filename);
}
cat->ntab = ntab;
free(tab);
if (prevtab)
{
prevtab->nexttab = cat->tab;
cat->tab->prevtab = prevtab;
}
else
return RETURN_ERROR;
/*rewind to the beginning*/
/*
QFSEEK(cat->file, 0, SEEK_SET, cat->filename);
*/
return RETURN_OK;
}
/****** new_cat ****************************************************************
PROTO catstruct *new_cat(int ncat)
PURPOSE Initialize a structure for a FITS catalog.
INPUT Number of catalogs.
OUTPUT A pointer to the catalog array.
NOTES All fields are initialized to 0.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 20/03/96
***/
catstruct *new_cat(int ncat)
{
catstruct *cat;
QCALLOC(cat, catstruct, ncat);
cat->access_type = WRITE_ONLY;
return cat;
}
/****** open_cat ***************************************************************
Emmanuel Bertin
committed
PROTO int open_cat(catstruct *cat, access_type_t at)
PURPOSE Open a FITS catalog with name filename.
INPUT catalog structure,
access type (can be WRITE_ONLY or READ_ONLY).
OUTPUT RETURN_OK if the cat is found, RETURN_ERROR otherwise.
NOTES If the file was already opened by this catalog, nothing is done.
AUTHOR E. Bertin (IAP & Leiden observatory)
Emmanuel Bertin
committed
VERSION 29/08/2012
Emmanuel Bertin
committed
int open_cat(catstruct *cat, access_type_t at)
{
if (cat->access_type == READ_ONLY && at == WRITE_ONLY)
error(EXIT_FAILURE, "*Internal Error*: Trying to write to the "
"READ_ONLY catalog ", cat->filename);
if (!cat->file)
{
if ((cat->file = fopen(cat->filename, at==WRITE_ONLY?"wb":"rb")) == NULL)
return RETURN_ERROR;
cat->access_type = at;
}
return RETURN_OK;
}