IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 5564


Ignore:
Timestamp:
Nov 21, 2005, 5:00:14 PM (20 years ago)
Author:
Paul Price
Message:

Mask and weight input and output

Location:
trunk/archive/scripts/src/phase2
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/scripts/src/phase2/Makefile

    r5104 r5564  
    33
    44#DEVFLAGS += -DPRODUCTION       # Produce binary as for production (don't use old APIs)
    5 DEVFLAGS += -DTESTING           # Testing version
     5DEVFLAGS += -DTESTING           # Testing version
     6#DEVFLAGS += -DPS_NO_TRACE      # No trace messages
    67
    7 CFLAGS += -O0 -g -std=c99 -Werror -I$(JHBUILD)/include/ -I$(JHBUILD)/include/libxml2/ -DPS_NO_TRACE -D_GNU_SOURCE $(DEVFLAGS)
     8CFLAGS += -O0 -g -std=c99 -Werror -I$(JHBUILD)/include/ -I$(JHBUILD)/include/libxml2/ -D_GNU_SOURCE $(DEVFLAGS)
    89PSLIB += -L$(JHBUILD)/lib/ -lpslib -lgsl -lgslcblas -lfftw3f -lsla -lcfitsio -lm -L$(JHBUILD)/lib/libxml2/ -L$(JHBUILD)/lib/mysql/ -lxml2 -lmysqlclient
    910PSMODULE += -L$(JHBUILD)/lib/ -lpsmodule
  • trunk/archive/scripts/src/phase2/ipprc.config

    r5467 r5564  
    2222TRACE           METADATA                        # Trace levels
    2323        pmConfigRead            S32     10
    24         pmFPARead               S32     10
    2524        pap                     S32     10
     25        pmFPAWriteMask          S32     10
     26        pmFPAWriteWeight        S32     10
     27        p_pmFPATranslateName    S32     10
    2628END
  • trunk/archive/scripts/src/phase2/megacam_raw.config

    r5105 r5564  
    159159        PHASE2          STR     phase2.config           # Phase 2 recipe details
    160160END
     161
     162
     163# How to get the supplementary stuff: mask and weight
     164SUPPLEMENTARY   METADATA
     165        MASK.SOURCE     STR     FILE            # Source type for mask: EXT | FILE
     166        MASK.NAME       STR     %a_mask.fits    # Name for mask extension or filename
     167        WEIGHT.SOURCE   STR     EXT             # Source type for weight: EXT | FILE
     168        WEIGHT.NAME     STR     %f-weight       # Name for weight extension or filename
     169END
  • trunk/archive/scripts/src/phase2/papPhase2.c

    r5467 r5564  
    2929// * Write calibrated image
    3030// * Write source catalogue, astrometry, etc.
    31  
     31
    3232// Currently neglecting different input types (F32, S32, U16, etc)
    3333
     
    302302#else
    303303    psFree(inputFile);
     304#endif
     305
     306
     307#if 1
     308    {
     309        // Generate mask and weight frame
     310        p_pmHDU *hdu = NULL;
     311        if (input->hdu) {
     312            hdu = input->hdu;
     313        }
     314        psArray *chips = input->chips;
     315        for (int chipNum = 0; chipNum < chips->n; chipNum++) {
     316            pmChip *chip = chips->data[chipNum];
     317            if (chip->valid) {
     318                if (chip->hdu) {
     319                    hdu = chip->hdu;
     320                }
     321                psArray *cells = chip->cells;
     322                for (int cellNum = 0; cellNum < cells->n; cellNum++) {
     323                    pmCell *cell = cells->data[cellNum];
     324                    if (cell->valid) {
     325                        if (cell->hdu) {
     326                            hdu = cell->hdu;
     327                        }
     328                       
     329                        hdu->masks = psArrayAlloc(hdu->images->n);
     330                        hdu->weights = psArrayAlloc(hdu->images->n);
     331                        psArray *readouts = cell->readouts;
     332                        for (int readNum = 0; readNum < hdu->images->n; readNum++) {
     333                            psImage *image = hdu->images->data[readNum];
     334                            psImage *mask = psImageAlloc(image->numCols, image->numRows, PS_TYPE_U8);
     335                            psImage *weight = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);
     336                            pmReadout *readout = readouts->data[readNum];
     337                            for (int j = 0; j < image->numRows; j++) {
     338                                for (int i = 0; i < image->numCols; i++) {
     339                                    mask->data.U8[j][i] = j + i;
     340                                    weight->data.F32[j][i] = sqrtf(image->data.F32[j][i]);
     341                                }
     342                            }
     343                            hdu->masks->data[readNum] = mask;
     344                            hdu->weights->data[readNum] = weight;
     345                            readout->mask = psMemIncrRefCounter(mask);
     346                            readout->weight = psMemIncrRefCounter(weight);
     347                        }
     348                    }
     349                }
     350            }
     351        }
     352    }
    304353#endif
    305354
     
    741790                                                       inputReadout->image->numRows,
    742791                                                       PS_TYPE_U8);
    743                     pmReadout *dummyMask = pmReadoutAlloc(NULL, dummyImage, NULL, 0, 0, 1, 1, 1, 1);
     792                    pmReadout *dummyMask = pmReadoutAlloc(NULL, dummyImage, NULL, 0, 0, 1, 1);
    744793                    (void)pmFlatField(inputReadout, dummyMask, flatReadout);
    745794                    psFree(dummyMask);
     
    771820    // Write the output
    772821    pmFPAWrite(outputFile, input, database);
    773 //    pmFPAWriteMask(outputMaskFile, input);
     822    pmFPAWriteMask(input, outputFile);
     823    pmFPAWriteWeight(input, outputFile);
     824
    774825#ifdef PRODUCTION
    775826    psFitsClose(outputFile);
  • trunk/archive/scripts/src/phase2/phase2.config

    r5467 r5564  
    33# List of tasks to perform
    44MASK            BOOL    FALSE           # Mask bad pixels
    5 NONLIN          BOOL    TRUE            # Non-linearity correction
     5NONLIN          BOOL    FALSE           # Non-linearity correction
    66BIAS            BOOL    FALSE           # Bias subtraction
    77DARK            BOOL    FALSE           # Dark subtraction
  • trunk/archive/scripts/src/phase2/pmFPA.c

    r5462 r5564  
    4040//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    4141
    42 pmPixelData *pmPixelDataAlloc(const char *extname)
    43 {
    44     pmPixelData *pd = psAlloc(sizeof(pmPixelData));
    45     psMemSetDeallocator(pd, (psFreeFunc)p_pmPixelDataFree);
     42p_pmHDU *p_pmHDUAlloc(const char *extname)
     43{
     44    p_pmHDU *pd = psAlloc(sizeof(p_pmHDU));
     45    psMemSetDeallocator(pd, (psFreeFunc)p_pmHDUFree);
    4646
    4747    pd->extname = extname;
     
    5454}
    5555
    56 void p_pmPixelDataFree(pmPixelData *pd)
    57 {
    58     psFree(pd->header);
    59     psFree(pd->images);
    60     psFree(pd->masks);
    61     psFree(pd->weights);
     56void p_pmHDUFree(p_pmHDU *hdu)
     57{
     58    psFree(hdu->header);
     59    psFree(hdu->images);
     60    psFree(hdu->masks);
     61    psFree(hdu->weights);
    6262}
    6363
     
    7777    fpa->chips = psArrayAlloc(0);
    7878
    79     fpa->data = NULL;
     79    fpa->hdu = NULL;
    8080
    8181    return fpa;
     
    9292    psFree(fpa->chips);
    9393
    94     psFree(fpa->data);
     94    psFree(fpa->hdu);
    9595}
    9696
     
    119119    chip->valid = true;   
    120120
    121     chip->data = NULL;
     121    chip->hdu = NULL;
    122122
    123123    psMetadataAddStr(chip->concepts, PS_LIST_HEAD, "CHIP.NAME", 0, "Chip name added at pmChipAlloc", name);
     
    134134    psFree(chip->cells);
    135135
    136     psFree(chip->data);
     136    psFree(chip->hdu);
    137137
    138138    // We don't free the parent member, since that would generate a circular call.  We don't increment the
     
    156156
    157157    cell->toChip = NULL;
    158     cell->fromChip = NULL;
    159158    cell->toFPA = NULL;
    160     cell->toTP = NULL;
    161159    cell->toSky = NULL;
    162160
     
    171169    cell->valid = true;
    172170
    173     cell->data = NULL;
     171    cell->hdu = NULL;
    174172
    175173    return cell;
     
    179177{
    180178    psFree(cell->toChip);
    181     psFree(cell->fromChip);
    182179    psFree(cell->toFPA);
    183     psFree(cell->toTP);
    184180    psFree(cell->toSky);
    185181
     
    188184    psFree(cell->readouts);
    189185
    190     psFree(cell->data);
     186    psFree(cell->hdu);
    191187
    192188    // We don't free the parent member, since that would generate a circular call.  We don't increment the
     
    197193                          psImage *image, // The pixels
    198194                          psImage *mask,// The mask pixels
    199                           int col0, int row0, int colParity, int rowParity, int colBin, int rowBin // Data
     195                          int col0, int row0, int colBin, int rowBin // Data
    200196    )
    201197{
     
    215211    *(int*)&readout->col0 = col0;
    216212    *(int*)&readout->row0 = row0;
    217     *(int*)&readout->colParity = colParity;
    218     *(int*)&readout->rowParity = rowParity;
    219213    *(int*)&readout->colBins = colBin;
    220214    *(int*)&readout->rowBins = rowBin;
  • trunk/archive/scripts/src/phase2/pmFPA.h

    r5462 r5564  
    1515    psArray *masks;                     // The mask data, if it corresponds to this level
    1616    psArray *weights;                   // The weight data, if it corresponds to this level
    17 } pmPixelData;
     17} p_pmHDU;
    1818
    1919typedef struct {
    2020    // Astrometric transformations
    21     psPlaneDistort* fromTangentPlane;   // Transformation from tangent plane to focal plane
    22     psPlaneDistort* toTangentPlane;     // Transformation from focal plane to tangent plane
    23     psProjection *projection;           // Projection from tangent plane to sky
     21    psPlaneDistort* fromTangentPlane;  // Transformation from tangent plane to focal plane
     22    psPlaneDistort* toTangentPlane;    // Transformation from focal plane to tangent plane
     23    psProjection *projection;          // Projection from tangent plane to sky
    2424    // Information
    25     psMetadata *camera;                 // Camera configuration
    26     psMetadata *concepts;               // Values for concepts
    27     psMetadata *phu;                    // Primary Header
    28     psArray *chips;                     // The chips
    29     pmPixelData *data;                  // FITS data
     25    psMetadata *concepts;              // Cache for PS concepts
     26    psMetadata *analysis;              // FPA-level analysis metadata
     27    const psMetadata *camera;          // Camera configuration
     28    psArray *chips;                    // The chips
     29    p_pmHDU *hdu;                      // FITS data
     30    psMetadata *phu;                   // Primary Header
    3031} pmFPA;
    3132
    3233typedef struct {
    3334    // Offset specifying position on focal plane
    34     const int col0;                     // Offset from the left of FPA
    35     const int row0;                     // Offset from the bottom of FPA
     35    int col0;                          // Offset from the left of FPA.
     36    int row0;                          // Offset from the bottom of FPA.
    3637    // Astrometric transformations
    37     psPlaneTransform* toFPA;            // Transformation from chip to FPA coordinates
    38     psPlaneTransform* fromFPA;          // Transformation from FPA to chip coordinates
     38    psPlaneTransform* toFPA;           // Transformation from chip to FPA coordinates
     39    psPlaneTransform* fromFPA;         // Transformation from FPA to chip coordinates
    3940    // Information
    40     psMetadata *concepts;               // Values for concepts
    41     psArray *cells;                     // The cells (referred to by name)
    42     pmFPA *parent;                      // Parent FPA
    43     bool valid;                         // Valid for reading in?
    44     pmPixelData *data;                  // FITS data
     41    psMetadata *concepts;              // Cache for PS concepts
     42    psMetadata *analysis;              // Chip-level analysis metadata
     43    psArray *cells;                    // The cells (referred to by name)
     44    pmFPA *parent;                     // Parent FPA
     45    bool valid;                        // Do we bother about reading and working with this chip?
     46    p_pmHDU *hdu;                       // FITS data
    4547} pmChip;
    4648
    4749typedef struct {
    4850    // Offset specifying position on chip
    49     const int col0;                     // Offset from the left of chip
    50     const int row0;                     // Offset from the bottom of chip
     51    int col0;                          // Offset from the left of chip.
     52    int row0;                          // Offset from the bottom of chip.
    5153    // Astrometric transformations
    52     psPlaneTransform* toChip;           // Transformations from cell to chip coordinates
    53     psPlaneTransform* fromChip;         // Transformations from cell to chip coordinates
    54     psPlaneTransform* toFPA;            // Transformations from cell to FPA coordinates
    55     psPlaneTransform* toTP;             // Transformations from cell to FPA coordinates
    56     psPlaneTransform* toSky;            // Transformations from cell to tangent plane coordinates
     54    psPlaneTransform* toChip;          // Transformations from cell to chip coordinates
     55    psPlaneTransform* toFPA;           // Transformations from cell to FPA coordinates
     56    psPlaneTransform* toSky;           // Transformations from cell to sky coordinates
    5757    // Information
    58     psMetadata *concepts;               // Values for concepts
    59     psMetadata *camera;                 // Camera information
    60     psArray *readouts;                  // The readouts (referred to by number)
    61     pmChip *parent;                     // Parent chip
    62     bool valid;                         // Valid for operating on?
    63     pmPixelData *data;                  // FITS data
     58    psMetadata *concepts;              // Cache for PS concepts
     59    psMetadata *camera;                // Camera information
     60    psMetadata *analysis;              // Cell-level analysis metadata
     61    psArray *readouts;                 // The readouts (referred to by number)
     62    pmChip *parent;                    // Parent chip
     63    bool valid;                        // Do we bother about reading and working with this cell?
     64    p_pmHDU *hdu;                      // FITS data
    6465} pmCell;
    6566
     67typedef struct {
     68    // Position on the cell
     69    int col0;                          // Offset from the left of cell.
     70    int row0;                          // Offset from the bottom of cell.
     71    int colBins;                       // Amount of binning in x-dimension and parity (from sign)
     72    int rowBins;                       // Amount of binning in y-dimension and parity (from sign)
     73    // Information
     74    psImage *image;                    // Imaging area of readout
     75    psImage *mask;                     // Mask for image
     76    psImage *weight;                   // Weight for image
     77    psList *bias;                      // List of bias section (sub-)images
     78    psMetadata *analysis;              // Readout-level analysis metadata
     79    pmCell *parent;                    // Parent cell
     80} pmReadout;
    6681
     82#if 0
    6783typedef struct {
    6884    // Details for position on the cell
     
    8096    psImage *weight;                    // Weight image
    8197} pmReadout;
    82 
     98#endif
    8399
    84100psList *pmReadoutGetBias(pmReadout *readout // Readout for which to get the bias sections
     
    87103
    88104// Allocators and deallocators
    89 pmPixelData *pmPixelDataAlloc(const char *extname);
    90 void p_pmPixelDataFree(pmPixelData *pd);
     105p_pmHDU *p_pmHDUAlloc(const char *extname);
     106void p_pmHDUFree(p_pmHDU *hdu);
    91107pmFPA *pmFPAAlloc(const psMetadata *camera // Camera configuration
    92108    );
     
    107123                          psImage *image, // The pixels
    108124                          psImage *mask,// The mask pixels
    109                           int col0, int row0, int colParity, int rowParity, int colBin, int rowBin // Data
     125                          int col0, int row0, int colBin, int rowBin // Data
    110126    );
    111127void p_pmReadoutFree(pmReadout *readout);
  • trunk/archive/scripts/src/phase2/pmFPAConceptsGet.c

    r5462 r5564  
    4646    if (mdStatus && strlen(keyword) > 0) {
    4747        // We have a FITS header to look up --- search each level
    48         if (cell && cell->data) {
    49             psMetadataItem *cellItem = psMetadataLookup(cell->data->header, keyword);
     48        if (cell && cell->hdu) {
     49            psMetadataItem *cellItem = psMetadataLookup(cell->hdu->header, keyword);
    5050            if (cellItem) {
    5151                // XXX: Need to clean up before returning
     
    5454        }
    5555
    56         if (chip && chip->data) {
    57             psMetadataItem *chipItem = psMetadataLookup(chip->data->header, keyword);
     56        if (chip && chip->hdu) {
     57            psMetadataItem *chipItem = psMetadataLookup(chip->hdu->header, keyword);
    5858            if (chipItem) {
    5959                // XXX: Need to clean up before returning
     
    6262        }
    6363
    64         if (fpa->data) {
    65             psMetadataItem *fpaItem = psMetadataLookup(fpa->data->header, keyword);
     64        if (fpa->hdu) {
     65            psMetadataItem *fpaItem = psMetadataLookup(fpa->hdu->header, keyword);
    6666            if (fpaItem) {
    6767                // XXX: Need to clean up before returning
     
    371371        switch (item->type) {
    372372          case PS_DATA_STRING:
    373             value = item->data.V;
     373            value = psMemIncrRefCounter(item->data.V);
     374            break;
     375          case PS_DATA_F32:
     376            psStringAppend(&value, "%f", item->data.F32);
     377            break;
     378          case PS_DATA_S32:
     379            psStringAppend(&value, "%d", item->data.S32);
    374380            break;
    375381          default:
     
    384390
    385391    psMetadataAdd(concepts, PS_LIST_TAIL, name, PS_DATA_STRING, comment, value);
     392    psFree(value);
    386393}
    387394
     
    400407    }
    401408
    402     // FPA.NAME --- added by pmFPAConstruct
     409    // FPA.NAME
     410    p_pmFPAConceptGetString(fpa, NULL, NULL, db, fpa->concepts, "FPA.NAME", "Name of FPA");
    403411
    404412    // FPA.AIRMASS
     
    450458           
    451459            // How to interpret the RA
    452             psMetadata *camera = fpa->camera; // Camera configuration data
     460            const psMetadata *camera = fpa->camera; // Camera configuration data
    453461            bool mdok = true;           // Status of MD lookup
    454462            psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS");
     
    519527           
    520528            // How to interpret the DEC
    521             psMetadata *camera = fpa->camera; // Camera configuration data
     529            const psMetadata *camera = fpa->camera; // Camera configuration data
    522530            bool mdok = true;           // Status of MD lookup
    523531            psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS");
     
    653661                } else if (strcasecmp(source, "HEADER") == 0) {
    654662                    psMetadata *header = NULL; // The FITS header
    655                     if (cell->data) {
    656                         header = cell->data->header;
    657                     } else if (chip->data) {
    658                         header = chip->data->header;
    659                     } else if (fpa->data) {
    660                             header = fpa->data->header;
     663                    if (cell->hdu) {
     664                        header = cell->hdu->header;
     665                    } else if (chip->hdu) {
     666                        header = chip->hdu->header;
     667                    } else if (fpa->hdu) {
     668                            header = fpa->hdu->header;
    661669                    }
    662670                    if (! header) {
     
    718726                    } else if (strcasecmp(source, "HEADER") == 0) {
    719727                        psMetadata *header = NULL; // The FITS header
    720                         if (cell->data) {
    721                             header = cell->data->header;
    722                         } else if (chip->data) {
    723                             header = chip->data->header;
    724                         } else if (fpa->data) {
    725                             header = fpa->data->header;
     728                        if (cell->hdu) {
     729                            header = cell->hdu->header;
     730                        } else if (chip->hdu) {
     731                            header = chip->hdu->header;
     732                        } else if (fpa->hdu) {
     733                            header = fpa->hdu->header;
    726734                        }
    727735                        if (! header) {
     
    832840        } else {
    833841            // Get format
    834             psMetadata *camera = fpa->camera; // The camera configuration data
     842            const psMetadata *camera = fpa->camera; // The camera configuration data
    835843            bool mdok = true;           // Status of MD lookup
    836844            psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS");
     
    848856                                // timeString contains headers for the date and time
    849857                                psMetadata *header = NULL; // The FITS header
    850                                 if (cell->data) {
    851                                     header = cell->data->header;
    852                                 } else if (chip->data) {
    853                                     header = chip->data->header;
    854                                 } else if (fpa->data) {
    855                                     header = fpa->data->header;
     858                                if (cell->hdu) {
     859                                    header = cell->hdu->header;
     860                                } else if (chip->hdu) {
     861                                    header = chip->hdu->header;
     862                                } else if (fpa->hdu) {
     863                                    header = fpa->hdu->header;
    856864                                }
    857865                                if (! header) {
  • trunk/archive/scripts/src/phase2/pmFPAConceptsSet.c

    r5462 r5564  
    130130
    131131        // We have a FITS header to look up --- search each level
    132         if (cell && cell->data) {
    133             psMetadataItem *cellItem = psMetadataLookup(cell->data->header, keyword);
     132        if (cell && cell->hdu) {
     133            psMetadataItem *cellItem = psMetadataLookup(cell->hdu->header, keyword);
    134134            if (cellItem) {
    135135                // XXX: Need to clean up before returning
    136                 psMetadataAddItem(cell->data->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);
     136                psMetadataAddItem(cell->hdu->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);
    137137                status = true;
    138138            }
    139         } else if (chip && chip->data) {
    140             psMetadataItem *chipItem = psMetadataLookup(chip->data->header, keyword);
     139        } else if (chip && chip->hdu) {
     140            psMetadataItem *chipItem = psMetadataLookup(chip->hdu->header, keyword);
    141141            if (chipItem) {
    142142                // XXX: Need to clean up before returning
    143                 psMetadataAddItem(chip->data->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);
     143                psMetadataAddItem(chip->hdu->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);
    144144                status = true;
    145145            }
    146         } else if (fpa->data) {
    147             psMetadataItem *fpaItem = psMetadataLookup(fpa->data->header, keyword);
     146        } else if (fpa->hdu) {
     147            psMetadataItem *fpaItem = psMetadataLookup(fpa->hdu->header, keyword);
    148148            if (fpaItem) {
    149149                // XXX: Need to clean up before returning
    150                 psMetadataAddItem(fpa->data->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);
     150                psMetadataAddItem(fpa->hdu->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);
    151151                status = true;
    152152            }
     
    376376    )
    377377{
    378     // FPA.NAME --- don't need to worry about this
     378    // FPA.NAME
     379    setConcept(fpa, NULL, NULL, db, fpa->concepts, "FPA.NAME");
    379380
    380381    // FPA.AIRMASS
     
    397398
    398399        // How to interpret the RA
    399         psMetadata *camera = fpa->camera; // Camera configuration data
     400        const psMetadata *camera = fpa->camera; // Camera configuration data
    400401        bool mdok = true;               // Status of MD lookup
    401402        psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS");
     
    444445
    445446        // How to interpret the DEC
    446         psMetadata *camera = fpa->camera; // Camera configuration data
     447        const psMetadata *camera = fpa->camera; // Camera configuration data
    447448        bool mdok = true;               // Status of MD lookup
    448449        psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS");
     
    571572                psString keyword = psMetadataLookupString(NULL, cell->camera, "CELL.TRIMSEC");
    572573                psMetadata *header = NULL; // The FITS header
    573                 if (cell->data) {
    574                     header = cell->data->header;
    575                 } else if (chip->data) {
    576                     header = chip->data->header;
    577                 } else if (fpa->data) {
    578                     header = fpa->data->header;
     574                if (cell->hdu) {
     575                    header = cell->hdu->header;
     576                } else if (chip->hdu) {
     577                    header = chip->hdu->header;
     578                } else if (fpa->hdu) {
     579                    header = fpa->hdu->header;
    579580                }
    580581                if (! header) {
     
    651652                } else {
    652653                    psMetadata *header = NULL; // The FITS header
    653                     if (cell->data) {
    654                         header = cell->data->header;
    655                     } else if (chip->data) {
    656                         header = chip->data->header;
    657                     } else if (fpa->data) {
    658                         header = fpa->data->header;
     654                    if (cell->hdu) {
     655                        header = cell->hdu->header;
     656                    } else if (chip->hdu) {
     657                        header = chip->hdu->header;
     658                    } else if (fpa->hdu) {
     659                        header = fpa->hdu->header;
    659660                    }
    660661                    if (! header) {
     
    689690
    690691        // If these are specified by the header, we need to check to see if it's the same header keyword
    691         psMetadata *camera = fpa->camera;
     692        const psMetadata *camera = fpa->camera;
    692693        psMetadata *translation = psMetadataLookupMD(NULL, camera, "TRANSLATION");
    693694        psString xKeyword = psMetadataLookupString(NULL, translation, "CELL.XBIN");
     
    707708                                                             binString);
    708709            psMetadata *header = NULL; // The FITS header
    709             if (cell->data) {
    710                 header = cell->data->header;
    711             } else if (chip->data) {
    712                 header = chip->data->header;
    713             } else if (fpa->data) {
    714                 header = fpa->data->header;
     710            if (cell->hdu) {
     711                header = cell->hdu->header;
     712            } else if (chip->hdu) {
     713                header = chip->hdu->header;
     714            } else if (fpa->hdu) {
     715                header = fpa->hdu->header;
    715716            }
    716717            if (! header) {
     
    784785                // We're working with two separate headers
    785786                psMetadata *header = NULL; // The FITS header
    786                 if (cell->data) {
    787                     header = cell->data->header;
    788                 } else if (chip->data) {
    789                     header = chip->data->header;
    790                 } else if (fpa->data) {
    791                     header = fpa->data->header;
     787                if (cell->hdu) {
     788                    header = cell->hdu->header;
     789                } else if (chip->hdu) {
     790                    header = chip->hdu->header;
     791                } else if (fpa->hdu) {
     792                    header = fpa->hdu->header;
    792793                }
    793794                if (! header) {
     
    795796                } else {
    796797                    // Get the headers
    797                     psMetadata *camera = fpa->camera;
     798                    const psMetadata *camera = fpa->camera;
    798799                    psMetadata *translation = psMetadataLookupMD(NULL, camera, "TRANSLATION");
    799800                    psString keywords = psMetadataLookupString(NULL, translation, "CELL.TIME");
  • trunk/archive/scripts/src/phase2/pmFPAConstruct.c

    r5462 r5564  
    8888                psString extName = contentItem->name; // The name of the extension
    8989                pmChip *chip = pmChipAlloc(fpa, extName); // The chip
    90                 chip->data = pmPixelDataAlloc(extName); // Prepare chip to receive FITS data
     90                chip->hdu = p_pmHDUAlloc(extName); // Prepare chip to receive FITS data
    9191                if (contentItem->type != PS_DATA_STRING) {
    9292                    psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n",
     
    146146                    pmCell *cell = pmCellAlloc(chip, cellData, extName); // The cell
    147147//                  psFree(cellData);
    148                     cell->data = pmPixelDataAlloc(extName); // Prepare cell to receive FITS data
     148                    cell->hdu = p_pmHDUAlloc(extName); // Prepare cell to receive FITS data
    149149
    150150                    psFree(chip);
     
    157157        } else if (strcasecmp(extType, "NONE") == 0) {
    158158            // No extensions; Content contains metadata, each entry is a chip with its component cells
    159             fpa->data = pmPixelDataAlloc("PHU"); // Prepare FPA to receive FITS data
     159            fpa->hdu = p_pmHDUAlloc("PHU"); // Prepare FPA to receive FITS data
    160160            while (contentItem = psMetadataGetAndIncrement(contentsIter)) {
    161161                psString chipName = contentItem->name; // The name of the chip
     
    196196        if (strcasecmp(extType, "NONE") == 0) {
    197197            // There are no extensions --- only the PHU
    198             chip->data = pmPixelDataAlloc("PHU");
     198            chip->hdu = p_pmHDUAlloc("PHU");
    199199
    200200            const char *contents = psMetadataLookupString(&mdStatus, camera, "CONTENTS");
     
    247247                pmCell *cell = pmCellAlloc(chip, cellData, extName); // The cell
    248248//              psFree(cellData);
    249                 cell->data = pmPixelDataAlloc(extName); // Prepare cell to receive FITS data
     249                cell->hdu = p_pmHDUAlloc(extName); // Prepare cell to receive FITS data
    250250            } // Iterating through contents
    251251            psFree(contentsIter);
     
    273273{
    274274    psTrace(__func__, 0, "FPA:\n");
    275     if (fpa->data) {
    276         psTrace(__func__, 1, "---> FPA is extension %s.\n", fpa->data->extname);
    277         if (! fpa->data->images) {
    278             psTrace(__func__, 1, "---> NO PIXELS for extension %s\n", fpa->data->extname);
     275    if (fpa->hdu) {
     276        psTrace(__func__, 1, "---> FPA is extension %s.\n", fpa->hdu->extname);
     277        if (! fpa->hdu->images) {
     278            psTrace(__func__, 1, "---> NO PIXELS for extension %s\n", fpa->hdu->extname);
    279279        }
    280280    }
     
    285285        psTrace(__func__, 1, "Chip: %d\n", i);
    286286        pmChip *chip = chips->data[i]; // The chip
    287         if (chip->data) {
    288             psTrace(__func__, 2, "---> Chip is extension %s.\n", chip->data->extname);
    289             if (! chip->data->images) {
    290                 psTrace(__func__, 2, "---> NO PIXELS for extension %s\n", chip->data->extname);
     287        if (chip->hdu) {
     288            psTrace(__func__, 2, "---> Chip is extension %s.\n", chip->hdu->extname);
     289            if (! chip->hdu->images) {
     290                psTrace(__func__, 2, "---> NO PIXELS for extension %s\n", chip->hdu->extname);
    291291            }
    292292        }
     
    296296            psTrace(__func__, 2, "Cell: %d\n", j);
    297297            pmCell *cell = cells->data[j]; // The cell
    298             if (cell->data) {
    299                 psTrace(__func__, 3, "---> Cell is extension %s.\n", cell->data->extname);
    300                 if (! cell->data->images) {
    301                     psTrace(__func__, 3, "---> NO PIXELS for extension %s\n", cell->data->extname);
     298            if (cell->hdu) {
     299                psTrace(__func__, 3, "---> Cell is extension %s.\n", cell->hdu->extname);
     300                if (! cell->hdu->images) {
     301                    psTrace(__func__, 3, "---> NO PIXELS for extension %s\n", cell->hdu->extname);
    302302                }
    303303            }
  • trunk/archive/scripts/src/phase2/pmFPARead.c

    r5371 r5564  
    1717
    1818// Read a FITS extension into a chip
    19 static bool readExtension(pmPixelData *pixelData, // Pixel data into which to read
     19static bool readExtension(p_pmHDU *hdu, // Pixel data into which to read
    2020                          psFits *fits  // The FITS file from which to read
    2121    )
    2222{
    23     const char *extName = pixelData->extname; // Extension name
     23    const char *extName = hdu->extname; // Extension name
    2424
    2525    psTrace(__func__, 7, "Moving to extension %s...\n", extName);
     
    9595
    9696    // Update the HDU with the new data
    97     pixelData->images = pixels;
    98     pixelData->header = header;
     97    hdu->images = pixels;
     98    hdu->header = header;
    9999
    100100    // XXX: Insert mask and weight reading here
     
    105105// Portion out an image into the cell
    106106static bool generateReadouts(pmCell *cell, // The cell that gets its bits
    107                              pmPixelData *data // Pixel data, containing image, mask, weights
     107                             p_pmHDU *hdu // Pixel data, containing image, mask, weights
    108108    )
    109109{
    110     psArray *images = data->images;     // Array of images (each of which is a readout)
    111     psArray *masks = data->masks;       // Array of masks (one for each readout)
     110    psArray *images = hdu->images;      // Array of images (each of which is a readout)
     111    psArray *masks = hdu->masks;        // Array of masks (one for each readout)
    112112    // Iterate over each of the image planes
    113113    for (int i = 0; i < images->n; i++) {
     
    118118        }
    119119
    120         pmReadout *readout = pmReadoutAlloc(cell, image, mask, 0,0,0,0,1,1);
     120        pmReadout *readout = pmReadoutAlloc(cell, image, mask, 0,0,1,1);
    121121        psFree(readout);                // This seems silly, but the alloc registers it with the cell, so we
    122122                                        // don't have to do anything with it.  Perhaps we should change
     
    137137    )
    138138{
    139     pmPixelData *pixelData = NULL;      // Pixel data from FITS file
     139    p_pmHDU *hdu = NULL;        // Pixel data from FITS file
    140140
    141141    // Read the PHU, if required
     
    152152    // Read in....
    153153    psTrace(__func__, 1, "Working on FPA...\n");
    154     if (fpa->data) {
    155         pixelData= fpa->data;
    156         psTrace(__func__, 3, "Reading pixels from extension %s into FPA.\n", pixelData->extname);
    157         if (! readExtension(pixelData, fits)) {
    158             psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", pixelData->extname);
     154    if (fpa->hdu) {
     155        hdu = fpa->hdu;
     156        psTrace(__func__, 3, "Reading pixels from extension %s into FPA.\n", hdu->extname);
     157        if (! readExtension(hdu, fits)) {
     158            psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", hdu->extname);
    159159            return false;
    160160        }
     
    174174        psTrace(__func__, 2, "Reading in chip %d...\n", i);
    175175
    176         if (chip->data) {
    177             pixelData = chip->data;
    178             psTrace(__func__, 3, "Reading pixels from extension %s into chip %d.\n", pixelData->extname, i);
    179             if (! readExtension(pixelData, fits)) {
    180                 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", pixelData->extname);
     176        if (chip->hdu) {
     177            hdu = chip->hdu;
     178            psTrace(__func__, 3, "Reading pixels from extension %s into chip %d.\n", hdu->extname, i);
     179            if (! readExtension(hdu, fits)) {
     180                psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", hdu->extname);
    181181                return false;
    182182            }
     
    196196            psTrace(__func__, 3, "Reading in cell %d...\n", j);
    197197
    198             if (cell->data) {
    199                 pixelData = cell->data;
    200                 psTrace(__func__, 5, "Reading pixels from extension %s into cell %d.\n", pixelData->extname,
     198            if (cell->hdu) {
     199                hdu = cell->hdu;
     200                psTrace(__func__, 5, "Reading pixels from extension %s into cell %d.\n", hdu->extname,
    201201                        j);
    202                 if (! readExtension(pixelData, fits)) {
     202                if (! readExtension(hdu, fits)) {
    203203                    psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n",
    204                             pixelData->extname);
     204                            hdu->extname);
    205205                    return false;
    206206                }
     
    210210            psTrace(__func__, 5, "Allocating readouts for chip %d cell %d...\n",
    211211                    i, j);
    212             generateReadouts(cell, pixelData);
     212            generateReadouts(cell, hdu);
    213213        }
    214214    }
     
    216216    return true;
    217217}
     218
     219// Translate a name from the configuration file, containing something like "%a_%d" to a real value
     220psString p_pmFPATranslateName(psString name, // The name to translate
     221                              pmCell *cell // The cell for which to translate
     222    )
     223{
     224    // %a is the FPA.NAME
     225    // %b is the CHIP.NAME
     226    // %c is the CELL.NAME
     227    // %d is the chip number
     228    // %e if the cell number
     229    // %f is the extension name
     230
     231    psString translation = psMemIncrRefCounter(name); // The translated string
     232    char *temp = NULL;                  // Temporary string
     233
     234    pmChip *chip = cell->parent;        // Chip of interest
     235    pmFPA *fpa = chip->parent;          // FPA of interest
     236
     237    // FPA.NAME
     238    if (temp = strstr(translation, "%a")) {
     239        // This is not particularly friendly to the memory allocation, but the cache should make it OK,
     240        // and there's not much of it anyway...
     241        psFree(translation);
     242        translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
     243        psString fpaName = psMetadataLookupString(NULL, fpa->concepts, "FPA.NAME"); // The value of FPA.NAME
     244        psStringAppend(&translation, "%s%s", fpaName, temp + 2);
     245        // So "translation" now contains the first part, the replaced string, and the last part.
     246    }
     247
     248    // CHIP.NAME
     249    if (temp = strstr(translation, "%b")) {
     250        // This is not particularly friendly to the memory allocation, but the cache should make it OK,
     251        // and there's not much of it anyway...
     252        psFree(translation);
     253        translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
     254        psString chipName = psMetadataLookupString(NULL, chip->concepts, "CHIP.NAME"); // CHIP.NAME's value
     255        psStringAppend(&translation, "%s%s", chipName, temp + 2);
     256        // So "translation" now contains the first part, the replaced string, and the last part.
     257    }
     258
     259    // CELL.NAME
     260    if (temp = strstr(translation, "%c")) {
     261        // This is not particularly friendly to the memory allocation, but the cache should make it OK,
     262        // and there's not much of it anyway...
     263        psFree(translation);
     264        translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
     265        psString cellName = psMetadataLookupString(NULL, cell->concepts, "CELL.NAME"); // CELL.NAME's value
     266        psStringAppend(&translation, "%s%s", cellName, temp + 2);
     267        // So "translation" now contains the first part, the replaced string, and the last part.
     268    }
     269
     270    // Chip number
     271    if (temp = strstr(translation, "%d")) {
     272        // This is not particularly friendly to the memory allocation, but the cache should make it OK,
     273        // and there's not much of it anyway...
     274        psFree(translation);
     275        translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
     276        // Search for the pointer to get the chip number
     277        int chipNum = -1;
     278        psArray *chips = fpa->chips;    // The array of chips
     279        for (int i = 0; i < chips->n && chipNum < 0; i++) {
     280            if (chips->data[i] == chip) {
     281                chipNum = i;
     282            }
     283        }
     284        if (chipNum < 0) {
     285            psError(PS_ERR_IO, true, "Unable to find chip to get name: %s\n", name);
     286            // Try to muddle on by leaving the number out
     287            psStringAppend(&translation, "%s", temp + 2);
     288        } else {
     289            psStringAppend(&translation, "%d%s", chipNum, temp + 2);
     290        }
     291        // So "translation" now contains the first part, the replaced string, and the last part.
     292    }
     293
     294    // Cell number
     295    if (temp = strstr(translation, "%e")) {
     296        // This is not particularly friendly to the memory allocation, but the cache should make it OK,
     297        // and there's not much of it anyway...
     298        psFree(translation);
     299        translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
     300        // Search for the pointer to get the cell number
     301        int cellNum = -1;
     302        psArray *cells = chip->cells;   // The array of cells
     303        for (int i = 0; i < cells->n && cellNum < 0; i++) {
     304            if (cells->data[i] == cell) {
     305                cellNum = i;
     306            }
     307        }
     308        if (cellNum < 0) {
     309            psError(PS_ERR_IO, true, "Unable to find cell to get name: %s\n", name);
     310            // Try to muddle on by leaving the number out
     311            psStringAppend(&translation, "%s", temp + 2);
     312        } else {
     313            psStringAppend(&translation, "%d%s", cellNum, temp + 2);
     314        }
     315        // So "translation" now contains the first part, the replaced string, and the last part.
     316    }
     317
     318    // Extension name
     319    if (temp = strstr(translation, "%f")) {
     320        // This is not particularly friendly to the memory allocation, but the cache should make it OK,
     321        // and there's not much of it anyway...
     322        psFree(translation);
     323        translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
     324        const char *extname = NULL;
     325        if (cell->hdu) {
     326            extname = cell->hdu->extname;
     327        } else if (chip->hdu) {
     328            extname = chip->hdu->extname;
     329        } else if (fpa->hdu) {
     330            extname = fpa->hdu->extname;
     331        }
     332        psStringAppend(&translation, "%s%s", extname, temp + 2);
     333        // So "translation" now contains the first part, the replaced string, and the last part.
     334    }
     335
     336    return translation;
     337}
     338
     339// Read a mask into the FPA
     340bool pmFPAReadMask(pmFPA *fpa,          // FPA to read into
     341                   psFits *source       // Source FITS file (for the original data)
     342    )
     343{
     344    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
     345    bool mdok = false;                  // Status of MD lookup
     346
     347    // Get the required information from the camera configuration
     348    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
     349    if (! mdok || ! supps) {
     350        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     351        return false;
     352    }
     353    psString sourceType = psMetadataLookupString(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE
     354    if (! mdok || strlen(sourceType) <= 0) {
     355        psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "
     356                "configuration!\n");
     357        return false;
     358    }
     359    psString name = psMetadataLookupString(&mdok, supps, "MASK.NAME"); // Name of mask
     360    if (! mdok || strlen(sourceType) <= 0) {
     361        psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "
     362                "configuration!\n");
     363        return false;
     364    }
     365
     366    // Go through the FPA to each cell/readout to get the mask
     367    p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the mask
     368    psArray *chips = fpa->chips;        // Array of chips
     369    for (int chipNum = 0; chipNum < chips->n; chipNum++) {
     370        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     371        if (chip->valid) {
     372            if (chip->hdu) {
     373                hdu = chip->hdu;
     374            }
     375            psArray *cells = chip->cells;       // Array of cells
     376            for (int cellNum = 0; cellNum < cells->n; cellNum++) {
     377                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     378                if (cell->valid) {
     379                    if (cell->hdu) {
     380                        hdu = cell->hdu;
     381                    }
     382
     383                    // Now, need to find out where to get the pixels
     384                    psFits *maskSource = source;        // Source of mask image
     385                    if (strcasecmp(sourceType, "FILE") == 0) {
     386                        // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"
     387                        psString filenameExt = p_pmFPATranslateName(name, cell);
     388                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     389                        psString filename = NULL; // The filename
     390                        psString extname = NULL;// The extenstion name
     391                        if (colon) {
     392                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     393                            if (strlen(colon) > 1) {
     394                                extname = psStringCopy(colon + 1);
     395                            }
     396                        } else {
     397                            filename = psMemIncrRefCounter(filenameExt);
     398                        }
     399                       
     400                        maskSource = psFitsAlloc(filename);
     401                        if (extname) {
     402                            if (! psFitsMoveExtName(maskSource, extname)) {
     403                                psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read mask.\n",
     404                                         extname);
     405                                return false;
     406                            }
     407                        }
     408                        psFree(filename);
     409                        psFree(extname);
     410                        psFree(filenameExt);
     411                    } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
     412                        // Source is an extension in the original file
     413                        psString extname = p_pmFPATranslateName(name, cell);
     414                        psFitsMoveExtName(maskSource, extname);
     415                    }
     416                   
     417                    // We've arrived where the pixels are.  Now we need to read them in.
     418                    psMetadata *header = psFitsReadHeader(NULL, maskSource); // The header
     419                    bool mdStatus = false;
     420                    int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS");
     421                    if (!mdStatus) {
     422                        psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for "
     423                                 "mask (%s)!\n", name);
     424                    }
     425                    if (nAxis != 2 && nAxis != 3) {
     426                        psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into "
     427                                 "a single image anyway.\n");
     428                    }
     429           
     430                    int numPlanes = 1;  // Number of planes
     431                    if (nAxis == 3) {
     432                        numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3");
     433                        if (!mdStatus) {
     434                            psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n");
     435                            // Try to proceed by taking only the first plane
     436                            numPlanes = 1;
     437                        }
     438                        if (numPlanes != 1 && numPlanes != cell->readouts->n) {
     439                            psError(PS_ERR_IO, false, "Number of masks (%d) does not match number of "
     440                                    "readouts (%d)\n", numPlanes, cell->readouts->n);
     441                            // Try to proceed by taking only the first plane
     442                            numPlanes = 1;
     443                        }
     444                    }
     445
     446                    hdu->masks = psArrayAlloc(hdu->images->n);
     447                   
     448                    // Read each plane into the array
     449                    psArray *readouts = cell->readouts; // The array of readouts
     450                    for (int i = 0; i < hdu->masks->n; i++) {
     451                        psImage *mask = NULL; // The mask to be added
     452                        if (i < numPlanes) {
     453                            // Read the mask from the file
     454                            psTrace(__func__, 9, "Reading plane %d\n", i);
     455                            psRegion region = {0, 0, 0, 0};
     456                            mask = psFitsReadImage(NULL, maskSource, region, i);
     457                        } else {
     458                            // One mask in the file is provided for all planes in the original image
     459                            psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i);
     460                            psImage *original = hdu->masks->data[0];
     461                            mask = psImageCopy(NULL, original, original->type.type);
     462                        }
     463                        hdu->masks->data[0] = mask;
     464                        pmReadout *readout = readouts->data[i];
     465                        readout->mask = mask;
     466                        // Check the dimensions
     467                        // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size?
     468                        if (mask->numCols < readout->image->numCols ||
     469                            mask->numRows < readout->image->numRows)
     470                        {
     471                            psError(PS_ERR_IO, false, "Mask size (%dx%d) not compatible with image (%dx%d)\n",
     472                                    mask->numCols, mask->numRows, readout->image->numCols,
     473                                    readout->image->numRows);
     474                            return false;
     475                        }
     476                    } // Iterating over readouts
     477                } // Valid cells
     478            } // Iterating over cells
     479        } // Valid chips
     480    } // Iterating over chips
     481
     482    return true;
     483}
     484
     485
     486// Read a mask into the FPA
     487// This is just a copy of the above pmFPAReadMask, replacing "mask" with "weight" throughout.
     488bool pmFPAReadWeight(pmFPA *fpa,        // FPA to read into
     489                     psFits *source     // Source FITS file (for the original data)
     490    )
     491{
     492    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
     493    bool mdok = false;                  // Status of MD lookup
     494
     495    // Get the required information from the camera configuration
     496    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
     497    if (! mdok || ! supps) {
     498        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     499        return false;
     500    }
     501    psString sourceType = psMetadataLookupString(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE
     502    if (! mdok || strlen(sourceType) <= 0) {
     503        psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "
     504                "configuration!\n");
     505        return false;
     506    }
     507    psString name = psMetadataLookupString(&mdok, supps, "WEIGHT.NAME"); // Name of weight
     508    if (! mdok || strlen(sourceType) <= 0) {
     509        psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "
     510                "configuration!\n");
     511        return false;
     512    }
     513
     514    // Go through the FPA to each cell/readout to get the weight
     515    p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the weight
     516    psArray *chips = fpa->chips;        // Array of chips
     517    for (int chipNum = 0; chipNum < chips->n; chipNum++) {
     518        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     519        if (chip->valid) {
     520            if (chip->hdu) {
     521                hdu = chip->hdu;
     522            }
     523            psArray *cells = chip->cells;       // Array of cells
     524            for (int cellNum = 0; cellNum < cells->n; cellNum++) {
     525                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     526                if (cell->valid) {
     527                    if (cell->hdu) {
     528                        hdu = cell->hdu;
     529                    }
     530
     531                    // Now, need to find out where to get the pixels
     532                    psFits *weightSource = source;      // Source of weight image
     533                    if (strcasecmp(sourceType, "FILE") == 0) {
     534                        // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"
     535                        psString filenameExt = p_pmFPATranslateName(name, cell);
     536                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     537                        psString filename = NULL; // The filename
     538                        psString extname = NULL;// The extenstion name
     539                        if (colon) {
     540                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     541                            if (strlen(colon) > 1) {
     542                                extname = psStringCopy(colon + 1);
     543                            }
     544                        } else {
     545                            filename = psMemIncrRefCounter(filenameExt);
     546                        }
     547                       
     548                        weightSource = psFitsAlloc(filename);
     549                        if (extname) {
     550                            if (! psFitsMoveExtName(weightSource, extname)) {
     551                                psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read "
     552                                         "weight.\n", extname);
     553                                return false;
     554                            }
     555                        }
     556                        psFree(filename);
     557                        psFree(extname);
     558                        psFree(filenameExt);
     559                    } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
     560                        // Source is an extension in the original file
     561                        psString extname = p_pmFPATranslateName(name, cell);
     562                        psFitsMoveExtName(weightSource, extname);
     563                    }
     564                   
     565                    // We've arrived where the pixels are.  Now we need to read them in.
     566                    psMetadata *header = psFitsReadHeader(NULL, weightSource); // The header
     567                    bool mdStatus = false;
     568                    int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS");
     569                    if (!mdStatus) {
     570                        psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for "
     571                                 "weight (%s)!\n", name);
     572                    }
     573                    if (nAxis != 2 && nAxis != 3) {
     574                        psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into "
     575                                 "a single image anyway.\n");
     576                    }
     577           
     578                    int numPlanes = 1;  // Number of planes
     579                    if (nAxis == 3) {
     580                        numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3");
     581                        if (!mdStatus) {
     582                            psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n");
     583                            // Try to proceed by taking only the first plane
     584                            numPlanes = 1;
     585                        }
     586                        if (numPlanes != 1 && numPlanes != cell->readouts->n) {
     587                            psError(PS_ERR_IO, false, "Number of weights (%d) does not match number of "
     588                                    "readouts (%d)\n", numPlanes, cell->readouts->n);
     589                            // Try to proceed by taking only the first plane
     590                            numPlanes = 1;
     591                        }
     592                    }
     593
     594                    hdu->weights = psArrayAlloc(hdu->images->n);
     595                   
     596                    // Read each plane into the array
     597                    psArray *readouts = cell->readouts; // The array of readouts
     598                    for (int i = 0; i < hdu->weights->n; i++) {
     599                        psImage *weight = NULL; // The weight to be added
     600                        if (i < numPlanes) {
     601                            // Read the weight from the file
     602                            psTrace(__func__, 9, "Reading plane %d\n", i);
     603                            psRegion region = {0, 0, 0, 0};
     604                            weight = psFitsReadImage(NULL, weightSource, region, i);
     605                        } else {
     606                            // One weight in the file is provided for all planes in the original image
     607                            psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i);
     608                            psImage *original = hdu->weights->data[0];
     609                            weight = psImageCopy(NULL, original, original->type.type);
     610                        }
     611                        hdu->weights->data[0] = weight;
     612                        pmReadout *readout = readouts->data[i];
     613                        readout->weight = weight;
     614                        // Check the dimensions
     615                        // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size?
     616                        if (weight->numCols < readout->image->numCols ||
     617                            weight->numRows < readout->image->numRows)
     618                        {
     619                            psError(PS_ERR_IO, false, "Weight size (%dx%d) not compatible with image "
     620                                    "(%dx%d)\n", weight->numCols, weight->numRows, readout->image->numCols,
     621                                    readout->image->numRows);
     622                            return false;
     623                        }
     624                    } // Iterating over readouts
     625                } // Valid cells
     626            } // Iterating over cells
     627        } // Valid chips
     628    } // Iterating over chips
     629
     630    return true;
     631}
  • trunk/archive/scripts/src/phase2/pmFPARead.h

    r5105 r5564  
    1010    );
    1111
     12psString p_pmFPATranslateName(psString name, // The name to translate
     13                              pmCell *cell // The cell for which to translate
     14    );
     15
     16bool pmFPAReadMask(pmFPA *fpa,          // FPA to read into
     17                     psFits *source     // Source FITS file (for the original data)
     18    );
     19
     20bool pmFPAReadWeight(pmFPA *fpa,        // FPA to read into
     21                     psFits *source     // Source FITS file (for the original data)
     22    );
     23
     24
    1225#endif
  • trunk/archive/scripts/src/phase2/pmFPAWrite.c

    r5371 r5564  
    55#include "pmFPA.h"
    66#include "pmFPAConceptsSet.h"
     7#include "pmFPARead.h"
    78
    89static bool writeHDU(psFits *fits,      // FITS file to which to write
    9                      pmPixelData *pixelData // Pixel data to write
     10                     p_pmHDU *hdu       // Pixel data to write
    1011    )
    1112{
    1213    bool status = true;                 // Status of write, to return
    13     for (int i = 0; i < pixelData->images->n; i++) {
    14         status &= psFitsWriteImage(fits, pixelData->header, pixelData->images->data[i], i);
     14    for (int i = 0; i < hdu->images->n; i++) {
     15        status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i);
    1516        // XXX: Insert here the writing on mask and weight images
    1617    }
     
    4748                    pmCellOutgestConcepts(cell, db);
    4849
    49                     if (cell->data && strlen(cell->data->extname) > 0) {
    50                         status &= writeHDU(fits, cell->data);
     50                    if (cell->hdu && strlen(cell->hdu->extname) > 0) {
     51                        status &= writeHDU(fits, cell->hdu);
    5152                    }
    5253                }
    5354            }
    5455           
    55             if (chip->data && strlen(chip->data->extname) > 0) {
    56                 status &= writeHDU(fits, chip->data);
     56            if (chip->hdu && strlen(chip->hdu->extname) > 0) {
     57                status &= writeHDU(fits, chip->hdu);
    5758            }
    5859        }
     
    6061    }
    6162
    62     if (fpa->data && strlen(fpa->data->extname) > 0) {
    63         status &= writeHDU(fits, fpa->data);
     63    if (fpa->hdu && strlen(fpa->hdu->extname) > 0) {
     64        status &= writeHDU(fits, fpa->hdu);
    6465    }
    6566
    6667    return status;
    6768}
     69
     70
     71bool pmFPAWriteMask(pmFPA *fpa,         // FPA containing mask to write
     72                    psFits *fits        // FITS file for image
     73    )
     74{
     75    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
     76    bool mdok = false;                  // Status of MD lookup
     77
     78    // Get the required information from the camera configuration
     79    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
     80    if (! mdok || ! supps) {
     81        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     82        return false;
     83    }
     84    psString sourceType = psMetadataLookupString(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE
     85    if (! mdok || strlen(sourceType) <= 0) {
     86        psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "
     87                "configuration!\n");
     88        return false;
     89    }
     90    psString name = psMetadataLookupString(&mdok, supps, "MASK.NAME"); // Name of mask
     91    if (! mdok || strlen(sourceType) <= 0) {
     92        psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "
     93                "configuration!\n");
     94        return false;
     95    }
     96
     97    // Go through the FPA to each cell/readout to get the mask
     98    p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the mask
     99    psArray *chips = fpa->chips;        // Array of chips
     100    for (int chipNum = 0; chipNum < chips->n; chipNum++) {
     101        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     102        if (chip->valid) {
     103            if (chip->hdu) {
     104                hdu = chip->hdu;
     105            }
     106            psArray *cells = chip->cells;       // Array of cells
     107            for (int cellNum = 0; cellNum < cells->n; cellNum++) {
     108                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     109                if (cell->valid) {
     110                    if (cell->hdu) {
     111                        hdu = cell->hdu;
     112                    }
     113
     114                    // Now, need to find out where to write the pixels
     115                    psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image
     116                    psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
     117                    if (strcasecmp(sourceType, "FILE") == 0) {
     118                        // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"
     119                        psString filenameExt = p_pmFPATranslateName(name, cell);
     120                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     121                        psString filename = NULL; // The filename
     122                        psString extname = NULL;// The extenstion name
     123                        if (colon) {
     124                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     125                            if (strlen(colon) > 1) {
     126                                extname = psStringCopy(colon + 1);
     127                            }
     128                        } else {
     129                            filename = psMemIncrRefCounter(filenameExt);
     130                        }
     131
     132                        psFree(maskDest);
     133                        maskDest = psFitsAlloc(filename);
     134                        if (extname) {
     135                            psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     136                        }
     137                        psFree(filename);
     138                        psFree(extname);
     139                        psFree(filenameExt);
     140                    } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
     141                        // Source is an extension in the original file
     142                        psString extname = p_pmFPATranslateName(name, cell);
     143                        psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     144                        psFree(extname);
     145                    }
     146                   
     147                    // We've arrived where the pixels are.  Now we need to write them out.
     148                    psArray *readouts = cell->readouts; // The array of readouts
     149                    for (int readNum = 0; readNum < readouts->n; readNum++) {
     150                        pmReadout *readout = readouts->data[readNum]; // The readout of interest
     151                        if (! readout->mask) {
     152                            psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n",
     153                                     chipNum, cellNum, readNum);
     154                        } else {
     155                            // XXX: Need to add the extname to the existing header
     156                            if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) {
     157                                psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n",
     158                                        readNum, hdu->extname);
     159                                return false;
     160                            }
     161                        }
     162                    } // Iterating over readouts
     163                    psFree(header);
     164                    psFree(maskDest);
     165                } // Valid cells
     166            } // Iterating over cells
     167        } // Valid chips
     168    } // Iterating over chips
     169
     170    return true;
     171}
     172
     173
     174bool pmFPAWriteWeight(pmFPA *fpa,       // FPA containing mask to write
     175                      psFits *fits      // FITS file for image
     176    )
     177{
     178    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
     179    bool mdok = false;                  // Status of MD lookup
     180
     181    // Get the required information from the camera configuration
     182    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
     183    if (! mdok || ! supps) {
     184        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     185        return false;
     186    }
     187    psString sourceType = psMetadataLookupString(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE
     188    if (! mdok || strlen(sourceType) <= 0) {
     189        psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "
     190                "configuration!\n");
     191        return false;
     192    }
     193    psString name = psMetadataLookupString(&mdok, supps, "WEIGHT.NAME"); // Name of weight
     194    if (! mdok || strlen(sourceType) <= 0) {
     195        psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "
     196                "configuration!\n");
     197        return false;
     198    }
     199
     200    // Go through the FPA to each cell/readout to get the weight
     201    p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the weight
     202    psArray *chips = fpa->chips;        // Array of chips
     203    for (int chipNum = 0; chipNum < chips->n; chipNum++) {
     204        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     205        if (chip->valid) {
     206            if (chip->hdu) {
     207                hdu = chip->hdu;
     208            }
     209            psArray *cells = chip->cells;       // Array of cells
     210            for (int cellNum = 0; cellNum < cells->n; cellNum++) {
     211                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     212                if (cell->valid) {
     213                    if (cell->hdu) {
     214                        hdu = cell->hdu;
     215                    }
     216
     217                    // Now, need to find out where to write the pixels
     218                    psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image
     219                    psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
     220                    if (strcasecmp(sourceType, "FILE") == 0) {
     221                        // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"
     222                        psString filenameExt = p_pmFPATranslateName(name, cell);
     223                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     224                        psString filename = NULL; // The filename
     225                        psString extname = NULL;// The extenstion name
     226                        if (colon) {
     227                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     228                            if (strlen(colon) > 1) {
     229                                extname = psStringCopy(colon + 1);
     230                            }
     231                        } else {
     232                            filename = psMemIncrRefCounter(filenameExt);
     233                        }
     234
     235                        psFree(weightDest);
     236                        weightDest = psFitsAlloc(filename);
     237                        if (extname) {
     238                            psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     239                        }
     240                        psFree(filename);
     241                        psFree(extname);
     242                        psFree(filenameExt);
     243                    } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
     244                        // Source is an extension in the original file
     245                        psString extname = p_pmFPATranslateName(name, cell);
     246                        psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     247                        psFree(extname);
     248                    }
     249
     250                    // We've arrived where the pixels are.  Now we need to write them out.
     251                    psArray *readouts = cell->readouts; // The array of readouts
     252                    for (int readNum = 0; readNum < readouts->n; readNum++) {
     253                        pmReadout *readout = readouts->data[readNum]; // The readout of interest
     254                        if (! readout->weight) {
     255                            psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n",
     256                                     chipNum, cellNum, readNum);
     257                        } else {
     258                            if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) {
     259                                psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n",
     260                                        readNum, hdu->extname);
     261                                return false;
     262                            }
     263                        }
     264                    } // Iterating over readouts
     265                    psFree(header);
     266                    psFree(weightDest);
     267                } // Valid cells
     268            } // Iterating over cells
     269        } // Valid chips
     270    } // Iterating over chips
     271
     272    return true;
     273}
  • trunk/archive/scripts/src/phase2/pmFPAWrite.h

    r5105 r5564  
    99    );
    1010
     11bool pmFPAWriteMask(pmFPA *fpa,         // FPA containing mask to write
     12                    psFits *fits        // FITS file for image
     13    );
     14
     15bool pmFPAWriteWeight(pmFPA *fpa,       // FPA containing mask to write
     16                      psFits *fits      // FITS file for image
     17    );
     18
    1119
    1220#endif
Note: See TracChangeset for help on using the changeset viewer.