readimage.c 7.93 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1
2
3
4
5
6
7
8
9
10
11
/*
 				readimage.c

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	Part of:	SExtractor
*
*	Author:		E.BERTIN (IAP)
*
*	Contents:	functions for input of image data.
*
Emmanuel Bertin's avatar
Emmanuel Bertin committed
12
*	Last modify:	29/09/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

#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
33
#include	"fitswcs.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
34
35
36
37
38
39
40
41
42
43
44
45
46
#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
47
   tabstruct	*tab;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
48
49
50
51
   checkstruct	*check;
   int		y, w, flags, interpflag;
   PIXTYPE	*data, *wdata, *rmsdata;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
52
  tab = field->tab;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  w = field->width;
  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;

    if (flags ^ FLAG_FIELD)
      {
/*---- 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
83
        read_body(tab, data, nbpix);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
      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
114
115
            if ((check = prefs.check[CHECK_SUBPROFILES]))
              writecheck(check, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            }
          if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
            {
            backrmsline(field, y, (PIXTYPE *)check->pix);
            writecheck(check, check->pix, w);
            }
          }
        }
      }
    else
      {
      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
131
      read_ibody(field->tab, field->fstrip, nbpix);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
      }

    field->ymax = field->stripheight;
    if (field->ymax < field->height)
      field->stripysclim = field->stripheight - field->stripmargin;
    }
  else
    {
/*- other strips */
    if (flags ^ FLAG_FIELD)
      {
      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);

      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
157
        read_body(tab, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
      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
183
184
          if ((check = prefs.check[CHECK_SUBPROFILES]))
            writecheck(check, data, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
185
186
187
188
189
190
191
192
193
          }
        if ((flags&DETECT_FIELD) && (check=prefs.check[CHECK_BACKRMS]))
          {
          backrmsline(field, field->ymax, (PIXTYPE *)check->pix);
          writecheck(check, check->pix, w);
          }
        }
      }
    else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
194
      read_ibody(tab, field->fstrip + field->stripylim*w, w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

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

  return (flags ^ FLAG_FIELD)?
		  (void *)(field->strip + field->stripy*w)
		: (void *)(field->fstrip + field->stripy*w);
  }


/******************************** 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
225
226
227
228
229
230
231
232
233
234
#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)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
235
236
237
    error(EXIT_FAILURE, field->filename, " does NOT contain 2D-data!");

/*---------------------------- Basic keywords ------------------------------*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
238
239
240
241
242
  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
243
244
    error(EXIT_FAILURE, "Sorry, I don't know that kind of data.", "");

Emmanuel Bertin's avatar
Emmanuel Bertin committed
245
246
  field->width = tab->naxisn[0];
  field->height = tab->naxisn[1];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
247
  field->npix = (KINGSIZE_T)field->width*field->height;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
248
249
  field->bitpix = tab->bitpix;
  field->bytepix = tab->bytepix;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
250
251
  if (tab->bitsgn && prefs.fitsunsigned_flag)
    tab->bitsgn = 0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
252

Emmanuel Bertin's avatar
Emmanuel Bertin committed
253
  FITSREADS(tab->headbuf, "OBJECT  ", field->ident, "Unnamed");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
254
255
256
257

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

Emmanuel Bertin's avatar
Emmanuel Bertin committed
260
  QFSEEK(field->file, tab->bodypos, SEEK_SET, field->filename);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
261
262
263

  return;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
264
#undef FITSREADS
Emmanuel Bertin's avatar
Emmanuel Bertin committed
265
266
267
268

  }