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

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	Part of:	SExtractor
*
*	Author:		E.BERTIN (IAP)
*
*	Contents:	handling of "check-images".
*
12
*	Last modify:	23/03/2010
Emmanuel Bertin's avatar
Emmanuel Bertin committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

#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	"fits/fitscat.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
29
#include	"fitswcs.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include	"check.h"

/********************************* addcheck **********************************/
/*
Add a PSF to a CHECK-image (with a multiplicative factor).
Outside boundaries are taken into account.
*/
void	addcheck(checkstruct *check, float *psf,
			int w,int h, int ix,int iy, float amplitude)
  {
   PIXTYPE	*pix;
   int		x,y, xmin,xmax,ymin,ymax,w2, dwpsf;

/* Set the image boundaries */
  w2 = w;
  ymin = iy-h/2;
  ymax = ymin + h;
  if (ymin<0)
    {
    psf -= ymin*w;
    ymin = 0;
    }
  if (ymax>check->height)
    ymax = check->height;

  xmin = ix-w/2;
  xmax = xmin + w;
  if (xmax>check->width)
    {
    w2 -= xmax-check->width;
    xmax = check->width;
    }
  if (xmin<0)
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
64
65
    psf -= xmin;
    w2 += xmin;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
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
    xmin = 0;
    }

  dwpsf = w-w2;
/* Subtract the right pixels to the destination */
  for (y=ymin; y<ymax; y++, psf += dwpsf)
    {
    pix = (float *)check->pix+y*check->width+xmin;
    for (x=w2; x--;)
      *(pix++) += amplitude**(psf++);
    }

  return;
  }


/********************************* blankcheck *******************************/
/*
Blank a part of the CHECK-image according to a mask.
*/
void	blankcheck(checkstruct *check, PIXTYPE *mask, int w,int h,
		int xmin,int ymin, PIXTYPE val)
  {
   PIXTYPE	*pixt;
   int		x,y, xmax,ymax,w2,wc;

/* Don't go further if out of frame!! */
  if (xmin+w<0 || xmin>=check->width
	|| ymin+h<0 || ymin>=check->height)
    return;
 
/* Set the image boundaries */
  w2 = w;
  ymax = ymin + h;
  if (ymin<0)
    {
    mask -= ymin*w;
    ymin = 0;
    }
  if (ymax>check->height)
    ymax = check->height;

  xmax = xmin + w;
  if (xmax>check->width)
    {
    w2 -= xmax - check->width;
    xmax = check->width;
    }
  if (xmin<0)
    {
    mask += -xmin;
    w2 -= -xmin;
    xmin = 0;
    }

  w -= w2;
  wc = check->width;
  ymin = ymin*wc+xmin;
  ymax = ymax*wc+xmin;

/* Blank the right pixels in the image */
  for (y=ymin; y<ymax; y+=wc, mask += w)
    {
    pixt = (float *)check->pix + y;
    for (x=w2; x--; pixt++)
      if (*(mask++) > -BIG)
        *pixt = val;
    }

  return;
  }


/******************************** initcheck **********************************/
/*
initialize check-image.
*/
checkstruct	*initcheck(char *filename, checkenum check_type, int next)

  {
   catstruct	*fitscat;
   checkstruct	*check;

  QCALLOC(check, checkstruct, 1);

  strcpy(check->filename, filename);
  check->type = check_type;

  if (next>1)
/*-- Create a "pure" primary HDU */
    {
    fitscat = new_cat(1);
    init_cat(fitscat);
    strcpy(fitscat->filename, filename);
    fitsadd(fitscat->tab->headbuf, "NEXTEND ", "Number of extensions");
    fitswrite(fitscat->tab->headbuf, "NEXTEND ", &next, H_INT, T_LONG);
    if (open_cat(fitscat, WRITE_ONLY) != RETURN_OK)
      error(EXIT_FAILURE,"*Error*: cannot open for writing ", filename);
    save_tab(fitscat, fitscat->tab);
    check->file = fitscat->file;
    fitscat->file = NULL;
    free_cat(&fitscat, 1);
    }
  else
    if (!(check->file = fopen(check->filename, "wb")))
      error(EXIT_FAILURE, "*Error*: Cannot open for output ", check->filename);

  return check;
  }


/******************************** reinitcheck ********************************/
/*
initialize check-image (for subsequent writing).
*/
void	reinitcheck(picstruct *field, checkstruct *check)

  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
184
   wcsstruct	*wcs;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
185
186
187
188
189
190
191
192
   char		*buf;
   int		i, ival;
   size_t	padsize;
   double	dval;
   ULONG	*ptri;
   PIXTYPE	*ptrf;

/* Inherit the field FITS header */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
193
194
  check->fitsheadsize = field->tab->headnblock*FBSIZE;
  QMEMCPY(field->tab->headbuf, check->fitshead, char, check->fitsheadsize);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
195
196
197
198
199
  check->y = 0;
/* Neutralize possible scaling factors */
  dval = 1.0;fitswrite(check->fitshead, "BSCALE  ", &dval, H_FLOAT, T_DOUBLE);
  dval = 0.0;fitswrite(check->fitshead, "BZERO   ", &dval, H_FLOAT, T_DOUBLE);
  ival = 1;fitswrite(check->fitshead, "BITSGN  ", &ival, H_INT, T_LONG);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
200
  if (field->tab->compress_type != COMPRESS_NONE)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
201
202
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
228
229
230
231
232
233
234
235
236
237
238
239
    fitswrite(check->fitshead, "IMAGECOD", "NONE", H_STRING, T_STRING);
  fitswrite(check->fitshead, "ORIGIN  ", BANNER, H_STRING, T_STRING);

  switch(check->type)
    {
    case CHECK_IDENTICAL:
    case CHECK_BACKGROUND:
    case CHECK_FILTERED:
    case CHECK_SUBTRACTED:
      ival = -32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      check->width = field->width;
      check->height = field->height;
      check->npix = field->npix;
      QMALLOC(ptrf, PIXTYPE, check->width);
      check->pix = (void *)ptrf;
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
      break;

    case CHECK_BACKRMS:
    case CHECK_SUBOBJECTS:
      ival = -32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      check->width = field->width;
      check->height = field->height;
      check->npix = field->npix;
      QMALLOC(ptrf, PIXTYPE, check->width);
      check->pix = (void *)ptrf;
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
/*---- Allocate memory for replacing the blanked pixels by 0 */
      if (!check->line)
        QMALLOC(check->line, PIXTYPE, field->width);
      break;

    case CHECK_OBJECTS:
    case CHECK_APERTURES:
    case CHECK_PSFPROTOS:
240
    case CHECK_SUBPSFPROTOS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
241
    case CHECK_PCPROTOS:
242
    case CHECK_SUBPCPROTOS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
243
    case CHECK_PCOPROTOS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
244
    case CHECK_PROFILES:
245
246
247
248
249
    case CHECK_SUBPROFILES:
    case CHECK_SPHEROIDS:
    case CHECK_SUBSPHEROIDS:
    case CHECK_DISKS:
    case CHECK_SUBDISKS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
250
    case CHECK_PATTERNS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
      ival = -32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      check->width = field->width;
      check->height = field->height;
      check->npix = field->npix;
      check->overlay = 30*field->backsig;
      QCALLOC(ptrf, PIXTYPE, check->npix);
      check->pix = (void *)ptrf;
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
      break;

    case CHECK_SEGMENTATION:
      ival = 32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      check->width = field->width;
      check->height = field->height;
      check->npix = field->npix;
      QCALLOC(ptri, ULONG, check->npix);
      check->pix = (void *)ptri;
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
      break;

    case CHECK_ASSOC:
      ival = -32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      check->width = field->width;
      check->height = field->height;
      check->npix = field->npix;
      QMALLOC(ptrf, PIXTYPE, check->npix);
      check->pix = (void *)ptrf;
/*---- Initialize the pixmap to IEEE NaN */
      memset(ptrf, 0xFF, check->npix*sizeof(LONG));
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
      break;

    case CHECK_MINIBACKGROUND:
    case CHECK_MINIBACKRMS:
      ival = -32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      check->width = field->nbackx;
      fitswrite(check->fitshead, "NAXIS1  ", &check->width, H_INT, T_LONG);
      check->height = field->nbacky;
      fitswrite(check->fitshead, "NAXIS2  ", &check->height, H_INT, T_LONG);
/*---- Scale the WCS information if present */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
298
      if ((wcs=field->wcs))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
299
        {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
300
        dval = wcs->cdelt[0]*field->backw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
301
        fitswrite(check->fitshead, "CDELT1  ", &dval, H_EXPO, T_DOUBLE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
302
        dval = wcs->cdelt[1]*field->backh;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
303
        fitswrite(check->fitshead, "CDELT2  ", &dval, H_EXPO, T_DOUBLE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
304
        dval = (wcs->crpix[0]-0.5)/field->backw + 0.5;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
305
        fitswrite(check->fitshead, "CRPIX1  ", &dval, H_EXPO, T_DOUBLE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
306
        dval = (wcs->crpix[1]-0.5)/field->backh + 0.5;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
307
308
        fitswrite(check->fitshead, "CRPIX2  ", &dval, H_EXPO, T_DOUBLE);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
309
        dval = wcs->cd[0]*field->backw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
310
        fitswrite(check->fitshead, "CD1_1   ", &dval, H_EXPO, T_DOUBLE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
311
        dval = wcs->cd[1]*field->backh;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
312
        fitswrite(check->fitshead, "CD1_2  ", &dval, H_EXPO, T_DOUBLE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
313
        dval = wcs->cd[wcs->naxis]*field->backw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
314
        fitswrite(check->fitshead, "CD2_1   ", &dval, H_EXPO, T_DOUBLE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
315
        dval = wcs->cd[wcs->naxis+1]*field->backh;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        fitswrite(check->fitshead, "CD2_2  ", &dval, H_EXPO, T_DOUBLE);
        }
      check->npix = check->width*check->height;
      QMALLOC(ptrf, PIXTYPE, check->npix);
      check->pix = (void *)ptrf;
      if (check->type==CHECK_MINIBACKRMS)
        memcpy(check->pix, field->sigma, check->npix*sizeof(float));
      else
        memcpy(check->pix, field->back, check->npix*sizeof(float));
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
      if (bswapflag)
        swapbytes(check->pix, sizeof(float), (int)check->npix);
      QFWRITE(check->pix,check->npix*sizeof(float),check->file,
	check->filename);
/*---- Put the buffer back to its original state */
      if (bswapflag)
        swapbytes(check->pix, sizeof(float), (int)check->npix);
      free(check->pix);
      QCALLOC(buf, char, FBSIZE);
      padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE))% FBSIZE;
      if (padsize)
        QFWRITE (buf, padsize, check->file, check->filename);
      free(buf);
      break;

    case CHECK_MAPSOM:
      ival = -32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      check->width = field->width;
      check->height = field->height;
      check->npix = field->npix;
      QMALLOC(ptrf, PIXTYPE, check->npix);
      check->pix = (void *)ptrf;
      for (i=check->npix; i--;)
        *(ptrf++) = -10.0;
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
      break;

356
357
358
359
360
361
362
363
364
365
366
367
368
369
    case CHECK_OTHER:
      ival = -32;
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
      fitswrite(check->fitshead, "NAXIS1  ", &check->width, H_INT, T_LONG);
      fitswrite(check->fitshead, "NAXIS2  ", &check->height, H_INT, T_LONG);
      check->npix = check->width*check->height;
      QMALLOC(ptrf, PIXTYPE, check->npix);
      check->pix = (void *)ptrf;
      for (i=check->npix; i--;)
        *(ptrf++) = -10.0;
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
      free(check->fitshead);
      break;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
370
    default:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
371
      error(EXIT_FAILURE, "*Internal Error* in ", "reinitcheck()!");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    }

  return;
  }


/******************************** writecheck *********************************/
/*
Write ONE line of npix pixels of a check-image.
*/
void	writecheck(checkstruct *check, PIXTYPE *data, int w)

  {
  if (check->type == CHECK_APERTURES || check->type == CHECK_SUBPSFPROTOS
Emmanuel Bertin's avatar
Emmanuel Bertin committed
386
	|| check->type == CHECK_SUBPCPROTOS || check->type == CHECK_PCOPROTOS
387
388
	|| check->type == CHECK_SUBPROFILES || check->type == CHECK_SUBSPHEROIDS
	|| check->type == CHECK_SUBDISKS)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
    {
    memcpy((PIXTYPE *)check->pix + w*(check->y++), data, w*sizeof(PIXTYPE));
    return;
    }
  else if (check->type == CHECK_SUBOBJECTS)
    {
     int	i;
     PIXTYPE	*pixt;

    pixt = check->line;
    for (i=w; i--; data++)
      *(pixt++) = (*data>-BIG)? *data:0.0;
    data = check->line;
    }

  if (bswapflag)
    swapbytes(data, sizeof(PIXTYPE), w);
  QFWRITE(data, w*sizeof(PIXTYPE), check->file, check->filename);
  if (bswapflag)
/*-- Put the buffer back to its original state */
    swapbytes(data, sizeof(PIXTYPE), w);

  return;
  }


/********************************* reendcheck ********************************/
/*
Finish current check-image.
*/
void	reendcheck(picstruct *field, checkstruct *check)
  {
   char		*buf;
   size_t	padsize;

  padsize = 0;				/* To avoid gcc -Wall warnings */
  switch(check->type)
    {
    case CHECK_MINIBACKGROUND:
    case CHECK_MINIBACKRMS:
      return;

    case CHECK_IDENTICAL:
    case CHECK_BACKGROUND:
    case CHECK_BACKRMS:
    case CHECK_FILTERED:
    case CHECK_SUBTRACTED:
      free(check->pix);
      free(check->line);
      check->line = NULL;
      padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
      break;

    case CHECK_OBJECTS:
    case CHECK_APERTURES:
    case CHECK_PSFPROTOS:
445
    case CHECK_SUBPSFPROTOS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
446
    case CHECK_PCPROTOS:
447
    case CHECK_SUBPCPROTOS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
448
    case CHECK_PCOPROTOS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
449
    case CHECK_PROFILES:
450
451
452
453
454
    case CHECK_SUBPROFILES:
    case CHECK_SPHEROIDS:
    case CHECK_SUBSPHEROIDS:
    case CHECK_DISKS:
    case CHECK_SUBDISKS:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
455
    case CHECK_ASSOC:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
456
    case CHECK_PATTERNS:
457
458
    case CHECK_MAPSOM:
    case CHECK_OTHER:
Emmanuel Bertin's avatar
Emmanuel Bertin committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
      if (bswapflag)
        swapbytes(check->pix, sizeof(PIXTYPE), (int)check->npix);
      QFWRITE(check->pix,check->npix*sizeof(PIXTYPE),
		check->file,check->filename);
      free(check->pix);
      padsize = (FBSIZE-((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
      break;

    case CHECK_SEGMENTATION:
      if (bswapflag)
        swapbytes(check->pix, sizeof(ULONG), (int)check->npix);
      QFWRITE(check->pix,check->npix*sizeof(ULONG),
		check->file,check->filename);
      free(check->pix);
      padsize = (FBSIZE -((check->npix*sizeof(ULONG))%FBSIZE)) % FBSIZE;
      break;

    case CHECK_SUBOBJECTS:
      {
       int	y;

      for (y=field->ymin; y<field->ymax; y++)
        writecheck(check, &PIX(field, 0, y), field->width);
      free(check->pix);
      free(check->line);
      check->line = NULL;
      padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
      break;
      }

    default:
      error(EXIT_FAILURE, "*Internal Error* in ", "endcheck()!");
    }

  QCALLOC(buf, char, FBSIZE);
  if (padsize)
    QFWRITE (buf, padsize, check->file, check->filename);
  free(buf);

  return;
  }

/********************************* endcheck **********************************/
/*
close check-image.
*/
void	endcheck(checkstruct *check)
  {

  fclose(check->file);
  free(check);

  return;
  }