Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/back.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*
* This file part of: SExtractor
*
* Copyright: (C) 1993-2020 IAP/CNRS/SorbonneU
* Copyright: (C) 1994,1997 ESO
* (C) 1995,1996 Leiden Observatory
* (C) 1998-2021 IAP/CNRS/SorbonneU
* (C) 2021-2023 CFHT/CNRS
* (C) 2023-2025 CEA/AIM/UParisSaclay
*
* License: GNU General Public License
*
Expand All @@ -22,7 +26,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: 23/09/2020
* Last modified: 25/03/2025
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

Expand Down Expand Up @@ -92,13 +96,13 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
wfcurpos = wfcurpos2 = 0; /* to avoid gcc -Wall warnings */
QFTELL(field->file, fcurpos, field->filename);
#ifdef HAVE_CFITSIO
currentElement = (tab->currentElement == 0) ? 1 : tab->currentElement;
currentElement = (tab->cfitsio_currentElement == 0) ? 1 : tab->cfitsio_currentElement;
#endif

if (wfield) {
QFTELL(wfield->file, wfcurpos, wfield->filename);
#ifdef HAVE_CFITSIO
wcurrentElement = (wtab->currentElement == 0) ? 1 : wtab->currentElement;
wcurrentElement = (wtab->cfitsio_currentElement == 0) ? 1 : wtab->cfitsio_currentElement;
#endif
}

Expand Down Expand Up @@ -189,12 +193,12 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
/*---- Image size too big, we have to skip a few data !*/
QFTELL(field->file, fcurpos2, field->filename);
#ifdef HAVE_CFITSIO
currentElement2 = (tab->currentElement == 0) ? 1 : tab->currentElement;
currentElement2 = (tab->cfitsio_currentElement == 0) ? 1 : tab->cfitsio_currentElement;
#endif
if (wfield){
QFTELL(wfield->file, wfcurpos2, wfield->filename);
#ifdef HAVE_CFITSIO
wcurrentElement2 = (wtab->currentElement == 0) ? 1 : wtab->currentElement;
wcurrentElement2 = (wtab->cfitsio_currentElement == 0) ? 1 : wtab->cfitsio_currentElement;
#endif
}
if (j == ny-1 && (n=field->height%field->backh))
Expand All @@ -218,7 +222,7 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
QFSEEK(field->file, bufshift*(OFF_T2)field->bytepix, SEEK_CUR,
field->filename);
#ifdef HAVE_CFITSIO
tab->currentElement += bufshift;
tab->cfitsio_currentElement += bufshift;
#endif
buft = buf;
for (i=nlines; i--; buft += w)
Expand All @@ -228,7 +232,7 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
QFSEEK(field->file, jumpsize*(OFF_T2)field->bytepix, SEEK_CUR,
field->filename);
#ifdef HAVE_CFITSIO
tab->currentElement += jumpsize;
tab->cfitsio_currentElement += jumpsize;
#endif
}
}
Expand All @@ -239,7 +243,7 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
QFSEEK(wfield->file, bufshift*(OFF_T2)wfield->bytepix, SEEK_CUR,
wfield->filename);
#ifdef HAVE_CFITSIO
wtab->currentElement += bufshift;
wtab->cfitsio_currentElement += bufshift;
#endif
wbuft = wbuf;
for (i=nlines; i--; wbuft += w)
Expand All @@ -250,7 +254,7 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
QFSEEK(wfield->file, jumpsize*(OFF_T2)wfield->bytepix, SEEK_CUR,
wfield->filename);
#ifdef HAVE_CFITSIO
wtab->currentElement += jumpsize;
wtab->cfitsio_currentElement += jumpsize;
#endif
}
}
Expand All @@ -259,7 +263,7 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
wfield?wfield->weight_thresh:0.0);
QFSEEK(field->file, fcurpos2, SEEK_SET, field->filename);
#ifdef HAVE_CFITSIO
tab->currentElement = currentElement2;
tab->cfitsio_currentElement = currentElement2;
#endif
bm = backmesh;
for (m=nx; m--; bm++)
Expand All @@ -271,7 +275,7 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
{
QFSEEK(wfield->file, wfcurpos2, SEEK_SET, wfield->filename);
#ifdef HAVE_CFITSIO
wtab->currentElement = wcurrentElement2;
wtab->cfitsio_currentElement = wcurrentElement2;
#endif
wbm = wbackmesh;
for (m=nx; m--; wbm++)
Expand Down Expand Up @@ -328,12 +332,12 @@ void makeback(picstruct *field, picstruct *wfield, int wscale_flag)
/* Go back to the original position */
QFSEEK(field->file, fcurpos, SEEK_SET, field->filename);
#ifdef HAVE_CFITSIO
tab->currentElement = currentElement;
tab->cfitsio_currentElement = currentElement;
#endif
if (wfield) {
QFSEEK(wfield->file, wfcurpos, SEEK_SET, wfield->filename);
#ifdef HAVE_CFITSIO
wfield->tab->currentElement = wcurrentElement;
wfield->tab->cfitsio_currentElement = wcurrentElement;
#endif
}

Expand Down
90 changes: 52 additions & 38 deletions src/fits/fitsbody.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*
* This file part of: AstrOmatic FITS/LDAC library
*
* Copyright: (C) 1995-2023 CFHT/IAP/CNRS/SorbonneU
* Copyright: (C) 1994,1997 ESO
* (C) 1995,1996 Leiden Observatory
* (C) 1998-2021 IAP/CNRS/SorbonneU
* (C) 2021-2023 CFHT/CNRS
* (C) 2023-2025 CEA/AIM/UParisSaclay
*
* License: GNU General Public License
*
Expand All @@ -23,7 +27,7 @@
* along with AstrOmatic software.
* If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 25/02/2023
* Last modified: 25/03/2025
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

Expand Down Expand Up @@ -63,13 +67,14 @@ PURPOSE Allocate memory for and read a FITS data body (read-only). If not
INPUT Table (tab) structure.
OUTPUT Pointer to the mapped data if OK, or NULL otherwise.
NOTES The file pointer must be positioned at the beginning of the data.
AUTHOR E. Bertin (IAP)
VERSION 02/10/2017
AUTHOR E. Bertin (CEA/AIM/UParisSaclay)
VERSION 25/03/2025
***/
PIXTYPE *alloc_body(tabstruct *tab, void (*func)(PIXTYPE *ptr, int npix))
{
FILE *file;
PIXTYPE *buffer;
int n;
size_t npix, size, sizeleft, spoonful;

if (!body_ramflag)
Expand All @@ -90,7 +95,12 @@ PIXTYPE *alloc_body(tabstruct *tab, void (*func)(PIXTYPE *ptr, int npix))

/* Decide if the data will go in physical memory or on swap-space */
#ifdef HAVE_CFITSIO
npix = tab->naxisn[0] * tab->naxisn[1];
if (tab->isTileCompressed) {
npix = (size_t)tab->naxisn[0];
for (n=1; n<tab->naxis; n++)
npix *= (size_t)tab->naxisn[n];
} else
npix = tab->tabsize/tab->bytepix;
#else
npix = tab->tabsize/tab->bytepix;
#endif
Expand All @@ -102,7 +112,7 @@ PIXTYPE *alloc_body(tabstruct *tab, void (*func)(PIXTYPE *ptr, int npix))
{
QFSEEK(tab->cat->file, tab->bodypos, SEEK_SET, tab->cat->filename);
#ifdef HAVE_CFITSIO
tab->currentElement = 1;
tab->cfitsio_currentElement = 1;
#endif
read_body(tab, (PIXTYPE *)tab->bodybuf, npix);
/*---- Apply pixel processing */
Expand Down Expand Up @@ -132,7 +142,7 @@ PIXTYPE *alloc_body(tabstruct *tab, void (*func)(PIXTYPE *ptr, int npix))
spoonful = DATA_BUFSIZE;
QFSEEK(tab->cat->file, tab->bodypos, SEEK_SET, tab->cat->filename);
#ifdef HAVE_CFITSIO
tab->currentElement = 1;
tab->cfitsio_currentElement = 1;
#endif
read_body(tab, buffer, spoonful/sizeof(PIXTYPE));
/*-- Apply pixel processing */
Expand Down Expand Up @@ -172,8 +182,8 @@ PURPOSE Allocate memory for and read a FITS integer data body (read-only).
INPUT Table (tab) structure.
OUTPUT Pointer to the mapped data if OK, or NULL otherwise.
NOTES The file pointer must be positioned at the beginning of the data.
AUTHOR E. Bertin (IAP)
VERSION 02/10/2017
AUTHOR E. Bertin (CEA/AIM/UParisSaclay)
VERSION 21/03/2025
***/
FLAGTYPE *alloc_ibody(tabstruct *tab,
void (*func)(FLAGTYPE *ptr, int npix))
Expand Down Expand Up @@ -208,7 +218,7 @@ FLAGTYPE *alloc_ibody(tabstruct *tab,
{
QFSEEK(tab->cat->file, tab->bodypos, SEEK_SET, tab->cat->filename);
#ifdef HAVE_CFITSIO
tab->currentElement = 1;
tab->cfitsio_currentElement = 1;
#endif
read_ibody(tab, (FLAGTYPE *)tab->bodybuf, npix);
/*---- Apply pixel processing */
Expand Down Expand Up @@ -238,7 +248,7 @@ FLAGTYPE *alloc_ibody(tabstruct *tab,
spoonful = DATA_BUFSIZE;
QFSEEK(tab->cat->file, tab->bodypos, SEEK_SET, tab->cat->filename);
#ifdef HAVE_CFITSIO
tab->currentElement = 1;
tab->cfitsio_currentElement = 1;
#endif
read_ibody(tab, buffer, spoonful/sizeof(FLAGTYPE));
/*-- Apply pixel processing */
Expand Down Expand Up @@ -322,19 +332,24 @@ void free_body(tabstruct *tab)
***/
void readTileCompressed(tabstruct *tab, size_t spoonful, void *bufdata0) {

int status, hdutype;
catstruct *cat;
int status, hdutype;

// first of all, move to correct HDU
status = 0;
fits_movabs_hdu(tab->infptr, tab->hdunum, &hdutype, &status);
// Exit if no parent catalog or CFITSIO information
if (!(cat = tab->cat) || !cat->cfitsio_infptr)
return;

// Move to correct HDU
fits_movabs_hdu(cat->cfitsio_infptr, tab->cfitsio_hdunum, &hdutype, &status);
if (status != 0) {
printf("Error moving to HDU %d\n", tab->hdunum);
fprintf(stderr, "Error moving to HDU %d\n", tab->cfitsio_hdunum);
fits_report_error(stderr, status);
}

// pixels count from 1
if (!tab->currentElement)
tab->currentElement = 1;
if (!tab->cfitsio_currentElement)
tab->cfitsio_currentElement = 1;

// now read section of image
int datatype;
Expand Down Expand Up @@ -364,24 +379,24 @@ void readTileCompressed(tabstruct *tab, size_t spoonful, void *bufdata0) {

// turn off any scaling so that we copy raw pixel values
status = 0;
fits_set_bscale(tab->infptr, bscale, bzero, &status);
fits_set_bscale(cat->cfitsio_infptr, bscale, bzero, &status);

// now read the image
status = 0;
fits_read_img(tab->infptr, datatype, tab->currentElement, spoonful, &nulval,
bufdata0, &anynul, &status);
fits_read_img(cat->cfitsio_infptr, datatype, tab->cfitsio_currentElement,
spoonful, &nulval, bufdata0, &anynul, &status);

// report reading error
if (status) {
printf("CFITSIO ERROR reading start=%d end=%d absolute end=%d\n",
tab->currentElement,
(tab->currentElement + spoonful),
fprintf(stderr, "CFITSIO ERROR reading start=%d end=%d absolute end=%d\n",
tab->cfitsio_currentElement,
(tab->cfitsio_currentElement + spoonful),
(tab->naxisn[0]*tab->naxisn[1]));
fits_report_error(stderr, status);
}

// update file 'pointer'
tab->currentElement += spoonful;
tab->cfitsio_currentElement += spoonful;
}

#endif // HAVE_CFITSIO
Expand All @@ -394,8 +409,8 @@ INPUT A pointer to the tab structure,
the number of elements to be read.
OUTPUT -.
NOTES .
AUTHOR E. Bertin (CFHT/IAP/CNRS/SorbonneU)
VERSION 25/02/2023
AUTHOR E. Bertin (CEA/AIM/UParisSaclay)
VERSION 21/03/2025
***/
void read_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
{
Expand Down Expand Up @@ -439,7 +454,7 @@ void read_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
bufdata = (char *)bufdata0;

#ifdef HAVE_CFITSIO
if (tab->isTileCompressed && tab->infptr)
if (tab->isTileCompressed && cat->cfitsio_infptr)
readTileCompressed(tab, spoonful, (void *)bufdata0);
else
QFREAD(bufdata, spoonful*tab->bytepix, cat->file, cat->filename);
Expand Down Expand Up @@ -954,8 +969,8 @@ INPUT A pointer to the tab structure,
the number of elements to be written.
OUTPUT -.
NOTES .
AUTHOR E. Bertin (IAP)
VERSION 26/08/2020
AUTHOR E. Bertin (CEA/AIM/UParisSaclay)
VERSION 21/03/2025
***/
void write_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
{
Expand Down Expand Up @@ -1073,7 +1088,7 @@ void write_body(tabstruct *tab, PIXTYPE *ptr, size_t size)
*(bufdata++) = (*(ptr++)-bz)/bs;

#ifdef HAVE_CFITSIO
if (!tab->infptr && bswapflag)
if (!cat->cfitsio_infptr && bswapflag)
#else
if (bswapflag)
#endif
Expand All @@ -1100,7 +1115,7 @@ void write_body(tabstruct *tab, PIXTYPE *ptr, size_t size)

#ifdef HAVE_CFITSIO
// if cfitsio output file has been set up, then proceed to write
if (tab->infptr) {
if (cat->cfitsio_infptr) {
// now read section of image
int datatype;
switch(tab->bitpix) {
Expand Down Expand Up @@ -1132,20 +1147,19 @@ void write_body(tabstruct *tab, PIXTYPE *ptr, size_t size)

int status = 0;

fits_set_bscale(tab->infptr, bscale, bzero, &status);
fits_set_bscale(cat->cfitsio_infptr, bscale, bzero, &status);
status = 0;
fits_write_img(tab->infptr, datatype, tab->currentElement, spoonful,
cbufdata0, &status);
fits_write_img(cat->cfitsio_infptr, datatype,
tab->cfitsio_currentElement, spoonful, cbufdata0, &status);

if (status) {

printf("CFITSIO ERROR writing start=%d end=%d absolute end=%d\n",
tab->currentElement, (tab->currentElement + spoonful),
fprintf(stderr, "CFITSIO ERROR writing start=%d end=%d absolute end=%d\n",
tab->cfitsio_currentElement, (tab->cfitsio_currentElement + spoonful),
(tab->naxisn[0]*tab->naxisn[1]));
fits_report_error(stderr, status);
}

tab->currentElement = tab->currentElement + spoonful;
tab->cfitsio_currentElement = tab->cfitsio_currentElement + spoonful;
} else
// otherwise, continue with usual AstrOmatic fits writing routine
QFWRITE(cbufdata0, spoonful*tab->bytepix, cat->file, cat->filename);
Expand Down
Loading