IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 13, 2005, 5:47:35 PM (20 years ago)
Author:
Paul Price
Message:

Upgrading for psLib-0.9.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/scripts/src/phase2/pmFPAWrite.c

    r5633 r5786  
    77#include "pmFPARead.h"
    88
    9 static bool writeHDU(psFits *fits,      // FITS file to which to write
    10                      p_pmHDU *hdu       // Pixel data to write
    11     )
    12 {
    13     bool status = true;                 // Status of write, to return
     9static bool writeHDU(psFits *fits,      // FITS file to which to write
     10                     p_pmHDU *hdu       // Pixel data to write
     11    )
     12{
     13    bool status = true;                 // Status of write, to return
    1414    for (int i = 0; i < hdu->images->n; i++) {
    15         status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i);
    16         // XXX: Insert here the writing on mask and weight images
     15        status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i);
     16        // XXX: Insert here the writing on mask and weight images
    1717    }
    1818
     
    2121
    2222
    23 bool pmFPAWrite(psFits *fits,           // FITS file to which to write
    24                 pmFPA *fpa,             // FPA to write
    25                 psDB *db                // Database to update
    26     )
    27 {
    28     bool status = true;                 // Status of writing, to return
    29 
     23bool pmFPAWrite(psFits *fits,           // FITS file to which to write
     24                pmFPA *fpa,             // FPA to write
     25                psDB *db                // Database to update
     26    )
     27{
     28    bool status = true;                 // Status of writing, to return
     29
     30    psTrace(__func__, 7, "Outgesting FPA concepts...\n");
    3031    pmFPAOutgestConcepts(fpa, db);
    3132
    3233    // Write the primary header
    3334    if (fpa->phu) {
    34         status &= psFitsMoveExtNum(fits, 0, false);
    35         status &= psFitsWriteHeader(fpa->phu, fits);
    36     }
    37 
    38     psArray *chips = fpa->chips;        // Array of component chips
     35        status &= psFitsMoveExtNum(fits, 0, false);
     36        status &= psFitsWriteHeader(fpa->phu, fits);
     37    }
     38
     39    psArray *chips = fpa->chips;        // Array of component chips
    3940    for (int i = 0; i < chips->n; i++) {
    40         pmChip *chip = chips->data[i];  // The component chip
    41         if (chip->valid) {
    42             pmChipOutgestConcepts(chip, db);
    43 
    44             psArray *cells = chip->cells;       // Array of component cells
    45             for (int j = 0; j < cells->n; j++) {
    46                 pmCell *cell = cells->data[j]; // The component cell
    47                 if (cell->valid) {
    48                     pmCellOutgestConcepts(cell, db);
    49 
    50                     if (cell->hdu && strlen(cell->hdu->extname) > 0) {
    51                         status &= writeHDU(fits, cell->hdu);
    52                     }
    53                 }
    54             }
    55            
    56             if (chip->hdu && strlen(chip->hdu->extname) > 0) {
    57                 status &= writeHDU(fits, chip->hdu);
    58             }
    59         }
     41        pmChip *chip = chips->data[i];  // The component chip
     42        if (chip->valid) {
     43            psTrace(__func__, 1, "Writing out chip %d...\n", i);
     44
     45            pmChipOutgestConcepts(chip, db);
     46
     47            psArray *cells = chip->cells;       // Array of component cells
     48            for (int j = 0; j < cells->n; j++) {
     49                pmCell *cell = cells->data[j]; // The component cell
     50                if (cell->valid) {
     51                    psTrace(__func__, 2, "Writing out cell, %d...\n", j);
     52
     53                    pmCellOutgestConcepts(cell, db);
     54
     55                    if (cell->hdu && strlen(cell->hdu->extname) > 0) {
     56                        status &= writeHDU(fits, cell->hdu);
     57                    }
     58                }
     59            }
     60
     61            if (chip->hdu && strlen(chip->hdu->extname) > 0) {
     62                status &= writeHDU(fits, chip->hdu);
     63            }
     64        }
    6065
    6166    }
    6267
    6368    if (fpa->hdu && strlen(fpa->hdu->extname) > 0) {
    64         status &= writeHDU(fits, fpa->hdu);
     69        status &= writeHDU(fits, fpa->hdu);
    6570    }
    6671
     
    7176
    7277
    73 bool pmFPAWriteMask(pmFPA *fpa,         // FPA containing mask to write
    74                     psFits *fits        // FITS file for image
     78bool pmFPAWriteMask(pmFPA *fpa,         // FPA containing mask to write
     79                    psFits *fits        // FITS file for image
    7580    )
    7681{
    7782    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    78     bool mdok = false;                  // Status of MD lookup
     83    bool mdok = false;                  // Status of MD lookup
    7984
    8085    // Get the required information from the camera configuration
    8186    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    8287    if (! mdok || ! supps) {
    83         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    84         return false;
     88        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     89        return false;
    8590    }
    8691    psString sourceType = psMetadataLookupString(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE
    8792    if (! mdok || strlen(sourceType) <= 0) {
    88         psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "
    89                 "configuration!\n");
    90         return false;
     93        psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "
     94                "configuration!\n");
     95        return false;
    9196    }
    9297    psString name = psMetadataLookupString(&mdok, supps, "MASK.NAME"); // Name of mask
    9398    if (! mdok || strlen(sourceType) <= 0) {
    94         psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "
    95                 "configuration!\n");
    96         return false;
     99        psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "
     100                "configuration!\n");
     101        return false;
    97102    }
    98103
    99104    // Go through the FPA to each cell/readout to get the mask
    100     p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the mask
    101     psArray *chips = fpa->chips;        // Array of chips
     105    p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the mask
     106    psArray *chips = fpa->chips;        // Array of chips
    102107    for (int chipNum = 0; chipNum < chips->n; chipNum++) {
    103         pmChip *chip = chips->data[chipNum]; // The current chip of interest
    104         if (chip->valid) {
    105             if (chip->hdu) {
    106                 hdu = chip->hdu;
    107             }
    108             psArray *cells = chip->cells;       // Array of cells
    109             for (int cellNum = 0; cellNum < cells->n; cellNum++) {
    110                 pmCell *cell = cells->data[cellNum]; // The current cell of interest
    111                 if (cell->valid) {
    112                     if (cell->hdu) {
    113                         hdu = cell->hdu;
    114                     }
    115 
    116                     // Now, need to find out where to write the pixels
    117                     psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image
    118                     psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
    119                     if (strcasecmp(sourceType, "FILE") == 0) {
    120                         // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"
    121                         psString filenameExt = p_pmFPATranslateName(name, cell);
    122                         char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
    123                         psString filename = NULL; // The filename
    124                         psString extname = NULL;// The extenstion name
    125                         if (colon) {
    126                             filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
    127                             if (strlen(colon) > 1) {
    128                                 extname = psStringCopy(colon + 1);
    129                             }
    130                         } else {
    131                             filename = psMemIncrRefCounter(filenameExt);
    132                         }
    133 
    134                         psFree(maskDest);
    135                         maskDest = psFitsAlloc(filename);
    136                         if (extname) {
    137                             psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    138                         }
    139                         psFree(filename);
    140                         psFree(extname);
    141                         psFree(filenameExt);
    142                     } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
    143                         // Source is an extension in the original file
    144                         psString extname = p_pmFPATranslateName(name, cell);
    145                         psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    146                         psFree(extname);
    147                     }
    148                    
    149                     // We've arrived where the pixels are.  Now we need to write them out.
    150                     psArray *readouts = cell->readouts; // The array of readouts
    151                     for (int readNum = 0; readNum < readouts->n; readNum++) {
    152                         pmReadout *readout = readouts->data[readNum]; // The readout of interest
    153                         if (! readout->mask) {
    154                             psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n",
    155                                      chipNum, cellNum, readNum);
    156                         } else {
    157                             // XXX: Need to add the extname to the existing header
    158                             if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) {
    159                                 psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n",
    160                                         readNum, hdu->extname);
    161                                 return false;
    162                             }
    163                         }
    164                     } // Iterating over readouts
    165                     psFree(header);
    166                     psFree(maskDest);
    167                 } // Valid cells
    168             } // Iterating over cells
    169         } // Valid chips
     108        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     109        if (chip->valid) {
     110            if (chip->hdu) {
     111                hdu = chip->hdu;
     112            }
     113            psArray *cells = chip->cells;       // Array of cells
     114            for (int cellNum = 0; cellNum < cells->n; cellNum++) {
     115                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     116                if (cell->valid) {
     117                    if (cell->hdu) {
     118                        hdu = cell->hdu;
     119                    }
     120
     121                    // Now, need to find out where to write the pixels
     122                    psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image
     123                    psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
     124                    if (strcasecmp(sourceType, "FILE") == 0) {
     125                        // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"
     126                        psString filenameExt = p_pmFPATranslateName(name, cell);
     127                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     128                        psString filename = NULL; // The filename
     129                        psString extname = NULL;// The extenstion name
     130                        if (colon) {
     131                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     132                            if (strlen(colon) > 1) {
     133                                extname = psStringCopy(colon + 1);
     134                            }
     135                        } else {
     136                            filename = psMemIncrRefCounter(filenameExt);
     137                        }
     138
     139                        psFree(maskDest);
     140                        maskDest = psFitsOpen(filename, "rw");
     141                        if (extname) {
     142                            psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     143                        }
     144                        psFree(filename);
     145                        psFree(extname);
     146                        psFree(filenameExt);
     147                    } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
     148                        // Source is an extension in the original file
     149                        psString extname = p_pmFPATranslateName(name, cell);
     150                        psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     151                        psFree(extname);
     152                    }
     153
     154                    // We've arrived where the pixels are.  Now we need to write them out.
     155                    psArray *readouts = cell->readouts; // The array of readouts
     156                    for (int readNum = 0; readNum < readouts->n; readNum++) {
     157                        pmReadout *readout = readouts->data[readNum]; // The readout of interest
     158                        if (! readout->mask) {
     159                            psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n",
     160                                     chipNum, cellNum, readNum);
     161                        } else {
     162                            // XXX: Need to add the extname to the existing header
     163                            if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) {
     164                                psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n",
     165                                        readNum, hdu->extname);
     166                                return false;
     167                            }
     168                        }
     169                    } // Iterating over readouts
     170                    psFree(header);
     171                    psFree(maskDest);
     172                } // Valid cells
     173            } // Iterating over cells
     174        } // Valid chips
    170175    } // Iterating over chips
    171176
     
    174179
    175180
    176 bool pmFPAWriteWeight(pmFPA *fpa,       // FPA containing mask to write
    177                       psFits *fits      // FITS file for image
     181bool pmFPAWriteWeight(pmFPA *fpa,       // FPA containing mask to write
     182                      psFits *fits      // FITS file for image
    178183    )
    179184{
    180185    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    181     bool mdok = false;                  // Status of MD lookup
     186    bool mdok = false;                  // Status of MD lookup
    182187
    183188    // Get the required information from the camera configuration
    184189    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    185190    if (! mdok || ! supps) {
    186         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    187         return false;
     191        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     192        return false;
    188193    }
    189194    psString sourceType = psMetadataLookupString(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE
    190195    if (! mdok || strlen(sourceType) <= 0) {
    191         psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "
    192                 "configuration!\n");
    193         return false;
     196        psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "
     197                "configuration!\n");
     198        return false;
    194199    }
    195200    psString name = psMetadataLookupString(&mdok, supps, "WEIGHT.NAME"); // Name of weight
    196201    if (! mdok || strlen(sourceType) <= 0) {
    197         psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "
    198                 "configuration!\n");
    199         return false;
     202        psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "
     203                "configuration!\n");
     204        return false;
    200205    }
    201206
    202207    // Go through the FPA to each cell/readout to get the weight
    203     p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the weight
    204     psArray *chips = fpa->chips;        // Array of chips
     208    p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the weight
     209    psArray *chips = fpa->chips;        // Array of chips
    205210    for (int chipNum = 0; chipNum < chips->n; chipNum++) {
    206         pmChip *chip = chips->data[chipNum]; // The current chip of interest
    207         if (chip->valid) {
    208             if (chip->hdu) {
    209                 hdu = chip->hdu;
    210             }
    211             psArray *cells = chip->cells;       // Array of cells
    212             for (int cellNum = 0; cellNum < cells->n; cellNum++) {
    213                 pmCell *cell = cells->data[cellNum]; // The current cell of interest
    214                 if (cell->valid) {
    215                     if (cell->hdu) {
    216                         hdu = cell->hdu;
    217                     }
    218 
    219                     // Now, need to find out where to write the pixels
    220                     psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image
    221                     psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
    222                     if (strcasecmp(sourceType, "FILE") == 0) {
    223                         // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"
    224                         psString filenameExt = p_pmFPATranslateName(name, cell);
    225                         char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
    226                         psString filename = NULL; // The filename
    227                         psString extname = NULL;// The extenstion name
    228                         if (colon) {
    229                             filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
    230                             if (strlen(colon) > 1) {
    231                                 extname = psStringCopy(colon + 1);
    232                             }
    233                         } else {
    234                             filename = psMemIncrRefCounter(filenameExt);
    235                         }
    236 
    237                         psFree(weightDest);
    238                         weightDest = psFitsAlloc(filename);
    239                         if (extname) {
    240                             psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    241                         }
    242                         psFree(filename);
    243                         psFree(extname);
    244                         psFree(filenameExt);
    245                     } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
    246                         // Source is an extension in the original file
    247                         psString extname = p_pmFPATranslateName(name, cell);
    248                         psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    249                         psFree(extname);
    250                     }
    251 
    252                     // We've arrived where the pixels are.  Now we need to write them out.
    253                     psArray *readouts = cell->readouts; // The array of readouts
    254                     for (int readNum = 0; readNum < readouts->n; readNum++) {
    255                         pmReadout *readout = readouts->data[readNum]; // The readout of interest
    256                         if (! readout->weight) {
    257                             psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n",
    258                                      chipNum, cellNum, readNum);
    259                         } else {
    260                             if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) {
    261                                 psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n",
    262                                         readNum, hdu->extname);
    263                                 return false;
    264                             }
    265                         }
    266                     } // Iterating over readouts
    267                     psFree(header);
    268                     psFree(weightDest);
    269                 } // Valid cells
    270             } // Iterating over cells
    271         } // Valid chips
     211        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     212        if (chip->valid) {
     213            if (chip->hdu) {
     214                hdu = chip->hdu;
     215            }
     216            psArray *cells = chip->cells;       // Array of cells
     217            for (int cellNum = 0; cellNum < cells->n; cellNum++) {
     218                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     219                if (cell->valid) {
     220                    if (cell->hdu) {
     221                        hdu = cell->hdu;
     222                    }
     223
     224                    // Now, need to find out where to write the pixels
     225                    psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image
     226                    psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
     227                    if (strcasecmp(sourceType, "FILE") == 0) {
     228                        // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"
     229                        psString filenameExt = p_pmFPATranslateName(name, cell);
     230                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     231                        psString filename = NULL; // The filename
     232                        psString extname = NULL;// The extenstion name
     233                        if (colon) {
     234                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     235                            if (strlen(colon) > 1) {
     236                                extname = psStringCopy(colon + 1);
     237                            }
     238                        } else {
     239                            filename = psMemIncrRefCounter(filenameExt);
     240                        }
     241
     242                        psFree(weightDest);
     243                        weightDest = psFitsOpen(filename, "rw");
     244                        if (extname) {
     245                            psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     246                        }
     247                        psFree(filename);
     248                        psFree(extname);
     249                        psFree(filenameExt);
     250                    } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
     251                        // Source is an extension in the original file
     252                        psString extname = p_pmFPATranslateName(name, cell);
     253                        psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     254                        psFree(extname);
     255                    }
     256
     257                    // We've arrived where the pixels are.  Now we need to write them out.
     258                    psArray *readouts = cell->readouts; // The array of readouts
     259                    for (int readNum = 0; readNum < readouts->n; readNum++) {
     260                        pmReadout *readout = readouts->data[readNum]; // The readout of interest
     261                        if (! readout->weight) {
     262                            psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n",
     263                                     chipNum, cellNum, readNum);
     264                        } else {
     265                            if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) {
     266                                psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n",
     267                                        readNum, hdu->extname);
     268                                return false;
     269                            }
     270                        }
     271                    } // Iterating over readouts
     272                    psFree(header);
     273                    psFree(weightDest);
     274                } // Valid cells
     275            } // Iterating over cells
     276        } // Valid chips
    272277    } // Iterating over chips
    273278
Note: See TracChangeset for help on using the changeset viewer.