Commit 7f18fd0d authored by Johan Richard's avatar Johan Richard
Browse files

Skip empty extensions when reading FITS files

parent 0c4d8340
......@@ -56,9 +56,9 @@ static int rdf_get_astro(fitsfile *fptr, char **pheader);
double ** rdf_fits(char *filename,int *nx,int *ny,char **pheader)
{
fitsfile *fptr; /* pointer to the FITS file, defined in fitsio.h */
int status, ii, jj, kk;
int status, hdutype, ii, jj, kk;
int nfound, anynull;
long naxes[2];
long naxes[2]={0,0};
long fpixel, nbuffer, npixels;
int nkeys;
// double crval1,crpix1,cdelt1;
......@@ -69,6 +69,7 @@ double ** rdf_fits(char *filename,int *nx,int *ny,char **pheader)
float buffer[buffsize];
status = 0; /* initialize status before calling fitsio routines */
ii = 0; /* start with first header */
if (fits_open_file(&fptr, filename, READONLY, &status))
printerror( status ); /* call printerror if error occurs */
......@@ -79,7 +80,19 @@ double ** rdf_fits(char *filename,int *nx,int *ny,char **pheader)
fpixel = 1; /* first pixel in the image */
npixels = naxes[0] * naxes[1]; /* number of pixels in the image */
while(npixels == 0)
{
printf("No pixels found in HDU %d. Switching to HDU %d\n",ii,ii+1);
ii++;
if (fits_movabs_hdu(fptr,ii, &hdutype, &status))
printerror( status );
if( fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxes, &nfound, &status) )
printerror( status );
npixels = naxes[0] * naxes[1]; /* number of pixels in the image */
}
fits_get_hdu_num(fptr,&jj);
printf("Reading HDU %d\n",jj);
*nx = naxes[0];
*ny = naxes[1];
......
......@@ -831,12 +831,34 @@ void wrf_from_header(char *filein, char *filename, int nx, int ny, double **tabp
int ii,jj,k;
int bitpix=FLOAT_IMG;
long naxes[2] = {nx,ny};
long naxesin[2] = {0,0};
long npixels;
int hdutype;
int nfound;
status = 0; /* initialize status before calling fitsio routines */
if( fits_open_file(&infptr, filein, READONLY, &status) )
printerror( status );
fpixel = 1; /* first pixel in the image */
ii=0;
npixels=0;
/* move to first extension of input file with data */
do
{
ii++;
if (fits_movabs_hdu(infptr,ii, &hdutype, &status))
printerror( status );
if( fits_read_keys_lng(infptr, "NAXIS", 1, 2, naxesin, &nfound, &status) )
printerror( status );
npixels = naxesin[0] * naxesin[1]; /* number of pixels in the input image */
}
while(npixels == 0);
fpixel = 1;
nelements = nx*ny;
p_ima=(float *)malloc((unsigned) (nelements)*sizeof(float));
......@@ -848,21 +870,30 @@ void wrf_from_header(char *filein, char *filename, int nx, int ny, double **tabp
k++;
}
remove(filename); /* Delete old file if it already exists */
status = CREATE_DISK_FILE; /* initialize status before calling fitsio routines */
if (fits_create_file(&outfptr, filename, &status)) /* create new FITS file */
printerror( status ); /* call printerror if error occurs */
if ( fits_copy_header(infptr, outfptr, &status) )
if ( fits_create_img(outfptr, FLOAT_IMG, 2, naxes, &status) )
printerror( status );
// if ( fits_create_img(outfptr, bitpix, 2, naxes, &status) )
// printerror( status );
if ( fits_copy_header(infptr, outfptr, &status) )
printerror( status );
if ( fits_write_img(outfptr, TFLOAT, fpixel, nelements, p_ima, &status) )
printerror( status );
if (fits_movabs_hdu(outfptr,1, &hdutype, &status))
printerror( status );
if (fits_delete_hdu(outfptr, &hdutype, &status))
printerror( status );
if ( fits_close_file(infptr, &status) ) /* close the file */
printerror( status );
if ( fits_close_file(outfptr, &status) )
printerror(status);
free(p_ima);
......
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