| 1 |
2 |
bertin |
/*
|
| 2 |
233 |
bertin |
* readimage.c.
|
| 3 |
2 |
bertin |
*
|
| 4 |
233 |
bertin |
* Read image data.
|
| 5 |
2 |
bertin |
*
|
| 6 |
233 |
bertin |
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
| 7 |
2 |
bertin |
*
|
| 8 |
233 |
bertin |
* This file part of: SExtractor
|
| 9 |
2 |
bertin |
*
|
| 10 |
233 |
bertin |
* Copyright: (C) 1993,1998-2010 IAP/CNRS/UPMC
|
| 11 |
|
|
* (C) 1994,1997 ESO
|
| 12 |
|
|
* (C) 1995,1996 Sterrewacht Leiden
|
| 13 |
2 |
bertin |
*
|
| 14 |
233 |
bertin |
* Author: Emmanuel Bertin (IAP)
|
| 15 |
|
|
*
|
| 16 |
|
|
* License: GNU General Public License
|
| 17 |
|
|
*
|
| 18 |
|
|
* SExtractor is free software: you can redistribute it and/or modify
|
| 19 |
|
|
* it under the terms of the GNU General Public License as published by
|
| 20 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
| 21 |
|
|
* (at your option) any later version.
|
| 22 |
|
|
* SExtractor is distributed in the hope that it will be useful,
|
| 23 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 24 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 25 |
|
|
* GNU General Public License for more details.
|
| 26 |
|
|
* You should have received a copy of the GNU General Public License
|
| 27 |
|
|
* along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
|
| 28 |
|
|
*
|
| 29 |
|
|
* Last modified: 11/10/2010
|
| 30 |
|
|
*
|
| 31 |
|
|
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
|
| 32 |
2 |
bertin |
|
| 33 |
|
|
#ifdef HAVE_CONFIG_H
|
| 34 |
|
|
#include "config.h"
|
| 35 |
|
|
#endif
|
| 36 |
|
|
|
| 37 |
|
|
#include <math.h>
|
| 38 |
|
|
#include <stdio.h>
|
| 39 |
|
|
#include <stdlib.h>
|
| 40 |
|
|
#include <string.h>
|
| 41 |
|
|
|
| 42 |
|
|
#include "wcs/wcs.h"
|
| 43 |
|
|
#include "define.h"
|
| 44 |
|
|
#include "globals.h"
|
| 45 |
|
|
#include "prefs.h"
|
| 46 |
|
|
#include "check.h"
|
| 47 |
|
|
#include "field.h"
|
| 48 |
|
|
#include "fits/fitscat.h"
|
| 49 |
173 |
bertin |
#include "fitswcs.h"
|
| 50 |
2 |
bertin |
#include "interpolate.h"
|
| 51 |
|
|
#include "back.h"
|
| 52 |
|
|
#include "astrom.h"
|
| 53 |
|
|
#include "weight.h"
|
| 54 |
7 |
bertin |
#include "wcs/tnx.h"
|
| 55 |
2 |
bertin |
|
| 56 |
|
|
/******************************* loadstrip ***********************************/
|
| 57 |
|
|
/*
|
| 58 |
|
|
Load a new strip of pixel data into the buffer.
|
| 59 |
|
|
*/
|
| 60 |
|
|
void *loadstrip(picstruct *field, picstruct *wfield)
|
| 61 |
|
|
|
| 62 |
|
|
{
|
| 63 |
173 |
bertin |
tabstruct *tab;
|
| 64 |
2 |
bertin |
checkstruct *check;
|
| 65 |
|
|
int y, w, flags, interpflag;
|
| 66 |
|
|
PIXTYPE *data, *wdata, *rmsdata;
|
| 67 |
|
|
|
| 68 |
173 |
bertin |
tab = field->tab;
|
| 69 |
2 |
bertin |
w = field->width;
|
| 70 |
|
|
flags = field->flags;
|
| 71 |
|
|
interpflag = (wfield && wfield->interp_flag);
|
| 72 |
|
|
wdata = NULL; /* To avoid gcc -Wall warnings */
|
| 73 |
|
|
|
| 74 |
|
|
if (!field->y)
|
| 75 |
|
|
{
|
| 76 |
|
|
/*- First strip */
|
| 77 |
|
|
int nbpix;
|
| 78 |
|
|
|
| 79 |
|
|
nbpix = w*field->stripheight;
|
| 80 |
|
|
|
| 81 |
|
|
if (flags ^ FLAG_FIELD)
|
| 82 |
|
|
{
|
| 83 |
|
|
/*---- Allocate space for the frame-buffer */
|
| 84 |
|
|
if (!(field->strip=(PIXTYPE *)malloc(field->stripheight*field->width
|
| 85 |
|
|
*sizeof(PIXTYPE))))
|
| 86 |
|
|
error(EXIT_FAILURE,"Not enough memory for the image buffer of ",
|
| 87 |
|
|
field->rfilename);
|
| 88 |
|
|
|
| 89 |
|
|
data = field->strip;
|
| 90 |
|
|
/*---- We assume weight data have been read just before */
|
| 91 |
|
|
if (interpflag)
|
| 92 |
|
|
wdata = wfield->strip;
|
| 93 |
|
|
if (flags & BACKRMS_FIELD)
|
| 94 |
|
|
for (y=0, rmsdata=data; y<field->stripheight; y++, rmsdata += w)
|
| 95 |
|
|
backrmsline(field, y, rmsdata);
|
| 96 |
|
|
else if (flags & INTERP_FIELD)
|
| 97 |
|
|
copydata(field, 0, nbpix);
|
| 98 |
|
|
else
|
| 99 |
173 |
bertin |
read_body(tab, data, nbpix);
|
| 100 |
2 |
bertin |
if (flags & (WEIGHT_FIELD|RMS_FIELD|BACKRMS_FIELD|VAR_FIELD))
|
| 101 |
|
|
weight_to_var(field, data, nbpix);
|
| 102 |
|
|
if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_IDENTICAL]))
|
| 103 |
|
|
writecheck(check, data, nbpix);
|
| 104 |
|
|
for (y=0; y<field->stripheight; y++, data += w)
|
| 105 |
|
|
{
|
| 106 |
|
|
/*------ This is the only place where one can pick-up safely the current bkg */
|
| 107 |
|
|
if (flags & (MEASURE_FIELD|DETECT_FIELD))
|
| 108 |
|
|
subbackline(field, y, data);
|
| 109 |
|
|
/*------ Go to interpolation process */
|
| 110 |
|
|
if (interpflag)
|
| 111 |
|
|
{
|
| 112 |
|
|
interpolate(field,wfield, data, wdata);
|
| 113 |
|
|
wdata += w;
|
| 114 |
|
|
}
|
| 115 |
|
|
/*------ Check-image stuff */
|
| 116 |
|
|
if (prefs.check_flag)
|
| 117 |
|
|
{
|
| 118 |
|
|
if (flags & MEASURE_FIELD)
|
| 119 |
|
|
{
|
| 120 |
|
|
if ((check = prefs.check[CHECK_BACKGROUND]))
|
| 121 |
|
|
writecheck(check, field->backline, w);
|
| 122 |
|
|
if ((check = prefs.check[CHECK_SUBTRACTED]))
|
| 123 |
|
|
writecheck(check, data, w);
|
| 124 |
|
|
if ((check = prefs.check[CHECK_APERTURES]))
|
| 125 |
|
|
writecheck(check, data, w);
|
| 126 |
|
|
if ((check = prefs.check[CHECK_SUBPSFPROTOS]))
|
| 127 |
|
|
writecheck(check, data, w);
|
| 128 |
|
|
if ((check = prefs.check[CHECK_SUBPCPROTOS]))
|
| 129 |
|
|
writecheck(check, data, w);
|
| 130 |
173 |
bertin |
if ((check = prefs.check[CHECK_SUBPROFILES]))
|
| 131 |
|
|
writecheck(check, data, w);
|
| 132 |
221 |
bertin |
if ((check = prefs.check[CHECK_SUBSPHEROIDS]))
|
| 133 |
|
|
writecheck(check, data, w);
|
| 134 |
|
|
if ((check = prefs.check[CHECK_SUBDISKS]))
|
| 135 |
|
|
writecheck(check, data, w);
|
| 136 |
2 |
bertin |
}
|
| 137 |
|
|
if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
|
| 138 |
|
|
{
|
| 139 |
|
|
backrmsline(field, y, (PIXTYPE *)check->pix);
|
| 140 |
|
|
writecheck(check, check->pix, w);
|
| 141 |
|
|
}
|
| 142 |
|
|
}
|
| 143 |
|
|
}
|
| 144 |
|
|
}
|
| 145 |
|
|
else
|
| 146 |
|
|
{
|
| 147 |
|
|
if (!(field->fstrip=(FLAGTYPE *)malloc(field->stripheight*field->width
|
| 148 |
|
|
*sizeof(FLAGTYPE))))
|
| 149 |
|
|
error(EXIT_FAILURE,"Not enough memory for the flag buffer of ",
|
| 150 |
|
|
field->rfilename);
|
| 151 |
173 |
bertin |
read_ibody(field->tab, field->fstrip, nbpix);
|
| 152 |
2 |
bertin |
}
|
| 153 |
|
|
|
| 154 |
|
|
field->ymax = field->stripheight;
|
| 155 |
|
|
if (field->ymax < field->height)
|
| 156 |
|
|
field->stripysclim = field->stripheight - field->stripmargin;
|
| 157 |
|
|
}
|
| 158 |
|
|
else
|
| 159 |
|
|
{
|
| 160 |
|
|
/*- other strips */
|
| 161 |
|
|
if (flags ^ FLAG_FIELD)
|
| 162 |
|
|
{
|
| 163 |
|
|
data = field->strip + field->stripylim*w;
|
| 164 |
|
|
/*---- We assume weight data have been read just before */
|
| 165 |
|
|
if (interpflag)
|
| 166 |
|
|
wdata = wfield->strip + field->stripylim*w;
|
| 167 |
|
|
|
| 168 |
|
|
/*---- copy to Check-image the "oldest" line before it is replaced */
|
| 169 |
|
|
if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_SUBOBJECTS]))
|
| 170 |
|
|
writecheck(check, data, w);
|
| 171 |
|
|
|
| 172 |
|
|
if (flags & BACKRMS_FIELD)
|
| 173 |
|
|
backrmsline(field, field->ymax, data);
|
| 174 |
|
|
else if (flags & INTERP_FIELD)
|
| 175 |
|
|
copydata(field, field->stripylim*w, w);
|
| 176 |
|
|
else
|
| 177 |
173 |
bertin |
read_body(tab, data, w);
|
| 178 |
2 |
bertin |
if (flags & (WEIGHT_FIELD|RMS_FIELD|BACKRMS_FIELD|VAR_FIELD))
|
| 179 |
|
|
weight_to_var(field, data, w);
|
| 180 |
|
|
|
| 181 |
|
|
if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_IDENTICAL]))
|
| 182 |
|
|
writecheck(check, data, w);
|
| 183 |
|
|
/*---- Interpolate and subtract the background at current line */
|
| 184 |
|
|
if (flags & (MEASURE_FIELD|DETECT_FIELD))
|
| 185 |
|
|
subbackline(field, field->ymax, data);
|
| 186 |
|
|
if (interpflag)
|
| 187 |
|
|
interpolate(field,wfield, data, wdata);
|
| 188 |
|
|
/*---- Check-image stuff */
|
| 189 |
|
|
if (prefs.check_flag)
|
| 190 |
|
|
{
|
| 191 |
|
|
if (flags & MEASURE_FIELD)
|
| 192 |
|
|
{
|
| 193 |
|
|
if ((check = prefs.check[CHECK_BACKGROUND]))
|
| 194 |
|
|
writecheck(check, field->backline, w);
|
| 195 |
|
|
if ((check = prefs.check[CHECK_SUBTRACTED]))
|
| 196 |
|
|
writecheck(check, data, w);
|
| 197 |
|
|
if ((check = prefs.check[CHECK_APERTURES]))
|
| 198 |
|
|
writecheck(check, data, w);
|
| 199 |
|
|
if ((check = prefs.check[CHECK_SUBPSFPROTOS]))
|
| 200 |
|
|
writecheck(check, data, w);
|
| 201 |
|
|
if ((check = prefs.check[CHECK_SUBPCPROTOS]))
|
| 202 |
|
|
writecheck(check, data, w);
|
| 203 |
173 |
bertin |
if ((check = prefs.check[CHECK_SUBPROFILES]))
|
| 204 |
|
|
writecheck(check, data, w);
|
| 205 |
221 |
bertin |
if ((check = prefs.check[CHECK_SUBSPHEROIDS]))
|
| 206 |
|
|
writecheck(check, data, w);
|
| 207 |
|
|
if ((check = prefs.check[CHECK_SUBDISKS]))
|
| 208 |
|
|
writecheck(check, data, w);
|
| 209 |
2 |
bertin |
}
|
| 210 |
|
|
if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
|
| 211 |
|
|
{
|
| 212 |
|
|
backrmsline(field, field->ymax, (PIXTYPE *)check->pix);
|
| 213 |
|
|
writecheck(check, check->pix, w);
|
| 214 |
|
|
}
|
| 215 |
|
|
}
|
| 216 |
|
|
}
|
| 217 |
|
|
else
|
| 218 |
173 |
bertin |
read_ibody(tab, field->fstrip + field->stripylim*w, w);
|
| 219 |
2 |
bertin |
|
| 220 |
|
|
field->stripylim = (++field->ymin)%field->stripheight;
|
| 221 |
|
|
if ((++field->ymax)<field->height)
|
| 222 |
|
|
field->stripysclim = (++field->stripysclim)%field->stripheight;
|
| 223 |
|
|
}
|
| 224 |
|
|
|
| 225 |
|
|
return (flags ^ FLAG_FIELD)?
|
| 226 |
|
|
(void *)(field->strip + field->stripy*w)
|
| 227 |
|
|
: (void *)(field->fstrip + field->stripy*w);
|
| 228 |
|
|
}
|
| 229 |
|
|
|
| 230 |
|
|
|
| 231 |
|
|
/******************************** copydata **********************************/
|
| 232 |
|
|
/*
|
| 233 |
|
|
Copy image data from one field to the other.
|
| 234 |
|
|
*/
|
| 235 |
|
|
void copydata(picstruct *field, int offset, int size)
|
| 236 |
|
|
{
|
| 237 |
|
|
memcpy(field->strip+offset, field->reffield->strip+offset,
|
| 238 |
|
|
size*sizeof(PIXTYPE));
|
| 239 |
|
|
return;
|
| 240 |
|
|
}
|
| 241 |
|
|
|
| 242 |
|
|
|
| 243 |
|
|
/******************************* readimagehead *******************************/
|
| 244 |
|
|
/*
|
| 245 |
|
|
extract some data from the FITS-file header
|
| 246 |
|
|
*/
|
| 247 |
|
|
void readimagehead(picstruct *field)
|
| 248 |
|
|
{
|
| 249 |
173 |
bertin |
#define FITSREADS(buf, k, str, def) \
|
| 250 |
|
|
{if (fitsread(buf,k,str, H_STRING,T_STRING) != RETURN_OK) \
|
| 251 |
|
|
strcpy(str, (def)); \
|
| 252 |
|
|
}
|
| 253 |
2 |
bertin |
|
| 254 |
173 |
bertin |
tabstruct *tab;
|
| 255 |
|
|
|
| 256 |
|
|
tab = field->tab;
|
| 257 |
|
|
|
| 258 |
|
|
if(tab->naxis < 2)
|
| 259 |
2 |
bertin |
error(EXIT_FAILURE, field->filename, " does NOT contain 2D-data!");
|
| 260 |
|
|
|
| 261 |
|
|
/*---------------------------- Basic keywords ------------------------------*/
|
| 262 |
173 |
bertin |
if (tab->bitpix != BP_BYTE
|
| 263 |
|
|
&& tab->bitpix != BP_SHORT
|
| 264 |
|
|
&& tab->bitpix != BP_LONG
|
| 265 |
|
|
&& tab->bitpix != BP_FLOAT
|
| 266 |
|
|
&& tab->bitpix != BP_DOUBLE)
|
| 267 |
2 |
bertin |
error(EXIT_FAILURE, "Sorry, I don't know that kind of data.", "");
|
| 268 |
|
|
|
| 269 |
173 |
bertin |
field->width = tab->naxisn[0];
|
| 270 |
|
|
field->height = tab->naxisn[1];
|
| 271 |
2 |
bertin |
field->npix = (KINGSIZE_T)field->width*field->height;
|
| 272 |
209 |
bertin |
field->bitpix = tab->bitpix;
|
| 273 |
|
|
field->bytepix = tab->bytepix;
|
| 274 |
173 |
bertin |
if (tab->bitsgn && prefs.fitsunsigned_flag)
|
| 275 |
|
|
tab->bitsgn = 0;
|
| 276 |
2 |
bertin |
|
| 277 |
173 |
bertin |
FITSREADS(tab->headbuf, "OBJECT ", field->ident, "Unnamed");
|
| 278 |
2 |
bertin |
|
| 279 |
|
|
/*----------------------------- Astrometry ---------------------------------*/
|
| 280 |
|
|
/* Presently, astrometry is done only on the measurement and detect images */
|
| 281 |
|
|
if (field->flags&(MEASURE_FIELD|DETECT_FIELD))
|
| 282 |
173 |
bertin |
field->wcs = read_wcs(tab);
|
| 283 |
2 |
bertin |
|
| 284 |
173 |
bertin |
QFSEEK(field->file, tab->bodypos, SEEK_SET, field->filename);
|
| 285 |
2 |
bertin |
|
| 286 |
|
|
return;
|
| 287 |
|
|
|
| 288 |
173 |
bertin |
#undef FITSREADS
|
| 289 |
2 |
bertin |
|
| 290 |
|
|
}
|
| 291 |
|
|
|
| 292 |
|
|
|