public software.sextractor

[/] [trunk/] [src/] [check.c] - Blame information for rev 233

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 bertin
/*
2 233 bertin
*                               check.c
3 2 bertin
*
4 233 bertin
* Manage "check-images".
5 2 bertin
*
6 233 bertin
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 2 bertin
*
8 233 bertin
*       This file part of:      SExtractor
9 2 bertin
*
10 233 bertin
*       Copyright:              (C) 1993,1998-2010 IAP/CNRS/UPMC
11
*                               (C) 1994,1997 ESO
12
*                               (C) 1995,1996 Sterrewacht Leiden
13 2 bertin
*
14 233 bertin
*       Author:                 Emmanuel Bertin (IAP)
15
*
16
*       License:                GNU General Public License
17
*
18
*       SExtractor is free software: you can redistribute it and/or modify
19
*       it under the terms of the GNU General Public License as published by
20
*       the Free Software Foundation, either version 3 of the License, or
21
*       (at your option) any later version.
22
*       SExtractor is distributed in the hope that it will be useful,
23
*       but WITHOUT ANY WARRANTY; without even the implied warranty of
24
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
*       GNU General Public License for more details.
26
*       You should have received a copy of the GNU General Public License
27
*       along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
28
*
29
*       Last modified:          11/10/2010
30
*
31
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32 2 bertin
 
33
#ifdef HAVE_CONFIG_H
34
#include        "config.h"
35
#endif
36
 
37
#include        <math.h>
38
#include        <stdio.h>
39
#include        <stdlib.h>
40
#include        <string.h>
41
 
42
#include        "define.h"
43
#include        "globals.h"
44
#include        "fits/fitscat.h"
45 173 bertin
#include        "fitswcs.h"
46 2 bertin
#include        "check.h"
47
 
48
/********************************* addcheck **********************************/
49
/*
50
Add a PSF to a CHECK-image (with a multiplicative factor).
51
Outside boundaries are taken into account.
52
*/
53
void    addcheck(checkstruct *check, float *psf,
54
                        int w,int h, int ix,int iy, float amplitude)
55
  {
56
   PIXTYPE      *pix;
57
   int          x,y, xmin,xmax,ymin,ymax,w2, dwpsf;
58
 
59
/* Set the image boundaries */
60
  w2 = w;
61
  ymin = iy-h/2;
62
  ymax = ymin + h;
63
  if (ymin<0)
64
    {
65
    psf -= ymin*w;
66
    ymin = 0;
67
    }
68
  if (ymax>check->height)
69
    ymax = check->height;
70
 
71
  xmin = ix-w/2;
72
  xmax = xmin + w;
73
  if (xmax>check->width)
74
    {
75
    w2 -= xmax-check->width;
76
    xmax = check->width;
77
    }
78
  if (xmin<0)
79
    {
80 173 bertin
    psf -= xmin;
81
    w2 += xmin;
82 2 bertin
    xmin = 0;
83
    }
84
 
85
  dwpsf = w-w2;
86
/* Subtract the right pixels to the destination */
87
  for (y=ymin; y<ymax; y++, psf += dwpsf)
88
    {
89
    pix = (float *)check->pix+y*check->width+xmin;
90
    for (x=w2; x--;)
91
      *(pix++) += amplitude**(psf++);
92
    }
93
 
94
  return;
95
  }
96
 
97
 
98
/********************************* blankcheck *******************************/
99
/*
100
Blank a part of the CHECK-image according to a mask.
101
*/
102
void    blankcheck(checkstruct *check, PIXTYPE *mask, int w,int h,
103
                int xmin,int ymin, PIXTYPE val)
104
  {
105
   PIXTYPE      *pixt;
106
   int          x,y, xmax,ymax,w2,wc;
107
 
108
/* Don't go further if out of frame!! */
109
  if (xmin+w<0 || xmin>=check->width
110
        || ymin+h<0 || ymin>=check->height)
111
    return;
112
 
113
/* Set the image boundaries */
114
  w2 = w;
115
  ymax = ymin + h;
116
  if (ymin<0)
117
    {
118
    mask -= ymin*w;
119
    ymin = 0;
120
    }
121
  if (ymax>check->height)
122
    ymax = check->height;
123
 
124
  xmax = xmin + w;
125
  if (xmax>check->width)
126
    {
127
    w2 -= xmax - check->width;
128
    xmax = check->width;
129
    }
130
  if (xmin<0)
131
    {
132
    mask += -xmin;
133
    w2 -= -xmin;
134
    xmin = 0;
135
    }
136
 
137
  w -= w2;
138
  wc = check->width;
139
  ymin = ymin*wc+xmin;
140
  ymax = ymax*wc+xmin;
141
 
142
/* Blank the right pixels in the image */
143
  for (y=ymin; y<ymax; y+=wc, mask += w)
144
    {
145
    pixt = (float *)check->pix + y;
146
    for (x=w2; x--; pixt++)
147
      if (*(mask++) > -BIG)
148
        *pixt = val;
149
    }
150
 
151
  return;
152
  }
153
 
154
 
155
/******************************** initcheck **********************************/
156
/*
157
initialize check-image.
158
*/
159
checkstruct     *initcheck(char *filename, checkenum check_type, int next)
160
 
161
  {
162
   catstruct    *fitscat;
163
   checkstruct  *check;
164
 
165
  QCALLOC(check, checkstruct, 1);
166
 
167
  strcpy(check->filename, filename);
168
  check->type = check_type;
169
 
170
  if (next>1)
171
/*-- Create a "pure" primary HDU */
172
    {
173
    fitscat = new_cat(1);
174
    init_cat(fitscat);
175
    strcpy(fitscat->filename, filename);
176
    fitsadd(fitscat->tab->headbuf, "NEXTEND ", "Number of extensions");
177
    fitswrite(fitscat->tab->headbuf, "NEXTEND ", &next, H_INT, T_LONG);
178
    if (open_cat(fitscat, WRITE_ONLY) != RETURN_OK)
179
      error(EXIT_FAILURE,"*Error*: cannot open for writing ", filename);
180
    save_tab(fitscat, fitscat->tab);
181
    check->file = fitscat->file;
182
    fitscat->file = NULL;
183
    free_cat(&fitscat, 1);
184
    }
185
  else
186
    if (!(check->file = fopen(check->filename, "wb")))
187
      error(EXIT_FAILURE, "*Error*: Cannot open for output ", check->filename);
188
 
189
  return check;
190
  }
191
 
192
 
193
/******************************** reinitcheck ********************************/
194
/*
195
initialize check-image (for subsequent writing).
196
*/
197
void    reinitcheck(picstruct *field, checkstruct *check)
198
 
199
  {
200 173 bertin
   wcsstruct    *wcs;
201 2 bertin
   char         *buf;
202
   int          i, ival;
203
   size_t       padsize;
204
   double       dval;
205
   ULONG        *ptri;
206
   PIXTYPE      *ptrf;
207
 
208
/* Inherit the field FITS header */
209 173 bertin
  check->fitsheadsize = field->tab->headnblock*FBSIZE;
210
  QMEMCPY(field->tab->headbuf, check->fitshead, char, check->fitsheadsize);
211 2 bertin
  check->y = 0;
212
/* Neutralize possible scaling factors */
213
  dval = 1.0;fitswrite(check->fitshead, "BSCALE  ", &dval, H_FLOAT, T_DOUBLE);
214
  dval = 0.0;fitswrite(check->fitshead, "BZERO   ", &dval, H_FLOAT, T_DOUBLE);
215
  ival = 1;fitswrite(check->fitshead, "BITSGN  ", &ival, H_INT, T_LONG);
216 173 bertin
  if (field->tab->compress_type != COMPRESS_NONE)
217 2 bertin
    fitswrite(check->fitshead, "IMAGECOD", "NONE", H_STRING, T_STRING);
218
  fitswrite(check->fitshead, "ORIGIN  ", BANNER, H_STRING, T_STRING);
219
 
220
  switch(check->type)
221
    {
222
    case CHECK_IDENTICAL:
223
    case CHECK_BACKGROUND:
224
    case CHECK_FILTERED:
225
    case CHECK_SUBTRACTED:
226
      ival = -32;
227
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
228
      check->width = field->width;
229
      check->height = field->height;
230
      check->npix = field->npix;
231
      QMALLOC(ptrf, PIXTYPE, check->width);
232
      check->pix = (void *)ptrf;
233
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
234
      free(check->fitshead);
235
      break;
236
 
237
    case CHECK_BACKRMS:
238
    case CHECK_SUBOBJECTS:
239
      ival = -32;
240
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
241
      check->width = field->width;
242
      check->height = field->height;
243
      check->npix = field->npix;
244
      QMALLOC(ptrf, PIXTYPE, check->width);
245
      check->pix = (void *)ptrf;
246
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
247
      free(check->fitshead);
248
/*---- Allocate memory for replacing the blanked pixels by 0 */
249
      if (!check->line)
250
        QMALLOC(check->line, PIXTYPE, field->width);
251
      break;
252
 
253
    case CHECK_OBJECTS:
254
    case CHECK_APERTURES:
255 221 bertin
    case CHECK_PSFPROTOS:
256 2 bertin
    case CHECK_SUBPSFPROTOS:
257 221 bertin
    case CHECK_PCPROTOS:
258 2 bertin
    case CHECK_SUBPCPROTOS:
259
    case CHECK_PCOPROTOS:
260 221 bertin
    case CHECK_PROFILES:
261 173 bertin
    case CHECK_SUBPROFILES:
262 221 bertin
    case CHECK_SPHEROIDS:
263
    case CHECK_SUBSPHEROIDS:
264
    case CHECK_DISKS:
265
    case CHECK_SUBDISKS:
266 173 bertin
    case CHECK_PATTERNS:
267 2 bertin
      ival = -32;
268
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
269
      check->width = field->width;
270
      check->height = field->height;
271
      check->npix = field->npix;
272
      check->overlay = 30*field->backsig;
273
      QCALLOC(ptrf, PIXTYPE, check->npix);
274
      check->pix = (void *)ptrf;
275
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
276
      free(check->fitshead);
277
      break;
278
 
279
    case CHECK_SEGMENTATION:
280
      ival = 32;
281
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
282
      check->width = field->width;
283
      check->height = field->height;
284
      check->npix = field->npix;
285
      QCALLOC(ptri, ULONG, check->npix);
286
      check->pix = (void *)ptri;
287
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
288
      free(check->fitshead);
289
      break;
290
 
291
    case CHECK_ASSOC:
292
      ival = -32;
293
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
294
      check->width = field->width;
295
      check->height = field->height;
296
      check->npix = field->npix;
297
      QMALLOC(ptrf, PIXTYPE, check->npix);
298
      check->pix = (void *)ptrf;
299
/*---- Initialize the pixmap to IEEE NaN */
300
      memset(ptrf, 0xFF, check->npix*sizeof(LONG));
301
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
302
      free(check->fitshead);
303
      break;
304
 
305
    case CHECK_MINIBACKGROUND:
306
    case CHECK_MINIBACKRMS:
307
      ival = -32;
308
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
309
      check->width = field->nbackx;
310
      fitswrite(check->fitshead, "NAXIS1  ", &check->width, H_INT, T_LONG);
311
      check->height = field->nbacky;
312
      fitswrite(check->fitshead, "NAXIS2  ", &check->height, H_INT, T_LONG);
313
/*---- Scale the WCS information if present */
314 173 bertin
      if ((wcs=field->wcs))
315 2 bertin
        {
316 173 bertin
        dval = wcs->cdelt[0]*field->backw;
317 2 bertin
        fitswrite(check->fitshead, "CDELT1  ", &dval, H_EXPO, T_DOUBLE);
318 173 bertin
        dval = wcs->cdelt[1]*field->backh;
319 2 bertin
        fitswrite(check->fitshead, "CDELT2  ", &dval, H_EXPO, T_DOUBLE);
320 173 bertin
        dval = (wcs->crpix[0]-0.5)/field->backw + 0.5;
321 2 bertin
        fitswrite(check->fitshead, "CRPIX1  ", &dval, H_EXPO, T_DOUBLE);
322 173 bertin
        dval = (wcs->crpix[1]-0.5)/field->backh + 0.5;
323 2 bertin
        fitswrite(check->fitshead, "CRPIX2  ", &dval, H_EXPO, T_DOUBLE);
324
 
325 173 bertin
        dval = wcs->cd[0]*field->backw;
326 2 bertin
        fitswrite(check->fitshead, "CD1_1   ", &dval, H_EXPO, T_DOUBLE);
327 173 bertin
        dval = wcs->cd[1]*field->backh;
328 2 bertin
        fitswrite(check->fitshead, "CD1_2  ", &dval, H_EXPO, T_DOUBLE);
329 173 bertin
        dval = wcs->cd[wcs->naxis]*field->backw;
330 2 bertin
        fitswrite(check->fitshead, "CD2_1   ", &dval, H_EXPO, T_DOUBLE);
331 173 bertin
        dval = wcs->cd[wcs->naxis+1]*field->backh;
332 2 bertin
        fitswrite(check->fitshead, "CD2_2  ", &dval, H_EXPO, T_DOUBLE);
333
        }
334
      check->npix = check->width*check->height;
335
      QMALLOC(ptrf, PIXTYPE, check->npix);
336
      check->pix = (void *)ptrf;
337
      if (check->type==CHECK_MINIBACKRMS)
338
        memcpy(check->pix, field->sigma, check->npix*sizeof(float));
339
      else
340
        memcpy(check->pix, field->back, check->npix*sizeof(float));
341
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
342
      free(check->fitshead);
343
      if (bswapflag)
344
        swapbytes(check->pix, sizeof(float), (int)check->npix);
345
      QFWRITE(check->pix,check->npix*sizeof(float),check->file,
346
        check->filename);
347
/*---- Put the buffer back to its original state */
348
      if (bswapflag)
349
        swapbytes(check->pix, sizeof(float), (int)check->npix);
350
      free(check->pix);
351
      QCALLOC(buf, char, FBSIZE);
352
      padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE))% FBSIZE;
353
      if (padsize)
354
        QFWRITE (buf, padsize, check->file, check->filename);
355
      free(buf);
356
      break;
357
 
358
    case CHECK_MAPSOM:
359
      ival = -32;
360
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
361
      check->width = field->width;
362
      check->height = field->height;
363
      check->npix = field->npix;
364
      QMALLOC(ptrf, PIXTYPE, check->npix);
365
      check->pix = (void *)ptrf;
366
      for (i=check->npix; i--;)
367
        *(ptrf++) = -10.0;
368
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
369
      free(check->fitshead);
370
      break;
371
 
372 221 bertin
    case CHECK_OTHER:
373
      ival = -32;
374
      fitswrite(check->fitshead, "BITPIX  ", &ival, H_INT, T_LONG);
375
      fitswrite(check->fitshead, "NAXIS1  ", &check->width, H_INT, T_LONG);
376
      fitswrite(check->fitshead, "NAXIS2  ", &check->height, H_INT, T_LONG);
377
      check->npix = check->width*check->height;
378
      QMALLOC(ptrf, PIXTYPE, check->npix);
379
      check->pix = (void *)ptrf;
380
      for (i=check->npix; i--;)
381
        *(ptrf++) = -10.0;
382
      QFWRITE(check->fitshead,check->fitsheadsize,check->file,check->filename);
383
      free(check->fitshead);
384
      break;
385
 
386 2 bertin
    default:
387 173 bertin
      error(EXIT_FAILURE, "*Internal Error* in ", "reinitcheck()!");
388 2 bertin
    }
389
 
390
  return;
391
  }
392
 
393
 
394
/******************************** writecheck *********************************/
395
/*
396
Write ONE line of npix pixels of a check-image.
397
*/
398
void    writecheck(checkstruct *check, PIXTYPE *data, int w)
399
 
400
  {
401
  if (check->type == CHECK_APERTURES || check->type == CHECK_SUBPSFPROTOS
402 173 bertin
        || check->type == CHECK_SUBPCPROTOS || check->type == CHECK_PCOPROTOS
403 221 bertin
        || check->type == CHECK_SUBPROFILES || check->type == CHECK_SUBSPHEROIDS
404
        || check->type == CHECK_SUBDISKS)
405 2 bertin
    {
406
    memcpy((PIXTYPE *)check->pix + w*(check->y++), data, w*sizeof(PIXTYPE));
407
    return;
408
    }
409
  else if (check->type == CHECK_SUBOBJECTS)
410
    {
411
     int        i;
412
     PIXTYPE    *pixt;
413
 
414
    pixt = check->line;
415
    for (i=w; i--; data++)
416
      *(pixt++) = (*data>-BIG)? *data:0.0;
417
    data = check->line;
418
    }
419
 
420
  if (bswapflag)
421
    swapbytes(data, sizeof(PIXTYPE), w);
422
  QFWRITE(data, w*sizeof(PIXTYPE), check->file, check->filename);
423
  if (bswapflag)
424
/*-- Put the buffer back to its original state */
425
    swapbytes(data, sizeof(PIXTYPE), w);
426
 
427
  return;
428
  }
429
 
430
 
431
/********************************* reendcheck ********************************/
432
/*
433
Finish current check-image.
434
*/
435
void    reendcheck(picstruct *field, checkstruct *check)
436
  {
437
   char         *buf;
438
   size_t       padsize;
439
 
440
  padsize = 0;                           /* To avoid gcc -Wall warnings */
441
  switch(check->type)
442
    {
443
    case CHECK_MINIBACKGROUND:
444
    case CHECK_MINIBACKRMS:
445
      return;
446
 
447
    case CHECK_IDENTICAL:
448
    case CHECK_BACKGROUND:
449
    case CHECK_BACKRMS:
450
    case CHECK_FILTERED:
451
    case CHECK_SUBTRACTED:
452
      free(check->pix);
453
      free(check->line);
454
      check->line = NULL;
455
      padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
456
      break;
457
 
458
    case CHECK_OBJECTS:
459
    case CHECK_APERTURES:
460 221 bertin
    case CHECK_PSFPROTOS:
461 2 bertin
    case CHECK_SUBPSFPROTOS:
462 221 bertin
    case CHECK_PCPROTOS:
463 2 bertin
    case CHECK_SUBPCPROTOS:
464
    case CHECK_PCOPROTOS:
465 221 bertin
    case CHECK_PROFILES:
466 173 bertin
    case CHECK_SUBPROFILES:
467 221 bertin
    case CHECK_SPHEROIDS:
468
    case CHECK_SUBSPHEROIDS:
469
    case CHECK_DISKS:
470
    case CHECK_SUBDISKS:
471 2 bertin
    case CHECK_ASSOC:
472 173 bertin
    case CHECK_PATTERNS:
473 221 bertin
    case CHECK_MAPSOM:
474
    case CHECK_OTHER:
475 2 bertin
      if (bswapflag)
476
        swapbytes(check->pix, sizeof(PIXTYPE), (int)check->npix);
477
      QFWRITE(check->pix,check->npix*sizeof(PIXTYPE),
478
                check->file,check->filename);
479
      free(check->pix);
480
      padsize = (FBSIZE-((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
481
      break;
482
 
483
    case CHECK_SEGMENTATION:
484
      if (bswapflag)
485
        swapbytes(check->pix, sizeof(ULONG), (int)check->npix);
486
      QFWRITE(check->pix,check->npix*sizeof(ULONG),
487
                check->file,check->filename);
488
      free(check->pix);
489
      padsize = (FBSIZE -((check->npix*sizeof(ULONG))%FBSIZE)) % FBSIZE;
490
      break;
491
 
492
    case CHECK_SUBOBJECTS:
493
      {
494
       int      y;
495
 
496
      for (y=field->ymin; y<field->ymax; y++)
497
        writecheck(check, &PIX(field, 0, y), field->width);
498
      free(check->pix);
499
      free(check->line);
500
      check->line = NULL;
501
      padsize = (FBSIZE -((check->npix*sizeof(PIXTYPE))%FBSIZE)) % FBSIZE;
502
      break;
503
      }
504
 
505
    default:
506
      error(EXIT_FAILURE, "*Internal Error* in ", "endcheck()!");
507
    }
508
 
509
  QCALLOC(buf, char, FBSIZE);
510
  if (padsize)
511
    QFWRITE (buf, padsize, check->file, check->filename);
512
  free(buf);
513
 
514
  return;
515
  }
516
 
517
/********************************* endcheck **********************************/
518
/*
519
close check-image.
520
*/
521
void    endcheck(checkstruct *check)
522
  {
523
 
524
  fclose(check->file);
525
  free(check);
526
 
527
  return;
528
  }
529