IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 12219


Ignore:
Timestamp:
Mar 4, 2007, 11:56:49 AM (19 years ago)
Author:
eugene
Message:

fixing fits

Location:
branches/dvo-mods-2007-02/Ohana/src/relastro/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/dvo-mods-2007-02/Ohana/src/relastro/src/FitChip.c

    r12205 r12219  
    44
    55  int i;
     6  CoordFit *fit;
    67
    7   fit_init (coords[0].Npolyterms);
     8  fit = fit_init (coords[0].Npolyterms);
    89  for (i = 0; i < Nmatch; i++) {
    910    if (raw[i].mask) continue;
    10     fit_add (raw[i].X, raw[i].Y, ref[i].L, ref[i].M);
     11    fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, 1.0);
    1112  }
    12   fit_eval ();
    13   fit_apply_coords (coords);
     13  fit_eval (fit);
     14  fit_apply_coords (fit, coords);
     15  fit_free (fit);
    1416  /* FitChip and FitSimple update the coords in different ways? maybe not... */
    1517}
  • branches/dvo-mods-2007-02/Ohana/src/relastro/src/fitpoly.c

    r12205 r12219  
    3232  ALLOCATE (fit[0].ysum, double *, fit[0].Nterms);
    3333  for (i = 0; i < fit[0].Nterms; i++) {
     34    ALLOCATE (fit[0].xsum[i], double, fit[0].Nterms);
     35    ALLOCATE (fit[0].ysum[i], double, fit[0].Nterms);
    3436    bzero (fit[0].xsum[i], fit[0].Nterms*sizeof(double));
    3537    bzero (fit[0].ysum[i], fit[0].Nterms*sizeof(double));
     
    130132  modfit = CoordsSetCenter (fit, xo, yo);
    131133
    132   // set pc1_1, pc1_2, pc2_1, pc2_2
     134  // set crpix1,2
     135  fit_to_coordterms (coords, modfit, 0, 0);
     136
     137  // set pc1_1, pc1_2, pc2_1, pc2_2 (cd1,cd2 = 1.0)
    133138  fit_to_coordterms (coords, modfit, 0, 1);
    134139  fit_to_coordterms (coords, modfit, 1, 0);
    135140
     141  // set the polyterm elements
    136142  fit_to_coordterms (coords, modfit, 0, 2);
    137143  fit_to_coordterms (coords, modfit, 1, 1);
     
    143149  fit_to_coordterms (coords, modfit, 3, 0);
    144150
    145   /* get the correct vector entries for the linear terms */
    146   coords[0].crval1 = vector[N][0]; 
    147   coords[0].crval2 = vector[N][1];
     151  /* we do not modify crval1,2: these are kept at the default values */
    148152
    149   N = mkvector (1, 0, NORDER);
    150   c11 = vector[N][0]; 
    151   c21 = vector[N][1];
    152   N = mkvector (0, 1, NORDER);
    153   c12 = vector[N][0]; 
    154   c22 = vector[N][1];
     153  /* normalize pc11,etc */
     154  c11 = coords[0].pc1_1;
     155  c21 = coords[0].pc1_1;
     156  c12 = coords[0].pc1_1;
     157  c22 = coords[0].pc1_1;
    155158  coords[0].cdelt1 = coords[0].cdelt2 = sqrt(fabs(c11*c22 - c12*c21));
    156159  R = 1 / coords[0].cdelt1;
     
    161164  coords[0].pc2_2  = c22*R;
    162165
    163   coords[0].crpix1 = 0;
    164   coords[0].crpix2 = 0;
    165 
    166   coords[0].Npolyterms = NORDER;
    167   strcpy (coords[0].ctype, "DEC--PLY");
     166  /* keep the order and type from initial values */
    168167}
    169168
    170 /*
    171   if we have just linear terms, the following holds for crpix1,2:
    172   D = R / (coords[0].pc1_1*coords[0].pc2_2 - coords[0].pc1_2*coords[0].pc2_1);
    173   coords[0].crpix1 = D * (coords[0].pc1_2*c20 - coords[0].pc2_2*c10);
    174   coords[0].crpix2 = D * (coords[0].pc2_1*c10 - coords[0].pc1_1*c20);
    175 */
    176 
    177 /* NORDER is order of gradient fit : Npolyterms is Norder + 1 */
    178 void fit_apply_grads (Coords *distort, Coords *project, int term) {
    179 
    180   int i, j, Np, Nv1, Nv2;
    181 
    182   distort[0].Npolyterms = NORDER + 1;
    183   if (distort[0].Npolyterms > 1) strcpy (distort[0].ctype, "DEC--PLY");
    184 
    185   for (i = 0; i < NPOWER + 1; i++) {
    186     for (j = 0; j < (NPOWER + 1 - i); j++) {
    187      
    188       if (i + j < 2) continue;
    189       Np  = mkpolyterm (i, j);
    190       Nv1 = mkvector (i-1, j, NORDER);
    191       Nv2 = mkvector (i, j-1, NORDER);
    192 
    193       /** why do we have the negative sign? **/
    194       if (j == 0) {
    195         distort[0].polyterms[Np][term] = vector[Nv1][0] / i;
    196       }
    197       if (i == 0) {
    198         distort[0].polyterms[Np][term] = vector[Nv2][1] / j;
    199       }
    200       if ((i > 0) && (j > 0)) {
    201         distort[0].polyterms[Np][term] = 0.5*(vector[Nv1][0] / i + vector[Nv2][1] / j);
    202       }
    203     }
    204   }
    205 
    206   Nv1 = mkvector (0, 0, NORDER);
    207   if (term == 0) {
    208     project[0].pc1_1 = project[0].pc1_1 * (1 + vector[Nv1][0]);
    209     project[0].pc1_2 = project[0].pc1_2 * (1 + vector[Nv1][1]);
    210   } else {
    211     project[0].pc2_1 = project[0].pc2_1 * (1 + vector[Nv1][0]);
    212     project[0].pc2_2 = project[0].pc2_2 * (1 + vector[Nv1][1]);
    213   }
    214 }
    215 
    216 void fit_correct_grads (Gradients *in, Gradients *out, int term) {
    217 
    218   int i, k, m, n;
    219   double x, y, dx, dy, dz1, dz2;
    220 
    221   for (i = 0; i < in[0].Npts; i++) {
    222    
    223     dx = in[0].Lo[i];
    224     dy = in[0].Mo[i];
    225     dz1 = dz2 = 0.0;
    226 
    227     k = 0;
    228     x = y = 1;
    229     for (m = 0; m < NPOWER; m++) {
    230       x = y;
    231       for (n = 0; n < NPOWER - m; n++, k++) {
    232         dz1 += vector[k][0]*x;
    233         dz2 += vector[k][1]*x;
    234         x = x * dx / SCALE;
    235       }
    236       y = y * dy / SCALE;
    237     }
    238 
    239     out[0].Lo[i] = dx;
    240     out[0].Mo[i] = dy;
    241     if (term == 0) {
    242       out[0].dPdL[i] = in[0].dPdL[i] - dz1;
    243       out[0].dPdM[i] = in[0].dPdM[i] - dz2;
    244     } else {
    245       out[0].dQdL[i] = in[0].dQdL[i] - dz1;
    246       out[0].dQdM[i] = in[0].dQdM[i] - dz2;
    247     }
    248   }
    249 }
    250 
    251 int mkvector (int n, int m, int norder) {
    252  
    253   int i, N;
    254  
    255   N = 0;
    256   for (i = 0; i < m; i++) {
    257     N += (norder - i + 1);
    258   }
    259   N += n;
    260   return (N);
    261 }
    262 
    263 void fit_free () {
     169void fit_free (CoordFit *fit) {
    264170
    265171  int i;
    266172
    267   for (i = 0; i < NTERM; i++) {
    268     free (sum[i]);
    269     free (xsum[i]);
    270     free (ysum[i]);
     173  for (i = 0; i < fit[0].Nterms; i++) {
     174    free (fit[0].xsum[i]);
     175    free (fit[0].ysum[i]);
     176  }     
     177  free (fit[0].xsum);
     178  free (fit[0].ysum);
     179
     180  for (i = 0; i < fit[0].Nsums; i++) {
     181    free (fit[0].sum[i]);
    271182  }
    272   free (sum);
    273   free (xsum);
    274   free (ysum);
     183  free (fit[0].sum);
    275184
    276   for (i = 0; i < NPARS; i++) {
    277     free (matrix[i]);
    278     free (vector[i]);
     185  for (i = 0; i < fit[0].Nelems; i++) {
     186    free (fit[0].matrix[i]);
     187    free (fit[0].vector[i]);
    279188  }
    280   free (matrix);
    281   free (vector);
     189  free (fit[0].matrix);
     190  free (fit[0].vector);
     191
     192  free (fit);
    282193}
    283194 
  • branches/dvo-mods-2007-02/Ohana/src/relastro/src/mkpolyterm.c

    r12205 r12219  
    3737}
    3838
     39
     40// XXXX use a separate structure for the map (x2,y2 = f,g(x1,y1)) ?
     41
     42CoordsGetCenter () {
     43
     44// given a 2D transformation -- L(x,y),M(x,y) -- find the coordinates x,y
     45// for which L,M = 0,0. tol is the allowed error on x,y.
     46psPlane *psPlaneTransformGetCenter (psPlaneTransform *trans, double tol)
     47{
     48
     49    // crpix1,2 = X,Y(crval1,2)
     50    // start with linear solution for Xo,Yo:
     51    double R  = (trans->x->coeff[1][0]*trans->y->coeff[0][1] - trans->x->coeff[0][1]*trans->y->coeff[1][0]);
     52    double Xo = (trans->y->coeff[0][0]*trans->x->coeff[0][1] - trans->x->coeff[0][0]*trans->y->coeff[0][1])/R;
     53    double Yo = (trans->x->coeff[0][0]*trans->y->coeff[1][0] - trans->y->coeff[0][0]*trans->x->coeff[1][0])/R;
     54
     55    // iterate to actual solution: requires small non-linear terms
     56    if (trans->x->nX > 1) {
     57        psPolynomial2D *XdX = psPolynomial2D_dX(NULL, trans->x);
     58        psPolynomial2D *XdY = psPolynomial2D_dY(NULL, trans->x);
     59
     60        psPolynomial2D *YdX = psPolynomial2D_dX(NULL, trans->y);
     61        psPolynomial2D *YdY = psPolynomial2D_dY(NULL, trans->y);
     62
     63        psImage *Alpha = psImageAlloc (2, 2, PS_DATA_F32);
     64        psVector *Beta = psVectorAlloc (2, PS_DATA_F32);
     65
     66        /* this loop uses the Newton-Raphson method to solve for Xo,Yo
     67        * it needs the high order terms to be small
     68        * Xo,Yo are in pixels;
     69        */
     70        double dPos = tol + 1;
     71        for (int i = 0; (dPos > tol) && (i < 20); i++) {
     72            // NOTE: order for Alpha is: [y][x]
     73            Alpha->data.F32[0][0] = psPolynomial2DEval (XdX, Xo, Yo);
     74            Alpha->data.F32[1][0] = psPolynomial2DEval (XdY, Xo, Yo);
     75            Alpha->data.F32[0][1] = psPolynomial2DEval (YdX, Xo, Yo);
     76            Alpha->data.F32[1][1] = psPolynomial2DEval (YdY, Xo, Yo);
     77
     78            Beta->data.F32[0] = psPolynomial2DEval (trans->x, Xo, Yo);
     79            Beta->data.F32[1] = psPolynomial2DEval (trans->y, Xo, Yo);
     80
     81            psMatrixGJSolveF32 (Alpha, Beta);
     82
     83            Xo -= Beta->data.F32[0];
     84            Yo -= Beta->data.F32[1];
     85            dPos = hypot(Beta->data.F32[0], Beta->data.F32[1]);
     86        }
     87        psFree (Alpha);
     88        psFree (Beta);
     89        psFree (XdX);
     90        psFree (XdY);
     91        psFree (YdX);
     92        psFree (YdY);
     93    }
     94    psPlane *center = psPlaneAlloc ();
     95    center->x = Xo;
     96    center->y = Yo;
     97
     98    return center;
     99}
     100
     101// convert a transformation L(x,y) to L'(x-xo,y-yo)
     102psPlaneTransform *psPlaneTransformSetCenter (psPlaneTransform *output, psPlaneTransform *input, double Xo, double Yo)
     103{
     104
     105    // validate fit order
     106
     107    if (output == NULL) {
     108        output = psPlaneTransformAlloc(input->x->nX, input->x->nY);
     109    }
     110
     111    /* given two equivalent polynomial representations L(x,y) = \sum_i \sum_j A_{i,j} x^i y^j
     112     * we can transform L(x,y) into L'(x-xo,y-yo) by taking the derivatives of both sides and
     113     * noting that the constant term in each is the coefficient in the case of L(x,y) and is the
     114     * value of L'(-xo,-yo) in the second case.
     115     */
     116
     117    psPolynomial2D *tmp;
     118
     119    psPolynomial2D *xPx = psPolynomial2DCopy (NULL, input->x);
     120    psPolynomial2D *yPx = psPolynomial2DCopy (NULL, input->y);
     121
     122    for (int i = 0; i <= input->x->nX; i++) {
     123        psPolynomial2D *xPy = psPolynomial2DCopy (NULL, xPx);
     124        psPolynomial2D *yPy = psPolynomial2DCopy (NULL, yPx);
     125        for (int j = 0; j <= input->x->nY; j++) {
     126            output->x->mask[i][j] = input->x->mask[i][j];
     127            output->y->mask[i][j] = input->y->mask[i][j];
     128            output->x->coeff[i][j] = (output->x->mask[i][j]) ? 0 : psPolynomial2DEval (xPy, Xo, Yo) / tgamma(i+1) / tgamma(j+1);
     129            output->y->coeff[i][j] = (output->y->mask[i][j]) ? 0 : psPolynomial2DEval (yPy, Xo, Yo) / tgamma(i+1) / tgamma(j+1);
     130
     131            // take the next derivative wrt y, catch output (is NULL on last pass)
     132            tmp = psPolynomial2D_dY(NULL, xPy);
     133            psFree (xPy);
     134            xPy = tmp;
     135            tmp = psPolynomial2D_dY(NULL, yPy);
     136            psFree (yPy);
     137            yPy = tmp;
     138        }
     139        // take the next derivative wrt x, catch output (is NULL on last pass)
     140        tmp = psPolynomial2D_dX(NULL, xPx);
     141        psFree (xPx);
     142        xPx = tmp;
     143        tmp = psPolynomial2D_dX(NULL, yPx);
     144        psFree (yPx);
     145        yPx = tmp;
     146    }
     147    return output;
     148}
     149
    39150int mkpolyterm (int n, int m) {
    40151 
Note: See TracChangeset for help on using the changeset viewer.