IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 30, 2005, 3:43:10 PM (21 years ago)
Author:
jhoblitt
Message:

astyle

File:
1 edited

Legend:

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

    r5633 r5648  
    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     )
     9static bool writeHDU(psFits *fits,  // FITS file to which to write
     10                     p_pmHDU *hdu // Pixel data to write
     11                    )
    1212{
    13     bool status = true;                 // Status of write, to return
    14     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
     13    bool status = true;   // Status of write, to return
     14    for (int i = 0; i < hdu->images->n; i++)
     15    {
     16        status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i);
     17        // XXX: Insert here the writing on mask and weight images
    1718    }
    1819
     
    2122
    2223
    23 bool pmFPAWrite(psFits *fits,           // FITS file to which to write
    24                 pmFPA *fpa,             // FPA to write
    25                 psDB *db                // Database to update
    26     )
     24bool pmFPAWrite(psFits *fits,   // FITS file to which to write
     25                pmFPA *fpa,   // FPA to write
     26                psDB *db  // Database to update
     27               )
    2728{
    28     bool status = true;                 // Status of writing, to return
     29    bool status = true;   // Status of writing, to return
    2930
    3031    pmFPAOutgestConcepts(fpa, db);
    3132
    3233    // Write the primary header
    33     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
    39     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         }
    60 
    61     }
    62 
    63     if (fpa->hdu && strlen(fpa->hdu->extname) > 0) {
    64         status &= writeHDU(fits, fpa->hdu);
    65     }
    66 
    67 
     34    if (fpa->phu)
     35    {
     36        status &= psFitsMoveExtNum(fits, 0, false);
     37        status &= psFitsWriteHeader(fpa->phu, fits);
     38    }
     39
     40    psArray *chips = fpa->chips; // Array of component chips
     41    for (int i = 0; i < chips->n; i++)
     42    {
     43        pmChip *chip = chips->data[i]; // The component chip
     44        if (chip->valid)
     45        {
     46            pmChipOutgestConcepts(chip, db);
     47
     48            psArray *cells = chip->cells; // Array of component cells
     49            for (int j = 0; j < cells->n; j++)
     50            {
     51                pmCell *cell = cells->data[j]; // The component cell
     52                if (cell->valid)
     53                {
     54                    pmCellOutgestConcepts(cell, db);
     55
     56                    if (cell->hdu && strlen(cell->hdu->extname) > 0)
     57                    {
     58                        status &= writeHDU(fits, cell->hdu);
     59                    }
     60                }
     61            }
     62
     63            if (chip->hdu && strlen(chip->hdu->extname) > 0)
     64            {
     65                status &= writeHDU(fits, chip->hdu);
     66            }
     67        }
     68
     69    }
     70
     71    if (fpa->hdu && strlen(fpa->hdu->extname) > 0)
     72    {
     73        status &= writeHDU(fits, fpa->hdu);
     74    }
    6875
    6976    return status;
     
    7178
    7279
    73 bool pmFPAWriteMask(pmFPA *fpa,         // FPA containing mask to write
    74                     psFits *fits        // FITS file for image
    75     )
     80bool pmFPAWriteMask(pmFPA *fpa,   // FPA containing mask to write
     81                    psFits *fits // FITS file for image
     82                   )
    7683{
    7784    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    78     bool mdok = false;                  // Status of MD lookup
     85    bool mdok = false;   // Status of MD lookup
    7986
    8087    // Get the required information from the camera configuration
    8188    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    82     if (! mdok || ! supps) {
    83         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    84         return false;
     89    if (! mdok || ! supps)
     90    {
     91        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     92        return false;
    8593    }
    8694    psString sourceType = psMetadataLookupString(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE
    87     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;
     95    if (! mdok || strlen(sourceType) <= 0)
     96    {
     97        psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "
     98                "configuration!\n");
     99        return false;
    91100    }
    92101    psString name = psMetadataLookupString(&mdok, supps, "MASK.NAME"); // Name of mask
    93     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;
     102    if (! mdok || strlen(sourceType) <= 0)
     103    {
     104        psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "
     105                "configuration!\n");
     106        return false;
    97107    }
    98108
    99109    // 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
    102     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
     110    p_pmHDU *hdu = fpa->hdu;  // The HDU into which we will read the mask
     111    psArray *chips = fpa->chips; // Array of chips
     112    for (int chipNum = 0; chipNum < chips->n; chipNum++)
     113    {
     114        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     115        if (chip->valid)
     116        {
     117            if (chip->hdu)
     118            {
     119                hdu = chip->hdu;
     120            }
     121            psArray *cells = chip->cells; // Array of cells
     122            for (int cellNum = 0; cellNum < cells->n; cellNum++)
     123            {
     124                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     125                if (cell->valid)
     126                {
     127                    if (cell->hdu)
     128                    {
     129                        hdu = cell->hdu;
     130                    }
     131
     132                    // Now, need to find out where to write the pixels
     133                    psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image
     134                    psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
     135                    if (strcasecmp(sourceType, "FILE") == 0)
     136                    {
     137                        // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"
     138                        psString filenameExt = p_pmFPATranslateName(name, cell);
     139                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     140                        psString filename = NULL; // The filename
     141                        psString extname = NULL; // The extenstion name
     142                        if (colon)
     143                        {
     144                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     145                            if (strlen(colon) > 1)
     146                            {
     147                                extname = psStringCopy(colon + 1);
     148                            }
     149                        }
     150                        else
     151                        {
     152                            filename = psMemIncrRefCounter(filenameExt);
     153                        }
     154
     155                        psFree(maskDest);
     156                        maskDest = psFitsAlloc(filename);
     157                        if (extname)
     158                        {
     159                            psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     160                        }
     161                        psFree(filename);
     162                        psFree(extname);
     163                        psFree(filenameExt);
     164                    }
     165                    else if (strncasecmp(sourceType, "EXT", 3) == 0)
     166                    {
     167                        // Source is an extension in the original file
     168                        psString extname = p_pmFPATranslateName(name, cell);
     169                        psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     170                        psFree(extname);
     171                    }
     172
     173                    // We've arrived where the pixels are.  Now we need to write them out.
     174                    psArray *readouts = cell->readouts; // The array of readouts
     175                    for (int readNum = 0; readNum < readouts->n; readNum++)
     176                    {
     177                        pmReadout *readout = readouts->data[readNum]; // The readout of interest
     178                        if (! readout->mask)
     179                        {
     180                            psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n",
     181                                     chipNum, cellNum, readNum);
     182                        }
     183                        else
     184                        {
     185                            // XXX: Need to add the extname to the existing header
     186                            if (! psFitsWriteImage(maskDest, header, readout->mask, readNum))
     187                            {
     188                                psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n",
     189                                        readNum, hdu->extname);
     190                                return false;
     191                            }
     192                        }
     193                    } // Iterating over readouts
     194                    psFree(header);
     195                    psFree(maskDest);
     196                } // Valid cells
     197            } // Iterating over cells
     198        } // Valid chips
    170199    } // Iterating over chips
    171200
     
    174203
    175204
    176 bool pmFPAWriteWeight(pmFPA *fpa,       // FPA containing mask to write
    177                       psFits *fits      // FITS file for image
    178     )
     205bool pmFPAWriteWeight(pmFPA *fpa,   // FPA containing mask to write
     206                      psFits *fits // FITS file for image
     207                     )
    179208{
    180209    const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    181     bool mdok = false;                  // Status of MD lookup
     210    bool mdok = false;   // Status of MD lookup
    182211
    183212    // Get the required information from the camera configuration
    184213    psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    185     if (! mdok || ! supps) {
    186         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    187         return false;
     214    if (! mdok || ! supps)
     215    {
     216        psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
     217        return false;
    188218    }
    189219    psString sourceType = psMetadataLookupString(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE
    190     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;
     220    if (! mdok || strlen(sourceType) <= 0)
     221    {
     222        psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "
     223                "configuration!\n");
     224        return false;
    194225    }
    195226    psString name = psMetadataLookupString(&mdok, supps, "WEIGHT.NAME"); // Name of weight
    196     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;
     227    if (! mdok || strlen(sourceType) <= 0)
     228    {
     229        psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "
     230                "configuration!\n");
     231        return false;
    200232    }
    201233
    202234    // 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
    205     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
     235    p_pmHDU *hdu = fpa->hdu;  // The HDU into which we will read the weight
     236    psArray *chips = fpa->chips; // Array of chips
     237    for (int chipNum = 0; chipNum < chips->n; chipNum++)
     238    {
     239        pmChip *chip = chips->data[chipNum]; // The current chip of interest
     240        if (chip->valid)
     241        {
     242            if (chip->hdu)
     243            {
     244                hdu = chip->hdu;
     245            }
     246            psArray *cells = chip->cells; // Array of cells
     247            for (int cellNum = 0; cellNum < cells->n; cellNum++)
     248            {
     249                pmCell *cell = cells->data[cellNum]; // The current cell of interest
     250                if (cell->valid)
     251                {
     252                    if (cell->hdu)
     253                    {
     254                        hdu = cell->hdu;
     255                    }
     256
     257                    // Now, need to find out where to write the pixels
     258                    psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image
     259                    psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
     260                    if (strcasecmp(sourceType, "FILE") == 0)
     261                    {
     262                        // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"
     263                        psString filenameExt = p_pmFPATranslateName(name, cell);
     264                        char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
     265                        psString filename = NULL; // The filename
     266                        psString extname = NULL; // The extenstion name
     267                        if (colon)
     268                        {
     269                            filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
     270                            if (strlen(colon) > 1)
     271                            {
     272                                extname = psStringCopy(colon + 1);
     273                            }
     274                        }
     275                        else
     276                        {
     277                            filename = psMemIncrRefCounter(filenameExt);
     278                        }
     279
     280                        psFree(weightDest);
     281                        weightDest = psFitsAlloc(filename);
     282                        if (extname)
     283                        {
     284                            psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     285                        }
     286                        psFree(filename);
     287                        psFree(extname);
     288                        psFree(filenameExt);
     289                    }
     290                    else if (strncasecmp(sourceType, "EXT", 3) == 0)
     291                    {
     292                        // Source is an extension in the original file
     293                        psString extname = p_pmFPATranslateName(name, cell);
     294                        psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
     295                        psFree(extname);
     296                    }
     297
     298                    // We've arrived where the pixels are.  Now we need to write them out.
     299                    psArray *readouts = cell->readouts; // The array of readouts
     300                    for (int readNum = 0; readNum < readouts->n; readNum++)
     301                    {
     302                        pmReadout *readout = readouts->data[readNum]; // The readout of interest
     303                        if (! readout->weight)
     304                        {
     305                            psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n",
     306                                     chipNum, cellNum, readNum);
     307                        }
     308                        else
     309                        {
     310                            if (! psFitsWriteImage(weightDest, header, readout->weight, readNum))
     311                            {
     312                                psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n",
     313                                        readNum, hdu->extname);
     314                                return false;
     315                            }
     316                        }
     317                    } // Iterating over readouts
     318                    psFree(header);
     319                    psFree(weightDest);
     320                } // Valid cells
     321            } // Iterating over cells
     322        } // Valid chips
    272323    } // Iterating over chips
    273324
Note: See TracChangeset for help on using the changeset viewer.