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

Bundle CFITSIO dependent code together

parent b6e8c209
Loading
Loading
Loading
Loading
+84 −80
Original line number Diff line number Diff line
@@ -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,6 +297,20 @@ 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;
@@ -354,6 +367,27 @@ void readTileCompressed(tabstruct *tab, size_t spoonful, double* bufdata0) {
}
#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;

+46 −42
Original line number Diff line number Diff line
@@ -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;
+3 −1
Original line number Diff line number Diff line
@@ -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 ");
  }