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

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	Part of:	SExtractor
*
*	Author:		E.BERTIN (IAP)
*
*	Contents:	functions for output of catalog data.
*
12
*	Last modify:	08/07/2010
Emmanuel Bertin's avatar
Emmanuel Bertin committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
64
65
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
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
#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	"param.h"
#include	"sexhead.h"
#include	"sexhead1.h"
#include	"sexheadsc.h"
#include	"xml.h"

catstruct	*fitscat;
tabstruct	*objtab = NULL;
FILE		*ascfile;
char		*buf;
int		catopen_flag = 0;

/******************************* readcatparams *******************************/
/*
Read the catalog config file
*/
void	readcatparams(char *filename)
  {
   keystruct	*key;
   FILE		*infile;
   char		str[MAXCHAR], *keyword, *sstr;
   int		i, size;

/* Prepare the OBJECTS tables*/
  objtab = new_tab("OBJECTS");

  if ((infile = fopen(filename,"r")) == NULL)
    error(EXIT_FAILURE, "*ERROR*: can't read ", filename);

/* Scan the catalog config file*/
  thecat.nparam = 0;
  while (fgets(str, MAXCHAR, infile))
    {
    sstr = str + strspn(str," \t");
    if (*sstr!=(char)'#' && *sstr!=(char)'\n')
      {
      keyword = strtok(sstr, " \t{[(\n\r");
      if (keyword &&
	(i = findkey(keyword,(char *)objkey,sizeof(keystruct)))!=RETURN_ERROR)
        {
        key = objkey+i;
        add_key(key, objtab, 0);
        *((char *)key->ptr) = (char)'\1';
        thecat.nparam++;
        if (key->naxis)
          {
          for (i=0; i<key->naxis; i++)
            key->naxisn[i] = 1;
          size=t_size[key->ttype];
          for (i=0; (sstr = strtok(NULL, " \t,;.)]}\r")) && *sstr!=(char)'#'
		&& *sstr!=(char)'\n'; i++)
            {
            if (i>=key->naxis)
              error(EXIT_FAILURE, "*Error*: too many dimensions for keyword ",
		keyword);
            if (!(size*=(key->naxisn[i]=atoi(sstr))))
              error(EXIT_FAILURE, "*Error*: wrong array syntax for keyword ",
		keyword);
            }
          key->nbytes = size;
          }
        }
      else
        warning(keyword, " catalog parameter unknown");
      }
    }

  fclose(infile);

/* Now we copy the flags to the proper structures */

  flagobj = outobj;
  flagobj2 = outobj2;
/* Differentiate between outobj and outobj2 vectors */
  memset(&outobj2, 0, sizeof(outobj2));
  updateparamflags();

Emmanuel Bertin's avatar
Emmanuel Bertin committed
106
107
108
109
110
111
112
113
114
115
116
117
118
  return;
  }


/******************************* alloccatparams ******************************/
/*
Allocate memory for parameter arrays
*/
void	alloccatparams(void)
  {
   keystruct	*key;
   int		i;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Go back to multi-dimensional arrays for memory allocation */
  if (thecat.nparam)
    for (i=objtab->nkey, key=objtab->key; i--; key = key->nextkey) 
      if (key->naxis)
        {
/*------ Only outobj2 vectors are dynamic */
        if (!*((char **)key->ptr))
          {
          QMALLOC(*((char **)key->ptr), char, key->nbytes);
          key->ptr = *((char **)key->ptr);
          key->allocflag = 1;
          }
        }

  return;
  }

Emmanuel Bertin's avatar
Emmanuel Bertin committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*************************** changecatparamarrays ****************************/
/*
Change parameter array dimensions
*/
void	changecatparamarrays(char *keyword, int *axisn, int naxis)
  {
   keystruct	*key;
   int		d,i, size;

  if (thecat.nparam)
    for (i=objtab->nkey, key=objtab->key; i--; key = key->nextkey) 
      if (key->naxis && !strcmp(keyword, key->name))
        {
        size = t_size[key->ttype];
        if (key->naxis != naxis)
          key->naxis = naxis;
        for (d=0; d<naxis; d++)
          size *= (key->naxisn[d]=axisn[d]);
        key->nbytes = size;
        break;
        }

  return;
  }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
160
161
162
163
164
165
166
167
168
169

/***************************** updateparamflags ******************************/
/*
Update parameter flags according to their mutual dependencies.
*/
void	updateparamflags()

  {
   int	i;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
170
171
172
173
174
175
176
177
/*----------------------------- Model-fitting -----------------------------*/

  prefs.pattern_flag |= FLAG(obj2.prof_disk_patternvector)
			| FLAG(obj2.prof_disk_patternmodvector)
			| FLAG(obj2.prof_disk_patternargvector)
			| FLAG(obj2.prof_disk_patternspiral);
  FLAG(obj2.prof_disk_scale) |= prefs.pattern_flag;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
178
  FLAG(obj2.prof_concentration) |= FLAG(obj2.prof_concentrationerr);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  FLAG(obj2.poserraw_prof) |= FLAG(obj2.poserrbw_prof);
  FLAG(obj2.poserrcxxw_prof) |= FLAG(obj2.poserrcyyw_prof)
			| FLAG(obj2.poserrcxyw_prof);
  FLAG(obj2.poserrthetas_prof) |= FLAG(obj2.poserrtheta1950_prof)
				| FLAG(obj2.poserrtheta2000_prof);
  FLAG(obj2.poserrthetaw_prof) |= FLAG(obj2.poserrthetas_prof);

  FLAG(obj2.poserrmx2w_prof) |= FLAG(obj2.poserrmy2w_prof)
			| FLAG(obj2.poserrmxyw_prof)
			| FLAG(obj2.poserrthetaw_prof)|FLAG(obj2.poserraw_prof)
			| FLAG(obj2.poserrcxxw_prof);

  FLAG(obj2.poserra_prof) |= FLAG(obj2.poserrb_prof)
			| FLAG(obj2.poserrtheta_prof)
			| FLAG(obj2.poserraw_prof);
  FLAG(obj2.poserrcxx_prof) |= FLAG(obj2.poserrcyy_prof)
			| FLAG(obj2.poserrcxy_prof);
196
197
198
  FLAG(obj2.poserrmx2_prof) |= FLAG(obj2.poserrmy2_prof)
			| FLAG(obj2.poserrmxy_prof)
			| FLAG(obj2.poserrmx2w_prof);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
199
200
201
202
203
204
205
206
207
  FLAG(obj2.alpha1950_prof) |= FLAG(obj2.delta1950_prof)
			| FLAG(obj2.poserrtheta1950_prof);
  FLAG(obj2.alpha2000_prof) |= FLAG(obj2.delta2000_prof)
			| FLAG(obj2.alpha1950_prof)
			| FLAG(obj2.poserrtheta2000_prof);
  FLAG(obj2.alphas_prof) |= FLAG(obj2.deltas_prof)
			| FLAG(obj2.alpha2000_prof);
  FLAG(obj2.xw_prof) |= FLAG(obj2.yw_prof)
			| FLAG(obj2.alphas_prof);
208
  FLAG(obj2.xf_prof) |= FLAG(obj2.yf_prof);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
209
210
  FLAG(obj2.x_prof) |= FLAG(obj2.y_prof)
			| FLAG(obj2.xw_prof)
211
			| FLAG(obj2.xf_prof)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
212
			| FLAG(obj2.poserra_prof)
213
			| FLAG(obj2.poserrcxx_prof)
214
			| FLAG(obj2.poserrmx2_prof)
215
216
			| FLAG(obj2.prof_concentration)
			| FLAG(obj2.prof_class_star);
217

Emmanuel Bertin's avatar
Emmanuel Bertin committed
218
219
  FLAG(obj2.fluxerr_prof) |= FLAG(obj2.magerr_prof)
			| FLAG(obj2.prof_concentrationerr);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  FLAG(obj2.flux_prof) |= FLAG(obj2.mag_prof) | FLAG(obj2.fluxerr_prof);
  FLAG(obj2.prof_spheroid_mag) |= FLAG(obj2.prof_spheroid_magerr);
  FLAG(obj2.prof_spheroid_reff) |= FLAG(obj2.prof_spheroid_refferr);
  FLAG(obj2.prof_spheroid_aspect) |= FLAG(obj2.prof_spheroid_aspecterr);
  FLAG(obj2.prof_spheroid_theta) |= FLAG(obj2.prof_spheroid_thetaerr);
  FLAG(obj2.prof_spheroid_sersicn) |= FLAG(obj2.prof_spheroid_sersicnerr);
  FLAG(obj2.prof_disk_mag) |= FLAG(obj2.prof_disk_magerr);
  FLAG(obj2.prof_disk_scale) |= FLAG(obj2.prof_disk_scaleerr);
  FLAG(obj2.prof_disk_aspect) |= FLAG(obj2.prof_disk_aspecterr);
  FLAG(obj2.prof_disk_inclination) |= FLAG(obj2.prof_disk_inclinationerr);
  FLAG(obj2.prof_disk_theta) |= FLAG(obj2.prof_disk_thetaerr);
  FLAG(obj2.prof_bar_mag) |= FLAG(obj2.prof_bar_magerr);
  FLAG(obj2.prof_bar_length) |= FLAG(obj2.prof_bar_lengtherr);
  FLAG(obj2.prof_bar_aspect) |= FLAG(obj2.prof_bar_aspecterr);
  FLAG(obj2.prof_bar_theta) |= FLAG(obj2.prof_bar_thetaerr);
  FLAG(obj2.prof_arms_mag) |= FLAG(obj2.prof_arms_magerr);
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  FLAG(obj2.prof_e1w) |= FLAG(obj2.prof_e2w)
			| FLAG(obj2.prof_pol1w) | FLAG(obj2.prof_pol2w);
  FLAG(obj2.prof_aw) |= FLAG(obj2.prof_bw);
  FLAG(obj2.prof_cxxw) |= FLAG(obj2.prof_cyyw) | FLAG(obj2.prof_cxyw);
  FLAG(obj2.prof_thetas) |= FLAG(obj2.prof_theta1950)
			| FLAG(obj2.prof_theta2000);
  FLAG(obj2.prof_thetaw) |= FLAG(obj2.prof_thetas);

  FLAG(obj2.prof_mx2w) |= FLAG(obj2.prof_my2w)
			| FLAG(obj2.prof_mxyw)
			| FLAG(obj2.prof_thetaw) | FLAG(obj2.prof_aw)
			| FLAG(obj2.prof_cxxw)
			| FLAG(obj2.prof_e1w);

  FLAG(obj2.dtheta1950) |= FLAG(obj2.prof_theta1950)
			| FLAG(obj2.prof_spheroid_theta1950)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
252
253
254
255
256
257
258
259
260
			| FLAG(obj2.prof_disk_theta1950)
//			| FLAG(obj2.prof_arms_theta1950)
			| FLAG(obj2.prof_bar_theta1950)
			| FLAG(obj2.poserrtheta1950_psf)
			| FLAG(obj2.win_theta1950)
			| FLAG(obj2.winposerr_theta1950)
			| FLAG(obj2.poserr_theta1950)
			| FLAG(obj2.theta1950)
			| FLAG(obj2.poserrtheta1950_prof);
261
262
  FLAG(obj2.dtheta2000) |= FLAG(obj2.prof_theta2000)
			| FLAG(obj2.prof_spheroid_theta2000)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
298
299
300
301
302
303
			| FLAG(obj2.prof_disk_theta2000)
//			| FLAG(obj2.prof_arms_theta2000)
			| FLAG(obj2.prof_bar_theta2000)
			| FLAG(obj2.poserrtheta2000_psf)
			| FLAG(obj2.win_theta2000)
			| FLAG(obj2.winposerr_theta2000)
			| FLAG(obj2.poserr_theta2000)
			| FLAG(obj2.theta2000)
			| FLAG(obj2.poserrtheta2000_prof);


  FLAG(obj2.prof_spheroid_thetas) |= FLAG(obj2.prof_spheroid_theta2000)
			| FLAG(obj2.prof_spheroid_theta1950);
  FLAG(obj2.prof_spheroid_thetaw) |= FLAG(obj2.prof_spheroid_thetas);
  FLAG(obj2.prof_disk_thetas) |= FLAG(obj2.prof_disk_theta2000)
			| FLAG(obj2.prof_disk_theta1950);
  FLAG(obj2.prof_disk_thetaw) |= FLAG(obj2.prof_disk_thetas);
  FLAG(obj2.prof_bar_thetas) |= FLAG(obj2.prof_bar_theta2000)
			| FLAG(obj2.prof_bar_theta1950);
  FLAG(obj2.prof_bar_thetaw) |= FLAG(obj2.prof_bar_thetas);
/*
  FLAG(obj2.prof_arms_thetaw) |= FLAG(obj2.prof_arms_thetas)
			| FLAG(obj2.prof_arms_theta2000)
			| FLAG(obj2.prof_arms_theta1950);
*/
  FLAG(obj2.prof_arms_scalew) |= FLAG(obj2.prof_arms_startw)
			| FLAG(obj2.prof_arms_scaleerrw)
			| FLAG(obj2.prof_arms_starterrw);

  FLAG(obj2.prof_bar_lengthw) |= FLAG(obj2.prof_bar_aspectw)
			| FLAG(obj2.prof_bar_thetaw)
			| FLAG(obj2.prof_bar_lengtherrw)
			| FLAG(obj2.prof_bar_aspecterrw)
			| FLAG(obj2.prof_bar_thetaerrw);

  FLAG(obj2.prof_disk_scalew) |= FLAG(obj2.prof_disk_aspectw)
			| FLAG(obj2.prof_disk_thetaw)
			| FLAG(obj2.prof_disk_scaleerrw)
			| FLAG(obj2.prof_disk_aspecterrw)
			| FLAG(obj2.prof_disk_thetaerrw)
			| FLAG(obj2.prof_arms_scalew);
304
  FLAG(obj2.prof_disk_fluxmean) |= FLAG(obj2.prof_disk_mumean);
305
306
  FLAG(obj2.prof_disk_fluxeff) |= FLAG(obj2.prof_disk_mueff);
  FLAG(obj2.prof_disk_peak) |= FLAG(obj2.prof_disk_mumax)
307
308
				| FLAG(obj2.prof_disk_fluxeff)
				| FLAG(obj2.prof_disk_fluxmean);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
309
310
311
312
313
314
315

  FLAG(obj2.prof_spheroid_reffw) |= FLAG(obj2.prof_spheroid_aspectw)
			| FLAG(obj2.prof_spheroid_thetaw)
			| FLAG(obj2.prof_spheroid_refferrw)
			| FLAG(obj2.prof_spheroid_aspecterrw)
			| FLAG(obj2.prof_spheroid_thetaerrw);

316
317
318
319
 FLAG(obj2.prof_mx2w) |= FLAG(obj2.prof_my2w) | FLAG(obj2.prof_mxyw)
			| FLAG(obj2.prof_e1w) |FLAG(obj2.prof_e2w)
			| FLAG(obj2.prof_pol1w) |FLAG(obj2.prof_pol2w);

320
  FLAG(obj2.prof_spheroid_fluxmean) |= FLAG(obj2.prof_spheroid_mumean);
321
322
  FLAG(obj2.prof_spheroid_fluxeff) |= FLAG(obj2.prof_spheroid_mueff);
  FLAG(obj2.prof_spheroid_peak) |= FLAG(obj2.prof_spheroid_mumax)
323
324
				| FLAG(obj2.prof_spheroid_fluxeff)
				| FLAG(obj2.prof_spheroid_fluxmean);
325

Emmanuel Bertin's avatar
Emmanuel Bertin committed
326
327
328
  FLAG(obj2.prof_flagw) |= FLAG(obj2.prof_spheroid_reffw)
			| FLAG(obj2.prof_disk_scalew)
			| FLAG(obj2.prof_bar_lengthw)
329
330
			| FLAG(obj2.prof_arms_scalew)
			| FLAG(obj2.prof_mx2w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
331
			
332
333
334
335
336
337
338
  FLAG(obj2.prof_e1) |= FLAG(obj2.prof_e2)
			| FLAG(obj2.prof_pol1) | FLAG(obj2.prof_pol2)
			| FLAG(obj2.prof_e1w);
  FLAG(obj2.prof_a) |= FLAG(obj2.prof_b) | FLAG(obj2.prof_theta)
			| FLAG(obj2.prof_aw);
  FLAG(obj2.prof_cxx) |= FLAG(obj2.prof_cyy)
			| FLAG(obj2.prof_cxy) | FLAG(obj2.prof_cxxw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
339
  FLAG(obj2.prof_mx2) |= FLAG(obj2.prof_my2) | FLAG(obj2.prof_mxy)
340
341
342
			| FLAG(obj2.prof_e1)
			| FLAG(obj2.prof_a) | FLAG(obj2.prof_cxx)
			| FLAG(obj2.prof_mx2w);
343
344
345
  FLAG(obj2.fluxmean_prof) |= FLAG(obj2.mumean_prof);
  FLAG(obj2.fluxeff_prof) |= FLAG(obj2.mueff_prof)
			| FLAG(obj2.fluxmean_prof);
346
  FLAG(obj2.peak_prof) |= FLAG(obj2.mumax_prof);
347

Emmanuel Bertin's avatar
Emmanuel Bertin committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  FLAG(obj2.prof_arms_flux) |= FLAG(obj2.prof_arms_fluxerr)
			| FLAG(obj2.prof_arms_mag)
			| FLAG(obj2.prof_arms_scalew)
			| FLAG(obj2.prof_arms_scale)
			| FLAG(obj2.prof_arms_posang)
			| FLAG(obj2.prof_arms_pitch)
			| FLAG(obj2.prof_arms_start)
			| FLAG(obj2.prof_arms_quadfrac);
  FLAG(obj2.prof_bar_theta) |= FLAG(obj2.prof_bar_lengthw);
  FLAG(obj2.prof_bar_flux) |= FLAG(obj2.prof_bar_fluxerr)
			| FLAG(obj2.prof_bar_mag)
			| FLAG(obj2.prof_bar_lengthw)
			| FLAG(obj2.prof_bar_length)
			| FLAG(obj2.prof_bar_aspect)
			| FLAG(obj2.prof_bar_posang)
			| FLAG(obj2.prof_bar_theta)
			| FLAG(obj2.prof_arms_flux);
  FLAG(obj2.prof_disk_flux) |= FLAG(obj2.prof_disk_fluxerr)
			| FLAG(obj2.prof_disk_mag)
			| FLAG(obj2.prof_disk_scalew)
			| FLAG(obj2.prof_disk_scale)
			| FLAG(obj2.prof_disk_aspect)
			| FLAG(obj2.prof_disk_inclination)
			| FLAG(obj2.prof_disk_theta)
372
			| FLAG(obj2.prof_disk_peak)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
373
374
375
376
377
378
379
			| FLAG(obj2.prof_bar_flux);
  FLAG(obj2.prof_spheroid_flux) |= FLAG(obj2.prof_spheroid_fluxerr)
			| FLAG(obj2.prof_spheroid_mag)
			| FLAG(obj2.prof_spheroid_reffw)
			| FLAG(obj2.prof_spheroid_reff)
			| FLAG(obj2.prof_spheroid_aspect)
			| FLAG(obj2.prof_spheroid_theta)
380
381
			| FLAG(obj2.prof_spheroid_sersicn)
			| FLAG(obj2.prof_spheroid_peak);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
382
383
  prefs.prof_flag |= FLAG(obj2.prof_chi2) | FLAG(obj2.prof_niter)
			| FLAG(obj2.prof_vector) | FLAG(obj2.prof_errvector)
384
			| FLAG(obj2.prof_errmatrix)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
385
			| FLAG(obj2.x_prof) | FLAG(obj2.y_prof)
386
387
			| FLAG(obj2.prof_flag)
			| FLAG(obj2.flux_prof)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
388
			| FLAG(obj2.prof_mx2)
389
			| FLAG(obj2.peak_prof)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
390
391
392
393
394
395
396
397
398
399
400
401
402
			| FLAG(obj2.prof_disk_flux)
			| FLAG(obj2.prof_spheroid_flux);


/* If only global parameters are requested, fit a Sersic model */
  if (prefs.prof_flag && !(FLAG(obj2.prof_spheroid_flux)
			| FLAG(obj2.prof_disk_flux)))
    {
    FLAG(obj2.prof_spheroid_flux) |= prefs.prof_flag;
    FLAG(obj2.prof_spheroid_sersicn) |= prefs.prof_flag;
    }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
445
446
447
448
449
450
451
452
/*------------------------------ Astrometry ---------------------------------*/
  FLAG(obj2.win_aw) |= FLAG(obj2.win_bw) | FLAG(obj2.win_polarw);
  FLAG(obj2.win_cxxw) |= FLAG(obj2.win_cyyw) | FLAG(obj2.win_cxyw);
  FLAG(obj2.win_thetas) |= FLAG(obj2.win_theta1950)
			| FLAG(obj2.win_theta2000);
  FLAG(obj2.win_thetaw) |= FLAG(obj2.win_thetas);

  FLAG(obj2.win_mx2w) |= FLAG(obj2.win_my2w)
			| FLAG(obj2.win_mxyw)
			| FLAG(obj2.win_thetaw) | FLAG(obj2.win_aw)
			| FLAG(obj2.win_cxxw);

  FLAG(obj2.win_a) |= FLAG(obj2.win_b) | FLAG(obj2.win_theta)
			| FLAG(obj2.win_polar) | FLAG(obj2.win_aw);
  FLAG(obj2.win_cxx) |= FLAG(obj2.win_cyy)
			| FLAG(obj2.win_cxy) | FLAG(obj2.win_cxxw);
  FLAG(obj2.win_mx2) |= FLAG(obj2.win_my2)
			| FLAG(obj2.win_mxy)
			| FLAG(obj2.win_a) | FLAG(obj2.win_cxx)
			| FLAG(obj2.win_mx2w);

  FLAG(obj2.winposerr_aw) |= FLAG(obj2.winposerr_bw);
  FLAG(obj2.winposerr_cxxw) |= FLAG(obj2.winposerr_cyyw)
			| FLAG(obj2.winposerr_cxyw);
  FLAG(obj2.winposerr_thetas) |= FLAG(obj2.winposerr_theta1950)
			| FLAG(obj2.winposerr_theta2000);
  FLAG(obj2.winposerr_thetaw) |= FLAG(obj2.winposerr_thetas);

  FLAG(obj2.winposerr_mx2w) |= FLAG(obj2.winposerr_my2w)
			| FLAG(obj2.winposerr_mxyw)
			| FLAG(obj2.winposerr_thetaw) | FLAG(obj2.winposerr_aw)
			| FLAG(obj2.winposerr_cxxw);

  FLAG(obj2.winposerr_a) |= FLAG(obj2.winposerr_b) | FLAG(obj2.winposerr_theta);
  FLAG(obj2.winposerr_cxx) |= FLAG(obj2.winposerr_cyy)
			| FLAG(obj2.winposerr_cxy);
  FLAG(obj2.winposerr_mx2) |= FLAG(obj2.winposerr_my2)
			| FLAG(obj2.winposerr_mxy)
			| FLAG(obj2.winposerr_a) | FLAG(obj2.winposerr_cxx)
			| FLAG(obj2.winposerr_mx2w);

  FLAG(obj2.winpos_alpha1950) |= FLAG(obj2.winpos_delta1950)
			| FLAG(obj2.win_theta1950)
			| FLAG(obj2.winposerr_theta1950);
  FLAG(obj2.winpos_alpha2000) |= FLAG(obj2.winpos_delta2000)
			| FLAG(obj2.winpos_alpha1950)
			| FLAG(obj2.win_theta2000)
			| FLAG(obj2.winposerr_theta2000);
  FLAG(obj2.winpos_alphas) |= FLAG(obj2.winpos_deltas)
			| FLAG(obj2.winpos_alpha2000);
453
  FLAG(obj2.winpos_xf) |= FLAG(obj2.winpos_yf);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  FLAG(obj2.winpos_xw) |= FLAG(obj2.winpos_yw)
			| FLAG(obj2.winpos_alphas);

  FLAG(obj2.poserr_aw) |= FLAG(obj2.poserr_bw);
  FLAG(obj2.poserr_cxxw) |= FLAG(obj2.poserr_cyyw) | FLAG(obj2.poserr_cxyw);
  FLAG(obj2.poserr_thetas) |= FLAG(obj2.poserr_theta1950)
				| FLAG(obj2.poserr_theta2000);
  FLAG(obj2.poserr_thetaw) |= FLAG(obj2.poserr_thetas);

  FLAG(obj2.poserr_mx2w) |= FLAG(obj2.poserr_my2w) | FLAG(obj2.poserr_mxyw)
			| FLAG(obj2.poserr_thetaw) | FLAG(obj2.poserr_aw)
			| FLAG(obj2.poserr_cxxw);

  FLAG(obj2.poserr_a) |= FLAG(obj2.poserr_b) | FLAG(obj2.poserr_theta)
			| FLAG(obj2.winposerr_a);
  FLAG(obj2.poserr_cxx) |= FLAG(obj2.poserr_cyy) | FLAG(obj2.poserr_cxy);
  FLAG(obj.poserr_mx2) |= FLAG(obj.poserr_my2) | FLAG(obj.poserr_mxy)
			| FLAG(obj2.poserr_a) | FLAG(obj2.poserr_cxx)
			| FLAG(obj2.poserr_mx2w) | FLAG(obj2.winposerr_mx2);

  FLAG(obj2.peakalpha1950) |= FLAG(obj2.peakdelta1950);
  FLAG(obj2.alpha1950) |= FLAG(obj2.delta1950) |  FLAG(obj2.theta1950)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
476
477
			| FLAG(obj2.poserr_theta1950) | FLAG(obj2.dtheta1950);
;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
478
479
480
  FLAG(obj2.peakalpha2000) |= FLAG(obj2.peakdelta2000)
			| FLAG(obj2.peakalpha1950);
  FLAG(obj2.alpha2000) |= FLAG(obj2.delta2000) | FLAG(obj2.alpha1950)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
481
			| FLAG(obj2.dtheta2000);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
482
483
484
485
486
487
488
489
  FLAG(obj2.peakalphas) |= FLAG(obj2.peakdeltas) | FLAG(obj2.peakalpha2000);
  FLAG(obj2.alphas) |= FLAG(obj2.deltas) | FLAG(obj2.alpha2000);
  FLAG(obj2.thetas) |= FLAG(obj2.theta1950) | FLAG(obj2.theta2000);
  FLAG(obj2.thetaw) |= FLAG(obj2.thetas);
  FLAG(obj2.aw) |= FLAG(obj2.bw) | FLAG(obj2.polarw);
  FLAG(obj2.cxxw) |= FLAG(obj2.cyyw) | FLAG(obj2.cxyw);

  FLAG(obj2.mx2w) |= FLAG(obj2.my2w) | FLAG(obj2.mxyw)
490
			| FLAG(obj2.thetaw) | FLAG(obj2.aw) | FLAG(obj2.cxxw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
491
  
492
  FLAG(obj2.peakxw) |= FLAG(obj2.peakyf);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
493
  FLAG(obj2.peakxw) |= FLAG(obj2.peakyw) | FLAG(obj2.peakalphas);
494
495
496
  FLAG(obj.peakx) |= FLAG(obj.peaky) | FLAG(obj2.peakxw) | FLAG(obj2.peakxf);

  FLAG(obj2.mxf) |= FLAG(obj2.myf);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
497
498
499
500
501
502
503
504

  FLAG(obj2.mxw) |= FLAG(obj2.myw) | FLAG(obj2.mx2w) | FLAG(obj2.alphas)
		| FLAG(obj2.poserr_mx2w);
  FLAG(obj2.mamaposx) |= FLAG(obj2.mamaposy);
  FLAG(obj2.flux_win) |= FLAG(obj2.mag_win)|FLAG(obj2.magerr_win)
			    | FLAG(obj2.flux_win) | FLAG(obj2.fluxerr_win);
  FLAG(obj2.winpos_x) |= FLAG(obj2.winpos_y)
			| FLAG(obj2.winposerr_mx2) | FLAG(obj2.win_mx2)
505
506
			| FLAG(obj2.winpos_xw) | FLAG(obj2.winpos_xf)
			| FLAG(obj2.win_flag)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
507
508
			| FLAG(obj2.flux_win) |FLAG(obj2.winpos_niter);

509
510
511
512
  FLAG(obj2.area_flagw) |= FLAG(obj2.npixw) | FLAG(obj2.fdnpixw)
			| FLAG(obj2.fwhmw)
			| FLAG(obj2.maxmu) | FLAG(obj2.threshmu)
			| FLAG(obj2.prof_spheroid_mumax)
513
514
			| FLAG(obj2.prof_disk_mumax)
			| FLAG(obj2.mumax_prof);
515

Emmanuel Bertin's avatar
Emmanuel Bertin committed
516
517
518
519
520
521
/*------------------------------ Photometry ---------------------------------*/

  FLAG(obj2.fluxerr_best) |= FLAG(obj2.magerr_best);

  FLAG(obj2.flux_best) |= FLAG(obj2.mag_best) | FLAG(obj2.fluxerr_best);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
522
  FLAG(obj2.hl_radius) |= FLAG(obj2.winpos_x) | prefs.prof_flag;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605

  FLAG(obj2.flux_auto)  |= FLAG(obj2.mag_auto) | FLAG(obj2.magerr_auto)
			| FLAG(obj2.fluxerr_auto)
			| FLAG(obj2.kronfactor)
			| FLAG(obj2.flux_best)
			| FLAG(obj2.flux_radius)
			| FLAG(obj2.hl_radius);
  FLAG(obj2.flux_petro) |= FLAG(obj2.mag_petro) | FLAG(obj2.magerr_petro)
			| FLAG(obj2.fluxerr_petro)
			| FLAG(obj2.petrofactor);

  FLAG(obj2.fluxerr_isocor) |= FLAG(obj2.magerr_isocor)
				| FLAG(obj2.fluxerr_best);

  FLAG(obj2.flux_isocor) |= FLAG(obj2.mag_isocor) | FLAG(obj2.fluxerr_isocor)
			 | FLAG(obj2.flux_best);

  FLAG(obj2.flux_aper) |= FLAG(obj2.mag_aper)|FLAG(obj2.magerr_aper)
			    | FLAG(obj2.fluxerr_aper);

  FLAG(obj2.flux_galfit) |= FLAG(obj2.mag_galfit) | FLAG(obj2.magerr_galfit)
			    | FLAG(obj2.fluxerr_galfit);

/*---------------------------- External flags -------------------------------*/
  VECFLAG(obj.imaflag) |= VECFLAG(obj.imanflag);

/*------------------------------ PSF-fitting --------------------------------*/
  FLAG(obj2.poserraw_psf) |= FLAG(obj2.poserrbw_psf);
  FLAG(obj2.poserrcxxw_psf) |= FLAG(obj2.poserrcyyw_psf)
			| FLAG(obj2.poserrcxyw_psf);
  FLAG(obj2.poserrthetas_psf) |= FLAG(obj2.poserrtheta1950_psf)
				| FLAG(obj2.poserrtheta2000_psf);
  FLAG(obj2.poserrthetaw_psf) |= FLAG(obj2.poserrthetas_psf);

  FLAG(obj2.poserrmx2w_psf) |= FLAG(obj2.poserrmy2w_psf)
			| FLAG(obj2.poserrmxyw_psf)
			| FLAG(obj2.poserrthetaw_psf) | FLAG(obj2.poserraw_psf)
			| FLAG(obj2.poserrcxxw_psf);

  FLAG(obj2.poserra_psf) |= FLAG(obj2.poserrb_psf)
			| FLAG(obj2.poserrtheta_psf);
  FLAG(obj2.poserrcxx_psf) |= FLAG(obj2.poserrcyy_psf)
			| FLAG(obj2.poserrcxy_psf);
  FLAG(obj2.poserrmx2_psf) |= FLAG(obj2.poserrmy2_psf)
			| FLAG(obj2.poserrmxy_psf)
			| FLAG(obj2.poserra_psf) | FLAG(obj2.poserrcxx_psf)
			| FLAG(obj2.poserrmx2w_psf);

  FLAG(obj2.alpha1950_psf) |= FLAG(obj2.delta1950_psf)
			| FLAG(obj2.poserrtheta1950_psf);
  FLAG(obj2.alpha2000_psf) |= FLAG(obj2.delta2000_psf)
			| FLAG(obj2.alpha1950_psf)
			| FLAG(obj2.poserrtheta2000_psf);
  FLAG(obj2.alphas_psf) |= FLAG(obj2.deltas_psf) | FLAG(obj2.alpha2000_psf);

  FLAG(obj2.xw_psf) |= FLAG(obj2.yw_psf) | FLAG(obj2.poserrmx2w_psf)
			| FLAG(obj2.alphas_psf);

  FLAG(obj2.fluxerr_psf) |= FLAG(obj2.poserrmx2_psf) | FLAG(obj2.magerr_psf);

  FLAG(obj2.mx2_pc) |= FLAG(obj2.my2_pc) | FLAG(obj2.mxy_pc)
			| FLAG(obj2.a_pc) | FLAG(obj2.b_pc)
			| FLAG(obj2.theta_pc) | FLAG(obj2.vector_pc)
			| FLAG(obj2.gdposang) | FLAG(obj2.gdscale)
			| FLAG(obj2.gdaspect) | FLAG(obj2.flux_galfit)
			| FLAG(obj2.gde1) | FLAG(obj2.gde2)
			| FLAG(obj2.gbposang) | FLAG(obj2.gbscale)
			| FLAG(obj2.gbaspect) | FLAG(obj2.gbratio);

  FLAG(obj2.flux_psf) |= FLAG(obj2.mag_psf) | FLAG(obj2.x_psf)
			| FLAG(obj2.y_psf) | FLAG(obj2.xw_psf)
			| FLAG(obj2.fluxerr_psf)
			| FLAG(obj2.niter_psf)
			| FLAG(obj2.chi2_psf)
			| FLAG(obj2.mx2_pc);

/*-------------------------------- Others -----------------------------------*/
  FLAG(obj.fwhm) |= FLAG(obj2.fwhmw);

  FLAG(obj.iso[0]) |= FLAG(obj2.sprob);
  for (i=0; i<NISO; i++)
    FLAG(obj.iso[0]) |= FLAG(obj.iso[i]);

606
607
  FLAG(obj.wflag) |= FLAG(obj.nzwpix) | FLAG(obj.nzdwpix);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
608
609
610
611
  return; 
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
612
613
614
615
616
617
618
619
620
621
/********************************** dumpparams *******************************/
/*
Initialize the catalog header
*/
void	dumpparams(void)
  {
   int		i;

  for (i=0; *objkey[i].name ; i++)
    if (*objkey[i].unit)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
622
      printf("#%-24.24s %-57.57s [%s]\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
623
624
		objkey[i].name, objkey[i].comment, objkey[i].unit);
    else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
625
      printf("#%-24.24s %-57.57s\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
626
627
628
629
630
631
		objkey[i].name, objkey[i].comment);

  return;
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/********************************** initcat **********************************/
/*
Initialize the catalog header
*/
void	initcat(void)
  {
   keystruct	*key;
   int		i, n;

  if (prefs.cat_type == CAT_NONE)
    return;

  update_tab(objtab);
  if (prefs.cat_type == ASCII_HEAD || prefs.cat_type == ASCII ||
	prefs.cat_type == ASCII_SKYCAT || prefs.cat_type == ASCII_VO)
    {
    if (prefs.pipe_flag)
      ascfile = stdout;
    else
      if (!(ascfile = fopen(prefs.cat_name, "w+")))
        error(EXIT_FAILURE,"*Error*: cannot open ", prefs.cat_name);
653
//  setlinebuf(ascfile);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
654
655
656
657
    if (prefs.cat_type == ASCII_HEAD && (key = objtab->key))
      for (i=0,n=1; i++<objtab->nkey; key=key->nextkey)
        {
        if (*key->unit)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
658
          fprintf(ascfile, "# %3d %-22.22s %-58.58s [%s]\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
659
660
		n, key->name,key->comment, key->unit);
        else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
661
          fprintf(ascfile, "# %3d %-22.22s %-58.58s\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
		n, key->name,key->comment);
        n += key->nbytes/t_size[key->ttype];
        }
    else if (prefs.cat_type == ASCII_SKYCAT && (key = objtab->key))
      {
      if (objtab->nkey<3)
        error(EXIT_FAILURE,"The SkyCat format requires at least 4 parameters:",
	      " Id Ra Dec Mag");
/*--- We add a tab between rows, as required by Skycat */
      fprintf(ascfile, skycathead, 8.0);
      for (i=1,key=key->nextkey; i++<objtab->nkey; key=key->nextkey)
        {
        if (i>4)
          fprintf(ascfile, "\t%s", key->name);
        sprintf(gstr, "\t%s", key->printf);
        strcpy(key->printf, gstr);
        }
      fprintf(ascfile, "\n------------------\n");
      }
    else if (prefs.cat_type == ASCII_VO && objtab->key) 
      {
      write_xml_header(ascfile);
      write_vo_fields(ascfile);
      fprintf(ascfile, "   <DATA><TABLEDATA>\n");
      }
    }
  else
    {
    fitscat = new_cat(1);
    init_cat(fitscat);
    strcpy(fitscat->filename, prefs.cat_name);
    if (open_cat(fitscat, WRITE_ONLY) != RETURN_OK)
      error(EXIT_FAILURE,"*Error*: cannot open for writing ",prefs.cat_name);
    switch(prefs.cat_type)
      {
      case FITS_LDAC:
      case FITS_TPX:
/*------ Save a "pure" primary HDU */
        save_tab(fitscat, fitscat->tab);
        break;

      case FITS_10:
/*------ Add to the primary HDU extraction parameters */
        key = headkey1;
        while (*key->name)
          addkeyto_head(fitscat->tab, key++);
        save_tab(fitscat, fitscat->tab);
        break;
      default:
        error (EXIT_FAILURE, "*Internal Error*: Unknown FITS type in ",
		"initcat()");
      }
    }

  catopen_flag = 1;

  return;
  }


/****** write_vo_fields *******************************************************
PROTO	int	write_vo_fields(FILE *file)
PURPOSE	Write the list of columns to an XML-VOTable file or stream
INPUT	Pointer to the output file (or stream).
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
VERSION	14/07/2006
 ***/
void	write_vo_fields(FILE *file)
  {
   keystruct	*key;
   char		datatype[40], arraysize[40], str[40];
   int		i, d;

  if (!objtab || !objtab->key)
    return;
  key=objtab->key;
  for (i=0; i++<objtab->nkey; key=key->nextkey)
    {
/*--- indicate datatype, arraysize, width and precision attributes */
/*--- Handle multidimensional arrays */
    arraysize[0] = '\0';
    if (key->naxis>1)
      {
      for (d=0; d<key->naxis; d++)
        {
        sprintf(str, "%s%d", d?"x":" arraysize=\"", key->naxisn[d]);
        strcat(arraysize, str);
        }
      strcat(arraysize, "\"");
      }
    switch(key->ttype)
      {
      case T_BYTE:	strcpy(datatype, "unsignedByte"); break;
      case T_SHORT:	strcpy(datatype, "short"); break;
      case T_LONG:	strcpy(datatype, "int"); break;
      case T_FLOAT:	strcpy(datatype, "float"); break;
      case T_DOUBLE:	strcpy(datatype, "double"); break;
      default:		error(EXIT_FAILURE,
			"*Internal Error*: Unknown datatype in ",
			"initcat()");
      }
    fprintf(file,
	"  <FIELD name=\"%s\" ucd=\"%s\" datatype=\"%s\" unit=\"%s\"%s>\n",
	key->name, key->voucd, datatype,key->vounit, arraysize);
    fprintf(file, "   <DESCRIPTION>%s</DESCRIPTION>\n", key->comment);
    fprintf(file, "  </FIELD>\n");
    }

  return;
  }


/********************************* reinitcat *********************************/
/*
Initialize the catalog header
*/
void	reinitcat(picstruct *field)
  {
   tabstruct	*tab, *asctab;
   keystruct	*key;

  if (prefs.cat_type == CAT_NONE)
    return;

  if (prefs.cat_type != ASCII_HEAD && prefs.cat_type != ASCII &&
	prefs.cat_type != ASCII_SKYCAT && prefs.cat_type != ASCII_VO)
    {
    update_tab(objtab);
    switch(prefs.cat_type)
      {
      case FITS_LDAC:
/*------ We create a dummy table (only used through its header) */
        QCALLOC(asctab, tabstruct, 1);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
797
798
799
        asctab->headnblock = field->tab->headnblock;
        QMEMCPY(field->tab->headbuf, asctab->headbuf, char,
		asctab->headnblock*FBSIZE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
        key = headkey;
        while (*key->name)
          addkeyto_head(asctab, key++);
        tab = new_tab("LDAC_IMHEAD");
        add_tab(tab, fitscat, 0);
        key = new_key("Field Header Card");
        key->ptr = asctab->headbuf;
        asctab->headbuf = NULL;
        free_tab(asctab);
        key->htype = H_STRING;
        key->ttype = T_STRING;
        key->nobj = 1;
        key->nbytes = 80*(fitsfind(key->ptr, "END     ")+1);
        key->naxis = 2;
        QMALLOC(key->naxisn, int, key->naxis);
        key->naxisn[0] = 80;
        key->naxisn[1] = key->nbytes/80;
        add_key(key, tab, 0);
        save_tab(fitscat, tab);
        strcpy(objtab->extname, "LDAC_OBJECTS");
        break;

      case FITS_TPX:
/*------ We create a dummy table (only used through its header) */
        QCALLOC(asctab, tabstruct, 1);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
825
826
827
        asctab->headnblock = field->tab->headnblock;
        QMEMCPY(field->tab->headbuf, asctab->headbuf, char,
		asctab->headnblock*FBSIZE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
        key = headkey;
        while (*key->name)
          addkeyto_head(asctab, key++);
        tab = new_tab("TPX_IMHEAD");
        add_tab(tab, fitscat, 0);
        key = new_key("Field Header Card");
        key->ptr = asctab->headbuf;
        asctab->headbuf = NULL;
        free_tab(asctab);
        key->htype = H_STRING;
        key->ttype = T_STRING;
        key->nobj = fitsfind(key->ptr, "END     ")+1;
        key->nbytes = 80;
        key->naxis = 1;
        QMALLOC(key->naxisn, int, key->naxis);
        key->naxisn[0] = 80;
        add_key(key, tab, 0);
        save_tab(fitscat, tab);
        strcpy(objtab->extname, "TPX_OBJECTS");
        break;

      case FITS_10:
/*------ Add to the primary HDU extraction parameters */
/*
        key = headkey1;
        while (*key->name)
          addkeyto_head(fitscat->tab, key++);
        save_tab(fitscat, fitscat->tab);
*/
        break;

      default:
        error (EXIT_FAILURE, "*Internal Error*: Unknown FITS type in ",
		"reinitcat()");
      }

    objtab->cat = fitscat;
    init_writeobj(fitscat, objtab, &buf);
    }

  return;
  }


/********************************* writecat **********************************/
/*
Write out in the catalog each one object.
*/
void	writecat(int n, objliststruct *objlist)
  {
  outobj = objlist->obj[n];

  switch(prefs.cat_type)
    {
    case FITS_10:
    case FITS_LDAC:
    case FITS_TPX:
      write_obj(objtab, buf);
      break;

    case ASCII:
    case ASCII_HEAD:
    case ASCII_SKYCAT:
      print_obj(ascfile, objtab);
      break;
    case ASCII_VO:
      voprint_obj(ascfile, objtab);
      break;

    case CAT_NONE:
      break;

    default:
      error (EXIT_FAILURE, "*Internal Error*: Unknown catalog type", "");
    }

  return;
  }


/********************************** endcat ***********************************/
/*
Terminate the catalog output.
*/
void	endcat(char *error)
  {
   keystruct	*key;
   int		i;

  if (!catopen_flag)
    {
    if (prefs.cat_type == ASCII_VO)
      write_xmlerror(prefs.cat_name, error);
    return;
    }
  switch(prefs.cat_type)
    {
    case ASCII:
    case ASCII_HEAD:
      if (!prefs.pipe_flag)
        fclose(ascfile);
      break;

    case ASCII_SKYCAT:
      fprintf(ascfile, skycattail);
      if (!prefs.pipe_flag)
        fclose(ascfile);
      break;

    case ASCII_VO:
      fprintf(ascfile, "    </TABLEDATA></DATA>\n");
      fprintf(ascfile, "  </TABLE>\n");
/*---- Add configuration file meta-data */
      write_xml_meta(ascfile, error);
      fprintf(ascfile, "</RESOURCE>\n");
      fprintf(ascfile, "</VOTABLE>\n");

      if (!prefs.pipe_flag)
        fclose(ascfile);
      break;

    case FITS_LDAC:
    case FITS_TPX:
    case FITS_10:
      free_cat(&fitscat,1);
      break;

    case CAT_NONE:
      break;

    default:
      break;
    }

/* Free allocated memory for arrays */
  key = objtab->key;
  for (i=objtab->nkey; i--; key=key->nextkey)
    if (key->naxis && key->allocflag)
      free(key->ptr);

  objtab->key = NULL;
  objtab->nkey = 0;
  free_tab(objtab);
  objtab = NULL;

  return;
  }


/******************************** reendcat ***********************************/
/*
Terminate the catalog output.
*/
void	reendcat()
  {
   keystruct	*key;
   tabstruct	*tab;
   OFF_T	pos;
   char		*head;

  switch(prefs.cat_type)
    {
    case ASCII:
    case ASCII_HEAD:
    case ASCII_SKYCAT:
    case ASCII_VO:
      break;

    case FITS_LDAC:
    case FITS_TPX:
      end_writeobj(fitscat, objtab, buf);
      key = NULL;
      if (!(tab=fitscat->tab->prevtab)
	|| !(key=name_to_key(tab, "Field Header Card")))
        error(EXIT_FAILURE,"*Error*: cannot update table ", "ASCFIELD");
      head = key->ptr;
      fitswrite(head, "SEXNDET ", &thecat.ndetect,H_INT,T_LONG);
      fitswrite(head, "SEXNFIN ", &thecat.ntotal, H_INT,T_LONG);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1006
1007
      fitswrite(head, "SEXDATE ", thecat.ext_date, H_STRING, T_STRING);
      fitswrite(head, "SEXTIME ", thecat.ext_time, H_STRING, T_STRING);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
      fitswrite(head, "SEXELAPS", &thecat.ext_elapsed, H_FLOAT, T_DOUBLE);
      QFTELL(fitscat->file, pos, fitscat->filename);
      QFSEEK(fitscat->file, tab->headpos, SEEK_SET, fitscat->filename);
      save_tab(fitscat, tab);
      QFSEEK(fitscat->file, pos, SEEK_SET, fitscat->filename);
      break;

    case FITS_10:
      end_writeobj(fitscat, objtab, buf);
      fitswrite(fitscat->tab->headbuf,"SEXNDET ",&thecat.ndetect,H_INT,T_LONG);
      fitswrite(fitscat->tab->headbuf,"SEXNFIN ",&thecat.ntotal, H_INT,T_LONG);
      QFTELL(fitscat->file, pos, fitscat->filename);
      QFSEEK(fitscat->file, fitscat->tab->headpos, SEEK_SET,fitscat->filename);
      save_tab(fitscat, fitscat->tab);
      QFSEEK(fitscat->file, pos, SEEK_SET, fitscat->filename);
      break;

    case CAT_NONE:
      break;

    default:
      break;
    }

  return;
  }