IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 20536


Ignore:
Timestamp:
Nov 4, 2008, 4:31:01 PM (18 years ago)
Author:
eugene
Message:

adding the direct inversion for Grid values (cf JT)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/relphot/src/GridOps.c

    r20363 r20536  
    1919static int    **Ymeas;
    2020
    21 static int    **clist;
    22 static int    **mlist;
    23 static int     *Nlist;
     21static int    **clist; // link from measurement on a cell to catalog containing measurement
     22static int    **mlist; // link from measurement on a cell to measurement in a catalog
     23static int     *Nlist; // list of measurements for each grid cell
    2424static int     *NLIST;
    2525
     
    282282}
    283283
     284/* direct (non-iterative) solution for Mgrid values for all grid bins */
     285void setMgridDirect (Catalog *catalog, int Ncatalog) {
     286
     287  int **gotstar, **gridmeas;
     288  int i, j, k, N, Nmax, Ngood, Nbad, Nmos, Ncal, Nrel, Nsys, Nfit;
     289  double **A, **B, *Mjx, *Wjx;
     290  float Msys, Mrel, Mcal, Mmos, Merr, Wsys;
     291  double Mj, Wj;
     292 
     293  if (!USE_GRID) return;
     294
     295  ALLOCATE (A, double *, Ngrid);
     296  ALLOCATE (B, double *, Ngrid);
     297  for (i = 0; i < Ngrid; i++) {
     298    ALLOCATE (A[i], double, Ngrid);
     299    ALLOCATE (B[i], double, 1);
     300    memset (A[i], 0, Ngrid*sizeof(double));
     301    memset (B[i], 0, sizeof(double));
     302  }
     303
     304  Ngood = Nbad = Ncal = Nmos = Nrel = Nsys = 0;
     305
     306  ALLOCATE (gotstar, int *, Ncatalog);
     307  for (i = 0; i < Ncatalog; i++) {
     308    ALLOCATE (gotstar[i], int, catalog[i].Naverage);
     309  } 
     310
     311  // set up gridmeas table : grid index for each measurement
     312  ALLOCATE (gridmeas, int *, Ncatalog);
     313  for (i = 0; i < Ncatalog; i++) {
     314    ALLOCATE (gridmeas[i], int, catalog[i].Nmeasure);
     315    for (j = 0; j < catalog[i].Nmeasure; j++) {
     316      gridmeas[i][j] = -1;
     317    }
     318  } 
     319  for (i = 0; i < Ngrid; i++) {
     320    for (j = 0; j < Nlist[i]; j++) {
     321      int m, c;
     322      m = mlist[i][j];
     323      c = clist[i][j];
     324      gridmeas[c][m] = i;
     325    }
     326  }
     327
     328  // as we loop over the grid cells, we need to accumulate the values of Wjx for each star
     329  ALLOCATE (Wjx, double, Ngrid);
     330  ALLOCATE (Mjx, double, Ngrid);
     331
     332  // accumulate the elements of the matrix equation.  We have an equation of the form: Ax = B
     333  // where x is the vector of grid cell values G_x (x = 0 - Ngrid), A is an Ngrid x Ngrid matrix,
     334  // and B is an Ngrid vector.  For a cell A(x,y), we need the following elements from each
     335  // star which touches grid cell (x):
     336  //
     337  // Mj  : sum over all measurements of Msys / dMsys^2
     338  // Wj  : sum over all measurements of 1.0 / dMsys^2
     339  // Mjx : sum over all measurements which touch cell (x) of Msys / dMsys^2
     340  // Wjx : sum over all measurements which touch cell (x) of 1.0 / dMsys^2
     341
     342  // Mjx and Wjx can be calculated by summing over all measurements which touch the cell
     343  // Mj requires looping over stars which touch (x)
     344
     345  // this is tricky because we need to know both the measurements which touch a cell
     346  // and the stars for which any measurement touches a cell.  we need to accumulate
     347  // sums for each star which touches as cell on both bases.
     348
     349  for (i = 0; i < Ngrid; i++) {
     350   
     351    for (j = 0; j < Ncatalog; j++) {
     352      memset (gotstar[j], 0, catalog[j].Naverage*sizeof(int));
     353    } 
     354
     355    // we are looping over the stars, but doing so by looping over the set of measurements:
     356    // every star which touches this grid cell has a measurement in Nlist[i]
     357    for (j = 0; j < Nlist[i]; j++) {
     358     
     359      int mx, c, n, m0, Npts;
     360
     361      mx = mlist[i][j];
     362      c  = clist[i][j];
     363      n  = catalog[c].measure[mx].averef;
     364     
     365      // if we have already visited this star, skip the stuff below
     366      if (gotstar[c][n]) continue;
     367      gotstar[c][n] = TRUE;
     368
     369      // skip stars marked as BAD
     370      if (catalog[c].average[n].code & STAR_BAD) {
     371        Nrel ++;
     372        continue;
     373      }
     374
     375      m0 = catalog[c].average[n].measureOffset;
     376
     377      // we accumuate an entry for each cell
     378      memset (Wjx, 0, Ngrid*sizeof(double));
     379      memset (Mjx, 0, Ngrid*sizeof(double));
     380      Npts = Mj = Wj = 0.0;
     381
     382      // if we have not yet visited this star, accumulate the Mj, Wj entries
     383      for (k = 0; k < catalog[c].average[n].Nmeasure; k++) {
     384
     385        int m, Ng;
     386
     387        m = m0 + k;
     388
     389        // skip measurements marked as BAD
     390        if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
     391          Nbad ++;
     392          continue;
     393        }
     394
     395        // skip images marked as BAD
     396        Mcal = getMcal  (m, c);
     397        if (isnan(Mcal)) {
     398          Ncal ++;
     399          continue;
     400        }
     401
     402        // skip mosaics marked as BAD
     403        Mmos = getMmos  (m, c);
     404        if (isnan(Mmos)) {
     405          Nmos ++;
     406          continue;
     407        }
     408
     409        // select the color- and airmass-corrected observed magnitude for this star
     410        // XXX need to be able to turn off the color-correction until initial average mags are found
     411        // Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
     412        Msys = PhotCat (&catalog[c].measure[m]);
     413        if (isnan(Msys)) {
     414          Nsys++;
     415          continue;
     416        }
     417
     418        // mag-error for this measurement
     419        Merr =  MAX (catalog[c].measure[m].dM, MIN_ERROR);
     420
     421        // Wsys = 1.0 / SQ(Merr);
     422        Wsys = 1.0;
     423
     424        Ng = gridmeas[c][m];
     425        if (Ng == -1) continue;  // skip measurements which do not touch any cell
     426
     427        Mj += Msys * Wsys;  // we are only including measurements touching this cell
     428        Wj += Wsys;  // we are only including measurements touching this cell
     429        Npts ++;
     430        Ngood ++;
     431
     432        Mjx[Ng] += Msys * Wsys;  // we are only including measurements touching cell (x)
     433        Wjx[Ng] += Wsys;  // we are only including measurements touching cell (x)
     434      }
     435
     436      // some stars will not have any valid measurements, skip these
     437      if (Npts == 0) continue;
     438
     439      B[i][0] += Mj*Wjx[i]/Wj - Mjx[i];
     440      A[i][i] -= Wjx[i];
     441      for (k = 0; k < Ngrid; k++) {
     442        A[i][k] += Wjx[i]*Wjx[k]/Wj;
     443        // fprintf (stderr, "%3.0f ", Wjx[k]);
     444      }
     445    }
     446  }
     447
     448  if (1) {
     449
     450    FILE *f;
     451    Header header, theader;
     452    Matrix matrix;
     453
     454    /* we are writing to this file */
     455    f = fopen ("matrix.fits", "w");
     456    if (f == (FILE *) NULL) {
     457      fprintf (stderr, "cannot open matrix.fits for output\n");
     458      return;
     459    }
     460
     461    /* save grid mag values */
     462    gfits_init_header (&theader);
     463    theader.Naxes = 2;
     464    theader.Naxis[0] = Ngrid;
     465    theader.Naxis[1] = Ngrid;
     466    theader.bitpix   = -32;
     467    gfits_create_Theader (&theader, "IMAGE");
     468    gfits_modify (&theader, "EXTNAME", "%s", 1, "MATRIX");
     469    gfits_create_matrix  (&theader, &matrix);
     470    for (i = 0; i < Ngrid; i++) {
     471      for (j = 0; j < Ngrid; j++) {
     472        gfits_set_matrix_value (&matrix, i, j, (double) A[i][j]);
     473      }
     474    }
     475    gfits_fwrite_header (f, &theader);
     476    gfits_fwrite_matrix (f, &matrix);
     477    gfits_free_matrix (&matrix);
     478
     479    gfits_modify (&theader, "EXTNAME", "%s", 1, "TRPOSE");
     480    gfits_create_matrix  (&theader, &matrix);
     481    for (i = 0; i < Ngrid; i++) {
     482      for (j = 0; j < Ngrid; j++) {
     483        gfits_set_matrix_value (&matrix, i, j, (double) A[j][i]);
     484      }
     485    }
     486    gfits_fwrite_header (f, &theader);
     487    gfits_fwrite_matrix (f, &matrix);
     488    gfits_free_matrix (&matrix);
     489    fclose (f);
     490  }
     491
     492  dgaussjordan (A, B, Ngrid, 1);
     493
     494  fprintf (stderr, "grid cells fitted (Ngood: %d, Nbad: %d, Nmos: %d, Ncal: %d, Nrel: %d, Nsys: %d)\n", Ngood, Nbad, Nmos, Ncal, Nrel, Nsys);
     495
     496  for (i = 0; i < Ngrid; i++) {
     497    gridM[i] = B[i][0];
     498    gridS[i] = sqrt(A[i][i]);
     499    gridN[i] = N;
     500  }
     501
     502  free (Wjx);
     503  free (Mjx);
     504
     505  for (i = 0; i < Ngrid; i++) {
     506    free (A[i]);
     507  }
     508  free (A);
     509  free (B);
     510
     511  for (i = 0; i < Ncatalog; i++) {
     512    free (gotstar[i]);
     513    free (gridmeas[i]);
     514  } 
     515  free (gotstar);
     516  free (gridmeas);
     517}
     518
    284519/* determine Mgrid values for all grid bins */
    285520void setMgrid (Catalog *catalog) {
     
    437672void dump_grid () {
    438673
    439     int i, j, Nimage, Nbytes, Nformat;
     674  int i, j, Nimage, Nbytes, Nformat;
    440675  int *imlist;
    441676  FILE *f;
Note: See TracChangeset for help on using the changeset viewer.