1
/*===========================================================================
2
Copyright (C) 1995-2009 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
19
Correspondence concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
.IDENTIFIER mo_links.c
30
.AUTHOR R.H. Warmels IPG-ESO Garching
31
.KEYWORDS alignment software
33
.PURPOSE Routine to compute the shift for each subframe
35
#include <ccd_def.h> Symbols used by the ccd package
36
.VERSION 1.0 16-May-1995 creation
39
------------------------------------------------------------*/
41
* Define _POSIX_SOURCE to indicate
42
* that this is a POSIX program
44
#define _POSIX_SOURCE 1
47
* definition of the used functions in this module
50
#include <midas_def.h>
62
MO_LINK -- Routine to compute the shifts directly. Input is given
63
by an input table containing the coordinates of common objects
66
int MO_LINKS(tblco, col, xrshift, yrshift, xcshift, ycshift, nrshift,
67
ncshift, ncols, nrows, nxrsub, nyrsub, nxsub, nysub, nxoverlap,
68
nyoverlap, order, nshifts)
70
int *col; /* columns containing the shifts */
71
float (*xrshift)[MAXFRM];
72
float (*yrshift)[MAXFRM];
73
float (*xcshift)[MAXFRM];
74
float (*ycshift)[MAXFRM];
75
int (*nrshift)[MAXFRM]; /* number of row shifts */
76
int (*ncshift)[MAXFRM]; /* number of column shifts */
77
int ncols; /* number of columns per subraster */
78
int nrows; /* number of rows per subraster */
79
int nxrsub; /* column index of reference subraster */
80
int nyrsub; /* column index of reference subraster */
81
int nxsub; /* number of subrasters in X */
82
int nysub; /* number of subrasters in y */
83
int nxoverlap; /* number of columns of overlap */
84
int nyoverlap; /* number of rows of overlap */
85
char *order; /* row or column order */
89
int i, j, nxsize, nysize, ilimit, olimit;
91
int *nrowavg, *ncolavg;
93
float *xcolavg, *ycolavg, *xrowavg, *yrowavg;
94
float isign, jsign, xrmed, yrmed, xcmed, ycmed;
100
if (strcmp(order,MO_COLUMN) == 0)
112
Accumulate the shifts.
114
nxsize = ncols - nxoverlap;
115
nysize = nrows - nyoverlap;
116
MO_GET_SHIFTS(tblco, col, xrshift, yrshift, nrshift, xcshift,
117
ycshift, ncshift, nxsub, nysub, nxrsub, nyrsub,
118
nxoverlap, nyoverlap, nxsize, nysize, &ns);
122
xcolavg = (float *) osmmget( olimit * sizeof( float ));
123
ycolavg = (float *) osmmget( olimit * sizeof( float ));
124
xrowavg = (float *) osmmget( olimit * sizeof( float ));
125
yrowavg = (float *) osmmget( olimit * sizeof( float ));
126
nrowavg = (int *) osmmget( olimit * sizeof( int ));
127
ncolavg = (int *) osmmget( olimit * sizeof( int ));
129
for (i = 0; i < olimit; i++) *(xcolavg+i) = 0.0;
130
for (i = 0; i < olimit; i++) *(ycolavg+i) = 0.0;
131
for (i = 0; i < olimit; i++) *(xrowavg+i) = 0.0;
132
for (i = 0; i < olimit; i++) *(yrowavg+i) = 0.0;
133
for (i = 0; i < olimit; i++) *(nrowavg+i) = 0.0;
134
for (i = 0; i < olimit; i++) *(ncolavg+i) = 0.0;
137
Compute the row or column sums.
139
if (strcmp(order,MO_COLUMN) == 0)
141
for (i = 0; i < nxsub; i++)
143
for (j = 0; j < nysub; j++)
145
if (nrshift[i][j] > 0)
147
*(xrowavg+i) = *(xrowavg+i) + fabs(xrshift[i][j]);
148
*(yrowavg+i) = *(yrowavg+i) + fabs(yrshift[i][j]);
149
*(nrowavg+i) = *(nrowavg+i) + 1;
152
if (ncshift[i][j] > 0)
154
*(xcolavg+i) = *(xcolavg+i) + fabs(xcshift[i][j]);
155
*(ycolavg+i) = *(ycolavg+i) + fabs(ycshift[i][j]);
156
*(ncolavg+i) = *(ncolavg+i) + 1;
164
for (i = 0; i < nysub; i++)
166
for (j = 0; j < nxsub; j++)
168
if (nrshift[j][i] > 0)
170
*(xrowavg+i) = *(xrowavg+i) + fabs(xrshift[j][i]);
171
*(yrowavg+i) = *(yrowavg+i) + fabs(yrshift[j][i]);
172
*(nrowavg+i) = *(nrowavg+i) + 1;
175
if (ncshift[j][i] > 0)
177
*(xcolavg+i) = *(xcolavg+i) + fabs(xcshift[j][i]);
178
*(ycolavg+i) = *(ycolavg+i) + fabs(ycshift[j][i]);
179
*(ncolavg+i) = *(ncolavg+i) + 1;
186
Compute the averages.
188
for (i = 0; i < olimit; i++)
190
if (*(nrowavg+i) > 0)
192
*(xrowavg+i) = *(xrowavg+i) / *(nrowavg+i);
193
*(yrowavg+i) = *(yrowavg+i) / *(nrowavg+i);
195
if ( (*ncolavg+i) > 0)
197
*(xcolavg+i) = *(xcolavg+i) / *(ncolavg+i);
198
*(ycolavg+i) = *(ycolavg+i) / *(ncolavg+i);
203
Compute the medians of the row and column averages.
205
MO_MEDR(xrowavg, nrowavg, olimit, &xrmed);
206
MO_MEDR(yrowavg, nrowavg, olimit, &yrmed);
207
MO_MEDR(xcolavg, ncolavg, olimit, &xcmed);
208
MO_MEDR(ycolavg, ncolavg, olimit, &ycmed);
211
Use the average shifts for subrasters with no information.
213
for (j = 1; j <= nysub; j++)
222
for (i = 1; i <= nxsub; i++)
231
if (nrshift[i-1][j-1] <= 0)
233
if ( *(nrowavg+i-1) <= 0)
235
xrshift[i-1][j-1] = isign * xrmed;
236
yrshift[i-1][j-1] = jsign * yrmed;
238
else if (strcmp(order,MO_COLUMN) == 0)
240
xrshift[i-1][j-1] = isign * *(xrowavg+i-1);
241
yrshift[i-1][j-1] = jsign * *(yrowavg+i-1);
245
xrshift[i-1][j-1] = isign * *(xrowavg+j-1);
246
yrshift[i-1][j-1] = jsign * *(yrowavg+j-1);
250
if (ncshift[i-1][j-1] <= 0)
252
if ( *(ncolavg+i-1) <= 0)
254
xcshift[i-1][j-1] = isign * xcmed;
255
ycshift[i-1][j-1] = jsign * ycmed;
257
else if (strcmp(order,MO_COLUMN) == 0)
259
xcshift[i-1][j-1] = isign * *(xcolavg+i-1);
260
ycshift[i-1][j-1] = jsign * *(ycolavg+i-1);
264
xcshift[i-1][j-1] = isign * *(xcolavg+j-1);
265
ycshift[i-1][j-1] = jsign * *(ycolavg+j-1);
272
osmmfree((char *) xcolavg);
273
osmmfree((char *) xrowavg);
274
osmmfree((char *) ycolavg);
275
osmmfree((char *) yrowavg);
276
osmmfree((char *) nrowavg);
277
osmmfree((char *) ncolavg);
284
MO_CLINKS -- Routine to compute the shift for each subframe.
285
Input is given by a fixed shift in x and y
288
int MO_CLINKS(xrshift, yrshift, xcshift, ycshift, nxrsub, nyrsub,
289
nxsub, nysub, xshift, yshift, nshifts)
291
float (*xrshift)[MAXFRM];
292
float (*yrshift)[MAXFRM];
293
float (*xcshift)[MAXFRM];
294
float (*ycshift)[MAXFRM];
307
for (j = 0; j < nysub; j++)
311
else if (j+1 < nyrsub)
316
for (i = 0; i < nxsub; i++)
320
else if (i+1 < nxrsub)
325
xrshift[i][j] = isign * fabs (xshift);
328
ycshift[i][j] = jsign * fabs (yshift);
336
MO_FLINKS -- Routine to fetch the shifts directly from the table
339
int MO_FLINKS(tblco,col,deltax, deltay, deltai, max_nshifts, nshifts)
340
int tblco; /* table containing the shifts */
341
int *col; /* columns containing the shifts */
342
float *deltax; /* x shifts */
343
float *deltay; /* y shifts */
344
float *deltai; /* intensity shifts */
345
int max_nshifts; /* maximum number of shifts */
359
(void) TCIGET(tblco, &ncolco, &nrowco, &nsortco, &allcolco, &allrowco);
362
for (i = 1; i <= nrowco; i++)
364
if (ns >= max_nshifts)
366
if (col[0] > 0 && col[1] > 0)
368
TCERDR(tblco,i,col[0],&data[0],&null);
369
TCERDR(tblco,i,col[1],&data[1],&null);
370
deltax[ns] = data[0];
371
deltay[ns] = data[1];
381
TCERDR(tblco,i,col[2],&data[2],&null);
382
deltai[ns] = data[2];
391
MO_GET_SHIFTS -- Procedure to accumulate shifts for each subraster.
394
int MO_GET_SHIFTS(tblco, col, xrshift, yrshift, nrshift, xcshift,
395
ycshift, ncshift, nxsub, nysub, nxrsub, nyrsub, nxoverlap,
396
nyoverlap, nxsize, nysize, nshift)
400
float (*xrshift)[MAXFRM];
401
float (*yrshift)[MAXFRM];
402
int (*nrshift)[MAXFRM];
403
float (*xcshift)[MAXFRM];
404
float (*ycshift)[MAXFRM];
405
int (*ncshift)[MAXFRM]; /* number of column shifts */
406
int nxsub; /* number of subrasters in x */
407
int nysub; /* number of subrasters in y */
408
int nxrsub; /* column index of reference subraster */
409
int nyrsub; /* column index of reference subraster */
410
int nxoverlap; /* number of columns of overlap */
411
int nyoverlap; /* number of rows of overlap */
412
int nxsize; /* number of columns per subraster */
413
int nysize; /* number of rows per subraster */
417
int allcolco, allrowco;
420
int ncolco, nrowco, nsortco;
422
int nx1, ny1, nx11, ny11;
423
int nx2, ny2, nx21, ny21;
426
float x1, y1, x2, y2;
427
float xdif, xdifm, ydif, ydifm;
434
(void) TCIGET(tblco, &ncolco, &nrowco, &nsortco, &allcolco, &allrowco);
438
if (col[0] == -1 || col[1] == -1)
440
stat1 = TCERDR(tblco,irow,col[0],&x1,&null);
441
stat2 = TCERDR(tblco,irow,col[1],&y1,&null);
442
if (stat1 !=0 || stat2 != 0)
446
Compute which subraster 1 belongs to.
448
if (fmod( (int) x1, nxsize) == 0)
449
nx1 = (int) x1 / nxsize;
451
nx1 = (int) x1 / nxsize + 1;
453
if (fmod( (int) y1, nysize) == 0)
454
ny1 = (int) y1 / nysize;
456
ny1 = (int) y1 / nysize + 1;
459
Get the second coordinate pair.
463
if (irow > nrowco) break;
464
stat1 = TCERDR(tblco,irow,col[0],&x2,&null);
465
stat2 = TCERDR(tblco,irow,col[1],&y2,&null);
468
Compute which subraster 2 belongs to.
470
if ((stat1 == 0) & (stat2 == 0))
472
if (fmod( (int) x2, nxsize) == 0)
473
nx2 = (int) x2 / nxsize;
475
nx2 = (int) x2 / nxsize + 1;
477
if (fmod( (int) y2, nysize) == 0)
478
ny2 = (int) y2 / nysize;
480
ny2 = (int) y2 / nysize + 1;
483
while (stat1 != 0 || stat2 != 0);
486
if ((irow > nrowco) || (stat1 != 0 || stat2 != 0))
489
r21 = (nx1 - nxrsub) * (nx1 - nxrsub) + (ny1 - nyrsub) * (ny1 - nyrsub);
490
r22 = (nx2 - nxrsub) * (nx2 - nxrsub) + (ny2 - nyrsub) * (ny2 - nyrsub);
493
Compute the shift for the first subraster.
504
xdifm = xdif - nxoverlap;
506
xdifm = xdif + nxoverlap;
515
ydifm = ydif - nyoverlap;
517
ydifm = ydif + nyoverlap;
526
xcshift[nx11][ny11] = xcshift[nx11][ny11] + xdif;
527
ycshift[nx11][ny11] = ycshift[nx11][ny11] + ydifm;
528
ncshift[nx11][ny11] = ncshift[nx11][ny11] + 1;
532
xrshift[nx11][ny11] = xrshift[nx1][ny11] + xdifm;
533
yrshift[nx11][ny11] = yrshift[nx1][ny11] + ydif;
534
nrshift[nx11][ny11] = nrshift[nx1][ny11] + 1;
543
Compute the shift for the second subraster.
551
xdifm = xdif - nxoverlap;
553
xdifm = xdif + nxoverlap;
562
ydifm = ydif - nyoverlap;
564
ydifm = ydif + nyoverlap;
574
xcshift[nx21][ny21] = xcshift[nx21][ny21] + xdif;
575
ycshift[nx21][ny21] = ycshift[nx21][ny21] + ydifm;
576
ncshift[nx21][ny21] = ncshift[nx21][ny21] + 1;
581
xrshift[nx21][ny21] = xrshift[nx21][ny21] + xdifm;
582
yrshift[nx21][ny21] = yrshift[nx21][ny21] + ydif;
583
nrshift[nx21][ny21] = nrshift[nx21][ny21] + 1;
591
while (irow <= nrowco);
594
Compute the final shifts.
596
for (j = 0; j < nysub; j++)
598
for (i = 0; i < nxsub; i++)
600
if (nrshift[i][j] > 0)
602
xrshift[i][j] = xrshift[i][j] / nrshift[i][j];
603
yrshift[i][j] = yrshift[i][j] / nrshift[i][j];
606
if (ncshift[i][j] > 0)
608
xcshift[i][j] = xcshift[i][j] / ncshift[i][j];
609
ycshift[i][j] = ycshift[i][j] / ncshift[i][j];