IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 9534


Ignore:
Timestamp:
Oct 13, 2006, 9:06:00 AM (20 years ago)
Author:
magnier
Message:

fixed polytomial to/from MD to treat missing components as masked components
added NELEMENTS to output MD to validate the polynomial metadata
added 1D to/from MD functions

Location:
trunk/psLib/src/types
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/types/psMetadata.c

    r9523 r9534  
    1212 *  @author Ross Harman, MHPCC
    1313 *
    14  *  @version $Revision: 1.137 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-10-12 23:43:58 $
     14 *  @version $Revision: 1.138 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2006-10-13 19:06:00 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    14131413}
    14141414
    1415 psPolynomial2D *psPolynomial2DfromMD (psMetadata *folder)
     1415psPolynomial1D *psPolynomial1DfromMD (psMetadata *folder)
    14161416{
    14171417    PS_ASSERT_PTR_NON_NULL(folder, NULL);
     
    14201420
    14211421    // get polynomial orders
    1422     // XXX add status failures tests
    14231422    int nXorder = psMetadataLookupS32 (&status, folder, "NORDER_X");
    1424     if (!status)
    1425         return NULL;
     1423    if (!status) {
     1424        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial1D in metadata is missing NORDER_X");
     1425        return NULL;
     1426    }
     1427    // how many polynomial coeffs are expected?
     1428    int nElementsExpected = psMetadataLookupS32 (&status, folder, "NELEMENTS");
     1429    if (!status) {
     1430        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial1D in metadata is missing NELEMENTS");
     1431        return NULL;
     1432    }
     1433
     1434    psPolynomial1D *poly = psPolynomial1DAlloc (PS_POLYNOMIAL_ORD, nXorder);
     1435
     1436    int nElements = 0;
     1437    for (int nx = 0; nx < poly->nX + 1; nx++) {
     1438        sprintf (keyword, "VAL_X%02d", nx);
     1439        poly->coeff[nx] = psMetadataLookupF64 (&status, folder, keyword);
     1440        if (!status) {
     1441            // an undefined component implies the component was masked
     1442            // this is symmetrical with the 1DtoMD function
     1443            poly->mask[nx] = 1;
     1444            poly->coeff[nx] = 0;
     1445            poly->coeffErr[nx] = 0;
     1446        } else {
     1447            poly->mask[nx] = 0;
     1448            nElements ++;
     1449        }
     1450        sprintf (keyword, "ERR_X%02d", nx);
     1451        poly->coeffErr[nx] = psMetadataLookupF64 (&status, folder, keyword);
     1452    }
     1453    if (nElements != nElementsExpected) {
     1454        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1455                "psPolynomial1D in metadata does not have the correct number of coefficients: "
     1456                "%d found vs %d expected", nElements, nElementsExpected);
     1457        psFree(poly);
     1458        return NULL;
     1459    }
     1460    return (poly);
     1461}
     1462
     1463// XXX : these may need F64, or %g format for output
     1464bool psPolynomial1DtoMD (psMetadata *md,
     1465                         psPolynomial1D *poly,
     1466                         char *format,
     1467                         ...)
     1468{
     1469    PS_ASSERT_PTR_NON_NULL(md, false);
     1470    PS_ASSERT_PTR_NON_NULL(poly, false);
     1471    //XXX:  Current implementation only supports ordinary polynomials.
     1472    if (poly->type != PS_POLYNOMIAL_ORD)
     1473        return false;
     1474
     1475    int Nbyte;
     1476    char tmp;
     1477    char *root;
     1478    va_list argp;
     1479
     1480    va_start (argp, format);
     1481    Nbyte = vsnprintf (&tmp, 0, format, argp);
     1482    va_end (argp);
     1483
     1484    if (Nbyte <= 0)
     1485        return false;
     1486
     1487    va_start (argp, format);
     1488    root = (char *) psAlloc (Nbyte + 1);
     1489    memset (root, 0, Nbyte + 1);
     1490    vsnprintf (root, Nbyte + 1, format, argp);
     1491    va_end (argp);
     1492
     1493    psMetadata *folder = psMetadataAlloc ();
     1494
     1495    // specify the polynomial orders
     1496    psMetadataAdd (folder, PS_LIST_TAIL, "NORDER_X", PS_DATA_S32, "number of x orders", poly->nX);
     1497
     1498    char namespace[80];
     1499    char namespace_err[80];
     1500    int nElements = 0;   // count the number of unmasked elements
     1501
     1502    // place polynomial entries on folder
     1503    for (int nx = 0; nx < poly->nX + 1; nx++) {
     1504        if (poly->mask[nx] == 0) {
     1505            sprintf(namespace, "VAL_X%02d", nx);
     1506            sprintf(namespace_err, "ERR_X%02d", nx);
     1507            psMetadataAdd (folder, PS_LIST_TAIL, namespace, PS_DATA_F64,
     1508                           "polynomial coefficient", poly->coeff[nx]);
     1509            psMetadataAdd (folder, PS_LIST_TAIL, namespace_err, PS_DATA_F64,
     1510                           "polynomial coefficient error", poly->coeffErr[nx]);
     1511            nElements ++;
     1512        }
     1513    }
     1514    psMetadataAdd (folder, PS_LIST_TAIL, "NELEMENTS", PS_DATA_S32, "number of unmasked coeffs", nElements);
     1515    psMetadataAdd (md, PS_LIST_TAIL, root, PS_DATA_METADATA, "folder for 1D polynomial", folder);
     1516    psFree (root);
     1517    psFree(folder);
     1518    return true;
     1519}
     1520
     1521psPolynomial2D *psPolynomial2DfromMD (psMetadata *folder)
     1522{
     1523    PS_ASSERT_PTR_NON_NULL(folder, NULL);
     1524    bool status;
     1525    char keyword[80];
     1526
     1527    // get polynomial orders
     1528    int nXorder = psMetadataLookupS32 (&status, folder, "NORDER_X");
     1529    if (!status) {
     1530        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial2D in metadata is missing NORDER_X");
     1531        return NULL;
     1532    }
    14261533    int nYorder = psMetadataLookupS32 (&status, folder, "NORDER_Y");
    1427     if (!status)
    1428         return NULL;
     1534    if (!status) {
     1535        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial2D in metadata is missing NORDER_Y");
     1536        return NULL;
     1537    }
     1538    // how many polynomial coeffs are expected?
     1539    int nElementsExpected = psMetadataLookupS32 (&status, folder, "NELEMENTS");
     1540    if (!status) {
     1541        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial2D in metadata is missing NELEMENTS");
     1542        return NULL;
     1543    }
    14291544
    14301545    psPolynomial2D *poly = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXorder, nYorder);
    14311546
     1547    int nElements = 0;
    14321548    for (int nx = 0; nx < poly->nX + 1; nx++) {
    14331549        for (int ny = 0; ny < poly->nY + 1; ny++) {
     
    14351551            poly->coeff[nx][ny] = psMetadataLookupF64 (&status, folder, keyword);
    14361552            if (!status) {
    1437                 //XXX: Setting the mask of a corrupted polynomial doesn't make sense.
    1438                 //The polynomial is garbage if ANY coeff-element is lost!!!
    1439                 //                    poly->mask[nx][ny] = 1;
    1440                 psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    1441                         "psPolynomial2D in metadata has lost a coefficient somewhere!"
    1442                         " >>position= [x,y] = %02d, %02d", nx, ny);
    1443                 psFree(poly);
    1444                 return NULL;
     1553                // an undefined component implies the component was masked
     1554                // this is symmetrical with the 2DtoMD function
     1555                poly->mask[nx][ny] = 1;
     1556                poly->coeff[nx][ny] = 0;
     1557                poly->coeffErr[nx][ny] = 0;
     1558            } else {
     1559                poly->mask[nx][ny] = 0;
     1560                nElements ++;
    14451561            }
    14461562            sprintf (keyword, "ERR_X%02d_Y%02d", nx, ny);
    14471563            poly->coeffErr[nx][ny] = psMetadataLookupF64 (&status, folder, keyword);
    14481564        }
     1565    }
     1566    if (nElements != nElementsExpected) {
     1567        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1568                "psPolynomial2D in metadata does not have the correct number of coefficients: "
     1569                "%d found vs %d expected", nElements, nElementsExpected);
     1570        psFree(poly);
     1571        return NULL;
    14491572    }
    14501573    return (poly);
     
    14621585    if (poly->type != PS_POLYNOMIAL_ORD)
    14631586        return false;
     1587
     1588    // XXX I'm puzzled by this test.  a polynomial of 0 order with a value of 0 is a
     1589    // perfectly valid polynomial and can be written out.  a polynomial with all elements
     1590    // masked still carries information.
    14641591    //Make sure polynomial isn't 0, completely empty
    1465     if (poly->nX == 0 && poly->nY == 0 && poly->coeff[0][0] == 0)
    1466         return false;
     1592    //if (poly->nX == 0 && poly->nY == 0 && poly->coeff[0][0] == 0)
     1593    //return false;
    14671594
    14681595    int Nbyte;
     
    14921619    char namespace[80];
    14931620    char namespace_err[80];
     1621    int nElements = 0;   // count the number of unmasked elements
     1622
    14941623    // place polynomial entries on folder
    14951624    for (int nx = 0; nx < poly->nX + 1; nx++) {
     
    15021631                psMetadataAdd (folder, PS_LIST_TAIL, namespace_err, PS_DATA_F64,
    15031632                               "polynomial coefficient error", poly->coeffErr[nx][ny]);
     1633                nElements ++;
    15041634            }
    15051635        }
    15061636    }
     1637    psMetadataAdd (folder, PS_LIST_TAIL, "NELEMENTS", PS_DATA_S32, "number of unmasked coeffs", nElements);
    15071638    psMetadataAdd (md, PS_LIST_TAIL, root, PS_DATA_METADATA, "folder for 2D polynomial", folder);
    15081639    psFree (root);
     
    15191650
    15201651    // get polynomial orders
    1521     // XXX add status failures tests
    15221652    int nXorder = psMetadataLookupS32 (&status, folder, "NORDER_X");
    1523     if (!status)
    1524         return NULL;
     1653    if (!status) {
     1654        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial3D in metadata is missing NORDER_X");
     1655        return NULL;
     1656    }
    15251657    int nYorder = psMetadataLookupS32 (&status, folder, "NORDER_Y");
    1526     if (!status)
    1527         return NULL;
     1658    if (!status) {
     1659        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial3D in metadata is missing NORDER_Y");
     1660        return NULL;
     1661    }
    15281662    int nZorder = psMetadataLookupS32 (&status, folder, "NORDER_Z");
    1529     if (!status)
    1530         return NULL;
     1663    if (!status) {
     1664        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial3D in metadata is missing NORDER_Z");
     1665        return NULL;
     1666    }
     1667    // how many polynomial coeffs are expected?
     1668    int nElementsExpected = psMetadataLookupS32 (&status, folder, "NELEMENTS");
     1669    if (!status) {
     1670        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial3D in metadata is missing NELEMENTS");
     1671        return NULL;
     1672    }
    15311673
    15321674    psPolynomial3D *poly = psPolynomial3DAlloc (PS_POLYNOMIAL_ORD, nXorder, nYorder, nZorder);
    15331675
     1676    int nElements = 0;
    15341677    for (int nx = 0; nx < poly->nX + 1; nx++) {
    15351678        for (int ny = 0; ny < poly->nY + 1; ny++) {
     
    15381681                poly->coeff[nx][ny][nz] = psMetadataLookupF64 (&status, folder, keyword);
    15391682                if (!status) {
    1540                     //XXX: Setting the mask of a corrupted polynomial doesn't make sense.
    1541                     //The polynomial is garbage if ANY coeff-element is lost!!!
    1542                     //                    poly->mask[nx][ny][nz] = 1;
    1543                     psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    1544                             "psPolynomial3D in metadata has lost a coefficient somewhere!"
    1545                             " >>position= [x,y,z] = %02d, %02d, %02d", nx, ny, nz);
    1546                     psFree(poly);
    1547                     return NULL;
     1683                    // an undefined component implies the component was masked
     1684                    // this is symmetrical with the 3DtoMD function
     1685                    poly->mask[nx][ny][nz] = 1;
     1686                    poly->coeff[nx][ny][nz] = 0;
     1687                    poly->coeffErr[nx][ny][nz] = 0;
     1688                } else {
     1689                    poly->mask[nx][ny][nz] = 0;
     1690                    nElements ++;
    15481691                }
    15491692                sprintf (keyword, "ERR_X%02d_Y%02d_Z%02d", nx, ny, nz);
     
    15511694            }
    15521695        }
     1696    }
     1697    if (nElements != nElementsExpected) {
     1698        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1699                "psPolynomial3D in metadata does not have the correct number of coefficients: "
     1700                "%d found vs %d expected", nElements, nElementsExpected);
     1701        psFree(poly);
     1702        return NULL;
    15531703    }
    15541704    return (poly);
     
    15651715    if (poly->type != PS_POLYNOMIAL_ORD)
    15661716        return false;
    1567     //Make sure polynomial isn't 0, completely empty
    1568     if (poly->nX == 0 && poly->nY == 0 && poly->nZ == 0 && poly->coeff[0][0][0] == 0)
    1569         return false;
    15701717
    15711718    int Nbyte;
     
    15961743    char namespace[80];
    15971744    char namespace_err[80];
     1745    int nElements = 0;   // count the number of unmasked elements
     1746
    15981747    // place polynomial entries on folder
    15991748    for (int nx = 0; nx < poly->nX + 1; nx++) {
     
    16091758                                   PS_DATA_F64, "polynomial coeffficient error",
    16101759                                   poly->coeffErr[nx][ny][nz], nx, ny, nz);
     1760                    nElements ++;
    16111761                }
    16121762            }
    16131763        }
    16141764    }
    1615 
     1765    psMetadataAdd (folder, PS_LIST_TAIL, "NELEMENTS", PS_DATA_S32, "number of unmasked coeffs", nElements);
    16161766    psMetadataAdd (md, PS_LIST_TAIL, root, PS_DATA_METADATA, "folder for 3D polynomial", folder);
    16171767    psFree(root);
     
    16201770}
    16211771
    1622 
    16231772psPolynomial4D *psPolynomial4DfromMD (psMetadata *folder)
    16241773{
     
    16281777    char keyword[80];
    16291778
    1630     // get polynomial orders
    1631     // XXX add status failures tests
    16321779    int nXorder = psMetadataLookupS32 (&status, folder, "NORDER_X");
    1633     if (!status)
    1634         return NULL;
     1780    if (!status) {
     1781        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial4D in metadata is missing NORDER_X");
     1782        return NULL;
     1783    }
    16351784    int nYorder = psMetadataLookupS32 (&status, folder, "NORDER_Y");
    1636     if (!status)
    1637         return NULL;
     1785    if (!status) {
     1786        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial4D in metadata is missing NORDER_Y");
     1787        return NULL;
     1788    }
    16381789    int nZorder = psMetadataLookupS32 (&status, folder, "NORDER_Z");
    1639     if (!status)
    1640         return NULL;
     1790    if (!status) {
     1791        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial4D in metadata is missing NORDER_Z");
     1792        return NULL;
     1793    }
    16411794    int nTorder = psMetadataLookupS32 (&status, folder, "NORDER_T");
    1642     if (!status)
    1643         return NULL;
    1644 
    1645     psPolynomial4D *poly = psPolynomial4DAlloc(PS_POLYNOMIAL_ORD,
    1646                            nXorder, nYorder, nZorder, nTorder);
    1647 
     1795    if (!status) {
     1796        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial4D in metadata is missing NORDER_T");
     1797        return NULL;
     1798    }
     1799    // how many polynomial coeffs are expected?
     1800    int nElementsExpected = psMetadataLookupS32 (&status, folder, "NELEMENTS");
     1801    if (!status) {
     1802        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "psPolynomial4D in metadata is missing NELEMENTS");
     1803        return NULL;
     1804    }
     1805
     1806    psPolynomial4D *poly = psPolynomial4DAlloc(PS_POLYNOMIAL_ORD, nXorder, nYorder, nZorder, nTorder);
     1807
     1808    int nElements = 0;
    16481809    for (int nx = 0; nx < poly->nX + 1; nx++) {
    16491810        for (int ny = 0; ny < poly->nY + 1; ny++) {
     
    16531814                    poly->coeff[nx][ny][nz][nt] = psMetadataLookupF64 (&status, folder, keyword);
    16541815                    if (!status) {
    1655                         //XXX: Setting the mask of a corrupted polynomial doesn't make sense.
    1656                         //The polynomial is garbage if ANY coeff-element is lost!!!
    1657                         //                    poly->mask[nx][ny][nz][nt] = 1;
    1658                         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    1659                                 "psPolynomial4D in metadata has lost a coefficient somewhere!"
    1660                                 " >>position= [x,y,z,t] = %02d, %02d, %02d, %02d", nx,ny,nz,nt);
    1661                         psFree(poly);
    1662                         return NULL;
     1816                        // an undefined component implies the component was masked
     1817                        // this is symmetrical with the 4DtoMD function
     1818                        poly->mask[nx][ny][nz][nt] = 1;
     1819                        poly->coeff[nx][ny][nz][nt] = 0;
     1820                        poly->coeffErr[nx][ny][nz][nt] = 0;
     1821                    } else {
     1822                        poly->mask[nx][ny][nz][nt] = 0;
     1823                        nElements ++;
    16631824                    }
    1664 
    16651825                    sprintf (keyword, "ERR_X%02d_Y%02d_Z%02d_T%02d", nx, ny, nz, nt);
    16661826                    poly->coeffErr[nx][ny][nz][nt] = psMetadataLookupF64 (&status, folder, keyword);
     
    16681828            }
    16691829        }
     1830    }
     1831    if (nElements != nElementsExpected) {
     1832        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1833                "psPolynomial4D in metadata does not have the correct number of coefficients: "
     1834                "%d found vs %d expected", nElements, nElementsExpected);
     1835        psFree(poly);
     1836        return NULL;
    16701837    }
    16711838    return (poly);
     
    16821849    if (poly->type != PS_POLYNOMIAL_ORD)
    16831850        return false;
    1684     //Make sure polynomial isn't 0, completely empty
    1685     if (poly->nX == 0 && poly->nY == 0 && poly->nZ == 0 && poly->nT == 0
    1686             && poly->coeff[0][0][0][0] == 0)
    1687         return false;
    1688 
    16891851
    16901852    int Nbyte;
     
    17161878    char namespace[80];
    17171879    char namespace_err[80];
     1880    int nElements = 0;   // count the number of unmasked elements
     1881
    17181882    // place polynomial entries on folder
    17191883    for (int nx = 0; nx < poly->nX + 1; nx++) {
     
    17301894                                       PS_DATA_F64, "polynomial coeffficient error",
    17311895                                       poly->coeffErr[nx][ny][nz][nt], nx, ny, nz, nt);
     1896                        nElements ++;
    17321897                    }
    17331898                }
     
    17351900        }
    17361901    }
     1902    psMetadataAdd (folder, PS_LIST_TAIL, "NELEMENTS", PS_DATA_S32, "number of unmasked coeffs", nElements);
    17371903    psMetadataAdd (md, PS_LIST_TAIL, root, PS_DATA_METADATA, "folder for 4D polynomial", folder);
    17381904    psFree(root);
  • trunk/psLib/src/types/psMetadata.h

    r9523 r9534  
    1111*  @author Ross Harman, MHPCC
    1212*
    13 *  @version $Revision: 1.86 $ $Name: not supported by cvs2svn $
    14 *  @date $Date: 2006-10-12 23:43:58 $
     13*  @version $Revision: 1.87 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2006-10-13 19:06:00 $
    1515*
    1616*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    10701070);
    10711071
     1072/** Allocates a new psPolynomial1D structure with information from a psMetadata.
     1073 *
     1074 *  Parses a psMetadata container with psPolynomial1D information.  The first two
     1075 *  elements of the metadata folder specify the order of the x & y variables.  (ie,
     1076 *  NORDER_X, NORDER_Y).  The following elements are the values of the coefficients
     1077 *  and the coefficient errors.  (ie, VAL_X00_Y00, ERR_X00_Y00, etc.).  If the orders
     1078 *  or any coefficients are missing or have incorrect syntax, NULL is returned.
     1079 *
     1080 *  @return psPolynomial1D*:        Newly allocated psPolynomial1D from metadata.
     1081 */
     1082psPolynomial1D *psPolynomial1DfromMD(
     1083    psMetadata *folder                 ///< folder containing the polynomial info.
     1084);
     1085
     1086/** Stores the information from a psPolynomial1D structure in a psMetadata container.
     1087 *
     1088 *  Creates a psMetadata folder with psPolynomial1D information.  The first two
     1089 *  elements of the metadata folder specify the order of the x & y variables.  (ie,
     1090 *  NORDER_X, NORDER_Y).  The following elements are the values of the coefficients
     1091 *  and the coefficient errors.  (ie, VAL_X00_Y00, ERR_X00_Y00, etc.).  The input
     1092 *  polynomial must be of ordinary type and have a valid name format.  False is also
     1093 *  returned if any inputs are NULL.  *If a particular mask element is non-zero, that
     1094 *  polynomial coefficient (and error) are skipped.
     1095 *
     1096 *  @return bool:       True if successful, otherwise false.
     1097 */
     1098bool psPolynomial1DtoMD(
     1099    psMetadata *md,                    ///< Metadata container for polynomial storage.
     1100    psPolynomial1D *poly,              ///< Polynomial information to be stored.
     1101    char *format,                      ///< Name of polynomial folder.
     1102    ...                                ///< Arguments for name formatting.
     1103);
     1104
    10721105/** Allocates a new psPolynomial2D structure with information from a psMetadata.
    10731106 *
Note: See TracChangeset for help on using the changeset viewer.