FloatImg/funcs/fimg-fits.c

117 lines
2.7 KiB
C
Raw Normal View History

2020-07-24 19:38:13 +11:00
/*
* FLOATIMG
* import/export to/from FITS files
2020-07-25 13:26:06 +11:00
https://heasarc.gsfc.nasa.gov/docs/software/fitsio/c/c_user/node1.html
2020-07-24 19:38:13 +11:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <fitsio.h>
#include "../floatimg.h"
extern int verbosity;
/* --------------------------------------------------------------------- */
2020-08-07 16:39:22 +11:00
int fimg_save_plane_as_fits(FloatImg *src, char *outname,
char plane, int flags)
2020-07-24 19:38:13 +11:00
{
fitsfile *fptr; /* pointer to the FITS file */
2020-07-25 19:34:18 +11:00
int status, sz;
2020-07-24 19:38:13 +11:00
int bitpix = FLOAT_IMG;
2020-08-07 16:39:22 +11:00
float *pplane;
2020-07-24 19:38:13 +11:00
long naxis = 2;
long naxes[2];
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p '%s' %d )\n", __func__, src, outname, flags);
#endif
status = 0;
2020-08-07 16:39:22 +11:00
switch (plane) {
case 'r': case 'R':
pplane = src->R; break;
case 'g': case 'G':
pplane = src->G; break;
case 'b': case 'B':
pplane = src->B; break;
default:
return -66;
}
2020-07-24 19:38:13 +11:00
remove(outname); /* Delete old file if it already exists */
if (fits_create_file(&fptr, outname, &status)) {
fits_report_error(stderr, status);
return -9;
}
naxes[0] = src->width; naxes[1] = src->height;
2020-07-25 01:21:24 +11:00
if (verbosity) fimg_describe(src, "to be saved as FITS");
2020-07-24 19:38:13 +11:00
if ( fits_create_img(fptr, bitpix, naxis, naxes, &status) ) {
fits_report_error(stderr, status);
return -10;
}
sz = naxes[0]*naxes[1];
2020-08-07 16:39:22 +11:00
if ( fits_write_img(fptr, TFLOAT, 1, sz, pplane, &status) ) {
2020-07-24 19:38:13 +11:00
fits_report_error(stderr, status);
return -10;
}
if ( fits_close_file(fptr, &status) ) {
fits_report_error(stderr, status);
return -9;
}
return 0;
}
/* --------------------------------------------------------------------- */
2020-08-07 16:39:22 +11:00
int fimg_save_R_as_fits(FloatImg *src, char *outname, int flags)
{
int retv;
retv = fimg_save_plane_as_fits(src, outname, 'r', flags);
return retv;
}
/* --------------------------------------------------------------------- */
int fimg_save_G_as_fits(FloatImg *src, char *outname, int flags)
{
int retv;
retv = fimg_save_plane_as_fits(src, outname, 'g', flags);
return retv;
}
/* --------------------------------------------------------------------- */
int fimg_save_B_as_fits(FloatImg *src, char *outname, int flags)
{
int retv;
retv = fimg_save_plane_as_fits(src, outname, 'b', flags);
return retv;
}
2020-07-25 19:34:18 +11:00
/************************************************************
***** MAGIC CODE FROM OUTERSPACE ?
function 'writeimage' from :
https://heasarc.gsfc.nasa.gov/docs/software/fitsio/cexamples/cookbook.c
float **array;
array = calloc(src->height, sizeof(float *));
array[0] = src->R;
#define REVERSE 1
for( idx=0; idx<naxes[1]; idx++ ) {
#if REVERSE
k = naxes[1] - idx - 1;
#else
k = idx;
#endif
array[idx] = src->R + (k*naxes[0]);
fprintf(stderr, " %6d %6d %p\n", idx, k, array[idx]);
}
**************************************************************/