Commit 90385513 authored by Emmanuel Bertin's avatar Emmanuel Bertin
Browse files

Fixed issue with output Check-image headers when reading individual extensions...

Fixed issue with output Check-image headers when reading individual extensions from MEFs (thanks to user Lantern for the bug report).
parent ec2f0c6f
.TH SEXTRACTOR "1" "November 2010" "SExtractor 2.13.1" "User Commands"
.TH SEXTRACTOR "1" "December 2010" "SExtractor 2.13.1" "User Commands"
.SH NAME
sex \- extract a source catalogue from an astronomical FITS image
.SH SYNOPSIS
......
......@@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 11/10/2010
* Last modified: 12/02/2010
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
......@@ -155,32 +155,28 @@ initialize check-image.
checkstruct *initcheck(char *filename, checkenum check_type, int next)
{
catstruct *fitscat;
catstruct *cat;
checkstruct *check;
QCALLOC(check, checkstruct, 1);
strcpy(check->filename, filename);
check->type = check_type;
check->next = next;
cat = check->cat = new_cat(1);
strcpy(cat->filename, filename);
if (next>1)
/*-- Create a "pure" primary HDU */
{
fitscat = new_cat(1);
init_cat(fitscat);
strcpy(fitscat->filename, filename);
fitsadd(fitscat->tab->headbuf, "NEXTEND ", "Number of extensions");
fitswrite(fitscat->tab->headbuf, "NEXTEND ", &next, H_INT, T_LONG);
if (open_cat(fitscat, WRITE_ONLY) != RETURN_OK)
init_cat(cat);
addkeywordto_head(cat->tab, "NEXTEND ", "Number of extensions");
fitswrite(cat->tab->headbuf, "NEXTEND ", &next, H_INT, T_LONG);
if (open_cat(cat, WRITE_ONLY) != RETURN_OK)
error(EXIT_FAILURE,"*Error*: cannot open for writing ", filename);
save_tab(fitscat, fitscat->tab);
check->file = fitscat->file;
fitscat->file = NULL;
free_cat(&fitscat, 1);
save_head(cat, cat->tab);
remove_tabs(cat);
}
else
if (!(check->file = fopen(check->filename, "wb")))
error(EXIT_FAILURE, "*Error*: Cannot open for output ", check->filename);
open_cat(cat, WRITE_ONLY);
return check;
}
......@@ -193,25 +189,37 @@ initialize check-image (for subsequent writing).
void reinitcheck(picstruct *field, checkstruct *check)
{
catstruct *cat;
tabstruct *tab;
wcsstruct *wcs;
char *buf;
int i, ival;
size_t padsize;
double dval;
ULONG *ptri;
char *fitshead;
PIXTYPE *ptrf;
double dval;
int i;
cat = check->cat;
/* Inherit the field FITS header */
check->fitsheadsize = field->tab->headnblock*FBSIZE;
QMEMCPY(field->tab->headbuf, check->fitshead, char, check->fitsheadsize);
remove_tabs(cat);
copy_tab_fromptr(field->tab, cat, 0);
tab = cat->tab;
tab->cat = cat;
if (check->next<=1)
prim_head(tab);
check->y = 0;
fitshead = tab->headbuf;
/* Neutralize possible scaling factors */
dval = 1.0;fitswrite(check->fitshead, "BSCALE ", &dval, H_FLOAT, T_DOUBLE);
dval = 0.0;fitswrite(check->fitshead, "BZERO ", &dval, H_FLOAT, T_DOUBLE);
ival = 1;fitswrite(check->fitshead, "BITSGN ", &ival, H_INT, T_LONG);
if (field->tab->compress_type != COMPRESS_NONE)
fitswrite(check->fitshead, "IMAGECOD", "NONE", H_STRING, T_STRING);
fitswrite(check->fitshead, "ORIGIN ", BANNER, H_STRING, T_STRING);
tab->bytepix = 4;
tab->bscale = 1.0;
tab->bzero = 0.0;
fitswrite(fitshead, "BSCALE ", &tab->bscale, H_FLOAT, T_DOUBLE);
fitswrite(fitshead, "BZERO ", &tab->bzero, H_FLOAT, T_DOUBLE);
fitswrite(fitshead, "BITSGN ", &tab->bitsgn, H_INT, T_LONG);
if (tab->compress_type != COMPRESS_NONE)
{
tab->compress_type = COMPRESS_NONE;
fitswrite(fitshead, "IMAGECOD", "NONE", H_STRING, T_STRING);
}
fitswrite(fitshead, "ORIGIN ", BANNER, H_STRING, T_STRING);
switch(check->type)
{
......@@ -219,28 +227,25 @@ void reinitcheck(picstruct *field, checkstruct *check)
case CHECK_BACKGROUND:
case CHECK_FILTERED:
case CHECK_SUBTRACTED:
ival = -32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
check->width = field->width;
check->height = field->height;
tab->bitpix = -32;
tab->bitsgn = 0;
tab->naxisn[0] = check->width = field->width;
tab->naxisn[1] = check->height = field->height;
check->npix = field->npix;
QMALLOC(ptrf, PIXTYPE, check->width);
check->pix = (void *)ptrf;
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
save_head(cat, cat->tab);
break;
case CHECK_BACKRMS:
case CHECK_SUBOBJECTS:
ival = -32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
check->width = field->width;
check->height = field->height;
tab->bitpix = -32;
tab->bitsgn = 0;
tab->naxisn[0] = check->width = field->width;
tab->naxisn[1] = check->height = field->height;
check->npix = field->npix;
QMALLOC(ptrf, PIXTYPE, check->width);
check->pix = (void *)ptrf;
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
QMALLOC(check->pix, PIXTYPE, check->width);
save_head(cat, cat->tab);
/*---- Allocate memory for replacing the blanked pixels by 0 */
if (!check->line)
QMALLOC(check->line, PIXTYPE, field->width);
......@@ -260,123 +265,97 @@ void reinitcheck(picstruct *field, checkstruct *check)
case CHECK_DISKS:
case CHECK_SUBDISKS:
case CHECK_PATTERNS:
ival = -32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
check->width = field->width;
check->height = field->height;
tab->bitpix = -32;
tab->bitsgn = 0;
tab->naxisn[0] = check->width = field->width;
tab->naxisn[1] = check->height = field->height;
check->npix = field->npix;
check->overlay = 30*field->backsig;
QCALLOC(ptrf, PIXTYPE, check->npix);
check->pix = (void *)ptrf;
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
QCALLOC(check->pix, PIXTYPE, check->npix);
save_head(cat, cat->tab);
break;
case CHECK_SEGMENTATION:
ival = 32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
check->width = field->width;
check->height = field->height;
tab->bitpix = 32;
tab->bitsgn = 1;
tab->naxisn[0] = check->width = field->width;
tab->naxisn[1] = check->height = field->height;
check->npix = field->npix;
QCALLOC(ptri, ULONG, check->npix);
check->pix = (void *)ptri;
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
QCALLOC(check->pix, ULONG, check->npix);
save_head(cat, cat->tab);
break;
case CHECK_ASSOC:
ival = -32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
check->width = field->width;
check->height = field->height;
tab->bitpix = -32;
tab->bitsgn = 0;
tab->naxisn[0] = check->width = field->width;
tab->naxisn[1] = check->height = field->height;
check->npix = field->npix;
QMALLOC(ptrf, PIXTYPE, check->npix);
check->pix = (void *)ptrf;
QMALLOC(check->pix, PIXTYPE, check->npix);
/*---- Initialize the pixmap to IEEE NaN */
memset(ptrf, 0xFF, check->npix*sizeof(LONG));
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
memset(check->pix, 0xFF, check->npix*sizeof(LONG));
save_head(cat, cat->tab);
break;
case CHECK_MINIBACKGROUND:
case CHECK_MINIBACKRMS:
ival = -32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
check->width = field->nbackx;
fitswrite(check->fitshead, "NAXIS1 ", &check->width, H_INT, T_LONG);
check->height = field->nbacky;
fitswrite(check->fitshead, "NAXIS2 ", &check->height, H_INT, T_LONG);
tab->bitpix = -32;
tab->bitsgn = 0;
tab->naxisn[0] = check->width = field->nbackx;
tab->naxisn[1] = check->height = field->nbacky;
/*---- Scale the WCS information if present */
if ((wcs=field->wcs))
{
dval = wcs->cdelt[0]*field->backw;
fitswrite(check->fitshead, "CDELT1 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CDELT1 ", &dval, H_EXPO, T_DOUBLE);
dval = wcs->cdelt[1]*field->backh;
fitswrite(check->fitshead, "CDELT2 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CDELT2 ", &dval, H_EXPO, T_DOUBLE);
dval = (wcs->crpix[0]-0.5)/field->backw + 0.5;
fitswrite(check->fitshead, "CRPIX1 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CRPIX1 ", &dval, H_EXPO, T_DOUBLE);
dval = (wcs->crpix[1]-0.5)/field->backh + 0.5;
fitswrite(check->fitshead, "CRPIX2 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CRPIX2 ", &dval, H_EXPO, T_DOUBLE);
dval = wcs->cd[0]*field->backw;
fitswrite(check->fitshead, "CD1_1 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CD1_1 ", &dval, H_EXPO, T_DOUBLE);
dval = wcs->cd[1]*field->backh;
fitswrite(check->fitshead, "CD1_2 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CD1_2 ", &dval, H_EXPO, T_DOUBLE);
dval = wcs->cd[wcs->naxis]*field->backw;
fitswrite(check->fitshead, "CD2_1 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CD2_1 ", &dval, H_EXPO, T_DOUBLE);
dval = wcs->cd[wcs->naxis+1]*field->backh;
fitswrite(check->fitshead, "CD2_2 ", &dval, H_EXPO, T_DOUBLE);
fitswrite(fitshead, "CD2_2 ", &dval, H_EXPO, T_DOUBLE);
}
check->npix = check->width*check->height;
QMALLOC(ptrf, PIXTYPE, check->npix);
check->pix = (void *)ptrf;
QMALLOC(check->pix, PIXTYPE, check->npix);
if (check->type==CHECK_MINIBACKRMS)
memcpy(check->pix, field->sigma, check->npix*sizeof(float));
else
memcpy(check->pix, field->back, check->npix*sizeof(float));
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
if (bswapflag)
swapbytes(check->pix, sizeof(float), (int)check->npix);
QFWRITE(check->pix,check->npix*sizeof(float),check->file,
check->filename);
/*---- Put the buffer back to its original state */
if (bswapflag)
swapbytes(check->pix, sizeof(float), (int)check->npix);
save_head(cat, cat->tab);
write_body(cat->tab, check->pix, check->npix);
free(check->pix);
QCALLOC(buf, char, FBSIZE);
padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE))% FBSIZE;
if (padsize)
QFWRITE (buf, padsize, check->file, check->filename);
free(buf);
break;
case CHECK_MAPSOM:
ival = -32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
check->width = field->width;
check->height = field->height;
tab->bitpix = -32;
tab->bitsgn = 0;
tab->naxisn[0] = check->width = field->width;
tab->naxisn[1] = check->height = field->height;
check->npix = field->npix;
QMALLOC(ptrf, PIXTYPE, check->npix);
check->pix = (void *)ptrf;
for (i=check->npix; i--;)
*(ptrf++) = -10.0;
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
save_head(cat, cat->tab);
break;
case CHECK_OTHER:
ival = -32;
fitswrite(check->fitshead, "BITPIX ", &ival, H_INT, T_LONG);
fitswrite(check->fitshead, "NAXIS1 ", &check->width, H_INT, T_LONG);
fitswrite(check->fitshead, "NAXIS2 ", &check->height, H_INT, T_LONG);
tab->bitpix = -32;
tab->bitsgn = 0;
tab->naxisn[0] = check->width;
tab->naxisn[1] = check->height;
check->npix = check->width*check->height;
QMALLOC(ptrf, PIXTYPE, check->npix);
check->pix = (void *)ptrf;
for (i=check->npix; i--;)
*(ptrf++) = 0.0;
QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
free(check->fitshead);
QCALLOC(check->pix, PIXTYPE, check->npix);
save_head(cat, cat->tab);
break;
default:
......@@ -413,12 +392,7 @@ void writecheck(checkstruct *check, PIXTYPE *data, int w)
data = check->line;
}
if (bswapflag)
swapbytes(data, sizeof(PIXTYPE), w);
QFWRITE(data, w*sizeof(PIXTYPE), check->file, check->filename);
if (bswapflag)
/*-- Put the buffer back to its original state */
swapbytes(data, sizeof(PIXTYPE), w);
write_body(check->cat->tab, data, w);
return;
}
......@@ -430,10 +404,10 @@ Finish current check-image.
*/
void reendcheck(picstruct *field, checkstruct *check)
{
catstruct *cat;
char *buf;
size_t padsize;
padsize = 0; /* To avoid gcc -Wall warnings */
cat = check->cat;
switch(check->type)
{
case CHECK_MINIBACKGROUND:
......@@ -448,7 +422,7 @@ void reendcheck(picstruct *field, checkstruct *check)
free(check->pix);
free(check->line);
check->line = NULL;
padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
pad_tab(cat, check->npix*sizeof(PIXTYPE));
break;
case CHECK_OBJECTS:
......@@ -468,21 +442,15 @@ void reendcheck(picstruct *field, checkstruct *check)
case CHECK_PATTERNS:
case CHECK_MAPSOM:
case CHECK_OTHER:
if (bswapflag)
swapbytes(check->pix, sizeof(PIXTYPE), (int)check->npix);
QFWRITE(check->pix,check->npix*sizeof(PIXTYPE),
check->file,check->filename);
write_body(cat->tab, check->pix, check->npix);
free(check->pix);
padsize = (FBSIZE-((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
pad_tab(cat, check->npix*sizeof(PIXTYPE));
break;
case CHECK_SEGMENTATION:
if (bswapflag)
swapbytes(check->pix, sizeof(ULONG), (int)check->npix);
QFWRITE(check->pix,check->npix*sizeof(ULONG),
check->file,check->filename);
write_ibody(cat->tab, check->pix, check->npix);
free(check->pix);
padsize = (FBSIZE -((check->npix*sizeof(ULONG))%FBSIZE)) % FBSIZE;
pad_tab(cat, check->npix*sizeof(FLAGTYPE));
break;
case CHECK_SUBOBJECTS:
......@@ -494,7 +462,7 @@ void reendcheck(picstruct *field, checkstruct *check)
free(check->pix);
free(check->line);
check->line = NULL;
padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
pad_tab(cat, check->npix*sizeof(PIXTYPE));
break;
}
......@@ -502,11 +470,6 @@ void reendcheck(picstruct *field, checkstruct *check)
error(EXIT_FAILURE, "*Internal Error* in ", "endcheck()!");
}
QCALLOC(buf, char, FBSIZE);
if (padsize)
QFWRITE (buf, padsize, check->file, check->filename);
free(buf);
return;
}
......@@ -516,8 +479,7 @@ close check-image.
*/
void endcheck(checkstruct *check)
{
fclose(check->file);
free_cat(&check->cat,1);
free(check);
return;
......
......@@ -22,18 +22,20 @@
* You should have received a copy of the GNU General Public License
* along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 11/10/2010
* Last modified: 02/12/2010
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#ifndef _FITSCAT_H_
#include "fits/fitscat.h"
#endif
/*--------------------------------- structures ------------------------------*/
/* Check-image parameters */
typedef struct structcheck
{
char filename[MAXCHAR]; /* ptr to check-image filename */
FILE *file; /* ptr to check-image file structure */
char *fitshead; /* ptr to check-image FITS header */
int fitsheadsize; /* size of check-image FITS header */
int next; /* Number of extensions */
catstruct *cat; /* FITS file structure */
void *pix; /* ptr to check-image pixmap */
int width, height, depth; /* size of check-image */
size_t npix; /* number of pixels in check-image */
......
......@@ -729,7 +729,7 @@ void write_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
{
unsigned char *bufdata = (unsigned char *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (unsigned char)((*(ptr++)-bz)/bs+0.49999);;
*(bufdata++) = (unsigned char)((*(ptr++)-bz)/bs+0.49999);
}
break;
......@@ -830,6 +830,158 @@ void write_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
}
/******* write_ibody ***********************************************************
PROTO write_ibody(tabstruct *tab, FLAGTYPE *ptr, long size)
PURPOSE Write integer values to a FITS body.
INPUT A pointer to the tab structure,
a pointer to the array in memory,
the number of elements to be written.
OUTPUT -.
NOTES .
AUTHOR E. Bertin (IAP)
VERSION 02/12/2010
***/
void write_ibody(tabstruct *tab, FLAGTYPE *ptr, size_t size)
{
static FLAGTYPE bufdata0[DATA_BUFSIZE/sizeof(FLAGTYPE)];
catstruct *cat;
char *cbufdata0;
size_t i, bowl, spoonful;
double bs,bz;
bs = tab->bscale;
bz = tab->bzero;
cat = tab->cat;
if (!cat)
error(EXIT_FAILURE, "*Internal Error*: no parent cat structure for table ",
tab->extname);
cbufdata0 = (char *)bufdata0; /* A trick to remove gcc aliasing warnings */
switch(tab->compress_type)
{
/*-- Uncompressed image */
case COMPRESS_NONE:
bowl = DATA_BUFSIZE/tab->bytepix;
spoonful = size<bowl?size:bowl;
for(; size>0; size -= spoonful)
{
if (spoonful>size)
spoonful = size;
switch(tab->bitpix)
{
case BP_BYTE:
if (tab->bitsgn)
{
char *bufdata = (char *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (char)*(ptr++);
}
else
{
unsigned char *bufdata = (unsigned char *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (unsigned char)*(ptr++);
}
break;
case BP_SHORT:
if (tab->bitsgn)
{
short *bufdata = (short *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (short)*(ptr++);
}
else
{
unsigned short *bufdata = (unsigned short *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (unsigned short)*(ptr++);
}
if (bswapflag)
swapbytes(cbufdata0, 2, spoonful);
break;
case BP_LONG:
if (tab->bitsgn)
{
int *bufdata = (int *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (int)*(ptr++);
}
else
{
unsigned int *bufdata = (unsigned int *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (unsigned int)*(ptr++);
}
if (bswapflag)
swapbytes(cbufdata0, 4, spoonful);
break;
#ifdef HAVE_LONG_LONG_INT
case BP_LONGLONG:
if (tab->bitsgn)
{
LONGLONG *bufdata = (LONGLONG *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (LONGLONG)*(ptr++);
}
else
{
ULONGLONG *bufdata = (ULONGLONG *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (ULONGLONG)*(ptr++);
}
if (bswapflag)
swapbytes(cbufdata0, 8, spoonful);
break;
#endif
case BP_FLOAT:
{
float *bufdata = (float *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = (float)((double)*(ptr++)-bz)/bs;
if (bswapflag)
swapbytes(cbufdata0, 4, spoonful);
}
break;
case BP_DOUBLE:
{
double *bufdata = (double *)cbufdata0;
for (i=spoonful; i--;)
*(bufdata++) = ((double)*(ptr++)-bz)/bs;
if (bswapflag)
swapbytes(cbufdata0, 8, spoonful);
}
break;
default:
error(EXIT_FAILURE,"*FATAL ERROR*: unknown BITPIX type in ",
"read_body()");
break;
}
QFWRITE(cbufdata0, spoonful*tab->bytepix, cat->file, cat->filename);
}
break;
/*-- Compressed image */
case COMPRESS_BASEBYTE:
break;
case COMPRESS_PREVPIX:
break;
default:
error(EXIT_FAILURE,"*Internal Error*: unknown compression mode in ",
"read_body()");
}
return;
}
/******* set_maxram ***********************************************************
PROTO int set_maxram(size_t maxram)
PURPOSE Set the maximum amount of silicon memory that can be allocated for
......
......@@ -23,7 +23,7 @@
* along with AstrOmatic software.
* If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 09/10/2010
* Last modified: 02/12/2010
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
......@@ -247,6 +247,7 @@ extern void add_cleanupfilename(char *filename),
voprint_obj(FILE *stream, tabstruct *tab),
warning(char *, char *),
write_body(tabstruct *tab, PIXTYPE *ptr, size_t size),
write_ibody(tabstruct *tab, FLAGTYPE *ptr, size_t size),
write_checksum(tabstruct *tab);
extern char *tdisptoprintf(char *tdisp, char *str),
......
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