field.c 7.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
*				field.c
*
* Handle field (image) structures.
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	This file part of:	SExtractor
*
10
*	Copyright:		(C) 1993-2012 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:		12/07/2012
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
45
46

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

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

#include	"define.h"
#include	"globals.h"
#include	"prefs.h"
#include	"fits/fitscat.h"
#include	"assoc.h"
#include	"astrom.h"
#include	"back.h"
#include	"field.h"
#include	"filter.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
47
#include	"fitswcs.h"
48
#include	"header.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
49
50
51
52
#include	"interpolate.h"

/********************************* newfield **********************************/
/*
53
Returns a pointer to a new field for extension ext, ready to go!
Emmanuel Bertin's avatar
Emmanuel Bertin committed
54
*/
55
picstruct	*newfield(char *filename, int flags, int ext)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
56
57
58
59
60

  {
   picstruct	*field;
   catstruct	*cat;
   tabstruct	*tab;
61
   char		*pstr;
62
   int		ext2, nok, ntab, margin;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
63
64
65

  if (!(cat = read_cat(filename)))
    error(EXIT_FAILURE, "*Error*: cannot open ", filename);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
66
67
68
69
70

/* First allocate memory for the new field (and nullify pointers) */
  QCALLOC(field, picstruct, 1);
  field->flags = flags;
  field->cat = cat;
71
  nok = 0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
72
  tab = cat->tab;
73
74
75
76
77
78
79
  if (tab->naxis >= 2
	&& strncmp(tab->xtension, "BINTABLE", 8)
	&& strncmp(tab->xtension, "ASCTABLE", 8))
    nok++;
  ext2 = ext;
  for (ntab=cat->ntab; ext2-- && ntab--;)
    {
80
    tab=tab->nexttab;
81
82
83
84
85
86
87
    if (tab->naxis >= 2
	&& strncmp(tab->xtension, "BINTABLE", 8)
	&& strncmp(tab->xtension, "ASCTABLE", 8))
      nok++;
    }
  if (!nok)
    error(EXIT_FAILURE, "Not a valid FITS image in ",filename);
88

89
  field->tab = tab;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
90
91
92
93
94
95
96
97
98
99
  if (ntab<0)
    error(EXIT_FAILURE, "Not enough valid FITS image extensions in ",filename);

  strcpy (field->filename, filename);
/* A short, "relative" version of the filename */
  if (!(field->rfilename = strrchr(field->filename, '/')))
    field->rfilename = field->filename;
  else
    field->rfilename++;

100
101
102
103
104
105
/* Create a file name with a "header" extension */
  strcpy(field->hfilename, filename);
  if (!(pstr = strrchr(field->hfilename, '.')))
    pstr = field->hfilename+strlen(field->hfilename);
  sprintf(pstr, "%s", prefs.head_suffix);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
106
107
108
  sprintf(gstr, "Looking for %s", field->rfilename);
  NFPRINTF(OUTPUT, gstr);
/* Check the image exists and read important info (image size, etc...) */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
109
  field->file = cat->file;
110

111
  field->headflag = !read_aschead(field->hfilename, nok, field->tab);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
112
  readimagehead(field);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
113

Emmanuel Bertin's avatar
Emmanuel Bertin committed
114
  if (cat->ntab>1)
115
    sprintf(gstr, " [%d/%d]", ext, cat->tab->naxis<2? cat->ntab-1 : cat->ntab);
116
  QPRINTF(OUTPUT, "----- %s %s%s\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
117
118
119
120
121
	flags&FLAG_FIELD?   "Flagging  from:" :
       (flags&(RMS_FIELD|VAR_FIELD|WEIGHT_FIELD)?
			     "Weighting from:" :
       (flags&MEASURE_FIELD? "Measuring from:" :
			     "Detecting from:")),
122
123
124
	field->rfilename,
        cat->ntab>1? gstr : "");
  QPRINTF(OUTPUT, "      \"%.20s\" / %s / %dx%d / %d bits %s\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
125
	field->ident,
126
	field->headflag? "EXT. HEADER" : "no ext. header",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
127
128
	field->width, field->height, field->bytepix*8,
	field->bitpix>0?
129
130
	(field->tab->compress_type!=COMPRESS_NONE?"(compressed)":"(integers)")
	:"(floats)");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
131
132
133
134
135
136
137

/* Check the astrometric system and do the setup of the astrometric stuff */
  if (prefs.world_flag && (flags & (MEASURE_FIELD|DETECT_FIELD)))
    initastrom(field);
  else
    field->pixscale=prefs.pixel_scale;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
138
139
140
141
142
143
144
145
146
147
148
/* Gain and Saturation */
  if (flags & (DETECT_FIELD|MEASURE_FIELD))
    {
    if (fitsread(field->tab->headbuf, prefs.gain_key, &field->gain,
	H_FLOAT, T_DOUBLE) != RETURN_OK)
      field->gain = prefs.gain;
    if (fitsread(field->tab->headbuf, prefs.satur_key, &field->satur_level,
	H_FLOAT, T_DOUBLE) !=RETURN_OK)
      field->satur_level = prefs.satur_level;
    }

Emmanuel Bertin's avatar
Emmanuel Bertin committed
149
150
151
152
153
154
155
156
157
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
183
184
185
/* Background */
  if (flags & (DETECT_FIELD|MEASURE_FIELD|WEIGHT_FIELD|VAR_FIELD|RMS_FIELD))
    {
    field->ngamma = prefs.mag_gamma/log(10.0);

    field->backw = prefs.backsize[0]<field->width ? prefs.backsize[0]
						  : field->width;
    field->backh = prefs.backsize[1]<field->height ? prefs.backsize[1]
						   : field->height;
    field->nbackp = field->backw * field->backh;
    if ((field->nbackx = (field->width-1)/field->backw + 1) < 1)
      field->nbackx = 1;
    if ((field->nbacky = (field->height-1)/field->backh + 1) < 1)
      field->nbacky = 1;
    field->nback = field->nbackx * field->nbacky;
    field->nbackfx = field->nbackx>1 ? prefs.backfsize[0] : 1;
    field->nbackfy = field->nbacky>1 ? prefs.backfsize[1] : 1;
/*--  Set the back_type flag if absolute background is selected */
    if (((flags & DETECT_FIELD) && prefs.back_type[0]==BACK_ABSOLUTE)
	|| ((flags & MEASURE_FIELD) && prefs.back_type[1]==BACK_ABSOLUTE))
      field->back_type = BACK_ABSOLUTE;
    }

/* Add a comfortable margin for local background estimates */
  margin = (prefs.pback_type == LOCAL)? prefs.pback_size + prefs.mem_bufsize/4
					: 0;

  field->stripheight = prefs.mem_bufsize + margin;
  if (field->stripheight>field->height)
    field->stripheight = field->height;
/* Compute the image buffer size */
/* Basically, only one margin line is sufficient... */
  field->stripmargin = 1 + margin;
/* ...but : */
  if (prefs.filter_flag)
    {
/*-- If filtering is on, one should consider the height of the conv. mask */
186
/*-- + 1 line for detectinhg zero-weight neighbours */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
187
188
    if (field->stripheight < thefilter->convh)
      field->stripheight = thefilter->convh;
189
    if (field->stripmargin < (margin = (thefilter->convh-1)/2+1))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
      field->stripmargin = margin;
    }

  return field;
  }


/******************************* inheritfield *******************************/
/*
Make a copy of a field structure, e.g. for interpolation purposes.
*/
picstruct	*inheritfield(picstruct *infield, int flags)

  {
   picstruct	*field;

/* First allocate memory for the new field (and nullify pointers) */
  QCALLOC(field, picstruct, 1);

/* Copy what is important and reset the remaining */
  *field = *infield;
  field->flags = flags;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
212
213
  if (infield->wcs)
    field->wcs = copy_wcs(infield->wcs);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  field->interp_flag = 0;
  field->assoc = NULL;
  field->strip = NULL;
  field->fstrip = NULL;
  field->reffield = infield;
  field->file = NULL;

  return field;
  }


/********************************* endfield **********************************/
/*
Free and close everything related to a field structure.
*/
void	endfield(picstruct *field)

  {

Emmanuel Bertin's avatar
Emmanuel Bertin committed
233
234
235
/* Free cat only if associated with an open file */
  if (field->file)
    free_cat(&field->cat, 1);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
236
237
  free(field->strip);
  free(field->fstrip);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
238
239
  if (field->wcs)
    end_wcs(field->wcs);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
240
241
242
243
244
245
246
247
  if (field->interp_flag)
    end_interpolate(field);
  endback(field);
  free(field);

  return;
  }