IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7566


Ignore:
Timestamp:
Jun 14, 2006, 12:23:18 PM (20 years ago)
Author:
Paul Price
Message:

Adding error checking, plug memory leaks, macro-ize repetitive code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/detrend/pmFringeStats.c

    r7562 r7566  
    33 *  @author Eugene Magnier, IfA
    44 *
    5  *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
    6  *  @date $Date: 2006-06-14 22:04:40 $
     5 *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
     6 *  @date $Date: 2006-06-14 22:23:18 $
    77 *
    88 *  Copyright 2004 IfA
     
    249249
    250250    psArray *table = psArrayAlloc(numRows); // The table
     251    table->n = numRows;
    251252    // Translate the vectors into the required format for psFitsWriteTable()
    252253    for (long i = 0; i < numRows; i++) {
     
    290291
    291292    psMetadata *header = psFitsReadHeader(NULL, fits); // The FITS header
     293    if (!header) {
     294        psError(PS_ERR_IO, false, "Unable to read header for extension %s\n", extname);
     295        return NULL;
     296    }
     297
     298    // Read the scalars from the header
     299    #define READ_SCALAR(SCALAR, NAME) \
     300    int SCALAR = psMetadataLookupS32(&mdok, header, NAME); \
     301    if (!mdok || SCALAR <= 0) { \
     302        psError(PS_ERR_IO, true, "Unable to find " NAME " in header of extension %s.\n", extname); \
     303        return NULL; \
     304    }
    292305
    293306    // Need to retrieve the scalars: dX, dY, nX, nY
    294307    bool mdok = true;                   // Status of MD lookup
    295     int dX = psMetadataLookupS32(&mdok, header, "PSFRNGDX");
    296     if (!mdok || dX <= 0) {
    297         psError(PS_ERR_IO, true, "Unable to find PSFRNGDX in header of extension %s.\n");
    298         return NULL;
    299     }
    300     int dY = psMetadataLookupS32(&mdok, header, "PSFRNGDY");
    301     if (!mdok || dY <= 0) {
    302         psError(PS_ERR_IO, true, "Unable to find PSFRNGDY in header of extension %s.\n");
    303         return NULL;
    304     }
    305     int nX = psMetadataLookupS32(&mdok, header, "PSFRNGNX");
    306     if (!mdok || nX <= 0) {
    307         psError(PS_ERR_IO, true, "Unable to find PSFRNGNX in header of extension %s.\n");
    308         return NULL;
    309     }
    310     int nY = psMetadataLookupS32(&mdok, header, "PSFRNGNY");
    311     if (!mdok || nY <= 0) {
    312         psError(PS_ERR_IO, true, "Unable to find PSFRNGNY in header of extension %s.\n");
    313         return NULL;
    314     }
     308    READ_SCALAR(dX, "PSFRNGDX");
     309    READ_SCALAR(dY, "PSFRNGDX");
     310    READ_SCALAR(nX, "PSFRNGDX");
     311    READ_SCALAR(nY, "PSFRNGDX");
     312    psFree(header);
    315313
    316314    // Now the vectors: x, y, mask, f, df
     
    319317
    320318    pmFringeRegions *regions = pmFringeRegionsAlloc(numRows, dX, dY, nX, nY); // The fringe regions
    321     if (regions->x) {
    322         psFree(regions->x);
    323     }
    324     if (regions->y) {
    325         psFree(regions->y);
    326     }
    327     if (regions->mask) {
    328         psFree(regions->mask);
    329     }
    330 
    331319    psVector *x = psVectorAlloc(numRows, PS_TYPE_F32); // x position
    332320    psVector *y = psVectorAlloc(numRows, PS_TYPE_F32); // y position
     
    341329    psVector *df = fringes->df;     // fringe stdev
    342330
     331    #define READ_ROW(VECTOR, TYPE, NAME, DESCRIPTION) \
     332    VECTOR->data.TYPE[i] = psMetadataLookup##TYPE(&mdok, row, NAME); \
     333    if (!mdok) { \
     334        psError(PS_ERR_IO, true, "Unable to find " #DESCRIPTION " .\n"); \
     335        psFree(table); \
     336        psFree(fringes); \
     337        return NULL; \
     338    }
     339
    343340    // Translate the table into vectors
    344341    for (long i = 0; i < numRows; i++) {
    345342        psMetadata *row = table->data[i]; // Table row
    346         x->data.F32[i] = psMetadataLookupF32(&mdok, row, "x");
    347         if (!mdok) {
    348             psError(PS_ERR_IO, true, "Unable to find x position.\n");
    349             psFree(fringes);
    350             return NULL;
    351         }
    352         y->data.F32[i] = psMetadataLookupF32(&mdok, row, "y");
    353         if (!mdok) {
    354             psError(PS_ERR_IO, true, "Unable to find y position.\n");
    355             psFree(fringes);
    356             return NULL;
    357         }
    358         mask->data.U8[i] = psMetadataLookupF32(&mdok, row, "mask");
    359         if (!mdok) {
    360             psError(PS_ERR_IO, true, "Unable to find mask value.\n");
    361             psFree(fringes);
    362             return NULL;
    363         }
    364         f->data.F32[i] = psMetadataLookupF32(&mdok, row, "f");
    365         if (!mdok) {
    366             psError(PS_ERR_IO, true, "Unable to find fringe measurement.\n");
    367             psFree(fringes);
    368             return NULL;
    369         }
    370         df->data.F32[i] = psMetadataLookupF32(&mdok, row, "df");
    371         if (!mdok) {
    372             psError(PS_ERR_IO, true, "Unable to find fringe stdev.\n");
    373             psFree(fringes);
    374             return NULL;
    375         }
    376     }
     343        READ_ROW(x, F32, "x", "x position");
     344        READ_ROW(y, F32, "y", "y position");
     345        READ_ROW(mask, U8, "mask", "mask value");
     346        READ_ROW(f, F32, "f", "fringe measurement");
     347        READ_ROW(df, F32, "df", "fringe standard deviation");
     348    }
     349    psFree(table);
    377350
    378351    return fringes;
Note: See TracChangeset for help on using the changeset viewer.