IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 24, 2017, 4:20:46 PM (9 years ago)
Author:
watersc1
Message:

HSC changes that haven't been committed yet. Includes a number of configuration changes, improved glint masking, unfinished ghost masking, and the PSF model that I forgot to include previously.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20170908/psastro/src/psastroLoadGhosts.c

    r33859 r40179  
    4040 * calculate ghost FPA and Chip positions for the stars loaded on the FPA
    4141 */
     42
     43                   
     44
    4245bool psastroLoadGhosts (pmConfig *config) {
    4346
     
    256259    return false;
    257260}
     261
     262
     263 
     264bool psastroLoadGhostsHSC (pmConfig *config) {
     265  bool status;
     266    pmChip *chip = NULL;
     267    pmCell *cell = NULL;
     268    pmReadout *readout = NULL;
     269    float zeropt, exptime, MAX_MAG;
     270    psMetadata *ghostModel = NULL;
     271    psVector *C_terms;
     272    psVector *R_terms;
     273    float glintPixelScale;
     274   
     275    psLogMsg ("psastro", PS_LOG_INFO, "determine ghost positions");
     276
     277    // select the current recipe
     278    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSASTRO_RECIPE);
     279    if (!recipe) {
     280        psError(PSASTRO_ERR_CONFIG, true, "Can't find PSASTRO recipe");
     281        return false;
     282    }
     283
     284    bool REFSTAR_MASK_GHOST = psMetadataLookupBool (&status, recipe, "REFSTAR_MASK_GHOST");
     285    if (!REFSTAR_MASK_GHOST) return true;
     286
     287    // Load model from file
     288    char *ghostFile = psMetadataLookupStr (&status, recipe, "GHOST_MODEL");
     289    if (!strcasecmp(ghostFile, "NONE")) return true;
     290
     291    if (!pmConfigFileRead (&ghostModel, ghostFile, "GHOST MODEL")) {
     292        psError(PSASTRO_ERR_CONFIG, true, "Trouble loading ghost model");
     293        return false;
     294    }
     295
     296    // Allocate FPA and refstars
     297    pmFPAview *view = pmFPAviewAlloc (0);
     298    // select the input astrometry data (also carries the refstars)
     299    pmFPAfile *astrom = psMetadataLookupPtr (NULL, config->files, "PSASTRO.INPUT");
     300    if (!astrom) {
     301        psError(PSASTRO_ERR_CONFIG, true, "Can't find input data");
     302        goto escape;
     303    }
     304    pmFPA *fpa = astrom->fpa;
     305
     306    char *filter = psMetadataLookupStr (&status, fpa->concepts, "FPA.FILTERID");
     307    psMetadataItem *item = psMetadataLookup(glintItem->data.md, "GLINT.FILTER.TERM");
     308    if (!item) {
     309      psLogMsg ("psastro", PS_LOG_INFO, "GLINT.FILTER.TERM data missing");
     310      return false;
     311    }
     312    if (item->type != PS_DATA_METADATA_MULTI) {
     313      psLogMsg ("psastro", PS_LOG_INFO, "GLINT.FILTER.TERM not multi");
     314      return false;
     315    }
     316    psListIterator *iter = psListIteratorAlloc(item->data.list,PS_LIST_HEAD, false);
     317    psMetadataItem *refItem = NULL;
     318    while ((refItem = psListGetAndIncrement(iter))) {
     319      if (refItem->type != PS_DATA_METADATA) {
     320        psLogMsg ("psastro", PS_LOG_INFO, "GLINT.FILTER.TERM entry not metadata");
     321        return false;
     322      }
     323      char *refFilter = psMetadataLookupStr (&status, refItem->data.md, "FILTER");
     324      if (!status) {
     325        // psLogMsg ("psastro", PS_LOG_INFO, "a PHOTCODE.DATA recipe folder is missing FILTER");
     326        continue;
     327      }
     328      if (strcmp(refFilter, filter)) continue;
     329
     330      C_terms = psMetadataLookupPtr(&status, refItem->data.md, "C_TERMS");
     331      R_terms = psMetadataLookupPtr(&status, refItem->data.md, "R_TERMS");
     332      MAX_MAG = psMetadataLookupF32(&status, refItem->data.md, "LIMIT");
     333      glintPixelScale = psMetadataLookupF32(&status, refItem->data.md, "SCALE");
     334    }
     335
     336    // really error-out here?  or just skip?
     337    //    if (!psastroZeroPointFromRecipe (&zeropt, &exptime, &MAX_MAG, fpa, recipe)) {
     338    //        psLogMsg ("psastro", PS_LOG_INFO, "failed to load zeropt data from recipe");
     339    //  goto escape;
     340    //    }
     341   
     342    // recipe values are given in instrumental magnitudes
     343    // use the zero point and exposure time to convert to apparent mags: M_ap = M_inst + C_0 + 2.5*log(exptime)
     344    //    float MagOffset = zeropt + 2.5*log10(exptime);
     345    //    MAX_MAG += MagOffset;
     346
     347    // this loop selects the matched stars for all chips
     348    while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
     349        psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
     350        if (!chip->process || !chip->file_exists) { continue; }
     351        if (!chip->fromFPA) { continue; }
     352
     353        while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
     354            psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
     355            if (!cell->process || !cell->file_exists) { continue; }
     356
     357            // process each of the readouts
     358            while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
     359                if (! readout->data_exists) { continue; }
     360
     361                // select the raw objects for this readout (loaded in psastroChooseRefstars.c)
     362                // XXX : note that we place limits on the refstar sample in psastroChooseRefstars.c:
     363                // 1) on chip and 2) < PSASTRO.MAX.NREF. magnitude limits and clump exclusion are only
     364                psArray *refstars = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.REFSTARS");
     365                if (refstars == NULL) { continue; }
     366
     367                // identify the bright stars of interest
     368                for (int i = 0; i < refstars->n; i++) {
     369                    pmAstromObj *ref = refstars->data[i];
     370                    psTrace("psastro.ghost",5,"Begin ghost %d/%ld: MAX_MAG: %g; ref_mag: %g @ (%.10g,%.10g) TPA: (%f %f)",
     371                            i,refstars->n,MAX_MAG,ref->Mag,ref->sky->r * 180 / M_PI,ref->sky->d * 180.0 / M_PI,ref->TP->x,ref->RP->y);
     372                    if (ref->Mag > MAX_MAG) continue;
     373                   
     374                    psastroGhost *ghost = psastroGhostAlloc ();
     375
     376                    double R_TPA  = sqrt(pow(star->TP->y,2) + pow(star->TP->x,2));
     377                    double theta0 = atan2(star->TP->y,star->TP->x);
     378                    double star_radius_deg = R_TPA * glintPixelScale;
     379                    if (star_radius_deg > 0.5) { continue; }
     380
     381                    // Calculate expected position.
     382                    double C = C_terms->data.F32[0] + C_terms->data.F32[1] * star_radius_deg + C_terms->data.F32[2] * pow(star_radius_deg,2) +
     383                      C_terms->data.F32[3] * pow(star_radius_deg,3) + C_terms->data.F32[4] * pow(star_radius_deg,4) +
     384                      C_terms->data.F32[5] * pow(star_radius_deg,5) + C_terms->data.F32[6] * pow(star_radius_deg,6) +
     385                      C_terms->data.F32[7] * pow(star_radius_deg,7);
     386                    double R = R_terms->data.F32[0] + R_terms->data.F32[1] * star_radius_deg + R_terms->data.F32[2] * pow(star_radius_deg,2) +
     387                      R_terms->data.F32[3] * pow(star_radius_deg,3) + R_terms->data.F32[4] * pow(star_radius_deg,4) +
     388                      R_terms->data.F32[5] * pow(star_radius_deg,5) + R_terms->data.F32[6] * pow(star_radius_deg,6) +
     389                      R_terms->data.F32[7] * pow(star_radius_deg,7);
     390                    R = 13.5 * (R / 0.015); // Convert to TPA units.
     391
     392                    psPlane *fp = psPlaneAlloc();
     393                    psPlane *tp = psPlaneAlloc();
     394
     395                    tp->x = 13.5 * (C * cos(theta0) / 0.015 + 12.78);
     396                    tp->y = 13.5 * (C * sin(theta0) / 0.015 + 57.74);
     397                    psPlaneTransformApply(fp,fpa->fromTPA, tp);
     398                   
     399                    ghost->srcFP->x = ref->FP->x;
     400                    ghost->srcFP->y = ref->FP->y;
     401                    ghost->FP->x    = fp->x;
     402                    ghost->FP->y    = fp->y;
     403
     404                    ghost->inner.major = 0.0;
     405                    ghost->inner.minor = 0.0;
     406                    ghost->outer.major = R;
     407                    ghost->outer.minor = R;
     408                   
     409                    pmChip *ghostChip = psastroFindChip (&ghost->chip->x, &ghost->chip->y, fpa, ghost->FP->x, ghost->FP->y);
     410                   
     411
     412                   
     413                    if (ghostChip) {
     414                      psTrace("psastro.ghost",5,"   in ghost: %d/%ld: ref: (%f,%f) or (%s) ghost: (%f,%f) or (%f,%f,%s) %f inner: (%f,%f,%f) outer: (%f,%f,%f)",
     415                              i,refstars->n,
     416                              ref->FP->x,ref->FP->y,
     417                              psMetadataLookupStr(NULL,chip->concepts,"CHIP.NAME"),
     418                              ghost->FP->x,ghost->FP->y,
     419                              ghost->chip->x,ghost->chip->y,psMetadataLookupStr(NULL,ghostChip->concepts,"CHIP.NAME"),
     420                              rSrc,
     421                              ghost->inner.major,ghost->inner.minor,ghost->inner.theta,
     422                              ghost->outer.major,ghost->outer.minor,ghost->outer.theta);
     423/*                    psWarning("   GHOST_DATA: %d/%ld: ref: (%f,%f) or (%f,%f,%s) ghost: (%f,%f) or (%f,%f,%s) %f inner: (%f,%f,%f) outer: (%f,%f,%f)", */
     424/*                            i,refstars->n, */
     425/*                            ref->FP->x,ref->FP->y, */
     426/*                            ref_chip_x,ref_chip_y,psMetadataLookupStr(NULL,chip->concepts,"CHIP.NAME"), */
     427/*                            ghost->FP->x,ghost->FP->y, */
     428/*                            ghost->chip->x,ghost->chip->y,psMetadataLookupStr(NULL,ghostChip->concepts,"CHIP.NAME"), */
     429/*                            rSrc, */
     430/*                            ghost->inner.major,ghost->inner.minor,ghost->inner.theta, */
     431/*                            ghost->outer.major,ghost->outer.minor,ghost->outer.theta); */
     432                    }
     433                    else {
     434                      psTrace("psastro.ghost",5,"   in ghost: %d/%ld: ref: (%f,%f) ghost: (%f,%f) or (%f,%f,%s) inner: (%f,%f,%f) outer: (%f,%f,%f)",
     435                              i,refstars->n,
     436                              ref->FP->x,ref->FP->y,
     437                              ghost->FP->x,ghost->FP->y,
     438                              ghost->chip->x,ghost->chip->y,"NONE",
     439                              ghost->inner.major,ghost->inner.minor,ghost->inner.theta,
     440                              ghost->outer.major,ghost->outer.minor,ghost->outer.theta);
     441                    }                 
     442                     
     443
     444                           
     445                    if (!ghostChip) goto skip;
     446                    if (!ghostChip->cells) goto skip;
     447                    if (!ghostChip->cells->n) goto skip;
     448
     449                   
     450                    pmCell *ghostCell = ghostChip->cells->data[0];
     451                    if (!ghostCell) goto skip;
     452                    if (!ghostCell->readouts) goto skip;
     453                    if (!ghostCell->readouts->n) goto skip;
     454                    pmReadout *ghostReadout = ghostCell->readouts->data[0];
     455                    if (!ghostReadout) goto skip;
     456
     457                    psArray *ghosts = psMetadataLookupPtr (&status, ghostReadout->analysis, "PSASTRO.GHOSTS");
     458                    if (ghosts == NULL) {
     459                        ghosts = psArrayAllocEmpty (100);
     460                        if (!psMetadataAdd (ghostReadout->analysis, PS_LIST_TAIL, "PSASTRO.GHOSTS", PS_DATA_ARRAY, "astrometry matches", ghosts)) {
     461                          psError(PSASTRO_ERR_CONFIG, false, "failure to add ghosts to readout");
     462                          goto escape;
     463                        }
     464                        psFree (ghosts);
     465                    }
     466
     467                    psArrayAdd (ghosts, 100, ghost);
     468
     469                skip:
     470                   
     471                    psFree (ghost);
     472                }
     473            }
     474        }
     475    }
     476
     477    psastroExtractFreeChipBounds();
     478
     479    psFree (C_terms);
     480    psFree (R_terms);
     481    psFree (ghostModel);
     482    psFree (view);
     483    return true;
     484
     485escape:
     486    psFree (C_terms);
     487    psFree (R_terms);
     488    psFree (ghostModel);
     489    psFree (view);
     490    return false;
     491}
Note: See TracChangeset for help on using the changeset viewer.