IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 29, 2012, 2:15:01 PM (14 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ipp-20120905
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20120905

  • branches/eam_branches/ipp-20120905/pstamp/src/ppstampMakeStamp.c

    r33504 r34735  
    1919static void chipToSky(pmAstromObj *pt, pmFPA *fpa, pmChip *chip);
    2020static bool setMaskedToNAN(pmConfig *config, psImage *image, psImage *mask, psImage *variance);
     21static bool copySources(pmReadout *outputReadout, pmReadout *inputReadout, pmReadout *astromReadout, psRegion extractRegion);
    2122
    2223// convert the input chip's transforms to the output
     
    8081}
    8182
    82 static bool copyMetadata(pmFPAfile *output, pmFPAfile *input, pmChip *inChip, ppstampOptions *options, pmAstromObj *center)
     83static bool copyMetadata(pmFPAfile *output, pmFPAfile *input, pmChip *inChip, ppstampOptions *options, pmAstromObj *center, pmFPAfile *astrom, pmFPAview *inView)
    8384{
    8485    pmChip    *outChip;
    85     pmFPAview *view = pmFPAviewAlloc(0);
     86    pmFPAview *outView = pmFPAviewAlloc(0);
    8687
    8788    // our output file has a single chip
    88     view->chip = 0;
    89     outChip = pmFPAviewThisChip(view, output->fpa);
    90     psFree(view);
    91 
    92     // copy data from the input chip header to the output.
     89    outView->chip = 0;
     90    outChip = pmFPAviewThisChip(outView, output->fpa);
     91    psFree(outView);
     92    pmHDU *outHDU = pmHDUGetHighest(output->fpa, outChip, NULL);
     93
     94    // copy data from the input's chip header to the output.
    9395    // since some of the keywords might be duplicated we may not want to copy both
    9496
    95     pmHDU *inHDU  = pmHDUFromChip(inChip);
    96     pmHDU *outHDU = pmHDUGetHighest(output->fpa, outChip, NULL);
     97    pmHDU *inHDU  = NULL;
     98    if (strcmp(options->stage, "raw") && input != astrom) {
     99        // Copy the header from the astrometry file since it contains the psphot and psastro results
     100        pmChip *astromChip = pmFPAviewThisChip(inView, astrom->fpa);
     101        inHDU  = pmHDUFromChip(astromChip);
     102    } else {
     103        inHDU  = pmHDUFromChip(inChip);
     104    }
    97105
    98106    if (inHDU->header) {
     
    102110    }
    103111
    104     // copy the fpa concepts
     112    // copy the fpa and chip concepts
    105113    pmConceptsCopyFPA(output->fpa, input->fpa, false, false);
     114    // Needed to preserve FPA.ZP
    106115    pmConceptsCopyChip(outChip, inChip, false);
     116
     117    // copy the cell concepts (needed to get CELL.EXPOSURE which gets used to set EXPTIME)
    107118    pmCell *outCell = outChip->cells->data[0]; // The only output cell
    108119
     
    122133    }
    123134
    124     // If input had WCS convert it for stamp
     135    // If input had WCS convert it for the new image coordinate system of our stamp
    125136    if (input->fpa->toSky) {
    126137        // copy the input fpa's transforms
     
    135146        }
    136147    } else {
    137         psWarning("No WCS present in input\n");
    138     }
    139 
     148        psLogMsg("ppstampMakeStamp", PS_LOG_WARN, "No WCS present in input.");
     149    }
     150
     151    // Save our specific metadata
    140152    psMetadataAddF64(outHDU->header, PS_LIST_TAIL, "RA_DEG", PS_META_REPLACE, "Right Ascension of stamp center", RAD_TO_DEG(center->sky->r));
    141153    psMetadataAddF64(outHDU->header, PS_LIST_TAIL, "DEC_DEG", PS_META_REPLACE, "Declination of stamp center", RAD_TO_DEG(center->sky->d));
     154
     155    // XXX: TODO: save the region of interest boundary and the offset to be applied.
     156    // XXX: Gene thinks that there may be a way to handle this in the WCS
     157
     158    // Now a few sanity checks
     159    // Make sure zero point in the fpa concepts matches the observed value in the input header
     160    bool status;
     161    float zpt_obs = psMetadataLookupF32(&status, outHDU->header, "ZPT_OBS");
     162    if (status) {
     163        psMetadataAddF32(output->fpa->concepts, PS_LIST_TAIL, "FPA.ZP", PS_META_REPLACE, "Magnitude zero point", zpt_obs);
     164    }
    142165
    143166    ppstampVersionMetadata(outHDU->header, options);
     
    324347
    325348            if (options->censorMasked && !setMaskedToNAN(config, outReadout->image, outReadout->mask, outReadout->variance)) {
    326                  psError(PS_ERR_UNKNOWN, false, "failed to create postage stamp mask image\n");
    327                  status = false;
    328                  break;
     349                psError(PS_ERR_UNKNOWN, false, "failed to create postage stamp mask image\n");
     350                status = false;
     351                break;
    329352            }
    330353        }
     
    335358        status = true;
    336359
     360        if (options->writeCMF) {
     361            pmReadout *astromReadout = NULL;
     362            if (astrom->fpa != input->fpa) {
     363                astromReadout = pmFPAviewThisReadout(srcView, astrom->fpa);
     364            }
     365            if (!copySources(outReadout, astromReadout, astromReadout, extractRegion)) {
     366                psError(PS_ERR_UNKNOWN, false, "failed to extract sources from region of interest.\n");
     367                status = false;
     368                break;
     369            }
     370        }
     371
    337372        psFree(outReadout); // drop reference
    338373    }
     
    341376
    342377    if (status) {
    343         status = copyMetadata(output, input, inChip, options, center);
     378        // For raw stage we need to do more work to build a proper header
     379        status = copyMetadata(output, input, inChip, options, center, astrom, view);
    344380    }
    345381    return status ? PS_EXIT_SUCCESS : PS_EXIT_UNKNOWN_ERROR;
     
    428464    double dy = 0.5 * options->roip.dDEC / fpa->toSky->Ys;
    429465
    430     if (dx > 5000) {
    431         fprintf(stderr, "requested width %f too large reducing to 5000\n", dx);
    432         dx = 5000;
    433     }
    434     if (dy > 5000) {
    435         fprintf(stderr, "requested height %f too large reducing to 5000\n", dy);
    436         dy = 5000;
     466    // XXX: why do we limit this?
     467    if (dx > 8000) {
     468        psWarning( "requested width %f too large reducing to 8000\n", dx);
     469        dx = 8000;
     470    }
     471    if (dy > 8000) {
     472        psWarning( "requested height %f too large reducing to 8000\n", dy);
     473        dy = 8000;
    437474    }
    438475
     
    480517    ppstampOverlap   returnval = PPSTAMP_OFF;
    481518
    482 //    fprintf(stderr, "ppstampChipBounds: %s\n", psRegionToString(*chipBounds));
    483 
    484519    // set up the astrometry
    485520    pmHDU *hdu = pmFPAviewThisHDU(view, astrom->fpa);
     
    498533        }
    499534    } else {
    500         // we use a default FPA pixel scale of 1.0
    501535        if (!pmAstromReadWCS (input->fpa, chip, hdu->header, 1.0) && mustHaveAstrometry) {
    502536            psError(PS_ERR_UNKNOWN, false, "Unable to read WCS astrometry for input FPA.");
     
    537571            center->chip->yErr = 0;
    538572            onChip = true;
    539             chipToSky(center, input->fpa, chip);
     573            if (input->fpa->toSky) {
     574                chipToSky(center, input->fpa, chip);
     575            }
    540576        }
    541577    }
     
    550586                int width  = options->roip.dX;
    551587                int height = options->roip.dY;
    552                 if (width > 7000) {
    553                     fprintf(stderr, "requested width %d too large reducing to 7000\n", width);
    554                     width = 7000;
     588                if (width > 8000) {
     589                    psWarning( "requested width %d too large reducing to 8000\n", width);
     590                    width = 8000;
    555591                }
    556                 if (height > 7000) {
    557                     fprintf(stderr, "requested height %d too large reducing to 7000\n", height);
    558                     height = 7000;
     592                if (height > 8000) {
     593                    psWarning( "requested height %d too large reducing to 8000\n", height);
     594                    height = 8000;
    559595                }
    560596
     
    675711
    676712    if (!foundOverlap && (returnval == PS_EXIT_SUCCESS)) {
    677         fprintf(stderr, "ROI not found in input\n");
     713        psWarning( "ROI not found in input\n");
    678714        returnval = PSTAMP_NO_OVERLAP;
    679715    }
     
    739775    }
    740776
    741     // XXX: this shouldn't be a fprintf
    742     fprintf(stderr, "Censored %ld masked pixels\n", numCensored);
     777    psLogMsg("ppstamp", PS_LOG_DETAIL, "Censored %ld masked pixels\n", numCensored);
    743778
    744779    return true;
    745780}
     781
     782static bool copySources(pmReadout *outReadout, pmReadout *inReadout, pmReadout *astromReadout, psRegion extractRegion) {
     783    bool status;
     784
     785    // first look for detections in the input readout. Those would have come from the -sources file
     786    pmDetections *inDetections = psMetadataLookupPtr (&status, inReadout->analysis, "PSPHOT.DETECTIONS");
     787    if (!inDetections) {
     788        // No detections on the input readout. Try the astrometry readout if supplied.
     789        if (astromReadout) {
     790            inDetections = psMetadataLookupPtr (&status, astromReadout->analysis, "PSPHOT.DETECTIONS");
     791            if (!inDetections) {
     792                psLogMsg("ppstampMakeStamp", PS_LOG_WARN, "no detections found on input or astrometry readout\n");
     793                return false;
     794            } else {
     795                psLogMsg("ppstampMakeStamp", PS_LOG_INFO, "using detections from astrom file\n");
     796            }
     797        } else {
     798            psLogMsg("ppstampMakeStamp", PS_LOG_WARN, "no detections found on input readout\n");
     799            return false;
     800        }
     801    }
     802    if (!inDetections->allSources) {
     803        psLogMsg("ppstampMakeStamp", PS_LOG_WARN, "no sources array found on detections structure\n");
     804        return false;
     805    }
     806
     807    // Ok we have sources, allocate a detections structure and copy them to the output readout
     808    psArray *inSources = inDetections->allSources;
     809
     810    pmDetections *outDetections = pmDetectionsAlloc();
     811    psMetadataAddPtr (outReadout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN,
     812        "psphot detections", outDetections);
     813
     814    psArray *outSources = outDetections->allSources = psArrayAllocEmpty( 100 );
     815
     816    // include sources that are off the stamp by just a little bit
     817    #define EXPAND_SIZE 16
     818    psRegion sourceRegion = extractRegion;
     819    sourceRegion.x0 -= EXPAND_SIZE;
     820    sourceRegion.y0 -= EXPAND_SIZE;
     821    sourceRegion.x1 += EXPAND_SIZE;
     822    sourceRegion.y1 += EXPAND_SIZE;
     823
     824    for (int i = 0; i < inSources->n; i++ ) {
     825        pmSource *src = inSources->data[i];
     826
     827        float x = src->peak->xf;
     828        float y = src->peak->yf;
     829        if (x < sourceRegion.x0 || x > sourceRegion.x1 ||
     830            y < sourceRegion.y0 || y > sourceRegion.y1) {
     831            continue;
     832        }
     833        psArrayAdd(outSources, 100, src);
     834    }
     835
     836    return true;
     837}
Note: See TracChangeset for help on using the changeset viewer.