readimage.c 10.9 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1
/*
2
3
4
5
6
7
8
9
*				readimage.c.
*
* Read image data.
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	This file part of:	SExtractor
*
10
*	Copyright:		(C) 1993-2020 IAP/CNRS/SorbonneU
Emmanuel Bertin's avatar
Emmanuel Bertin committed
11
*
12
*	License:		GNU General Public License
Emmanuel Bertin's avatar
Emmanuel Bertin committed
13
*
14
15
16
17
18
19
20
21
22
23
*	SExtractor is free software: you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation, either version 3 of the License, or
*	(at your option) any later version.
*	SExtractor is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	You should have received a copy of the GNU General Public License
*	along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
24
*
25
*	Last modified:		23/09/2020
Emmanuel Bertin's avatar
Emmanuel Bertin committed
26
*
27
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#ifdef HAVE_CONFIG_H
#include        "config.h"
#endif

#include	<math.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

#include	"wcs/wcs.h"
#include	"define.h"
#include	"globals.h"
#include	"prefs.h"
#include	"check.h"
#include	"field.h"
#include	"fits/fitscat.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
45
#include	"fitswcs.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
46
47
48
49
50
51
52
53
54
55
56
57
58
#include	"interpolate.h"
#include	"back.h"
#include	"astrom.h"
#include	"weight.h"
#include        "wcs/tnx.h"

/******************************* loadstrip ***********************************/
/*
Load a new strip of pixel data into the buffer.
*/
void	*loadstrip(picstruct *field, picstruct *wfield)

  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
59
   tabstruct	*tab;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
60
   checkstruct	*check;
61
   int		y, w, flags, interpflag, npixtot;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
62
63
   PIXTYPE	*data, *wdata, *rmsdata;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
64
  tab = field->tab;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
65
  w = field->width;
66
  npixtot = field->width*field->height;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
67
68
69
70
71
72
73
74
75
76
77
  flags = field->flags;
  interpflag = (wfield && wfield->interp_flag);
  wdata = NULL;			/* To avoid gcc -Wall warnings */

  if (!field->y)
    {
/*- First strip */
     int	nbpix;

    nbpix = w*field->stripheight;

78
    if (!(flags & (FLAG_FIELD|DGEO_FIELD)))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      {
/*---- Allocate space for the frame-buffer */
      if (!(field->strip=(PIXTYPE *)malloc(field->stripheight*field->width
        *sizeof(PIXTYPE))))
        error(EXIT_FAILURE,"Not enough memory for the image buffer of ",
		field->rfilename);

      data = field->strip;
/*---- We assume weight data have been read just before */
      if (interpflag)
        wdata = wfield->strip;
      if (flags & BACKRMS_FIELD)
        for (y=0, rmsdata=data; y<field->stripheight; y++, rmsdata += w)
          backrmsline(field, y, rmsdata);
      else if (flags & INTERP_FIELD)
        copydata(field, 0, nbpix);
      else
96
      {
97
#ifdef HAVE_CFITSIO
98
        tab->currentElement = 1;
99
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
100
        read_body(tab, data, nbpix);
101
      } 
Emmanuel Bertin's avatar
Emmanuel Bertin committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
      if (flags & (WEIGHT_FIELD|RMS_FIELD|BACKRMS_FIELD|VAR_FIELD))
        weight_to_var(field, data, nbpix);
      if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_IDENTICAL]))
        writecheck(check, data, nbpix);
      for (y=0; y<field->stripheight; y++, data += w)
        {
/*------ This is the only place where one can pick-up safely the current bkg */
        if (flags & (MEASURE_FIELD|DETECT_FIELD))
          subbackline(field, y, data);
/*------ Go to interpolation process */
        if (interpflag)
          {
          interpolate(field,wfield, data, wdata);
          wdata += w;
          }
/*------ Check-image stuff */
        if (prefs.check_flag)
          {
          if (flags & MEASURE_FIELD)
            {
            if ((check = prefs.check[CHECK_BACKGROUND]))
              writecheck(check, field->backline, w);
            if ((check = prefs.check[CHECK_SUBTRACTED]))
              writecheck(check, data, w);
            if ((check = prefs.check[CHECK_APERTURES]))
              writecheck(check, data, w);
            if ((check = prefs.check[CHECK_SUBPSFPROTOS]))
              writecheck(check, data, w);
            if ((check = prefs.check[CHECK_SUBPCPROTOS]))
              writecheck(check, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
132
133
            if ((check = prefs.check[CHECK_SUBPROFILES]))
              writecheck(check, data, w);
134
135
136
137
            if ((check = prefs.check[CHECK_SUBSPHEROIDS]))
              writecheck(check, data, w);
            if ((check = prefs.check[CHECK_SUBDISKS]))
              writecheck(check, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
138
139
140
141
142
143
144
145
146
            }
          if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
            {
            backrmsline(field, y, (PIXTYPE *)check->pix);
            writecheck(check, check->pix, w);
            }
          }
        }
      }
147
    else if (flags & FLAG_FIELD)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
148
      {
149
/*---- flag map */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
150
151
152
153
      if (!(field->fstrip=(FLAGTYPE *)malloc(field->stripheight*field->width
		*sizeof(FLAGTYPE))))
      error(EXIT_FAILURE,"Not enough memory for the flag buffer of ",
	field->rfilename);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
154
      read_ibody(field->tab, field->fstrip, nbpix);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
155
      }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    else
      {
/*---- differential geometry map */
      if (!(field->dgeostrip[0]=(PIXTYPE *)malloc(field->stripheight
		* field->width * sizeof(PIXTYPE)))
	|| !(field->dgeostrip[1]=(PIXTYPE *)malloc(field->stripheight
		* field->width * sizeof(PIXTYPE))))
        error(EXIT_FAILURE,
		"Not enough memory for differential geometry buffers of ",
		field->rfilename);
/*---- Read first data plane (x shift) */
      read_body(tab, field->dgeostrip[0], nbpix);
/*---- Move to second data plane */
      QFSEEK(tab->cat->file,tab->bodypos + npixtot*tab->bytepix, SEEK_SET,
	field->rfilename);
/*---- Read second data plane (y shift) */
      read_body(tab, field->dgeostrip[1], nbpix);
/*---- Move back to first data plane */
      QFSEEK(tab->cat->file,tab->bodypos + nbpix*tab->bytepix, SEEK_SET,
	field->rfilename);
      }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
177
178
179
180
181
182
183
184

    field->ymax = field->stripheight;
    if (field->ymax < field->height)
      field->stripysclim = field->stripheight - field->stripmargin;
    }
  else
    {
/*- other strips */
185
    if (!(flags & (FLAG_FIELD|DGEO_FIELD)))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
186
187
188
189
190
191
192
193
194
195
      {
      data = field->strip + field->stripylim*w;
/*---- We assume weight data have been read just before */
      if (interpflag)
        wdata = wfield->strip + field->stripylim*w;

/*---- copy to Check-image the "oldest" line before it is replaced */
      if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_SUBOBJECTS]))
        writecheck(check, data, w);

196
197
198
199
200
201
      if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_MASK]))
        writecheck(check, data, w);

      if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_SUBMASK]))
        writecheck(check, data, w);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
202
203
204
205
      if (flags & BACKRMS_FIELD)
        backrmsline(field, field->ymax, data);
      else if (flags & INTERP_FIELD)
        copydata(field, field->stripylim*w, w);
206
      else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
207
        read_body(tab, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
      if (flags & (WEIGHT_FIELD|RMS_FIELD|BACKRMS_FIELD|VAR_FIELD))
        weight_to_var(field, data, w);

      if ((flags & MEASURE_FIELD) && (check=prefs.check[CHECK_IDENTICAL]))
        writecheck(check, data, w);
/*---- Interpolate and subtract the background at current line */
      if (flags & (MEASURE_FIELD|DETECT_FIELD))
        subbackline(field, field->ymax, data);
      if (interpflag)
        interpolate(field,wfield, data, wdata);
/*---- Check-image stuff */
      if (prefs.check_flag)
        {
        if (flags & MEASURE_FIELD)
          {
          if ((check = prefs.check[CHECK_BACKGROUND]))
            writecheck(check, field->backline, w);
          if ((check = prefs.check[CHECK_SUBTRACTED]))
            writecheck(check, data, w);
          if ((check = prefs.check[CHECK_APERTURES]))
            writecheck(check, data, w);
          if ((check = prefs.check[CHECK_SUBPSFPROTOS]))
            writecheck(check, data, w);
          if ((check = prefs.check[CHECK_SUBPCPROTOS]))
            writecheck(check, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
233
234
          if ((check = prefs.check[CHECK_SUBPROFILES]))
            writecheck(check, data, w);
235
236
237
238
          if ((check = prefs.check[CHECK_SUBSPHEROIDS]))
            writecheck(check, data, w);
          if ((check = prefs.check[CHECK_SUBDISKS]))
            writecheck(check, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
239
240
241
242
243
244
245
246
          }
        if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
          {
          backrmsline(field, field->ymax, (PIXTYPE *)check->pix);
          writecheck(check, check->pix, w);
          }
        }
      }
247
    else if (flags & FLAG_FIELD)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
248
      read_ibody(tab, field->fstrip + field->stripylim*w, w);
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
    else
      {
/*---- differential geometry map */
/*---- Read first data plane (x shift) */
      read_body(tab, field->dgeostrip[0] + field->stripylim*w, w);
/*---- Move to second data plane */
      QFSEEK(tab->cat->file,tab->bodypos
	+ (field->ymax * w + npixtot) * tab->bytepix, SEEK_SET,
	field->rfilename);
/*---- Read second data plane (y shift) */
      read_body(tab, field->dgeostrip[1] + field->stripylim*w, w);
/*---- Move back to first data plane */
      QFSEEK(tab->cat->file, tab->bodypos
	+ (field->ymax + 1) * w * tab->bytepix, SEEK_SET, field->rfilename);
      }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
264
265
266
267
268
269

    field->stripylim = (++field->ymin)%field->stripheight;
    if ((++field->ymax)<field->height)
      field->stripysclim = (++field->stripysclim)%field->stripheight;
    }

270
271
272
273
274
  return !(flags & (FLAG_FIELD|DGEO_FIELD))?
		(void *)(field->strip + field->stripy*w)
		: ((flags & FLAG_FIELD)?
			(void *)(field->fstrip + field->stripy*w)
			: (void *)(field->dgeostrip[0] + field->stripy*w));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  }


/******************************** copydata **********************************/
/*
Copy image data from one field to the other.
*/
void	copydata(picstruct *field, int offset, int size)
  {
  memcpy(field->strip+offset, field->reffield->strip+offset,
		size*sizeof(PIXTYPE));
  return;
  }


/******************************* readimagehead *******************************/
/*
extract some data from the FITS-file header
*/
void	readimagehead(picstruct *field)
  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
296
297
298
299
300
301
302
303
304
305
#define FITSREADS(buf, k, str, def) \
                {if (fitsread(buf,k,str, H_STRING,T_STRING) != RETURN_OK) \
                   strcpy(str, (def)); \
                }

   tabstruct	*tab;

  tab = field->tab;

  if(tab->naxis < 2)
306
    error(EXIT_FAILURE, field->filename, " does NOT contain image data!");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
307
308

/*---------------------------- Basic keywords ------------------------------*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
309
310
311
312
313
  if (tab->bitpix != BP_BYTE
	&& tab->bitpix != BP_SHORT
	&& tab->bitpix != BP_LONG
	&& tab->bitpix != BP_FLOAT
	&& tab->bitpix != BP_DOUBLE)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
314
315
    error(EXIT_FAILURE, "Sorry, I don't know that kind of data.", "");

Emmanuel Bertin's avatar
Emmanuel Bertin committed
316
317
  field->width = tab->naxisn[0];
  field->height = tab->naxisn[1];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
318
  field->npix = (KINGSIZE_T)field->width*field->height;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
319
320
  field->bitpix = tab->bitpix;
  field->bytepix = tab->bytepix;
321
  if (tab->bitsgn && (prefs.fitsunsigned_flag || tab->bitpix==BP_BYTE))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
322
    tab->bitsgn = 0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
323

Emmanuel Bertin's avatar
Emmanuel Bertin committed
324
  FITSREADS(tab->headbuf, "OBJECT  ", field->ident, "Unnamed");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
325
326
327
328

/*----------------------------- Astrometry ---------------------------------*/
/* Presently, astrometry is done only on the measurement and detect images */
  if (field->flags&(MEASURE_FIELD|DETECT_FIELD))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
329
    field->wcs = read_wcs(tab);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
330

Emmanuel Bertin's avatar
Emmanuel Bertin committed
331
  QFSEEK(field->file, tab->bodypos, SEEK_SET, field->filename);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
332
333
334

  return;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
335
#undef FITSREADS
Emmanuel Bertin's avatar
Emmanuel Bertin committed
336
337
338
339

  }