public software.sextractor

[/] [branches/] [multi/] [src/] [check.c] - Blame information for rev 267

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