Commit a7f2fe76 authored by Teake Nutma's avatar Teake Nutma
Browse files

Bundle CFITSIO dependent code together

parent b6e8c209
......@@ -85,7 +85,6 @@ PIXTYPE *alloc_body(tabstruct *tab, void (*func)(PIXTYPE *ptr, int npix))
tab->extname);
/* Decide if the data will go in physical memory or on swap-space */
//npix = tab->tabsize/tab->bytepix;
npix = tab->naxisn[0] * tab->naxisn[1];
size = npix*sizeof(PIXTYPE);
if (size < body_ramleft)
......@@ -298,62 +297,97 @@ void free_body(tabstruct *tab)
}
#ifdef HAVE_CFITSIO
void writeCfitsio(tabstruct *tab, size_t spoonful, void *cbufdata0) {
int status = 0; fits_write_img(tab->infptr, TFLOAT, tab->currentElement, spoonful, cbufdata0, &status);
if (status != 0) {
printf("CFITSIO ERROR writing start=%d end=%d absolute end=%d\n", tab->currentElement, (tab->currentElement + spoonful) , (tab->naxisn[0]*tab->naxisn[1]));
fits_report_error(stderr, status);
}
tab->currentElement = tab->currentElement + spoonful;
}
void readTileCompressed(tabstruct *tab, size_t spoonful, double* bufdata0) {
int status, hdutype;
// first of all, move to correct HDU
status = 0; fits_movabs_hdu(tab->infptr, tab->hdunum, &hdutype, &status);
if (status != 0) {
printf("Error moving to HDU %d\n", tab->hdunum);
fits_report_error(stderr, status);
}
// pixels count from 1
if (tab->currentElement == 0) tab->currentElement = 1;
// now read section of image
int datatype;
switch(tab->bitpix){
case BYTE_IMG:
datatype = TBYTE;
break;
case SHORT_IMG:
datatype = TSHORT;
break;
case LONG_IMG:
datatype = TLONG;
break;
case FLOAT_IMG:
datatype = TFLOAT;
break;
case DOUBLE_IMG:
datatype = TDOUBLE;
break;
}
int status, hdutype;
// first of all, move to correct HDU
status = 0; fits_movabs_hdu(tab->infptr, tab->hdunum, &hdutype, &status);
if (status != 0) {
printf("Error moving to HDU %d\n", tab->hdunum);
fits_report_error(stderr, status);
}
// pixels count from 1
if (tab->currentElement == 0) tab->currentElement = 1;
int anynul;
double bscale = 1.0, bzero = 0.0, nulval = 0.;
// now read section of image
int datatype;
switch(tab->bitpix){
case BYTE_IMG:
datatype = TBYTE;
break;
case SHORT_IMG:
datatype = TSHORT;
break;
case LONG_IMG:
datatype = TLONG;
break;
case FLOAT_IMG:
datatype = TFLOAT;
break;
case DOUBLE_IMG:
datatype = TDOUBLE;
break;
}
// turn off any scaling so that we copy raw pixel values
status = 0; fits_set_bscale(tab->infptr, bscale, bzero, &status);
int anynul;
double bscale = 1.0, bzero = 0.0, nulval = 0.;
// now read the image
status = 0; fits_read_img(tab->infptr, datatype, tab->currentElement, spoonful, &nulval, bufdata0, &anynul, &status);
// turn off any scaling so that we copy raw pixel values
status = 0; fits_set_bscale(tab->infptr, bscale, bzero, &status);
// report reading error
if (status != 0) {
// now read the image
status = 0; fits_read_img(tab->infptr, datatype, tab->currentElement, spoonful, &nulval, bufdata0, &anynul, &status);
printf("CFITSIO ERROR reading start=%d end=%d absolute end=%d\n", tab->currentElement, (tab->currentElement + spoonful) , (tab->naxisn[0]*tab->naxisn[1]));
fits_report_error(stderr, status);
}
// report reading error
if (status != 0) {
// update file 'pointer'
tab->currentElement += spoonful;
}
printf("CFITSIO ERROR reading start=%d end=%d absolute end=%d\n", tab->currentElement, (tab->currentElement + spoonful) , (tab->naxisn[0]*tab->naxisn[1]));
fits_report_error(stderr, status);
}
// update file 'pointer'
tab->currentElement += spoonful;
}
#endif
void readSpoonful(tabstruct *tab, size_t spoonful, double* bufdata0, char* bufdata) {
#ifdef HAVE_CFITSIO
if(tab->isTileCompressed)
readTileCompressed(tab, spoonful, bufdata0);
else
#endif
QFREAD(bufdata, spoonful*tab->bytepix, tab->cat->file, tab->cat->filename);
}
void writeSpoonful(tabstruct *tab, size_t spoonful, void *cbufdata0) {
#ifdef HAVE_CFITSIO
if (0 && tab->infptr != NULL)
writeCfitsio(tab, spoonful, cbufdata0);
else
#endif
QFWRITE(cbufdata0, spoonful * tab->bytepix, tab->cat->file, tab->cat->filename);
}
/******* read_body ************************************************************
PROTO read_body(tabstruct *tab, PIXTYPE *ptr, long size)
PURPOSE Read floating point values from the body of a FITS table.
......@@ -409,13 +443,7 @@ void read_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
if (spoonful>size)
spoonful = size;
bufdata = (char *)bufdata0;
#ifdef HAVE_CFITSIO
if (tab->isTileCompressed)
readTileCompressed(tab, spoonful, bufdata0);
else
#endif
QFREAD(bufdata, spoonful*tab->bytepix, cat->file, cat->filename);
readSpoonful(tab, spoonful, bufdata0, bufdata);
switch(tab->bitpix)
{
......@@ -732,14 +760,7 @@ void read_ibody(tabstruct *tab, FLAGTYPE *ptr, size_t size)
if (spoonful>size)
spoonful = size;
bufdata = (char *)bufdata0;
// CFITSIO
#ifdef HAVE_CFITSIO
if (tab->isTileCompressed)
readTileCompressed(tab, spoonful, bufdata0);
else
#endif
QFREAD(bufdata, spoonful*tab->bytepix, cat->file, cat->filename);
readSpoonful(tab, spoonful, bufdata0, bufdata);
switch(tab->bitpix)
{
......@@ -1036,24 +1057,7 @@ void write_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
break;
}
// CFITSIO - if cfitsio output file has been set up, then proceed to write using cfitsio
#ifdef HAVE_CFITSIO
if (0 && tab->infptr != NULL) { // TODO
int status = 0; fits_write_img(tab->infptr, TFLOAT, tab->currentElement, spoonful, cbufdata0, &status);
if (status != 0) {
printf("CFITSIO ERROR writing start=%d end=%d absolute end=%d\n", tab->currentElement, (tab->currentElement + spoonful) , (tab->naxisn[0]*tab->naxisn[1]));
fits_report_error(stderr, status);
}
tab->currentElement = tab->currentElement + spoonful;
}
// otherwise, continue with usual AstrOmatic fits writing routine
else
#endif
QFWRITE(cbufdata0, spoonful*tab->bytepix, cat->file, cat->filename);
writeSpoonful(tab, spoonful, cbufdata0);
}
break;
......
......@@ -165,27 +165,64 @@ int close_cat(catstruct *cat)
return RETURN_OK;
}
int open_cfitsio(catstruct *cat)
{
#ifdef HAVE_CFITSIO
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;
if (cat->tab->infptr) {
int status = 0; fits_close_file(cat->tab->infptr, &status);
; 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 {
//printf("SUCCESS CFITSIO CLOSE\n\n");
cat->tab->infptr == NULL;
}
}
//printf("NO CFITSIO FILE TO CLOSE\n");
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);
#endif
}
/****** free_cat ***************************************************************
PROTO void free_cat(catstruct **cat, int ncat)
......@@ -343,19 +380,9 @@ int map_cat(catstruct *cat)
prevtab = NULL;
QCALLOC(tab, tabstruct, 1);
tab->cat = cat;
cat->tab = tab;
QFTELL(cat->file, tab->headpos, cat->filename);
#ifdef HAVE_CFITSIO
// CFITSIO
fitsfile *infptr;
int status, hdutype, hdunum;
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);
}
hdunum = 1;
#endif
open_cfitsio(cat);
for (ntab=0; !get_head(tab); ntab++)
{
......@@ -363,40 +390,17 @@ int map_cat(catstruct *cat)
readbintabparam_head(tab);
QFTELL(cat->file, tab->bodypos, cat->filename);
tab->nseg = tab->seg = 1;
#ifdef HAVE_CFITSIO
tab->hdunum = hdunum;
tab->infptr = infptr;
status = 0; 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, cat->filename);
//tab->tabsize = infptr->Fptr->rowlength;
//printf("TABSIZE = %ld\n", tab->tabsize);
#endif
if (tab->tabsize) {
#ifdef HAVE_CFITSIO
// IMPORTANT: moving to start of next header using fseek and cfitsio position rather than table size, as done previously
fseek(cat->file, infptr->Fptr->headstart[hdunum], SEEK_SET);
#else
// this is how it was done previously
QFSEEK(cat->file, PADTOTAL(tab->tabsize), SEEK_CUR, cat->filename);
#endif
}
tab->hdunum = ntab + 1;
if (prevtab)
{
tab->prevtab = prevtab;
prevtab->nexttab = tab;
}
else
cat->tab = tab;
prevtab = tab;
hdu_seek(tab);
QCALLOC(tab, tabstruct, 1);
tab->cat = cat;
QFTELL(cat->file, tab->headpos, cat->filename);
#ifdef HAVE_CFITSIO
hdunum++;
#endif
}
cat->ntab = ntab;
......
......@@ -121,7 +121,9 @@ void readbasic_head(tabstruct *tab)
if (fitsread(tab->headbuf, "ZIMAGE ", str, H_STRING, T_STRING) == RETURN_OK) {
tab->isTileCompressed = 1;
#ifndef HAVE_CFITSIO
error(EXIT_FAILURE, "*Error*: No CFITSIO support but detected tile compression in ", filename);
#endif
strcpy(NAXIS_KEYWORD, "ZNAXIS ");
strcpy(BITPIX_KEYWORD, "ZBITPIX ");
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment