public software.sextractor

[/] [trunk/] [src/] [makeit.c] - Blame information for rev 16

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

Line No. Rev Author Line
1 2 bertin
/*
2
                                makeit.c
3
 
4
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
*
6
*       Part of:        SExtractor
7
*
8
*       Author:         E.BERTIN, IAP & Leiden observatory
9
*
10
*       Contents:       main program.
11
*
12 16 bertin
*       Last modify:    13/07/2006
13 2 bertin
*
14
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15
*/
16
 
17
#ifdef HAVE_CONFIG_H
18
#include        "config.h"
19
#endif
20
 
21
#include        <math.h>
22
#include        <stdio.h>
23
#include        <stdlib.h>
24
#include        <string.h>
25
#include        <time.h>
26
 
27
#include        "define.h"
28
#include        "globals.h"
29
#include        "prefs.h"
30
#include        "fits/fitscat.h"
31
#include        "assoc.h"
32
#include        "back.h"
33
#include        "check.h"
34
#include        "field.h"
35
#include        "filter.h"
36
#include        "growth.h"
37
#include        "interpolate.h"
38
#include        "psf.h"
39
#include        "som.h"
40
#include        "weight.h"
41 8 bertin
#include        "xml.h"
42 2 bertin
 
43 12 bertin
time_t  thetimet, thetimet2;
44 11 bertin
 
45 2 bertin
/******************************** makeit *************************************/
46
/*
47
Manage the whole stuff.
48
*/
49
void    makeit()
50
 
51
  {
52
   checkstruct          *check;
53
   picstruct            *dfield, *field,*pffield[MAXFLAG], *wfield,*dwfield;
54
   catstruct            *imacat;
55
   tabstruct            *imatab;
56
   static time_t        thetime1, thetime2;
57 8 bertin
   struct tm            *tm;
58 16 bertin
   FILE                 *file;
59 2 bertin
   int                  i, nok, ntab, next;
60
 
61 11 bertin
/* Install error logging */
62 16 bertin
  error_installfunc(write_error);
63 11 bertin
 
64 8 bertin
/* Processing start date and time */
65
  thetimet = time(NULL);
66
  tm = localtime(&thetimet);
67
  sprintf(prefs.sdate_start,"%04d-%02d-%02d",
68
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
69
  sprintf(prefs.stime_start,"%02d:%02d:%02d",
70
        tm->tm_hour, tm->tm_min, tm->tm_sec);
71
 
72
  NFPRINTF(OUTPUT, "");
73
  QPRINTF(OUTPUT, "----- %s %s started on %s at %s with %d thread%s\n\n",
74
                BANNER,
75
                MYVERSION,
76
                prefs.sdate_start,
77
                prefs.stime_start,
78
                prefs.nthreads,
79
                prefs.nthreads>1? "s":"");
80
 
81 2 bertin
/* Initialize globals variables */
82
  initglob();
83
 
84
  NFPRINTF(OUTPUT, "Setting catalog parameters");
85
  readcatparams(prefs.param_name);
86
  useprefs();                   /* update things accor. to prefs parameters */
87
 
88
  if (prefs.psf_flag)
89
    {
90
    NFPRINTF(OUTPUT, "Reading PSF information");
91 5 bertin
    thepsf = psf_load(prefs.psf_name[0]);
92
    if (prefs.dpsf_flag)
93
      ppsf = psf_load(prefs.psf_name[1]);
94 2 bertin
 /*-- Need to check things up because of PSF context parameters */
95
    updateparamflags();
96
    useprefs();
97
    }
98
 
99
  if (prefs.filter_flag)
100
    {
101
    NFPRINTF(OUTPUT, "Reading detection filter");
102
    getfilter(prefs.filter_name);       /* get the detection filter */
103
    }
104
 
105
  if (FLAG(obj2.sprob))
106
    {
107
    NFPRINTF(OUTPUT, "Initializing Neural Network");
108
    neurinit();
109
    NFPRINTF(OUTPUT, "Reading Neural Network Weights");
110
    getnnw();
111
    }
112
 
113
  if (prefs.somfit_flag)
114
    {
115
     int        margin;
116
 
117
    thesom = som_load(prefs.som_name);
118
    if ((margin=(thesom->inputsize[1]+1)/2) > prefs.cleanmargin)
119
      prefs.cleanmargin = margin;
120
    if (prefs.somfit_vectorsize>thesom->neurdim)
121
      {
122
      prefs.somfit_vectorsize = thesom->neurdim;
123
      sprintf(gstr,"%d", prefs.somfit_vectorsize);
124
      warning("Dimensionality of the SOM-fit vector limited to ", gstr);
125
      }
126
    }
127
 
128
/* Prepare growth-curve buffer */
129
  if (prefs.growth_flag)
130
    initgrowth();
131
 
132
/* Compute the number of valid input extensions */
133
  if (!(imacat = read_cat(prefs.image_name[0])))
134
    error(EXIT_FAILURE, "*Error*: cannot open ", prefs.image_name[0]);
135
  close_cat(imacat);
136
  imatab = imacat->tab;
137
  next = 0;
138
  for (ntab = 0 ; ntab<imacat->ntab; ntab++, imatab = imatab->nexttab)
139
    {
140
/*--  Check for the next valid image extension */
141
    if ((imatab->naxis < 2)
142
        || !strncmp(imatab->xtension, "BINTABLE", 8)
143
        || !strncmp(imatab->xtension, "ASCTABLE", 8))
144
      continue;
145
    next++;
146
    }
147
  thecat.next = next;
148
 
149
/*-- Init the CHECK-images */
150
  if (prefs.check_flag)
151
    {
152
     checkenum  c;
153
 
154
    NFPRINTF(OUTPUT, "Initializing check-image(s)");
155
    for (i=0; i<prefs.ncheck_type; i++)
156
    if ((c=prefs.check_type[i]) != CHECK_NONE)
157
      {
158
      if (prefs.check[c])
159
         error(EXIT_FAILURE,"*Error*: 2 CHECK_IMAGEs cannot have the same ",
160
                        " CHECK_IMAGE_TYPE");
161
      prefs.check[c] = initcheck(prefs.check_name[i], prefs.check_type[i],
162
                        next);
163
      free(prefs.check_name[i]);
164
      }
165
    }
166
 
167
  NFPRINTF(OUTPUT, "Initializing catalog");
168
  initcat();
169
 
170 8 bertin
/* Initialize XML data */
171 15 bertin
  if (prefs.xml_flag || prefs.cat_type==ASCII_VO)
172 8 bertin
    init_xml(next);
173 2 bertin
 
174
/* Go through all images */
175
  nok = -1;
176
  for (ntab = 0 ; ntab<imacat->ntab; ntab++, imatab = imatab->nexttab)
177
    {
178
/*--  Check for the next valid image extension */
179
    if ((imatab->naxis < 2)
180
        || !strncmp(imatab->xtension, "BINTABLE", 8)
181
        || !strncmp(imatab->xtension, "ASCTABLE", 8))
182
      continue;
183
    nok++;
184
 
185
/*-- Initial time measurement*/
186
    time(&thetime1);
187
    thecat.currext = nok+1;
188
 
189
    dfield = field = wfield = dwfield = NULL;
190
 
191
    if (prefs.dimage_flag)
192
      {
193
/*---- Init the Detection and Measurement-images */
194
      dfield = newfield(prefs.image_name[0], DETECT_FIELD, nok);
195
      field = newfield(prefs.image_name[1], MEASURE_FIELD, nok);
196
      if ((field->width!=dfield->width) || (field->height!=dfield->height))
197
        error(EXIT_FAILURE, "*Error*: Frames have different sizes","");
198
/*---- Prepare interpolation */
199
      if (prefs.dweight_flag && prefs.interp_type[0] == INTERP_ALL)
200
        init_interpolate(dfield, -1, -1);
201
      if (prefs.interp_type[1] == INTERP_ALL)
202
        init_interpolate(field, -1, -1);
203
      }
204
    else
205
      {
206
      field = newfield(prefs.image_name[0], DETECT_FIELD | MEASURE_FIELD, nok);
207
/*-- Prepare interpolation */
208
      if ((prefs.dweight_flag || prefs.weight_flag)
209
        && prefs.interp_type[0] == INTERP_ALL)
210
      init_interpolate(field, -1, -1);       /* 0.0 or anything else */
211
      }
212
 
213
/*-- Init the WEIGHT-images */
214
    if (prefs.dweight_flag || prefs.weight_flag)
215
      {
216
       weightenum       wtype;
217
       PIXTYPE  interpthresh;
218
 
219
      if (prefs.nweight_type>1)
220
        {
221
/*------ Double-weight-map mode */
222
        if (prefs.weight_type[1] != WEIGHT_NONE)
223
          {
224
/*-------- First: the "measurement" weights */
225
          wfield = newweight(prefs.wimage_name[1],field,prefs.weight_type[1],
226
                nok);
227
          wtype = prefs.weight_type[1];
228
          interpthresh = prefs.weight_thresh[1];
229
/*-------- Convert the interpolation threshold to variance units */
230
          weight_to_var(wfield, &interpthresh, 1);
231
          wfield->weight_thresh = interpthresh;
232
          if (prefs.interp_type[1] != INTERP_NONE)
233
            init_interpolate(wfield,
234
                prefs.interp_xtimeout[1], prefs.interp_ytimeout[1]);
235
          }
236
/*------ The "detection" weights */
237
        if (prefs.weight_type[0] != WEIGHT_NONE)
238
          {
239
          interpthresh = prefs.weight_thresh[0];
240
          if (prefs.weight_type[0] == WEIGHT_FROMINTERP)
241
            {
242
            dwfield=newweight(prefs.wimage_name[0],wfield,prefs.weight_type[0],
243
                nok);
244
            weight_to_var(wfield, &interpthresh, 1);
245
            }
246
          else
247
            {
248
            dwfield = newweight(prefs.wimage_name[0], dfield?dfield:field,
249
                prefs.weight_type[0], nok);
250
            weight_to_var(dwfield, &interpthresh, 1);
251
            }
252
          dwfield->weight_thresh = interpthresh;
253
          if (prefs.interp_type[0] != INTERP_NONE)
254
            init_interpolate(dwfield,
255
                prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]);
256
          }
257
        }
258
      else
259
        {
260
/*------ Single-weight-map mode */
261
        wfield = newweight(prefs.wimage_name[0], dfield?dfield:field,
262
                        prefs.weight_type[0], nok);
263
        wtype = prefs.weight_type[0];
264
        interpthresh = prefs.weight_thresh[0];
265
/*------ Convert the interpolation threshold to variance units */
266
        weight_to_var(wfield, &interpthresh, 1);
267
        wfield->weight_thresh = interpthresh;
268
        if (prefs.interp_type[0] != INTERP_NONE)
269
          init_interpolate(wfield,
270
                prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]);
271
        }
272
      }
273
 
274
/*-- Init the FLAG-images */
275
    for (i=0; i<prefs.nimaflag; i++)
276
      {
277
      pffield[i] = newfield(prefs.fimage_name[i], FLAG_FIELD, nok);
278
      if ((pffield[i]->width!=field->width)
279
        || (pffield[i]->height!=field->height))
280
        error(EXIT_FAILURE,
281
        "*Error*: Incompatible FLAG-map size in ", prefs.fimage_name[i]);
282
      }
283
 
284
/*-- Compute background maps for `standard' fields */
285
    QPRINTF(OUTPUT, dfield? "Measurement image:"
286
                        : "Detection+Measurement image: ");
287
    makeback(field, wfield);
288
    QPRINTF(OUTPUT, (dfield || (dwfield&&dwfield->flags^INTERP_FIELD))? "(M)   "
289
                "Background: %-10g RMS: %-10g / Threshold: %-10g \n"
290
                : "(M+D) "
291
                "Background: %-10g RMS: %-10g / Threshold: %-10g \n",
292
        field->backmean, field->backsig, (field->flags & DETECT_FIELD)?
293
        field->dthresh: field->thresh);
294
    if (dfield)
295
    {
296
      QPRINTF(OUTPUT, "Detection image: ");
297
      makeback(dfield, dwfield? dwfield
298
                        : (prefs.weight_type[0] == WEIGHT_NONE?NULL:wfield));
299
      QPRINTF(OUTPUT, "(D)   "
300
                "Background: %-10g RMS: %-10g / Threshold: %-10g \n",
301
        dfield->backmean, dfield->backsig, dfield->dthresh);
302
      }
303
    else if (dwfield && dwfield->flags^INTERP_FIELD)
304
      {
305
      makeback(field, dwfield);
306
      QPRINTF(OUTPUT, "(D)   "
307
                "Background: %-10g RMS: %-10g / Threshold: %-10g \n",
308
        field->backmean, field->backsig, field->dthresh);
309
      }
310
 
311
/*-- For interpolated weight-maps, copy the background structure */
312
    if (dwfield && dwfield->flags&(INTERP_FIELD|BACKRMS_FIELD))
313
      copyback(dwfield->reffield, dwfield);
314
    if (wfield && wfield->flags&(INTERP_FIELD|BACKRMS_FIELD))
315
      copyback(wfield->reffield, wfield);
316
 
317
/*-- Prepare learn and/or associations */
318
    if (prefs.assoc_flag)
319
      init_assoc(field);                  /* initialize assoc tasks */
320
 
321
/*-- Update the CHECK-images */
322
    if (prefs.check_flag)
323
      for (i=0; i<MAXCHECK; i++)
324
        if ((check=prefs.check[i]))
325
          reinitcheck(field, check);
326
 
327
/*-- Initialize PSF contexts and workspace */
328
    if (prefs.psf_flag)
329
      {
330
      psf_readcontext(thepsf, field);
331
      psf_init(thepsf);
332 5 bertin
      if (prefs.dpsf_flag)
333
        {
334
        psf_readcontext(thepsf, dfield);
335
        psf_init(thepsf); /*?*/
336
        }
337 2 bertin
      }
338
 
339
/*-- Copy field structures to static ones (for catalog info) */
340
    if (dfield)
341
      {
342
      thefield1 = *field;
343
      thefield2 = *dfield;
344
      }
345
    else
346
      thefield1 = thefield2 = *field;
347
 
348
    if (wfield)
349
      {
350
      thewfield1 = *wfield;
351
      thewfield2 = dwfield? *dwfield: *wfield;
352
      }
353
    else if (dwfield)
354
      thewfield2 = *dwfield;
355
 
356
    reinitcat(field);
357
 
358
/*-- Start the extraction pipeline */
359
    NFPRINTF(OUTPUT, "Scanning image");
360
    scanimage(field, dfield, pffield, prefs.nimaflag, wfield, dwfield);
361
 
362
/*-- Finish the current CHECK-image processing */
363
    if (prefs.check_flag)
364
      for (i=0; i<MAXCHECK; i++)
365
        if ((check=prefs.check[i]))
366
          reendcheck(field, check);
367
 
368
/*-- Final time measurements*/
369
    if (time(&thetime2)!=-1)
370
      {
371
      if (!strftime(thecat.ext_date, 12, "%d/%m/%Y", localtime(&thetime2)))
372
        error(EXIT_FAILURE, "*Internal Error*: Date string too long ","");
373
      if (!strftime(thecat.ext_time, 10, "%H:%M:%S", localtime(&thetime2)))
374
        error(EXIT_FAILURE, "*Internal Error*: Time/date string too long ","");
375
      thecat.ext_elapsed = difftime(thetime2, thetime1);
376
      }
377
 
378
    reendcat();
379
 
380 8 bertin
/* Update XML data */
381 15 bertin
  if (prefs.xml_flag || prefs.cat_type==ASCII_VO)
382 9 bertin
    update_xml(&thecat, dfield? dfield:field, field,
383
        dwfield? dwfield:wfield, wfield);
384 8 bertin
 
385
 
386 2 bertin
/*-- Close ASSOC routines */
387
    end_assoc(field);
388
 
389
    for (i=0; i<prefs.nimaflag; i++)
390
      endfield(pffield[i]);
391
    endfield(field);
392
    if (dfield)
393
      endfield(dfield);
394
    if (wfield)
395
      endfield(wfield);
396
    if (dwfield)
397
      endfield(dwfield);
398
 
399 6 bertin
    QPRINTF(OUTPUT, "Objects: detected %-8d / sextracted %-8d               \n",
400 2 bertin
        thecat.ndetect, thecat.ntotal);
401
    }
402
 
403
  if (nok<0)
404
    error(EXIT_FAILURE, "Not enough valid FITS image extensions in ",
405
        prefs.image_name[0]);
406
  free_cat(&imacat, 1);
407
 
408
  NFPRINTF(OUTPUT, "Closing files");
409
 
410 16 bertin
  endcat((char *)NULL);
411 2 bertin
 
412
/* End CHECK-image processing */
413
  if (prefs.check_flag)
414
    for (i=0; i<MAXCHECK; i++)
415
      {
416
      if ((check=prefs.check[i]))
417
        endcheck(check);
418
      prefs.check[i] = NULL;
419
      }
420
 
421
  if (prefs.filter_flag)
422
    endfilter();
423
 
424
  if (prefs.somfit_flag)
425
    som_end(thesom);
426
 
427
  if (prefs.growth_flag)
428
    endgrowth();
429
 
430
  if (prefs.psf_flag)
431 5 bertin
    psf_end(thepsf,thepsfit); /*?*/
432 2 bertin
 
433 5 bertin
  if (prefs.dpsf_flag)
434
    psf_end(ppsf,ppsfit);
435
 
436 2 bertin
  if (FLAG(obj2.sprob))
437
    neurclose();
438
 
439 8 bertin
/* Processing end date and time */
440
  thetimet2 = time(NULL);
441
  tm = localtime(&thetimet2);
442
  sprintf(prefs.sdate_end,"%04d-%02d-%02d",
443
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
444
  sprintf(prefs.stime_end,"%02d:%02d:%02d",
445
        tm->tm_hour, tm->tm_min, tm->tm_sec);
446
  prefs.time_diff = difftime(thetimet2, thetimet);
447
 
448
/* Write XML */
449 16 bertin
  if (prefs.xml_flag && (file = fopen(prefs.xml_name, "w")))
450
    write_xml(file);
451
 
452 15 bertin
  if (prefs.xml_flag || prefs.cat_type==ASCII_VO)
453
    end_xml();
454 8 bertin
 
455 2 bertin
  return;
456
  }
457
 
458
 
459
/******************************** initglob ***********************************/
460
/*
461
Initialize a few global variables
462
*/
463
void    initglob()
464
  {
465
   int  i;
466
 
467
  for (i=0; i<37; i++)
468
    {
469
    ctg[i] = cos(i*PI/18);
470
    stg[i] = sin(i*PI/18);
471
    }
472
 
473
 
474
  return;
475
  }
476
 
477
 
478 16 bertin
/****** write_error ********************************************************
479
PROTO   int     write_error(char *msg1, char *msg2)
480
PURPOSE Manage files in case of a catched error
481
INPUT   a character string,
482
        another character string
483
OUTPUT  RETURN_OK if everything went fine, RETURN_ERROR otherwise.
484
NOTES   -.
485
AUTHOR  E. Bertin (IAP)
486
VERSION 13/07/2006
487
 ***/
488
void    write_error(char *msg1, char *msg2)
489
  {
490
   FILE                 *file;
491
   char                 error[MAXCHAR];
492
 
493
  sprintf(error, "%s%s", msg1,msg2);
494
  if (prefs.xml_flag)
495
    write_xmlerror(prefs.xml_name, error);
496
 
497
/* Also close existing catalog */
498
  endcat(error);
499
 
500
  end_xml();
501
 
502
  return;
503
  }
504