IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 11, 2012, 2:04:31 PM (13 years ago)
Author:
watersc1
Message:

Merge from my branch including background restoration and stack stage projection cell binned images.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/ppSkycell/src/ppSkycellLoop.c

    r28375 r34800  
    1515                         )
    1616{
    17     base->x0 = PS_MIN(base->x0, compare->x0);
    18     base->x1 = PS_MAX(base->x1, compare->x1);
     17    base->x0 = PS_MAX(base->x0, compare->x0);
     18    base->x1 = PS_MIN(base->x1, compare->x1);
    1919    base->y0 = PS_MIN(base->y0, compare->y0);
    2020    base->y1 = PS_MAX(base->y1, compare->y1);
     
    2727{
    2828    psPlane *fromCoords = psPlaneAlloc(), *toCoords = psPlaneAlloc(); // Coordinates for transforms
    29     psRegion *region = psRegionAlloc(INFINITY, -INFINITY, INFINITY, -INFINITY); // Region for skycell
     29    psRegion *region = psRegionAlloc(-INFINITY, INFINITY, INFINITY, -INFINITY); // Region for skycell
    3030
    3131// Limit the region using the nominated point (X,Y)
     
    3838    toCoords->x += wcs->crpix1; \
    3939    toCoords->y += wcs->crpix2; \
    40     region->x0 = PS_MIN(region->x0, toCoords->x); \
    41     region->x1 = PS_MAX(region->x1, toCoords->x); \
     40    region->x0 = PS_MAX(region->x0, toCoords->x); \
     41    region->x1 = PS_MIN(region->x1, toCoords->x); \
    4242    region->y0 = PS_MIN(region->y0, toCoords->y); \
    4343    region->y1 = PS_MAX(region->y1, toCoords->y);
     
    132132    psVector *target = psVectorAlloc(data->numInputs, PS_TYPE_S32); // Target for each input
    133133    psArray *imageRegions = psArrayAlloc(data->numInputs); // Region for image
    134 
     134    psArray *regionHDUs = psArrayAlloc(data->numInputs);
     135    // Determine which projection cells we have to deal with.
    135136    for (int i = 0; i < data->numInputs; i++) {
    136137        pmFPAfile *file = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.IMAGE", i); // File to examine
     
    178179            target->data.S32[i] = numProj;
    179180            psTrace("ppSkycell", 3, "Image %d uses new projection\n", i);
    180 
     181            psArrayAdd(regionHDUs,1,hdu);
    181182            numProj++;
    182183        }
     
    184185
    185186    pmFPAfileActivate(data->config->files, false, NULL);
    186 
     187    // Loop over projections
    187188    for (int i = 0; i < numProj; i++) {
    188189        psRegion *projRegion = projRegions->data[i]; // Region for skycell projection
    189190        psTrace("ppSkycell", 2, "Projection %d: [%.0f:%.0f,%.0f:%.0f]\n",
    190191                i, projRegion->x0, projRegion->x1, projRegion->y0, projRegion->y1);
    191         int xSize = projRegion->x1 - projRegion->x0 + 1; // Size of unbinned image
     192        int xSize = -projRegion->x1 + projRegion->x0 + 1; // Size of unbinned image
    192193        int ySize = projRegion->y1 - projRegion->y0 + 1; // Size of unbinned image
    193194        // Size of binned image 1
     
    198199        psImage *image1 = psImageAlloc(numCols1, numRows1, PS_TYPE_F32); // Binned image
    199200        psImage *image2 = psImageAlloc(numCols2, numRows2, PS_TYPE_F32); // Binned image
    200         psImageInit(image1, 0);
    201         psImageInit(image2, 0);
    202 
     201        psImageInit(image1,NAN);
     202        psImageInit(image2,NAN);
     203       
    203204        psImage *mask1 = NULL, *mask2 = NULL; // Binned masks
    204205        if (data->masksName) {
     
    208209            psImageInit(mask2, 0xFF);
    209210        }
    210 
     211        pmHDU *projhdu = NULL;
     212        int modify_wcs1 = 1;
     213        int modify_wcs2 = 1;
     214        // Loop over inputs to this projection.
    211215        for (int j = 0; j < data->numInputs; j++) {
    212216            if (target->data.S32[j] != i) {
    213217                continue;
    214218            }
    215 
    216219            pmFPAfileActivateSingle(data->config->files, true, "PPSKYCELL.IMAGE", j);
    217220            if (data->masksName) {
     
    227230
    228231            pmFPAfile *file = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.IMAGE", j);
     232            if (!projhdu) {
     233              projhdu = file->fpa->hdu;
     234              //              psMetadataCopy(projhdu->header,file->fpa->hdu->header);
     235            }
     236#if 0
     237            else {
     238              if ((psMetadataLookupF32(NULL,file->fpa->hdu->header,"CRPIX1") >=
     239                   psMetadataLookupF32(NULL,projhdu->header,"CRPIX1"))&&
     240                  (psMetadataLookupF32(NULL,file->fpa->hdu->header,"CRPIX2") >=
     241                   psMetadataLookupF32(NULL,projhdu->header,"CRPIX1"))) {
     242                projhdu = file->fpa->hdu;
     243                psMetadataCopy(projhdu->header,file->fpa->hdu->header);
     244              }
     245            }
     246#endif   
     247           
    229248            pmReadout *inRO = pmFPAviewThisReadout(view, file->fpa); // Readout with input
    230249            psFree(view);
    231250
    232             // Flip images; no idea why this has to be done, but apparently it does
    233             {
    234                 psImage *rot = psImageRotate(NULL, inRO->image, M_PI, NAN, PS_INTERPOLATE_BILINEAR);
    235                 psFree(inRO->image);
    236                 inRO->image = rot;
    237             }
    238             if (inRO->mask) {
    239                 psImage *rot = psImageRotate(NULL, inRO->mask, M_PI, 0, PS_INTERPOLATE_FLAT);
    240                 psFree(inRO->mask);
    241                 inRO->mask = rot;
    242             }
    243 
     251            //      data->maskVal = 0xffff;
    244252            pmReadout *bin1RO = pmReadoutAlloc(NULL), *bin2RO = pmReadoutAlloc(NULL); // Binned readouts
    245253            if (!pmReadoutRebin(bin1RO, inRO, data->maskVal, data->bin1, data->bin1)) {
     
    256264            psRegion *imageRegion = imageRegions->data[j]; // Region for image
    257265            // Offsets for image on skycell
    258             int xOffset1 = (imageRegion->x0 - projRegion->x0) / (float)data->bin1;
    259             int yOffset1 = (imageRegion->y0 - projRegion->y0) / (float)data->bin1;
     266            int xOffset1 = (-imageRegion->x0 + projRegion->x0) / (float)data->bin1;
     267            int yOffset1 = (-imageRegion->y1 + projRegion->y1) / (float)data->bin1;
    260268            int xOffset2 = xOffset1 / (float)data->bin2, yOffset2 = yOffset1 / (float)data->bin2;
    261 
    262269            // XXX Completely neglecting rotations
    263270            // The skycells are divided up neatly with them all having the same orientation
    264             psImageOverlaySection(image1, bin1RO->image, xOffset1, yOffset1, "=");
    265             psImageOverlaySection(image2, bin2RO->image, xOffset2, yOffset2, "=");
     271            psImageOverlaySection(image1, bin1RO->image, xOffset1, yOffset1, "E");
     272            psImageOverlaySection(image2, bin2RO->image, xOffset2, yOffset2, "E");
    266273            if (data->masksName) {
    267                 psImageOverlaySection(mask1, bin1RO->mask, xOffset1, yOffset1, "=");
    268                 psImageOverlaySection(mask2, bin2RO->mask, xOffset2, yOffset2, "=");
     274                psImageOverlaySection(mask1, bin1RO->mask, xOffset1, yOffset1, "M");
     275                psImageOverlaySection(mask2, bin2RO->mask, xOffset2, yOffset2, "M");
    269276            }
    270277
     
    287294          pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN1");
    288295          pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN2");
     296          if (data->masksName) {
     297            pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN1.MASK");
     298            pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN2.MASK");
     299          }
    289300        }
    290301       
     
    313324
    314325        if (data->doFits) {
     326
     327          pmFPAfile *fits1 = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.BIN1", 0);
     328          // Copy header from projection root hdu
     329          fits1->fpa->hdu = pmHDUAlloc(NULL);
     330          fits1->fpa->hdu->header = psMetadataAlloc();
     331          psMetadataCopy(fits1->fpa->hdu->header,projhdu->header);
     332
     333          // Change wcs here
     334#define WCS_DEBUG 0
     335          if (modify_wcs1) {
     336            pmAstromWCS *WCS = pmAstromWCSfromHeader(fits1->fpa->hdu->header);
     337            double cd1f = 1.0 * data->bin1;
     338            double cd2f = 1.0 * data->bin1;
     339#if WCS_DEBUG
     340            fprintf(stderr,">>> %d %d (%g %g) (%g %g) (%g %g)\n",data->bin1,data->bin2,
     341                    cd1f,cd2f,WCS->cdelt1,WCS->cdelt2,
     342                    WCS->crpix1,WCS->crpix2
     343                    );
     344#endif
     345            WCS->cdelt1 *= cd1f;
     346            WCS->cdelt2 *= cd2f;
     347            WCS->crpix1 = WCS->crpix1 / cd1f;
     348            WCS->crpix2 = WCS->crpix2 / cd2f;
     349#if WCS_DEBUG
     350            fprintf(stderr,">>> %d %d (%g %g) (%g %g) (%g %g) %d %d %d %d\n",data->bin1,data->bin2,
     351                    cd1f,cd2f,WCS->cdelt1,WCS->cdelt2,
     352                    WCS->crpix1,WCS->crpix2,
     353                    WCS->trans->x->nX,              WCS->trans->x->nY,
     354                    WCS->trans->y->nX,              WCS->trans->y->nY
     355                    );
     356#endif
     357            for (int q = 0; q <= WCS->trans->x->nX; q++) {
     358              for (int r = 0; r <= WCS->trans->x->nY; r++) {
     359                WCS->trans->x->coeff[q][r] *= pow(cd1f,q) * pow(cd2f,r);
     360#if WCS_DEBUG
     361                fprintf(stderr,"PC: %d %d %g %g\n",
     362                        q,r,pow(cd1f,q) * pow(cd2f,r),
     363                        WCS->trans->x->coeff[q][r]);
     364#endif
     365              }
     366            }
     367            for (int q = 0; q <= WCS->trans->y->nX; q++) {
     368              for (int r = 0; r <= WCS->trans->y->nY; r++) {
     369                WCS->trans->y->coeff[q][r] *= pow(cd1f,q) * pow(cd2f,r);
     370#if WCS_DEBUG
     371                fprintf(stderr,"PC: %d %d %g %g\n",
     372                         q,r,pow(cd1f,q) * pow(cd2f,r),
     373                         WCS->trans->y->coeff[q][r]);
     374#endif
     375              }
     376            }
     377            pmAstromWCStoHeader (fits1->fpa->hdu->header,WCS);
     378            WCS = pmAstromWCSfromHeader(fits1->fpa->hdu->header);
     379#if WCS_DEBUG
     380            fprintf(stderr,">>> %d %d (%g %g) (%g %g) (%g %g)\n",data->bin1,data->bin2,
     381                    cd1f,cd2f,WCS->cdelt1,WCS->cdelt2,
     382                    WCS->crpix1,WCS->crpix2
     383                    );
     384#endif
     385            modify_wcs1 = 0;
     386          }
     387
     388         
     389          pmChip *Fchip1 = pmFPAfileThisChip(data->config->files, view, "PPSKYCELL.BIN1");
     390          psMetadataAddS32(Fchip1->concepts,PS_LIST_TAIL,"CHIP.XPARITY", PS_META_REPLACE,"",1);
     391          psMetadataAddS32(Fchip1->concepts,PS_LIST_TAIL,"CHIP.YPARITY", PS_META_REPLACE,"",1);
     392
    315393          pmCell *Fcell1 = pmFPAfileThisCell(data->config->files, view, "PPSKYCELL.BIN1"); // Rebinned cell 1
    316           pmCell *Fcell2 = pmFPAfileThisCell(data->config->files, view, "PPSKYCELL.BIN2"); // Rebinned cell 2
    317 
    318           // This is a hack to get a functioning header created so the fits images can be written out.
     394/*        // This is a hack to get a functioning header created so the fits images can be written out. */
    319395          psMetadataAddS32(Fcell1->concepts,PS_LIST_TAIL,"CELL.XPARITY", PS_META_REPLACE,"",1);
    320396          psMetadataAddS32(Fcell1->concepts,PS_LIST_TAIL,"CELL.YPARITY", PS_META_REPLACE,"",1);
    321397          psMetadataAddS32(Fcell1->concepts,PS_LIST_TAIL,"CELL.READDIR", PS_META_REPLACE,"",1);
     398
     399          pmReadout *Fro1 = pmReadoutAlloc(Fcell1);
     400          Fro1->image = image1;
     401          Fro1->mask = mask1;
     402          Fro1->data_exists = Fcell1->data_exists = Fcell1->parent->data_exists = true;
     403         
     404          fits1->save = true;
     405          fits1->fileIndex = i;
     406
     407          // Do the mask if we need to
     408          if (data->masksName) {
     409/*          pmFPAfile *Mask1 = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.BIN1.MASK", 0); */
     410/*          //      Mask1->fpa->hdu = pmHDUAlloc(NULL); */
     411/*          //      Mask1->fpa->hdu->header = psMetadataAlloc(); */
     412/*          //      psMetadataCopy(Mask1->fpa->hdu->header,fits1->fpa->hdu->header); */
     413/*          pmChip *Mchip1 = pmFPAfileThisChip(data->config->files, view, "PPSKYCELL.BIN1.MASK"); */
     414/*          psMetadataAddS32(Mchip1->concepts,PS_LIST_TAIL,"CHIP.XPARITY", PS_META_REPLACE,"",1); */
     415/*          psMetadataAddS32(Mchip1->concepts,PS_LIST_TAIL,"CHIP.YPARITY", PS_META_REPLACE,"",1); */
     416           
     417/*          pmCell *Mcell1 = pmFPAfileThisCell(data->config->files, view, "PPSKYCELL.BIN1.MASK"); // Rebinned cell 1 */
     418/*          /\*           // This is a hack to get a functioning header created so the fits images can be written out. *\/ */
     419/*          psMetadataAddS32(Mcell1->concepts,PS_LIST_TAIL,"CELL.XPARITY", PS_META_REPLACE,"",1); */
     420/*          psMetadataAddS32(Mcell1->concepts,PS_LIST_TAIL,"CELL.YPARITY", PS_META_REPLACE,"",1); */
     421/*          psMetadataAddS32(Mcell1->concepts,PS_LIST_TAIL,"CELL.READDIR", PS_META_REPLACE,"",1); */
     422           
     423/*          pmReadout *Mro1 = pmReadoutAlloc(Mcell1); */
     424/*          Mro1->image = image1; */
     425/*          Mro1->mask = mask1; */
     426/*          Mro1->data_exists = Mcell1->data_exists = Mcell1->parent->data_exists = true; */
     427           
     428/*          Mask1->save = true; */
     429/*          Mask1->fileIndex = i; */
     430          }
     431           
     432           
     433         
     434          // Repeat with second binned image
     435          pmFPAfile *fits2 = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.BIN2", 0);
     436          fits2->fpa->hdu = pmHDUAlloc(NULL);
     437          fits2->fpa->hdu->header = psMetadataAlloc();
     438          psMetadataCopy(fits2->fpa->hdu->header,projhdu->header);
     439
     440          if (modify_wcs2) {
     441            pmAstromWCS *WCS = pmAstromWCSfromHeader(fits2->fpa->hdu->header);
     442            double cd1f = 1.0 * data->bin2 * data->bin1;
     443            double cd2f = 1.0 * data->bin2 * data->bin1;
     444           
     445            WCS->cdelt1 *= cd1f;
     446            WCS->cdelt2 *= cd2f;
     447            WCS->crpix1 = WCS->crpix1 / cd1f;
     448            WCS->crpix2 = WCS->crpix2 / cd2f;
     449           
     450            for (int q = 0; q < WCS->trans->x->nX; q++) {
     451              for (int r = 0; r < WCS->trans->x->nY; r++) {
     452                WCS->trans->x->coeff[q][r] *= pow(cd1f,q) * pow(cd2f,r);
     453              }
     454            }
     455            for (int q = 0; q < WCS->trans->y->nX; q++) {
     456              for (int r = 0; r < WCS->trans->y->nY; r++) {
     457                WCS->trans->y->coeff[q][r] *= pow(cd1f,q) * pow(cd2f,r);
     458              }
     459            }
     460            pmAstromWCStoHeader (fits2->fpa->hdu->header,WCS);
     461            modify_wcs2 = 0;
     462          }
     463         
     464          pmChip *Fchip2 = pmFPAfileThisChip(data->config->files, view, "PPSKYCELL.BIN2");
     465          psMetadataAddS32(Fchip2->concepts,PS_LIST_TAIL,"CHIP.XPARITY", PS_META_REPLACE,"",1);
     466          psMetadataAddS32(Fchip2->concepts,PS_LIST_TAIL,"CHIP.YPARITY", PS_META_REPLACE,"",1);
     467
     468         
     469          pmCell *Fcell2 = pmFPAfileThisCell(data->config->files, view, "PPSKYCELL.BIN2"); // Rebinned cell 2
    322470          psMetadataAddS32(Fcell2->concepts,PS_LIST_TAIL,"CELL.XPARITY", PS_META_REPLACE,"",1);
    323471          psMetadataAddS32(Fcell2->concepts,PS_LIST_TAIL,"CELL.YPARITY", PS_META_REPLACE,"",1);
    324472          psMetadataAddS32(Fcell2->concepts,PS_LIST_TAIL,"CELL.READDIR", PS_META_REPLACE,"",1);
    325 
    326           psMetadataAddS32(Fcell1->parent->concepts,PS_LIST_TAIL,"CHIP.XPARITY", PS_META_REPLACE,"",1);
    327           psMetadataAddS32(Fcell1->parent->concepts,PS_LIST_TAIL,"CHIP.YPARITY", PS_META_REPLACE,"",1);
    328           psMetadataAddS32(Fcell2->parent->concepts,PS_LIST_TAIL,"CHIP.XPARITY", PS_META_REPLACE,"",1);
    329           psMetadataAddS32(Fcell2->parent->concepts,PS_LIST_TAIL,"CHIP.YPARITY", PS_META_REPLACE,"",1);
    330 
    331           pmReadout *Fro1 = pmReadoutAlloc(Fcell1), *Fro2 = pmReadoutAlloc(Fcell2); // Binned readouts
    332          
    333           Fro1->image = image1;
     473         
     474          pmReadout *Fro2 = pmReadoutAlloc(Fcell2);
    334475          Fro2->image = image2;
    335           Fro1->mask = mask1;
    336476          Fro2->mask = mask2;
    337          
    338           Fro1->data_exists = Fcell1->data_exists = Fcell1->parent->data_exists = true;
    339477          Fro2->data_exists = Fcell2->data_exists = Fcell2->parent->data_exists = true;
    340          
    341           pmFPAfile *fits1 = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.BIN1", 0);
    342           fits1->save = true;
    343           fits1->fileIndex = i;
    344           pmFPAfile *fits2 = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.BIN2", 0);
     478
    345479          fits2->save = true;
    346480          fits2->fileIndex = i;
     481
     482         
    347483        }
    348484
Note: See TracChangeset for help on using the changeset viewer.