catout.c 35.4 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1
/*
2
*				catout.c
Emmanuel Bertin's avatar
Emmanuel Bertin committed
3
*
4
* Functions related to catalogue output.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
5
*
6
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Emmanuel Bertin's avatar
Emmanuel Bertin committed
7
*
8
*	This file part of:	SExtractor
Emmanuel Bertin's avatar
Emmanuel Bertin committed
9
*
10
*	Copyright:		(C) 1993-2020 IAP/CNRS/SorbonneU
11
12
13
14
15
16
17
18
19
20
21
22
23
24
*
*	License:		GNU General Public License
*
*	SExtractor is free software: you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation, either version 3 of the License, or
*	(at your option) any later version.
*	SExtractor is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	You should have received a copy of the GNU General Public License
*	along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
*
25
*	Last modified:		15/07/2020
26
27
28
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

Emmanuel Bertin's avatar
Emmanuel Bertin committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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"

48
49
50
objstruct	outobj, flagobj;
obj2struct	outobj2, flagobj2;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
51
52
53
54
55
56
catstruct	*fitscat;
tabstruct	*objtab = NULL;
FILE		*ascfile;
char		*buf;
int		catopen_flag = 0;

57
58
59
double		ddummy;
int		idummy;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
/******************************* 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;
89
90
        if (add_key(key, objtab, 0) != RETURN_ERROR)
          thecat.nparam++;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        *((char *)key->ptr) = (char)'\1';
        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
125
126
127
128
129
130
131
132
133
134
135
136
137
  return;
  }


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

Emmanuel Bertin's avatar
Emmanuel Bertin committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*************************** 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
179
180
181
182
183
184
185
186
187
188

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

  {
   int	i;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
189
190
191
192
193
194
195
196
/*----------------------------- 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
197
  FLAG(obj2.prof_concentration) |= FLAG(obj2.prof_concentrationerr);
198
199
200
201
  FLAG(obj2.fluxcorerr_prof) |= FLAG(obj2.magcorerr_prof);
  FLAG(obj2.fluxcor_prof) |= FLAG(obj2.magcor_prof)
			| FLAG(obj2.fluxcorerr_prof);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  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);
219
220
221
  FLAG(obj2.poserrmx2_prof) |= FLAG(obj2.poserrmy2_prof)
			| FLAG(obj2.poserrmxy_prof)
			| FLAG(obj2.poserrmx2w_prof);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
222
223
224
225
226
227
228
229
230
  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);
231
  FLAG(obj2.xf_prof) |= FLAG(obj2.yf_prof);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
232
233
  FLAG(obj2.x_prof) |= FLAG(obj2.y_prof)
			| FLAG(obj2.xw_prof)
234
			| FLAG(obj2.xf_prof)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
235
			| FLAG(obj2.poserra_prof)
236
			| FLAG(obj2.poserrcxx_prof)
237
			| FLAG(obj2.poserrmx2_prof)
238
			| FLAG(obj2.prof_concentration)
239
			| FLAG(obj2.prof_class_star)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
240
			| FLAG(obj2.fluxcor_prof);
241

242
  FLAG(obj2.prof_dirac_fluxratio) |= FLAG(obj2.prof_dirac_fluxratioerr);
243
  FLAG(obj2.prof_dirac_mag) |= FLAG(obj2.prof_dirac_magerr);
244
  FLAG(obj2.prof_spheroid_fluxratio) |= FLAG(obj2.prof_spheroid_fluxratioerr);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
245
246
247
248
249
  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);
250
  FLAG(obj2.prof_disk_fluxratio) |= FLAG(obj2.prof_disk_fluxratioerr);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
251
252
253
254
255
  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);
256
  FLAG(obj2.prof_bar_fluxratio) |= FLAG(obj2.prof_bar_fluxratioerr);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
257
258
259
260
  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);
261
  FLAG(obj2.prof_arms_fluxratio) |= FLAG(obj2.prof_arms_fluxratioerr);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
262
  FLAG(obj2.prof_arms_mag) |= FLAG(obj2.prof_arms_magerr);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
263
264
265
266
267
  FLAG(obj2.prof_e1errw) |= FLAG(obj2.prof_e2errw) | FLAG(obj2.prof_e12corrw);
  FLAG(obj2.prof_e1w) |= FLAG(obj2.prof_e2w) | FLAG(obj2.prof_e1errw);
  FLAG(obj2.prof_pol1errw) |= FLAG(obj2.prof_pol2errw)
			| FLAG(obj2.prof_pol12corrw);
  FLAG(obj2.prof_pol1w) |= FLAG(obj2.prof_pol2w) | FLAG(obj2.prof_pol1errw);
268
269
270
271
272
273
274
275
276
277
  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)
278
			| FLAG(obj2.prof_e1w) | FLAG(obj2.prof_pol1w);
279
280
281

  FLAG(obj2.dtheta1950) |= FLAG(obj2.prof_theta1950)
			| FLAG(obj2.prof_spheroid_theta1950)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
282
283
284
285
286
287
288
289
290
			| 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);
291
292
  FLAG(obj2.dtheta2000) |= FLAG(obj2.prof_theta2000)
			| FLAG(obj2.prof_spheroid_theta2000)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
			| 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);
334
  FLAG(obj2.prof_disk_fluxmean) |= FLAG(obj2.prof_disk_mumean);
335
336
  FLAG(obj2.prof_disk_fluxeff) |= FLAG(obj2.prof_disk_mueff);
  FLAG(obj2.prof_disk_peak) |= FLAG(obj2.prof_disk_mumax)
337
338
				| FLAG(obj2.prof_disk_fluxeff)
				| FLAG(obj2.prof_disk_fluxmean);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
339
340
341
342
343
344
345

  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);

346
  FLAG(obj2.prof_spheroid_fluxmean) |= FLAG(obj2.prof_spheroid_mumean);
347
348
  FLAG(obj2.prof_spheroid_fluxeff) |= FLAG(obj2.prof_spheroid_mueff);
  FLAG(obj2.prof_spheroid_peak) |= FLAG(obj2.prof_spheroid_mumax)
349
350
				| FLAG(obj2.prof_spheroid_fluxeff)
				| FLAG(obj2.prof_spheroid_fluxmean);
351

Emmanuel Bertin's avatar
Emmanuel Bertin committed
352
353
354
  FLAG(obj2.prof_flagw) |= FLAG(obj2.prof_spheroid_reffw)
			| FLAG(obj2.prof_disk_scalew)
			| FLAG(obj2.prof_bar_lengthw)
355
356
			| FLAG(obj2.prof_arms_scalew)
			| FLAG(obj2.prof_mx2w);
357

Emmanuel Bertin's avatar
Emmanuel Bertin committed
358
359
  FLAG(obj2.prof_e1err) |= FLAG(obj2.prof_e2err) | FLAG(obj2.prof_e12corr)
			| FLAG(obj2.prof_e1errw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
360
			
361
  FLAG(obj2.prof_e1) |= FLAG(obj2.prof_e2)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
362
363
364
			| FLAG(obj2.prof_e1err) | FLAG(obj2.prof_e1w);
  FLAG(obj2.prof_pol1err) |= FLAG(obj2.prof_pol2err)
			| FLAG(obj2.prof_pol12corr) | FLAG(obj2.prof_pol1errw);
365
  FLAG(obj2.prof_pol1) |= FLAG(obj2.prof_pol2)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
366
			| FLAG(obj2.prof_pol1err) | FLAG(obj2.prof_pol1w);
367
368
369
370
  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
371
  FLAG(obj2.prof_mx2) |= FLAG(obj2.prof_my2) | FLAG(obj2.prof_mxy)
372
			| FLAG(obj2.prof_e1) | FLAG(obj2.prof_pol1)
373
374
			| FLAG(obj2.prof_a) | FLAG(obj2.prof_cxx)
			| FLAG(obj2.prof_mx2w);
375
376
377
378
  FLAG(obj2.prof_conva) |= FLAG(obj2.prof_convb) | FLAG(obj2.prof_convtheta);
  FLAG(obj2.prof_convcxx) |= FLAG(obj2.prof_convcyy)
			/*| FLAG(obj2.fluxcor_prof) */;
  FLAG(obj2.prof_convmx2) |= FLAG(obj2.prof_convcxx) | FLAG(obj2.prof_conva);
379
  FLAG(obj2.fluxmean_prof) |= FLAG(obj2.mumean_prof);
380
  FLAG(obj2.peak_prof) |= FLAG(obj2.mumax_prof);
381
382
  FLAG(obj2.fluxeff_prof) |= FLAG(obj2.mueff_prof)
			| FLAG(obj2.fluxmean_prof) | FLAG(obj2.peak_prof);
383

Emmanuel Bertin's avatar
Emmanuel Bertin committed
384
385
  FLAG(obj2.prof_arms_flux) |= FLAG(obj2.prof_arms_fluxerr)
			| FLAG(obj2.prof_arms_mag)
386
			| FLAG(obj2.prof_arms_fluxratio)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
387
388
389
390
391
392
393
394
395
			| 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)
396
			| FLAG(obj2.prof_bar_fluxratio)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
397
398
399
400
401
402
403
404
			| 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)
405
			| FLAG(obj2.prof_disk_fluxratio)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
406
407
408
409
410
			| 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)
411
			| FLAG(obj2.prof_disk_peak)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
412
413
414
			| FLAG(obj2.prof_bar_flux);
  FLAG(obj2.prof_spheroid_flux) |= FLAG(obj2.prof_spheroid_fluxerr)
			| FLAG(obj2.prof_spheroid_mag)
415
			| FLAG(obj2.prof_spheroid_fluxratio)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
416
417
418
419
			| FLAG(obj2.prof_spheroid_reffw)
			| FLAG(obj2.prof_spheroid_reff)
			| FLAG(obj2.prof_spheroid_aspect)
			| FLAG(obj2.prof_spheroid_theta)
420
421
			| FLAG(obj2.prof_spheroid_sersicn)
			| FLAG(obj2.prof_spheroid_peak);
422
  FLAG(obj2.prof_dirac_flux) |= FLAG(obj2.prof_dirac_fluxerr)
423
424
			| FLAG(obj2.prof_dirac_mag)
			| FLAG(obj2.prof_dirac_fluxratio);
425
  FLAG(obj2.prof_offset_flux) |= FLAG(obj2.prof_offset_fluxerr);
426
427
428
429
430
431
432
  FLAG(obj2.fluxerr_dprof) |= FLAG(obj2.magerr_dprof);
  FLAG(obj2.flux_dprof) |= FLAG(obj2.mag_dprof)
			| FLAG(obj2.fluxerr_dprof);
  prefs.dprof_flag |= FLAG(obj2.dprof_chi2)
			| FLAG(obj2.dprof_flag)
			| FLAG(obj2.dprof_niter)
			| FLAG(obj2.flux_dprof);
433
434
435
436
437
438
439
440
441
442
443
444
445

  FLAG(obj2.fluxerr_prof) |= FLAG(obj2.magerr_prof)
			| FLAG(obj2.prof_concentrationerr)
			| FLAG(obj2.fluxcorerr_prof)
			| FLAG(obj2.prof_arms_fluxratio)
			| FLAG(obj2.prof_bar_fluxratio)
			| FLAG(obj2.prof_disk_fluxratio)
			| FLAG(obj2.prof_spheroid_fluxratio)
			| FLAG(obj2.prof_dirac_fluxratio);
  FLAG(obj2.flux_prof) |= FLAG(obj2.mag_prof)
			| FLAG(obj2.fluxerr_prof)
			| FLAG(obj2.fluxcor_prof);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
446
447
  prefs.prof_flag |= FLAG(obj2.prof_chi2) | FLAG(obj2.prof_niter)
			| FLAG(obj2.prof_vector) | FLAG(obj2.prof_errvector)
448
			| FLAG(obj2.prof_errmatrix)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
449
			| FLAG(obj2.x_prof) | FLAG(obj2.y_prof)
450
451
			| FLAG(obj2.prof_flag)
			| FLAG(obj2.flux_prof)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
452
			| FLAG(obj2.prof_mx2)
453
			| FLAG(obj2.fluxeff_prof)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
454
			| FLAG(obj2.prof_disk_flux)
455
456
			| FLAG(obj2.prof_spheroid_flux)
			| FLAG(obj2.prof_dirac_flux)
457
458
			| FLAG(obj2.prof_offset_flux)
			| prefs.dprof_flag;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
459
460
461
462


/* If only global parameters are requested, fit a Sersic model */
  if (prefs.prof_flag && !(FLAG(obj2.prof_spheroid_flux)
463
464
465
			| FLAG(obj2.prof_disk_flux)
			| FLAG(obj2.prof_dirac_flux)
			| FLAG(obj2.prof_offset_flux)))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
466
467
468
469
470
471
    {
    FLAG(obj2.prof_spheroid_flux) |= prefs.prof_flag;
    FLAG(obj2.prof_spheroid_sersicn) |= prefs.prof_flag;
    }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*------------------------------ 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)
511
			| FLAG(obj2.winposerr_mx2w);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
512
513
514
515
516
517
518
519
520
521

  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);
522
  FLAG(obj2.winpos_xf) |= FLAG(obj2.winpos_yf);
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
  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
545
546
			| FLAG(obj2.poserr_theta1950) | FLAG(obj2.dtheta1950);
;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
547
548
549
  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
550
			| FLAG(obj2.dtheta2000);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
551
552
553
554
555
556
557
558
  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)
559
			| FLAG(obj2.thetaw) | FLAG(obj2.aw) | FLAG(obj2.cxxw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
560
  
561
  FLAG(obj2.peakxw) |= FLAG(obj2.peakyf);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
562
  FLAG(obj2.peakxw) |= FLAG(obj2.peakyw) | FLAG(obj2.peakalphas);
563
564
565
  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
566
567

  FLAG(obj2.mxw) |= FLAG(obj2.myw) | FLAG(obj2.mx2w) | FLAG(obj2.alphas)
568
569
570
		| FLAG(obj2.poserr_mx2w)
		| ((FLAG(obj2.assoc) | FLAG(obj2.assoc_number))
			&& (prefs.assoccoord_type==ASSOCCOORD_WORLD));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
571
  FLAG(obj2.mamaposx) |= FLAG(obj2.mamaposy);
572
  FLAG(obj2.fluxerr_win) |= FLAG(obj2.snr_win);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
573
  FLAG(obj2.flux_win) |= FLAG(obj2.mag_win)|FLAG(obj2.magerr_win)
574
			| FLAG(obj2.flux_win) | FLAG(obj2.fluxerr_win);
575
576
  FLAG(obj2.winpos_dgeox) |= FLAG(obj2.winpos_dgeoy);
  FLAG(obj2.winpos_x) |= FLAG(obj2.winpos_y) | FLAG(obj2.winpos_dgeox)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
577
			| FLAG(obj2.winposerr_mx2) | FLAG(obj2.win_mx2)
578
579
			| FLAG(obj2.winpos_xw) | FLAG(obj2.winpos_xf)
			| FLAG(obj2.win_flag)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
580
581
			| FLAG(obj2.flux_win) |FLAG(obj2.winpos_niter);

582
583
584
585
  FLAG(obj2.area_flagw) |= FLAG(obj2.npixw) | FLAG(obj2.fdnpixw)
			| FLAG(obj2.fwhmw)
			| FLAG(obj2.maxmu) | FLAG(obj2.threshmu)
			| FLAG(obj2.prof_spheroid_mumax)
586
587
			| FLAG(obj2.prof_disk_mumax)
			| FLAG(obj2.mumax_prof);
588

Emmanuel Bertin's avatar
Emmanuel Bertin committed
589
590
591
592
593
594
/*------------------------------ 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
595
  FLAG(obj2.hl_radius) |= FLAG(obj2.winpos_x) | prefs.prof_flag;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
596
597
598
599
600
601

  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)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
602
603
			| FLAG(obj2.hl_radius)
			| FLAG(obj2.fluxcor_prof);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
  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 --------------------------------*/
624
  FLAG(obj2.fwhm_psf) |= FLAG(obj2.fwhmw_psf);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
  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]);

681
682
  FLAG(obj.wflag) |= FLAG(obj.nzwpix) | FLAG(obj.nzdwpix);

Emmanuel Bertin's avatar
Emmanuel Bertin committed
683
684
685
686
  return; 
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
687
688
689
690
691
692
693
694
695
696
/********************************** 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
697
      printf("#%-24.24s %-57.57s [%s]\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
698
699
		objkey[i].name, objkey[i].comment, objkey[i].unit);
    else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
700
      printf("#%-24.24s %-57.57s\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
701
702
703
704
705
706
		objkey[i].name, objkey[i].comment);

  return;
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/********************************** 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);
728
//  setlinebuf(ascfile);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
729
730
731
732
    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
733
          fprintf(ascfile, "# %3d %-22.22s %-58.58s [%s]\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
734
735
		n, key->name,key->comment, key->unit);
        else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
736
          fprintf(ascfile, "# %3d %-22.22s %-58.58s\n",
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
797
798
799
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
825
826
827
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
		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
872
873
874
        asctab->headnblock = field->tab->headnblock;
        QMEMCPY(field->tab->headbuf, asctab->headbuf, char,
		asctab->headnblock*FBSIZE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
        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
900
901
902
        asctab->headnblock = field->tab->headnblock;
        QMEMCPY(field->tab->headbuf, asctab->headbuf, char,
		asctab->headnblock*FBSIZE);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
        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);
    }

943
944
  zerocat();

Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
1006
1007
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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
  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;
1062
   OFF_T2	pos;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
   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
1083
1084
      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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
      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;
  }


1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
/********************************** zerocat **********************************/
/*
Reset to 0 all measurements in the current catalog.
*/
void	zerocat(void)
  {
   keystruct	*key;
   int		i;

  key = objtab->key;
  for (i=objtab->nkey; i--; key=key->nextkey)
    memset(key->ptr, 0, key->nbytes);

  return;
  }