readimage.c 10.8 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-2015 Emmanuel Bertin -- IAP/CNRS/UPMC
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:		14/01/2015
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
Emmanuel Bertin's avatar
Emmanuel Bertin committed
96
        read_body(tab, data, nbpix);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
97
98
99
100
101
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
      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
127
128
            if ((check = prefs.check[CHECK_SUBPROFILES]))
              writecheck(check, data, w);
129
130
131
132
            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
133
134
135
136
137
138
139
140
141
            }
          if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
            {
            backrmsline(field, y, (PIXTYPE *)check->pix);
            writecheck(check, check->pix, w);
            }
          }
        }
      }
142
    else if (flags & FLAG_FIELD)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
143
      {
144
/*---- flag map */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
145
146
147
148
      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
149
      read_ibody(field->tab, field->fstrip, nbpix);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
150
      }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    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
172
173
174
175
176
177
178
179

    field->ymax = field->stripheight;
    if (field->ymax < field->height)
      field->stripysclim = field->stripheight - field->stripmargin;
    }
  else
    {
/*- other strips */
180
    if (!(flags & (FLAG_FIELD|DGEO_FIELD)))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
181
182
183
184
185
186
187
188
189
190
      {
      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);

191
192
193
194
195
196
      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
197
198
199
200
201
      if (flags & BACKRMS_FIELD)
        backrmsline(field, field->ymax, data);
      else if (flags & INTERP_FIELD)
        copydata(field, field->stripylim*w, w);
      else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
202
        read_body(tab, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
      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
228
229
          if ((check = prefs.check[CHECK_SUBPROFILES]))
            writecheck(check, data, w);
230
231
232
233
          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
234
235
236
237
238
239
240
241
          }
        if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
          {
          backrmsline(field, field->ymax, (PIXTYPE *)check->pix);
          writecheck(check, check->pix, w);
          }
        }
      }
242
    else if (flags & FLAG_FIELD)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
243
      read_ibody(tab, field->fstrip + field->stripylim*w, w);
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
    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
259
260
261
262
263
264

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

265
266
267
268
269
  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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  }


/******************************** 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
291
292
293
294
295
296
297
298
299
300
#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)
301
    error(EXIT_FAILURE, field->filename, " does NOT contain image data!");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
302
303

/*---------------------------- Basic keywords ------------------------------*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
304
305
306
307
308
  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
309
310
    error(EXIT_FAILURE, "Sorry, I don't know that kind of data.", "");

Emmanuel Bertin's avatar
Emmanuel Bertin committed
311
312
  field->width = tab->naxisn[0];
  field->height = tab->naxisn[1];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
313
  field->npix = (KINGSIZE_T)field->width*field->height;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
314
315
  field->bitpix = tab->bitpix;
  field->bytepix = tab->bytepix;
316
  if (tab->bitsgn && (prefs.fitsunsigned_flag || tab->bitpix==BP_BYTE))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
317
    tab->bitsgn = 0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
318

Emmanuel Bertin's avatar
Emmanuel Bertin committed
319
  FITSREADS(tab->headbuf, "OBJECT  ", field->ident, "Unnamed");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
320
321
322
323

/*----------------------------- 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
324
    field->wcs = read_wcs(tab);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
325

Emmanuel Bertin's avatar
Emmanuel Bertin committed
326
  QFSEEK(field->file, tab->bodypos, SEEK_SET, field->filename);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
327
328
329

  return;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
330
#undef FITSREADS
Emmanuel Bertin's avatar
Emmanuel Bertin committed
331
332
333
334

  }