astrom.c 35.5 KB
Newer Older
1
2
/*
*				astrom.c
Emmanuel Bertin's avatar
Emmanuel Bertin committed
3
*
4
* High level astrometric computations.
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
*
Emmanuel Bertin's avatar
Emmanuel Bertin committed
10
*	Copyright:		(C) 1993-2010 Emmanuel Bertin -- IAP/CNRS/UPMC
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
*
*	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/>.
*
*	Last modified:		11/10/2010
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#ifdef HAVE_CONFIG_H
#include        "config.h"
#endif

#include	<math.h>
#include	<stdlib.h>
#include	<string.h>

#include 	"wcs/wcs.h"
#include	"define.h"
#include	"globals.h"
#include	"prefs.h"
#include	"astrom.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
42
#include	"fitswcs.h"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
43
44
45
46
47
48
49
50
51
52
53
#include	"wcs/tnx.h"

static obj2struct	*obj2 = &outobj2;

/****************************** initastrom **********************************/
/*
Initialize astrometrical structures.
*/
void	initastrom(picstruct *field)

  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
54
   wcsstruct	*wcs;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
55

Emmanuel Bertin's avatar
Emmanuel Bertin committed
56
  wcs = field->wcs;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
57
58

/* Test if the WCS is in use */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
59
  if (wcs->lng != wcs->lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
60
    {
61
    if (FLAG(obj2.dtheta2000) || FLAG(obj2.dtheta1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
62
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
63
64
      if (fabs(wcs->equinox-2000.0)>0.003)
        precess(wcs->equinox, 0.0, 90.0, 2000.0, &wcs->ap2000, &wcs->dp2000);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
65
66
      else
        {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
67
68
        wcs->ap2000 = 0.0;
        wcs->dp2000 = 90.0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
69
70
71
        }

      if (FLAG(obj2.theta1950) || FLAG(obj2.poserr_theta1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
72
        j2b(wcs->equinox, wcs->ap2000, wcs->dp2000, &wcs->ap1950, &wcs->dp1950);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
73
74
75
76
77
      }
    }

/* Override astrometric definitions only if user supplies a pixel-scale */
  if (prefs.pixel_scale == 0.0)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
78
    field->pixscale = wcs->pixscale*3600.0;	/* in arcsec */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
79
  else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
80
    field->pixscale = prefs.pixel_scale;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
81
82
83
84
85

  return;
  }


86
/***************************** astrom_pos **********************************/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
87
/*
88
Compute real FOCAL and WORLD coordinates according to FITS info.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
89
*/
90
void	astrom_pos(picstruct *field, objstruct *obj)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
91
92

  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
93
94
   wcsstruct	*wcs;
   double	rawpos[NAXIS], wcspos[NAXIS],
95
		da,dd;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
96
   int		lng,lat;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
97

Emmanuel Bertin's avatar
Emmanuel Bertin committed
98
99
100
  wcs = field->wcs;
  lng = wcs->lng;
  lat = wcs->lat;
101
102
103
104
105
106
107
108
109
110

/* If working with WCS, compute FOCAL coordinates and local matrix */
  if (FLAG(obj2.mxf))
    {
    rawpos[0] = obj2->posx;
    rawpos[1] = obj2->posy;
    raw_to_red(wcs, rawpos, wcspos);
    obj2->mxf = wcspos[0];
    obj2->myf = wcspos[1];
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
111
112
113
114

/* If working with WCS, compute WORLD coordinates and local matrix */
  if (FLAG(obj2.mxw))
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
115
116
117
118
119
120
    rawpos[0] = obj2->posx;
    rawpos[1] = obj2->posy;
    raw_to_wcs(wcs, rawpos, wcspos);
    obj2->mxw = wcspos[0];
    obj2->myw = wcspos[1];
    if (lng != lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
121
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
122
123
      obj2->alphas = lng<lat? obj2->mxw : obj2->myw;
      obj2->deltas = lng<lat? obj2->myw : obj2->mxw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
124
125
      if (FLAG(obj2.alpha2000))
        {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
126
127
        if (fabs(wcs->equinox-2000.0)>0.003)
          precess(wcs->equinox, wcspos[lng<lat?0:1], wcspos[lng<lat?1:0],
Emmanuel Bertin's avatar
Emmanuel Bertin committed
128
129
130
		2000.0, &obj2->alpha2000, &obj2->delta2000);
        else
          {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
131
132
133
134
135
136
137
138
139
140
141
          obj2->alpha2000 = lng<lat? obj2->mxw : obj2->myw;
          obj2->delta2000 = lng<lat? obj2->myw : obj2->mxw;
          }
        if (FLAG(obj2.dtheta2000))
          {
          da = wcs->ap2000 - obj2->alpha2000;
          dd = (sin(wcs->dp2000*DEG)
		-sin(obj2->delta2000*DEG)*sin(obj2->deltas*DEG))
		/(cos(obj2->delta2000*DEG)*cos(obj2->deltas*DEG));
          dd = dd<1.0? (dd>-1.0?acos(dd)/DEG:180.0) : 0.0;
          obj2->dtheta2000 = (((da>0.0 && da<180.0) || da<-180.0)?-dd:dd);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
142
143
          }
        if (FLAG(obj2.alpha1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
144
145
          {
          j2b(wcs->equinox, obj2->alpha2000, obj2->delta2000,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
146
		&obj2->alpha1950, &obj2->delta1950);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
147
148
149
150
151
152
153
154
155
156
          if (FLAG(obj2.dtheta1950))
            {
            da = wcs->ap1950 - obj2->alpha1950;
            dd = (sin(wcs->dp1950*DEG)
		-sin(obj2->delta1950*DEG)*sin(obj2->deltas*DEG))
		/(cos(obj2->delta1950*DEG)*cos(obj2->deltas*DEG));
            dd = dd<1.0? (dd>-1.0?acos(dd)/DEG:180.0) : 0.0;
            obj2->dtheta1950 = (((da>0.0 && da<180.0) || da<-180.0)?-dd:dd);
           }
          }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
157
158
159
160
        }
      }
    }

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Custom coordinate system for the MAMA machine */
  if (FLAG(obj2.mamaposx))
    {
    rawpos[0] = obj2->posx - 0.5;
    rawpos[1] = obj2->posy - 0.5;
    raw_to_wcs(wcs, rawpos, wcspos);
    obj2->mamaposx = wcspos[1]*(MAMA_CORFLEX+1.0);
    obj2->mamaposy = wcspos[0]*(MAMA_CORFLEX+1.0);
    }

  return;
  }


/***************************** astrom_peakpos *******************************/
/*
Compute real FOCAL and WORLD peak coordinates according to FITS info.
*/
void	astrom_peakpos(picstruct *field, objstruct *obj)

  {
   wcsstruct	*wcs;
   double	rawpos[NAXIS], wcspos[NAXIS];
   int		lng,lat;

  wcs = field->wcs;
  lng = wcs->lng;
  lat = wcs->lat;

  if (FLAG(obj2.peakxf))
    {
    rawpos[0] = obj->peakx;
    rawpos[1] = obj->peaky;
    raw_to_red(wcs, rawpos, wcspos);
    obj2->peakxf = wcspos[0];
    obj2->peakyf = wcspos[1];
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
198
199
  if (FLAG(obj2.peakxw))
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
200
201
202
203
204
205
    rawpos[0] = obj->peakx;
    rawpos[1] = obj->peaky;
    raw_to_wcs(wcs, rawpos, wcspos);
    obj2->peakxw = wcspos[0];
    obj2->peakyw = wcspos[1];
    if (lng != lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
206
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
207
208
      obj2->peakalphas = lng<lat? obj2->peakxw : obj2->peakyw;
      obj2->peakdeltas = lng<lat? obj2->peakyw : obj2->peakxw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
209
210
      if (FLAG(obj2.peakalpha2000))
        {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
211
212
        if (fabs(wcs->equinox-2000.0)>0.003)
          precess(wcs->equinox, wcspos[lng<lat?0:1], wcspos[lng<lat?1:0],
Emmanuel Bertin's avatar
Emmanuel Bertin committed
213
214
215
		2000.0, &obj2->peakalpha2000, &obj2->peakdelta2000);
        else
          {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
216
217
          obj2->peakalpha2000 = lng<lat? obj2->peakxw : obj2->peakyw;
          obj2->peakdelta2000 = lng<lat? obj2->peakyw : obj2->peakxw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
218
219
          }
        if (FLAG(obj2.peakalpha1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
220
          j2b(wcs->equinox, obj2->peakalpha2000, obj2->peakdelta2000,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
221
222
223
224
225
		&obj2->peakalpha1950, &obj2->peakdelta1950);
        }
      }
    }

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  return;
  }


/****************************** astrom_winpos *******************************/
/*
Compute real FOCAL and WORLD windowed coordinates according to FITS info.
*/
void	astrom_winpos(picstruct *field, objstruct *obj)

  {
   wcsstruct	*wcs;
   double	rawpos[NAXIS], wcspos[NAXIS];
   int		lng,lat;

  wcs = field->wcs;
  lng = wcs->lng;
  lat = wcs->lat;

  if (FLAG(obj2.winpos_xf))
    {
    rawpos[0] = obj2->winpos_x;
    rawpos[1] = obj2->winpos_y;
    raw_to_red(wcs, rawpos, wcspos);
    obj2->winpos_xf = wcspos[0];
    obj2->winpos_yf = wcspos[1];
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
253
254
  if (FLAG(obj2.winpos_xw))
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
255
256
257
258
259
260
    rawpos[0] = obj2->winpos_x;
    rawpos[1] = obj2->winpos_y;
    raw_to_wcs(wcs, rawpos, wcspos);
    obj2->winpos_xw = wcspos[0];
    obj2->winpos_yw = wcspos[1];
    if (lng != lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
261
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
262
263
      obj2->winpos_alphas = lng<lat? obj2->winpos_xw : obj2->winpos_yw;
      obj2->winpos_deltas = lng<lat? obj2->winpos_yw : obj2->winpos_xw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
264
265
      if (FLAG(obj2.winpos_alpha2000))
        {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
266
267
        if (fabs(wcs->equinox-2000.0)>0.003)
          precess(wcs->equinox, wcspos[0], wcspos[1],
Emmanuel Bertin's avatar
Emmanuel Bertin committed
268
269
270
		2000.0, &obj2->winpos_alpha2000, &obj2->winpos_delta2000);
        else
          {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
271
272
          obj2->winpos_alpha2000 = lng<lat? obj2->winpos_xw : obj2->winpos_yw;
          obj2->winpos_delta2000 = lng<lat? obj2->winpos_yw : obj2->winpos_xw;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
273
274
          }
        if (FLAG(obj2.winpos_alpha1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
275
          j2b(wcs->equinox, obj2->winpos_alpha2000, obj2->winpos_delta2000,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
276
277
278
		&obj2->winpos_alpha1950, &obj2->winpos_delta1950);
        }
      }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
279
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
280

281
282
283
284
  return;
  }


285
286
287
288
289
290
291
292
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
334
335
336
337
338
339
/****************************** astrom_psfpos *******************************/
/*
Compute real FOCAL and WORLD PSF coordinates according to FITS info.
*/
void	astrom_psfpos(picstruct *field, objstruct *obj)

  {
   wcsstruct	*wcs;
   double	rawpos[NAXIS], wcspos[NAXIS];
   int		lng,lat;

  wcs = field->wcs;
  lng = wcs->lng;
  lat = wcs->lat;

  if (FLAG(obj2.xf_psf))
    {
    rawpos[0] = obj2->x_psf;
    rawpos[1] = obj2->y_psf;
    raw_to_red(wcs, rawpos, wcspos);
    obj2->xf_psf = wcspos[0];
    obj2->yf_psf = wcspos[1];
    }
  if (FLAG(obj2.xw_psf))
    {
    rawpos[0] = obj2->x_psf;
    rawpos[1] = obj2->y_psf;
    raw_to_wcs(wcs, rawpos, wcspos);
    obj2->xw_psf = wcspos[0];
    obj2->yw_psf = wcspos[1];
    if (lng != lat)
      {
      obj2->alphas_psf = lng<lat? obj2->xw_psf : obj2->yw_psf;
      obj2->deltas_psf = lng<lat? obj2->yw_psf : obj2->xw_psf;
      if (FLAG(obj2.alpha2000_psf))
        {
        if (fabs(wcs->equinox-2000.0)>0.003)
          precess(wcs->equinox, wcspos[0], wcspos[1],
		2000.0, &obj2->alpha2000_psf, &obj2->delta2000_psf);
        else
          {
          obj2->alpha2000_psf = lng<lat? obj2->xw_psf : obj2->yw_psf;
          obj2->delta2000_psf = lng<lat? obj2->yw_psf : obj2->xw_psf;
          }
        if (FLAG(obj2.alpha1950_psf))
          j2b(wcs->equinox, obj2->alpha2000_psf, obj2->delta2000_psf,
		&obj2->alpha1950_psf, &obj2->delta1950_psf);
        }
      }
    }

  return;
  }


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/****************************** astrom_profpos *******************************/
/*
Compute real FOCAL and WORLD profit coordinates according to FITS info.
*/
void	astrom_profpos(picstruct *field, objstruct *obj)

  {
   wcsstruct	*wcs;
   double	rawpos[NAXIS], wcspos[NAXIS];
   int		lng,lat;

  wcs = field->wcs;
  lng = wcs->lng;
  lat = wcs->lat;

  if (FLAG(obj2.xf_prof))
    {
    rawpos[0] = obj2->x_prof;
    rawpos[1] = obj2->y_prof;
    raw_to_red(wcs, rawpos, wcspos);
    obj2->xf_prof = wcspos[0];
    obj2->yf_prof = wcspos[1];
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  if (FLAG(obj2.xw_prof))
    {
    rawpos[0] = obj2->x_prof;
    rawpos[1] = obj2->y_prof;
    raw_to_wcs(wcs, rawpos, wcspos);
    obj2->xw_prof = wcspos[0];
    obj2->yw_prof = wcspos[1];
    if (lng != lat)
      {
      obj2->alphas_prof = lng<lat? obj2->xw_prof : obj2->yw_prof;
      obj2->deltas_prof = lng<lat? obj2->yw_prof : obj2->xw_prof;
      if (FLAG(obj2.alpha2000_prof))
        {
        if (fabs(wcs->equinox-2000.0)>0.003)
          precess(wcs->equinox, wcspos[0], wcspos[1],
		2000.0, &obj2->alpha2000_prof, &obj2->delta2000_prof);
        else
          {
          obj2->alpha2000_prof = lng<lat? obj2->xw_prof : obj2->yw_prof;
          obj2->delta2000_prof = lng<lat? obj2->yw_prof : obj2->xw_prof;
          }
        if (FLAG(obj2.alpha1950_prof))
          j2b(wcs->equinox, obj2->alpha2000_prof, obj2->delta2000_prof,
		&obj2->alpha1950_prof, &obj2->delta1950_prof);
        }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
388
389
390
391
392
393
394
395
396
397
398
399
400
      }
    }

  return;
  }


/****************************** astrom_shapeparam ****************************/
/*
Compute shape parameters in WORLD and SKY coordinates.
*/
void	astrom_shapeparam(picstruct *field, objstruct *obj)
  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
401
402
   wcsstruct	*wcs;
   double	dx2,dy2,dxy, xm2,ym2,xym, temp,pm2, lm0,lm1,lm2,lm3;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
403
404
   int		lng,lat, naxis;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
405
406
407
408
  wcs = field->wcs;
  naxis = wcs->naxis;
  lng = wcs->lng;
  lat = wcs->lat;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
409
410
411
412
413
  if (lng == lat)
    {
    lng = 0;
    lat = 1;
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
414
415
416
417
  lm0 = obj2->jacob[lng+naxis*lng];
  lm1 = obj2->jacob[lat+naxis*lng];
  lm2 = obj2->jacob[lng+naxis*lat];
  lm3 = obj2->jacob[lat+naxis*lat];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
418
419
420
421
422
423
424
425
426
427
428
429


/* All WORLD params based on 2nd order moments have to pass through here */
  dx2 = obj->mx2;
  dy2 = obj->my2;
  dxy = obj->mxy;
  obj2->mx2w = xm2 = lm0*lm0*dx2 + lm1*lm1*dy2 + lm0*lm1*dxy;
  obj2->my2w = ym2 = lm2*lm2*dx2 + lm3*lm3*dy2 + lm2*lm3*dxy;
  obj2->mxyw = xym = lm0*lm2*dx2 + lm1*lm3*dy2 + (lm0*lm3+lm1*lm2)*dxy;
  temp=xm2-ym2;
  if (FLAG(obj2.thetaw))
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
430
431
    obj2->thetaw = fmod_m90_p90((temp == 0.0)?
				(45.0) : (0.5*atan2(2.0 * xym,temp)/DEG));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
432
433

/*-- Compute position angles in J2000 or B1950 reference frame */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
434
    if (wcs->lng != wcs->lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
435
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
436
437
438
439
      if (FLAG(obj2.thetas))
        obj2->thetas = fmod_m90_p90(lng<lat?
				((obj2->thetaw>0.0?90:-90.0) - obj2->thetaw)
				: obj2->thetaw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
440
      if (FLAG(obj2.theta2000))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
441
        obj2->theta2000 = fmod_m90_p90(obj2->thetas + obj2->dtheta2000);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
442
      if (FLAG(obj2.theta1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
443
        obj2->theta1950 = fmod_m90_p90(obj2->thetas + obj2->dtheta1950);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
      }
    }

  if (FLAG(obj2.aw))
    {
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->aw = (float)sqrt(pm2+temp);
    obj2->bw = (float)sqrt(pm2-temp);
    obj2->polarw = temp / pm2;
    }

  if (FLAG(obj2.cxxw))
    {
/*-- Handle large, fully correlated profiles (can cause a singularity...) */
    if ((temp=xm2*ym2-xym*xym)<1e-6)
      {
      temp = 1e-6;
      xym *= 0.99999;
      }
    obj2->cxxw = (float)(ym2/temp);
    obj2->cyyw = (float)(xm2/temp);
    obj2->cxyw = (float)(-2*xym/temp);
    }

  return;
  }


/**************************** astrom_winshapeparam ***************************/
/*
Compute shape parameters in WORLD and SKY coordinates.
*/
void	astrom_winshapeparam(picstruct *field, objstruct *obj)
  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
479
480
   wcsstruct	*wcs;
   double	dx2,dy2,dxy, xm2,ym2,xym, temp,pm2, lm0,lm1,lm2,lm3;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
481
482
   int		lng,lat, naxis;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
483
484
485
486
  wcs = field->wcs;
  naxis = wcs->naxis;
  lng = wcs->lng;
  lat = wcs->lat;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
487
488
489
490
491
  if (lng == lat)
    {
    lng = 0;
    lat = 1;
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
492
493
494
495
  lm0 = obj2->jacob[lng+naxis*lng];
  lm1 = obj2->jacob[lat+naxis*lng];
  lm2 = obj2->jacob[lng+naxis*lat];
  lm3 = obj2->jacob[lat+naxis*lat];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
496
497
498
499
500
501
502
503
504
505
506

/* All WORLD params based on 2nd order moments have to pass through here */
  dx2 = obj2->win_mx2;
  dy2 = obj2->win_my2;
  dxy = obj2->win_mxy;
  obj2->win_mx2w = xm2 = lm0*lm0*dx2 + lm1*lm1*dy2 + lm0*lm1*dxy;
  obj2->win_my2w = ym2 = lm2*lm2*dx2 + lm3*lm3*dy2 + lm2*lm3*dxy;
  obj2->win_mxyw = xym = lm0*lm2*dx2 + lm1*lm3*dy2 + (lm0*lm3+lm1*lm2)*dxy;
  temp=xm2-ym2;
  if (FLAG(obj2.win_thetaw))
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
507
508
    obj2->win_thetaw = fmod_m90_p90((temp == 0.0)?
			(45.0) : (0.5*atan2(2.0*xym,temp)/DEG));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
509
510

/*-- Compute position angles in J2000 or B1950 reference frame */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
511
    if (wcs->lng != wcs->lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
512
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
513
514
515
516
      if (FLAG(obj2.win_thetas))
        obj2->win_thetas = fmod_m90_p90(lng<lat?
			((obj2->win_thetaw>0.0?90:-90.0) - obj2->win_thetaw)
			: obj2->win_thetaw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
517
      if (FLAG(obj2.win_theta2000))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
518
        obj2->win_theta2000 = fmod_m90_p90(obj2->win_thetas + obj2->dtheta2000);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
519
      if (FLAG(obj2.win_theta1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
520
        obj2->win_theta1950 = fmod_m90_p90(obj2->win_thetas + obj2->dtheta1950);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
521
522
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
      }
    }

  if (FLAG(obj2.win_aw))
    {
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->win_aw = (float)sqrt(pm2+temp);
    obj2->win_bw = (float)sqrt(pm2-temp);
    obj2->win_polarw = temp / pm2;
    }

  if (FLAG(obj2.win_cxxw))
    {
/*-- Handle large, fully correlated profiles (can cause a singularity...) */
    if ((temp=xm2*ym2-xym*xym)<1e-6)
      {
      temp = 1e-6;
      xym *= 0.99999;
      }
    obj2->win_cxxw = (float)(ym2/temp);
    obj2->win_cyyw = (float)(xm2/temp);
    obj2->win_cxyw = (float)(-2*xym/temp);
    }

  return;
  }


/******************************* astrom_errparam *****************************/
/*
Compute error ellipse parameters in WORLD and SKY coordinates.
*/
void	astrom_errparam(picstruct *field, objstruct *obj)
  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
556
557
   wcsstruct	*wcs;
   double	dx2,dy2,dxy, xm2,ym2,xym, temp,pm2, lm0,lm1,lm2,lm3;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
558
559
   int		lng,lat, naxis;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
560
561
562
563
  wcs = field->wcs;
  naxis = wcs->naxis;
  lng = wcs->lng;
  lat = wcs->lat;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
564
565
566
567
568
  if (lng == lat)
    {
    lng = 0;
    lat = 1;
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
569
570
571
572
  lm0 = obj2->jacob[lng+naxis*lng];
  lm1 = obj2->jacob[lat+naxis*lng];
  lm2 = obj2->jacob[lng+naxis*lat];
  lm3 = obj2->jacob[lat+naxis*lat];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
573
574
575
576
577
578
579
580
581
582
583

/* All WORLD params based on 2nd order moments have to pass through here */
  dx2 = obj->poserr_mx2;
  dy2 = obj->poserr_my2;
  dxy = obj->poserr_mxy;
  obj2->poserr_mx2w = xm2 = lm0*lm0*dx2 + lm1*lm1*dy2 + lm0*lm1*dxy;
  obj2->poserr_my2w = ym2 = lm2*lm2*dx2 + lm3*lm3*dy2 + lm2*lm3*dxy;
  obj2->poserr_mxyw = xym = lm0*lm2*dx2 + lm1*lm3*dy2 + (lm0*lm3+lm1*lm2)*dxy;
  temp=xm2-ym2;
  if (FLAG(obj2.poserr_thetaw))
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
584
585
    obj2->poserr_thetaw = fmod_m90_p90((temp==0.0)?
				(45.0):(0.5*atan2(2.0*xym,temp)/DEG));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
586
587

/*-- Compute position angles in J2000 or B1950 reference frame */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
588
    if (wcs->lng != wcs->lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
589
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
590
591
592
593
      if (FLAG(obj2.poserr_thetas))
        obj2->poserr_thetas = fmod_m90_p90(lng<lat?
		((obj2->poserr_thetaw>0.0?90:-90.0) - obj2->poserr_thetaw)
		: obj2->poserr_thetaw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
594
      if (FLAG(obj2.poserr_theta2000))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
595
596
        obj2->poserr_theta2000 = fmod_m90_p90(obj2->poserr_thetas
				+ obj2->dtheta2000);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
597
      if (FLAG(obj2.poserr_theta1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
598
599
        obj2->poserr_theta1950 = fmod_m90_p90(obj2->poserr_thetas
				+ obj2->dtheta1950);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
      }
    }

  if (FLAG(obj2.poserr_aw))
    {
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->poserr_aw = (float)sqrt(pm2+temp);
    obj2->poserr_bw = (float)sqrt(pm2-temp);
    }

  if (FLAG(obj2.poserr_cxxw))
    {
/*-- Handle large, fully correlated profiles (can cause a singularity...) */
    if ((temp=xm2*ym2-xym*xym)<1e-6)
      {
      temp = 1e-6;
      xym *= 0.99999;
      }
    obj2->poserr_cxxw = (float)(ym2/temp);
    obj2->poserr_cyyw = (float)(xm2/temp);
    obj2->poserr_cxyw = (float)(-2*xym/temp);
    }

  return;
  }


/***************************** astrom_winerrparam ***************************/
/*
Compute error ellipse parameters in WORLD and SKY coordinates.
*/
void	astrom_winerrparam(picstruct *field, objstruct *obj)
  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
634
635
   wcsstruct	*wcs;
   double	dx2,dy2,dxy, xm2,ym2,xym, temp,pm2, lm0,lm1,lm2,lm3;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
636
637
   int		lng,lat, naxis;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
638
639
640
641
  wcs = field->wcs;
  naxis = wcs->naxis;
  lng = wcs->lng;
  lat = wcs->lat;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
642
643
644
645
646
  if (lng == lat)
    {
    lng = 0;
    lat = 1;
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
647
648
649
650
  lm0 = obj2->jacob[lng+naxis*lng];
  lm1 = obj2->jacob[lat+naxis*lng];
  lm2 = obj2->jacob[lng+naxis*lat];
  lm3 = obj2->jacob[lat+naxis*lat];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
651
652
653
654
655
656
657
658
659
660
661

/* All WORLD params based on 2nd order moments have to pass through here */
  dx2 = obj2->winposerr_mx2;
  dy2 = obj2->winposerr_my2;
  dxy = obj2->winposerr_mxy;
  obj2->winposerr_mx2w = xm2 = lm0*lm0*dx2 + lm1*lm1*dy2 + lm0*lm1*dxy;
  obj2->winposerr_my2w = ym2 = lm2*lm2*dx2 + lm3*lm3*dy2 + lm2*lm3*dxy;
  obj2->winposerr_mxyw = xym = lm0*lm2*dx2 + lm1*lm3*dy2 + (lm0*lm3+lm1*lm2)*dxy;
  temp=xm2-ym2;
  if (FLAG(obj2.winposerr_thetaw))
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
662
663
    obj2->winposerr_thetaw = (fmod_m90_p90(temp==0.0)?
				(45.0):(0.5*atan2(2.0*xym,temp)/DEG));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
664
665

/*-- Compute position angles in J2000 or B1950 reference frame */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
666
    if (wcs->lng != wcs->lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
667
      {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
668
669
670
671
      if (FLAG(obj2.winposerr_thetas))
        obj2->winposerr_thetas = fmod_m90_p90(lng<lat?
		((obj2->winposerr_thetaw>0.0?90:-90.0) - obj2->winposerr_thetaw)
		: obj2->winposerr_thetaw);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
672
      if (FLAG(obj2.winposerr_theta2000))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
673
674
        obj2->winposerr_theta2000 = fmod_m90_p90(obj2->winposerr_thetas
				+ obj2->dtheta2000);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
675
      if (FLAG(obj2.winposerr_theta1950))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
676
677
        obj2->winposerr_theta1950 = fmod_m90_p90(obj2->winposerr_thetas
				+ obj2->dtheta1950);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
      }
    }

  if (FLAG(obj2.winposerr_aw))
    {
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->winposerr_aw = (float)sqrt(pm2+temp);
    obj2->winposerr_bw = (float)sqrt(pm2-temp);
    }

  if (FLAG(obj2.winposerr_cxxw))
    {
/*-- Handle large, fully correlated profiles (can cause a singularity...) */
    if ((temp=xm2*ym2-xym*xym)<1e-6)
      {
      temp = 1e-6;
      xym *= 0.99999;
      }
    obj2->winposerr_cxxw = (float)(ym2/temp);
    obj2->winposerr_cyyw = (float)(xm2/temp);
    obj2->winposerr_cxyw = (float)(-2*xym/temp);
    }

  return;
  }


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
/***************************** astrom_psferrparam ***************************/
/*
Compute error ellipse parameters in WORLD and SKY coordinates.
*/
void	astrom_psferrparam(picstruct *field, objstruct *obj)
  {
   wcsstruct	*wcs;
   double	dx2,dy2,dxy, xm2,ym2,xym, temp,pm2, lm0,lm1,lm2,lm3;
   int		lng,lat, naxis;

  wcs = field->wcs;
  naxis = wcs->naxis;
  lng = wcs->lng;
  lat = wcs->lat;
  if (lng == lat)
    {
    lng = 0;
    lat = 1;
    }
  lm0 = obj2->jacob[lng+naxis*lng];
  lm1 = obj2->jacob[lat+naxis*lng];
  lm2 = obj2->jacob[lng+naxis*lat];
  lm3 = obj2->jacob[lat+naxis*lat];

/* All WORLD params based on 2nd order moments have to pass through here */
  dx2 = obj2->poserrmx2_psf;
  dy2 = obj2->poserrmy2_psf;
  dxy = obj2->poserrmxy_psf;
  obj2->poserrmx2w_psf = xm2 = lm0*lm0*dx2+lm1*lm1*dy2+lm0*lm1*dxy;
  obj2->poserrmy2w_psf = ym2 = lm2*lm2*dx2+lm3*lm3*dy2+lm2*lm3*dxy;
  obj2->poserrmxyw_psf = xym = lm0*lm2*dx2+lm1*lm3*dy2+(lm0*lm3+lm1*lm2)*dxy;
  temp=xm2-ym2;
  if (FLAG(obj2.poserrthetaw_psf))
    {
    obj2->poserrthetaw_psf = (fmod_m90_p90(temp==0.0)?
				(45.0):(0.5*atan2(2.0*xym,temp)/DEG));

/*-- Compute position angles in J2000 or B1950 reference frame */
    if (wcs->lng != wcs->lat)
      {
      if (FLAG(obj2.poserrthetas_psf))
        obj2->poserrthetas_psf = fmod_m90_p90(lng<lat?
		((obj2->poserrthetaw_psf>0.0?90:-90.0)-obj2->poserrthetaw_psf)
		: obj2->poserrthetaw_psf);
      if (FLAG(obj2.poserrtheta2000_psf))
        obj2->poserrtheta2000_psf = fmod_m90_p90(obj2->poserrthetas_psf
				+ obj2->dtheta2000);
      if (FLAG(obj2.poserrtheta1950_psf))
        obj2->poserrtheta1950_psf = fmod_m90_p90(obj2->poserrthetas_psf
				+ obj2->dtheta1950);
      }
    }

  if (FLAG(obj2.poserraw_psf))
    {
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->poserraw_psf = (float)sqrt(pm2+temp);
    obj2->poserrbw_psf = (float)sqrt(pm2-temp);
    }

  if (FLAG(obj2.poserrcxxw_psf))
    {
/*-- Handle large, fully correlated profiles (can cause a singularity...) */
    if ((temp=xm2*ym2-xym*xym)<1e-6)
      {
      temp = 1e-6;
      xym *= 0.99999;
      }
    obj2->poserrcxxw_psf = (float)(ym2/temp);
    obj2->poserrcyyw_psf = (float)(xm2/temp);
    obj2->poserrcxyw_psf = (float)(-2*xym/temp);
    }

  return;
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
784
/***************************** astrom_proferrparam ***************************/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
785
/*
Emmanuel Bertin's avatar
Emmanuel Bertin committed
786
Compute error ellipse parameters in WORLD and SKY coordinates.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
787
*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
788
void	astrom_proferrparam(picstruct *field, objstruct *obj)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
789
  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
790
791
792
   wcsstruct	*wcs;
   double	dx2,dy2,dxy, xm2,ym2,xym, temp,pm2, lm0,lm1,lm2,lm3;
   int		lng,lat, naxis;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
793

Emmanuel Bertin's avatar
Emmanuel Bertin committed
794
795
796
797
798
  wcs = field->wcs;
  naxis = wcs->naxis;
  lng = wcs->lng;
  lat = wcs->lat;
  if (lng == lat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
799
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
800
801
    lng = 0;
    lat = 1;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
802
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
803
804
805
806
  lm0 = obj2->jacob[lng+naxis*lng];
  lm1 = obj2->jacob[lat+naxis*lng];
  lm2 = obj2->jacob[lng+naxis*lat];
  lm3 = obj2->jacob[lat+naxis*lat];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
807

Emmanuel Bertin's avatar
Emmanuel Bertin committed
808
809
810
811
812
813
814
815
816
817
818
819
/* All WORLD params based on 2nd order moments have to pass through here */
  dx2 = obj2->poserrmx2_prof;
  dy2 = obj2->poserrmy2_prof;
  dxy = obj2->poserrmxy_prof;
  obj2->poserrmx2w_prof = xm2 = lm0*lm0*dx2+lm1*lm1*dy2+lm0*lm1*dxy;
  obj2->poserrmy2w_prof = ym2 = lm2*lm2*dx2+lm3*lm3*dy2+lm2*lm3*dxy;
  obj2->poserrmxyw_prof = xym = lm0*lm2*dx2+lm1*lm3*dy2+(lm0*lm3+lm1*lm2)*dxy;
  temp=xm2-ym2;
  if (FLAG(obj2.poserrthetaw_prof))
    {
    obj2->poserrthetaw_prof = (fmod_m90_p90(temp==0.0)?
				(45.0):(0.5*atan2(2.0*xym,temp)/DEG));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
820

Emmanuel Bertin's avatar
Emmanuel Bertin committed
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*-- Compute position angles in J2000 or B1950 reference frame */
    if (wcs->lng != wcs->lat)
      {
      if (FLAG(obj2.poserrthetas_prof))
        obj2->poserrthetas_prof = fmod_m90_p90(lng<lat?
		((obj2->poserrthetaw_prof>0.0?90:-90.0)-obj2->poserrthetaw_prof)
		: obj2->poserrthetaw_prof);
      if (FLAG(obj2.poserrtheta2000_prof))
        obj2->poserrtheta2000_prof = fmod_m90_p90(obj2->poserrthetas_prof
				+ obj2->dtheta2000);
      if (FLAG(obj2.poserrtheta1950_prof))
        obj2->poserrtheta1950_prof = fmod_m90_p90(obj2->poserrthetas_prof
				+ obj2->dtheta1950);
      }
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
836

Emmanuel Bertin's avatar
Emmanuel Bertin committed
837
  if (FLAG(obj2.poserraw_prof))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
838
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
839
840
841
842
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->poserraw_prof = (float)sqrt(pm2+temp);
    obj2->poserrbw_prof = (float)sqrt(pm2-temp);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
843
844
    }

Emmanuel Bertin's avatar
Emmanuel Bertin committed
845
846
847
848
849
850
851
852
853
854
855
856
  if (FLAG(obj2.poserrcxxw_prof))
    {
/*-- Handle large, fully correlated profiles (can cause a singularity...) */
    if ((temp=xm2*ym2-xym*xym)<1e-6)
      {
      temp = 1e-6;
      xym *= 0.99999;
      }
    obj2->poserrcxxw_prof = (float)(ym2/temp);
    obj2->poserrcyyw_prof = (float)(xm2/temp);
    obj2->poserrcxyw_prof = (float)(-2*xym/temp);
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
857
858
859
860
861

  return;
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
862
/*************************** astrom_profshapeparam ***************************/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
863
/*
Emmanuel Bertin's avatar
Emmanuel Bertin committed
864
Compute profile-fitting shape parameters in WORLD and SKY coordinates.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
865
*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
866
void	astrom_profshapeparam(picstruct *field, objstruct *obj)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
867
  {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
868
   wcsstruct	*wcs;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
869
870
871
   double	mat[9], tempmat[9], mx2wcov[9], dpdmx2[6], cov[4],
		dx2,dy2,dxy, xm2,ym2,xym, pm2, lm0,lm1,lm2,lm3, ct,st,
		temp, invstemp, den, invden, dval;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
872
   int		lng,lat, naxis;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
873

Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
  wcs = field->wcs;
  naxis = wcs->naxis;
  lng = wcs->lng;
  lat = wcs->lat;
  if (lng == lat)
    {
    lng = 0;
    lat = 1;
    }
  lm0 = obj2->jacob[lng+naxis*lng];
  lm1 = obj2->jacob[lat+naxis*lng];
  lm2 = obj2->jacob[lng+naxis*lat];
  lm3 = obj2->jacob[lat+naxis*lat];

/* Spheroid World coordinates */
  obj2->prof_spheroid_thetaerrw=obj2->prof_spheroid_thetaerr; /* quick & dirty*/
  if (FLAG(obj2.prof_spheroid_reffw))
    {
    ct = cos(obj2->prof_spheroid_theta*DEG);
    st = sin(obj2->prof_spheroid_theta*DEG);
    dx2 = obj2->prof_spheroid_reff*obj2->prof_spheroid_reff * (ct*ct
	+ st*st * obj2->prof_spheroid_aspect*obj2->prof_spheroid_aspect);
    dy2 = obj2->prof_spheroid_reff*obj2->prof_spheroid_reff * (st*st
	+ ct*ct * obj2->prof_spheroid_aspect*obj2->prof_spheroid_aspect);
    dxy = ct*st * obj2->prof_spheroid_reff*obj2->prof_spheroid_reff
	*(1.0 - obj2->prof_spheroid_aspect*obj2->prof_spheroid_aspect);
    xm2 = lm0*lm0*dx2 + lm1*lm1*dy2 + lm0*lm1*dxy;
    ym2 = lm2*lm2*dx2 + lm3*lm3*dy2 + lm2*lm3*dxy;
    xym = lm0*lm2*dx2 + lm1*lm3*dy2 + (lm0*lm3+lm1*lm2)*dxy;
    temp=xm2-ym2;
    if (FLAG(obj2.prof_spheroid_thetaw))
      {
      obj2->prof_spheroid_thetaw = fmod_m90_p90((temp == 0.0)?
		(45.0) : (0.5*atan2(2.0 * xym,temp)/DEG));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
908

Emmanuel Bertin's avatar
Emmanuel Bertin committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
      if (wcs->lng != wcs->lat)
        {
        if (FLAG(obj2.prof_spheroid_thetas))
          obj2->prof_spheroid_thetas = fmod_m90_p90(lng<lat?
			((obj2->prof_spheroid_thetaw>0.0?90:-90.0)
				- obj2->prof_spheroid_thetaw)
			: obj2->prof_spheroid_thetaw);
        if (FLAG(obj2.prof_spheroid_theta2000))
          obj2->prof_spheroid_theta2000=fmod_m90_p90(obj2->prof_spheroid_thetas
					+ obj2->dtheta2000);
        if (FLAG(obj2.prof_spheroid_theta1950))
          obj2->prof_spheroid_theta1950=fmod_m90_p90(obj2->prof_spheroid_thetas
					+ obj2->dtheta1950);
        }
      }
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->prof_spheroid_reffw = sqrt(pm2+temp);
    obj2->prof_spheroid_refferrw = obj2->prof_spheroid_reff > 0.0?
	obj2->prof_spheroid_refferr/obj2->prof_spheroid_reff
				*obj2->prof_spheroid_reffw
	: 0.0;
931
    obj2->prof_spheroid_aspectw = (obj2->prof_spheroid_reffw>0.0 && pm2>temp)?
Emmanuel Bertin's avatar
Emmanuel Bertin committed
932
933
934
935
936
937
938
				  sqrt(pm2-temp) / obj2->prof_spheroid_reffw
				: obj2->prof_spheroid_aspect;
    obj2->prof_spheroid_aspecterrw = obj2->prof_spheroid_aspect > 0.0?
	obj2->prof_spheroid_aspecterr/obj2->prof_spheroid_aspect
				*obj2->prof_spheroid_aspectw
	: 0.0;
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
939

Emmanuel Bertin's avatar
Emmanuel Bertin committed
940
941
942
/* Disk World coordinates */
  obj2->prof_disk_thetaerrw = obj2->prof_disk_thetaerr; /* quick & dirty*/
  if (FLAG(obj2.prof_disk_scalew))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
943
    {
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
    ct = cos(obj2->prof_disk_theta*DEG);
    st = sin(obj2->prof_disk_theta*DEG);
    dx2 = obj2->prof_disk_scale*obj2->prof_disk_scale * (ct*ct
	+ st*st * obj2->prof_disk_aspect*obj2->prof_disk_aspect);
    dy2 = obj2->prof_disk_scale*obj2->prof_disk_scale * (st*st
	+ ct*ct * obj2->prof_disk_aspect*obj2->prof_disk_aspect);
    dxy = ct*st * obj2->prof_disk_scale*obj2->prof_disk_scale
	*(1.0 - obj2->prof_disk_aspect*obj2->prof_disk_aspect);
    xm2 = lm0*lm0*dx2 + lm1*lm1*dy2 + lm0*lm1*dxy;
    ym2 = lm2*lm2*dx2 + lm3*lm3*dy2 + lm2*lm3*dxy;
    xym = lm0*lm2*dx2 + lm1*lm3*dy2 + (lm0*lm3+lm1*lm2)*dxy;
    temp=xm2-ym2;
    if (FLAG(obj2.prof_disk_thetaw))
      {
      obj2->prof_disk_thetaw = fmod_m90_p90((temp == 0.0)?
		(45.0) : (0.5*atan2(2.0 * xym,temp)/DEG));

/*---- Compute position angles in J2000 or B1950 reference frame */
      if (wcs->lng != wcs->lat)
        {
        if (FLAG(obj2.prof_disk_thetas))
          obj2->prof_disk_thetas = fmod_m90_p90(lng<lat?
			((obj2->prof_disk_thetaw>0.0?90:-90.0)
				- obj2->prof_disk_thetaw)
			: obj2->prof_disk_thetaw);
        if (FLAG(obj2.prof_disk_theta2000))
          obj2->prof_disk_theta2000 = fmod_m90_p90(obj2->prof_disk_thetas
					+ obj2->dtheta2000);
        if (FLAG(obj2.prof_disk_theta1950))
          obj2->prof_disk_theta1950 = fmod_m90_p90(obj2->prof_disk_thetas
					+ obj2->dtheta1950);
        }
      }
    temp = sqrt(0.25*temp*temp+xym*xym);
    pm2 = 0.5*(xm2+ym2);
    obj2->prof_disk_scalew = sqrt(pm2+temp);
    obj2->prof_disk_scaleerrw = obj2->prof_disk_scale > 0.0?
	obj2->prof_disk_scaleerr/obj2->prof_disk_scale*obj2->prof_disk_scalew
	: 0.0;
983
    obj2->prof_disk_aspectw = (obj2->prof_disk_scalew>0.0 && pm2>temp)?
Emmanuel Bertin's avatar
Emmanuel Bertin committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
				  sqrt(pm2-temp) / obj2->prof_disk_scalew
				: obj2->prof_disk_aspect;
    obj2->prof_disk_aspecterrw = obj2->prof_disk_aspect > 0.0?
	obj2->prof_disk_aspecterr/obj2->prof_disk_aspect*obj2->prof_disk_aspectw
	: 0.0;
/*-- Arms World coordinates */
    if (FLAG(obj2.prof_arms_scalew))
      {
      obj2->prof_arms_scalew = (obj2->prof_disk_scale > 0.0) ?
	  obj2->prof_arms_scale*obj2->prof_disk_scalew/obj2->prof_disk_scale
	: 0.0;
      obj2->prof_arms_scaleerrw = (obj2->prof_arms_scale > 0.0) ?
	obj2->prof_arms_scaleerr/obj2->prof_arms_scale*obj2->prof_arms_scalew
	: 0.0;
      obj2->prof_arms_startw = (obj2->prof_disk_scale > 0.0) ?
	  obj2->prof_arms_start*obj2->prof_disk_scalew/obj2->prof_disk_scale
	: 0.0;
For faster browsing, not all history is shown. View entire blame