IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 6, 2012, 1:24:39 PM (14 years ago)
Author:
watersc1
Message:

Broken background warping. Most of the code is in place, still trying to figure out why the results don't look good.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20120906/pswarp/src/pswarpLoop.c

    r29928 r34411  
    179179        // read WCS data from the corresponding header
    180180        pmHDU *hdu = pmFPAviewThisHDU (view, astrom->fpa);
     181
     182       
    181183        if (bilevelAstrometry) {
    182184            if (!pmAstromReadBilevelChip (chip, hdu->header)) {
     
    196198        }
    197199
     200       
    198201        pmCell *cell;
    199202        while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
     
    227230
    228231                pswarpTransformReadout(output, readout, config);
    229 
     232               
    230233                if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
    231234                    psError(psErrorCodeLast(), false, "Unable to write files.");
     
    255258        goto DONE;
    256259    }
    257 
     260   
    258261    pmCell *outCell = output->parent;   ///< Output cell
    259262    pmChip *outChip = outCell->parent;  ///< Output chip
     
    450453    return true;
    451454}
     455
     456// Loop over the inputs, warp them to the output skycell and then write out the output.
     457bool pswarpLoopBackground(pmConfig *config, psMetadata *stats)
     458{
     459    bool status;
     460    bool mdok;                          // Status of MD lookup
     461
     462    const char *skyCamera = psMetadataLookupStr(NULL, config->arguments,
     463                                                "SKYCELL.CAMERA");  // Name of camera for skycell
     464    pmConfigCamerasCull(config, skyCamera);
     465    pmConfigRecipesCull(config, "PSWARP,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
     466
     467    // load the recipe
     468    psMetadata *recipe = psMetadataLookupPtr (&status, config->recipes, PSWARP_RECIPE);
     469    if (!recipe) {
     470        psError(PSWARP_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE);
     471        return false;
     472    }
     473
     474    if (!pswarpSetMaskBits(config)) {
     475        psError(psErrorCodeLast(), false, "failed to set mask bits");
     476        return NULL;
     477    }
     478
     479    // select the input data sources
     480    pmFPAfile *input = psMetadataLookupPtr(NULL, config->files, "PSWARP.BKGMODEL");
     481    if (!input) {
     482        psError(PSWARP_ERR_CONFIG, true, "Can't find input data!\n");
     483        return false;
     484    }
     485
     486    // use the external astrometry source if supplied
     487    pmFPAfile *astrom = psMetadataLookupPtr(NULL, config->files, "PSWARP.ASTROM");
     488    if (!astrom) {
     489        astrom = input;
     490    }
     491
     492    if (astrom->camera != input->camera) {
     493        psError(PSWARP_ERR_DATA, true, "Input camera and astrometry camera do not match.");
     494        return false;
     495    }
     496
     497    // select the output readout
     498    pmFPAview *view = pmFPAviewAlloc(0);
     499    view->chip = 0;
     500    view->cell = 0;
     501    view->readout = 0;
     502    pmReadout *output = pmFPAfileThisReadout(config->files, view, "PSWARP.OUTPUT.BKGMODEL");
     503    if (!output) {
     504        psError(PSWARP_ERR_CONFIG, true, "Can't find output background data!\n");
     505        return false;
     506    }
     507    psFree (view);
     508
     509    // Turn all skycell files on to generate them, and then turn them off for the loop over the input images
     510    // the input, which is in a different format.
     511    {
     512        pswarpFileActivation(config, detectorFiles, false);
     513        pswarpFileActivation(config, photFiles, false);
     514        pswarpFileActivation(config, independentFiles, false);
     515        pswarpFileActivation(config, skycellFiles, true);
     516        if (!pswarpIOChecksBefore(config)) {
     517            psError(psErrorCodeLast(), false, "Unable to read files.");
     518            goto DONE;
     519        }
     520        pswarpFileActivation(config, skycellFiles, false);
     521    }
     522
     523    // Read the input astrometry
     524    // XXX rather than use the activations here, this should just explicitly loop over the desired filerule
     525    {
     526        pmFPAfileActivate(config->files, true, "PSWARP.ASTROM");
     527
     528        pmChip *chip;
     529        pmFPAview *view = pmFPAviewAlloc(0);
     530        if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     531            psError(psErrorCodeLast(), false, "Unable to read files.");
     532            goto DONE;
     533        }
     534        while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
     535          psTrace ("pswarp", 4, "AChip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
     536            if (!chip->process || !chip->file_exists) { continue; }
     537            if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     538                psError(psErrorCodeLast(), false, "Unable to read files.");
     539                goto DONE;
     540            }
     541            pmCell *cell;
     542            while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
     543                psTrace ("pswarp", 4, "ACell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
     544                if (!cell->process || !cell->file_exists) { continue; }
     545                if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE) ||
     546                    !pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
     547                    psError(psErrorCodeLast(), false, "Unable to read files.");
     548                    goto DONE;
     549                }
     550            }
     551            if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
     552                psError(psErrorCodeLast(), false, "Unable to write files.");
     553                goto DONE;
     554            }
     555        }
     556        if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
     557            psError(psErrorCodeLast(), false, "Unable to write files.");
     558            goto DONE;
     559        }
     560        psFree(view);
     561
     562        pswarpFileActivation(config, detectorFiles, true);
     563        pmFPAfileActivate(config->files, false, "PSWARP.ASTROM");
     564    }
     565
     566/*     // Turn on the source output --- we need to get rid of these so that we can measure the PSF */
     567/*     pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT.SOURCES"); */
     568
     569    // Don't care about the skycell anymore --- we've read it, and that's all we need to do.
     570    pmFPAfileActivate(config->files, false, "PSWARP.SKYCELL");
     571    view = pmFPAviewAlloc(0);
     572
     573    // find the FPA phu
     574    bool bilevelAstrometry = false;
     575    pmHDU *phu = pmFPAviewThisPHU(view, astrom->fpa);
     576    if (phu) {
     577        char *ctype = psMetadataLookupStr(NULL, phu->header, "CTYPE1");
     578        if (ctype) {
     579            bilevelAstrometry = !strcmp (&ctype[4], "-DIS");
     580        }
     581    }
     582    if (bilevelAstrometry) {
     583        if (!pmAstromReadBilevelMosaic(input->fpa, phu->header)) {
     584            psError(psErrorCodeLast(), false, "Unable to read bilevel mosaic astrometry for input FPA.");
     585            psFree(view);
     586            psFree(stats);
     587            goto DONE;
     588        }
     589    }
     590
     591
     592
     593    psList *cells = psListAlloc(NULL);  // List of cells, for concepts averaging
     594
     595    // files associated with the science image
     596    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
     597        psError(psErrorCodeLast(), false, "Unable to read files.");
     598        goto DONE;
     599    }
     600
     601    pmChip *chip;
     602    while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
     603        psTrace ("pswarp", 4, "DChip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
     604        if (!chip->process || !chip->file_exists) { continue; }
     605        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
     606            psError(psErrorCodeLast(), false, "Unable to read files.");
     607            goto DONE;
     608        }
     609
     610        // read WCS data from the corresponding header
     611        pmHDU *hdu = pmFPAviewThisHDU (view, astrom->fpa);
     612        psMetadataAddS32(config->arguments,PS_LIST_TAIL,
     613                         "INTERPOLATION.MODE",PS_META_REPLACE,"", PS_INTERPOLATE_BILINEAR); ///< Mode
     614        psMetadataAddS32(config->arguments,PS_LIST_TAIL,
     615                         "INTERPOLATION.NUM",PS_META_REPLACE,"", 64); ///< Mode
     616       
     617        if (bilevelAstrometry) {
     618            if (!pmAstromReadBilevelChip (chip, hdu->header)) {
     619                psError(psErrorCodeLast(), false, "Unable to read bilevel chip astrometry for input FPA.");
     620                psFree(view);
     621                psFree(stats);
     622                goto DONE;
     623            }
     624        } else {
     625            // we use a default FPA pixel scale of 1.0
     626            if (!pmAstromReadWCS (input->fpa, chip, hdu->header, 1.0)) {
     627                psError(psErrorCodeLast(), false, "Unable to read WCS astrometry for input FPA.");
     628                psFree(view);
     629                psFree(stats);
     630                goto DONE;
     631            }
     632        }
     633#define CORRECT_INPUT_WCS 1
     634#if CORRECT_INPUT_WCS
     635        // Correct the input WCS
     636        pmAstromWCS *WCS = pmAstromWCSfromHeader(hdu->header);
     637
     638        double cd1f = 1.0 * 400;
     639        double cd2f = 1.0 * 400;
     640
     641        WCS->crpix1 = WCS->crpix1 / cd1f + 0.5;
     642        WCS->crpix2 = WCS->crpix2 / cd2f - 0.5;
     643
     644        WCS->cdelt1 *= cd1f;
     645        WCS->cdelt2 *= cd2f;
     646
     647        WCS->trans->x->coeff[1][0] *= cd1f;
     648        WCS->trans->x->coeff[0][1] *= cd2f;
     649        WCS->trans->y->coeff[1][0] *= cd1f;
     650        WCS->trans->y->coeff[0][1] *= cd2f;
     651
     652
     653        pmAstromWCStoHeader (hdu->header,WCS);
     654        // End WCS work.
     655#endif
     656       
     657        pmCell *cell;
     658        while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
     659            psTrace ("pswarp", 4, "DCell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
     660            if (!cell->process || !cell->file_exists) { continue; }
     661            if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
     662                psError(psErrorCodeLast(), false, "Unable to read files.");
     663                goto DONE;
     664            }
     665
     666            psListAdd(cells, PS_LIST_TAIL, cell);
     667
     668            // process each of the readouts
     669            pmReadout *readout;
     670            while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) {
     671                if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     672                    psError(psErrorCodeLast(), false, "Unable to read files.");
     673                    goto DONE;
     674                }
     675                if (!readout->data_exists) {
     676                    continue;
     677                }
     678
     679                // Copy the detections from the astrometry carrier to the input, so they can be accessed by
     680                // pswarpTransformReadout
     681                pmReadout *astromRO = pmFPAviewThisReadout(view, astrom->fpa); // Readout for astrometry
     682                pmDetections *detections = psMetadataLookupPtr(&mdok, astromRO->analysis, "PSPHOT.DETECTIONS"); // Sources from astrometry
     683                if (detections) {
     684                    psMetadataAddPtr(readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_ARRAY, "Sources from input astrometry", detections);
     685                }
     686
     687                for (int x = 0; x < readout->image->numCols; x++) {
     688                  for (int y = 0; y < readout->image->numRows; y++) {
     689                    readout->image->data.F32[y][x] = readout->image->data.F32[y][x] * (cd1f * cd2f) /
     690                      (psMetadataLookupS32(&mdok,config->arguments,"BKG.XGRID") *
     691                       psMetadataLookupS32(&mdok,config->arguments,"BKG.YGRID"));
     692                  }
     693                }
     694                psMetadataAddS32(config->arguments,PS_LIST_TAIL, "INTERPOLATION.MODE", PS_META_REPLACE, "", 2);
     695/*              psMetadataAddS32(config->arguments,PS_LIST_TAIL, "GRID.NX",PS_META_REPLACE,"",100); ///< Number of grid points in x */
     696/*              psMetadataAddS32(config->arguments,PS_LIST_TAIL, "GRID.NY",PS_META_REPLACE,"",100); ///< Number of grid points in y */
     697
     698                pswarpTransformReadout(output, readout, config);
     699               
     700                if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     701                    psError(psErrorCodeLast(), false, "Unable to write files.");
     702                    goto DONE;
     703                }
     704            }
     705            if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     706                psError(psErrorCodeLast(), false, "Unable to write files.");
     707                goto DONE;
     708            }
     709        }
     710        if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     711            psError(psErrorCodeLast(), false, "Unable to write files.");
     712            goto DONE;
     713        }
     714    }
     715
     716    if (!output->data_exists) {
     717        psWarning("No overlap between input and skycell.");
     718/*         if (stats) { */
     719/*             psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE, */
     720/*                              "No overlap between input and skycell", PSWARP_ERR_NO_OVERLAP); */
     721/*         } */
     722        psphotFilesActivate(config, false);
     723        psFree(cells);
     724        psFree(view);
     725        goto DONE;
     726    }
     727   
     728    pmCell *outCell = output->parent;   ///< Output cell
     729    pmChip *outChip = outCell->parent;  ///< Output chip
     730    pmFPA *outFPA = outChip->parent;    ///< Output FP
     731
     732    if (!pswarpPixelsLit(output, stats, config)) {
     733        psError(psErrorCodeLast(), false, "Unable to calculate pixel regions.");
     734        psFree(cells);
     735        psFree(view);
     736        goto DONE;
     737    }
     738/*     bool doStats = psMetadataLookupBool(&mdok,recipe,"MASK.STATS"); */
     739/*     if (doStats) { */
     740/*       if (!pswarpMaskStats(output, stats, config)) { */
     741/*      psError(psErrorCodeLast(), false, "Unable to calculate mask stats."); */
     742/*      psFree(cells); */
     743/*      psFree(view); */
     744/*      goto DONE; */
     745/*       } */
     746/*     } */
     747/*     // Set covariance matrix for output */
     748/*     { */
     749/*         psList *covariances = psMetadataLookupPtr(&mdok, output->analysis, */
     750/*                                                   PSWARP_ANALYSIS_COVARIANCES); // Covariance matrices */
     751/*         psAssert(covariances, "Should be there"); */
     752/*         psArray *covars = psListToArray(covariances); // Array of covariance matrices */
     753/*         psKernel *covar = psImageCovarianceAverage(covars); */
     754/*         psFree(covars); */
     755/*         psMetadataRemoveKey(output->analysis, PSWARP_ANALYSIS_COVARIANCES); */
     756
     757/*         // Correct covariance matrix scale for the mean (square root of the) Jacobian */
     758/*         double jacobian = psMetadataLookupF64(NULL, output->analysis, PSWARP_ANALYSIS_JACOBIAN); // Jacobian */
     759/*         int goodPixels = psMetadataLookupS32(NULL, output->analysis, PSWARP_ANALYSIS_GOODPIX);   // Good pixels */
     760/*         jacobian /= goodPixels; */
     761/*         output->covariance = psImageCovarianceScale(covar, jacobian); */
     762/*         psFree(covar); */
     763
     764/*         if (output->variance) { */
     765/*             psImageCovarianceTransfer(output->variance, output->covariance); */
     766/*         } */
     767/*     } */
     768
     769/*     if (!pmConceptsAverageCells(outCell, cells, NULL, NULL, false)) { */
     770/*         psError(psErrorCodeLast(), false, "Unable to average cell concepts."); */
     771/*         psFree(stats); */
     772/*         psFree(cells); */
     773/*         psFree(view); */
     774/*         goto DONE; */
     775/*     } */
     776/*     psFree(cells); */
     777
     778    psRegion *trimsec = psMetadataLookupPtr(NULL, outCell->concepts, "CELL.TRIMSEC"); ///< Trim section
     779    trimsec->x0 = trimsec->x1 = trimsec->y0 = trimsec->y1 = 0; ///< All pixels
     780
     781    if (!psMetadataCopy(outFPA->concepts, input->fpa->concepts)) {
     782        psError(psErrorCodeLast(), false, "Unable to copy FPA concepts from input to output.");
     783        psFree(stats);
     784        psFree(view);
     785        goto DONE;
     786    }
     787
     788    // Update ZP from the astrometry
     789/*     { */
     790/*         psMetadataItem *item = psMetadataLookup(outFPA->concepts, "FPA.ZP"); */
     791/*         item->data.F32 = psMetadataLookupF32(NULL, astrom->fpa->concepts, "FPA.ZP"); */
     792/*     } */
     793
     794    pmHDU *hdu = outFPA->hdu;           ///< HDU for the output warped image
     795
     796    // Copy header from target
     797    {
     798        pmFPAview *skyView = pmFPAviewAlloc(0); ///< View into skycell
     799        skyView->chip = skyView->cell = 0;
     800        pmCell *cell = pmFPAfileThisCell(config->files, skyView, "PSWARP.SKYCELL"); // Skycell cell
     801        psFree(skyView);
     802        pmHDU *skyHDU = pmHDUFromCell(cell); ///< HDU
     803        if (!skyHDU) {
     804            psError(PSWARP_ERR_DATA, false, "Unable to find skycell HDU.");
     805            psFree(view);
     806            goto DONE;
     807        }
     808        hdu->header = psMetadataCopy(hdu->header, skyHDU->header);
     809    }
     810
     811    pswarpVersionHeader(hdu->header);
     812   
     813    if (!pmAstromWriteWCS(hdu->header, outFPA, outChip, WCS_NONLIN_TOL)) {
     814        psError(psErrorCodeLast(), false, "Unable to generate WCS header.");
     815        psFree(stats);
     816        goto DONE;
     817    }
     818
     819    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     820        psError(psErrorCodeLast(), false, "Unable to write files.");
     821        goto DONE;
     822    }
     823
     824    // Done with the detector side of things
     825    pswarpFileActivation(config, detectorFiles, false);
     826    pswarpFileActivation(config, independentFiles, false);
     827
     828
     829    // We need a new PSF model for the warped frame.  It would be good to generate this analytically, but
     830    // that's going to be tricky.  We have a list of sources, so we use those to redetermine the PSF model.
     831
     832/*     if (psMetadataLookupBool(&mdok, recipe, "PSF")) { */
     833/*         pswarpFileActivation(config, photFiles, true); */
     834/*         if (!pswarpIOChecksBefore(config)) { */
     835/*             psError(psErrorCodeLast(), false, "Unable to read files."); */
     836/*             goto DONE; */
     837/*         } */
     838
     839/*         // supply the readout and fpa of interest to psphot */
     840/*         pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT"); */
     841/*         pmFPACopy(photFile->fpa, outFPA); */
     842
     843/*         pmFPAview *view = pmFPAviewAlloc(0); ///< View into skycell */
     844/*         view->chip = view->cell = view->readout = 0; */
     845
     846/*         // grab the sources of interest from the storage location (pmFPAfile PSPHOT.INPUT.CMF) */
     847/*         psArray *sources = psphotLoadPSFSources (config, view); */
     848/*         if (!sources) { */
     849/*             psError(psErrorCodeLast(), false, "No sources supplied to measure PSF"); */
     850/*             goto DONE; */
     851/*         } */
     852
     853/*         pmModelClassSetLimits(PM_MODEL_LIMITS_STRICT); */
     854
     855/*         // measure the PSF using these sources */
     856/*         if (!psphotReadoutFindPSF(config, view, "PSPHOT.INPUT", sources)) { */
     857/*             // This is likely a data quality issue */
     858/*             // XXX Split into multiple cases using error codes? */
     859/*             psErrorStackPrint(stderr, "Unable to determine PSF"); */
     860/*             psWarning("Unable to determine PSF --- suspect bad data quality."); */
     861/*             if (stats && psMetadataLookupS32(NULL, stats, "QUALITY") == 0) { */
     862/*                 psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE, */
     863/*                                  "Unable to determine PSF", psErrorCodeLast()); */
     864/*             } */
     865/*             psErrorClear(); */
     866/*             psphotFilesActivate(config, false); */
     867/*         } */
     868
     869/*         // Ensure seeing is carried over */
     870/*         pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa);                 // Chip with seeing */
     871/*         psMetadataItem *item = psMetadataLookup(outChip->concepts, "CHIP.SEEING"); // Concept with seeing */
     872/*         item->data.F32 = psMetadataLookupF32(NULL, photChip->concepts, "CHIP.SEEING"); */
     873
     874/* // XXX EAM : put this in a visualization function */
     875/* #if (TESTING) */
     876/*         { */
     877/*             #define PSF_SIZE 20         ///< Half-size of PSF */
     878/*             #define PSF_FLUX 10000      ///< Central flux for PSF */
     879/*             pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa); */
     880/*             pmPSF *psf = psMetadataLookupPtr(NULL, photChip->analysis, "PSPHOT.PSF"); */
     881/*             psImage *image = psImageAlloc(2 * PSF_SIZE + 1, 2 * PSF_SIZE + 1, PS_TYPE_F32); */
     882/*             psImageInit(image, 0); */
     883/*             pmModel *model = pmModelFromPSFforXY(psf, PSF_SIZE, PSF_SIZE, PSF_FLUX); */
     884/*             pmModelAdd(image, NULL, model, PM_MODEL_OP_FULL, 0); */
     885/*             psFree(model); */
     886/*             psFits *fits = psFitsOpen("psf.fits", "w"); */
     887/*             psFitsWriteImage(fits, NULL, image, 0, NULL); */
     888/*             psFitsClose(fits); */
     889/*             psFree(image); */
     890/*         } */
     891/* #endif */
     892
     893/*         psFree(view); */
     894/*     } */
     895
     896/*     // Perform statistics on the output image */
     897/*     if (stats) { */
     898/*         if (!ppStatsFPA(stats, output->parent->parent->parent, view, maskValue, config)) { */
     899/*             psWarning("Unable to perform statistics on warped image."); */
     900/*         } */
     901/*     } */
     902   
     903
     904    // Add MD5 information for readout
     905    const char *chipName = psMetadataLookupStr(NULL, output->parent->parent->concepts, "CHIP.NAME");
     906    const char *cellName = psMetadataLookupStr(NULL, output->parent->concepts, "CELL.NAME");
     907    psString headerName = NULL; ///< Header name for MD5
     908    psStringAppend(&headerName, "MD5_%s_%s_%d", chipName, cellName, view->readout);
     909    psVector *md5 = psImageMD5(output->image); ///< md5 hash
     910    psString md5string = psMD5toString(md5); ///< String
     911    psFree(md5);
     912    psMetadataAddStr(hdu->header, PS_LIST_TAIL, headerName, PS_META_REPLACE,
     913                     "Image MD5", md5string);
     914    psFree(md5string);
     915    psFree(headerName);
     916    psFree(view);
     917
     918 DONE:
     919
     920    return true;
     921}
Note: See TracChangeset for help on using the changeset viewer.