Crystallographic Information Framework

CIFPARSE-OBJ
Usage Example: V7-0-25

----------------CIF File/CIF Parser Usage Examples-------------

1. Reading the content of a CIF file and accessing its data.

2. Updating the content of a CIF file.

3. Creating a CIF file from scratch.


#include "CifFile.h"
#include "CifParserBase.h"


    // The (absolute or relative) name of the CIF file should be
    // stored in cifFileName
    string cifFileName;

    // Parsing diagnostics
    string diags;

    // The name of block
    string blockName;

    // The name of table
    string tableName;

    
    /*
    ** Example 1: How to read the content of a CIF file and
    **   access its data.
    */

    // Create CIF file object.
    // Note that this is only an in-memory object, which will not be
    // serialized to its binary file equivalent.
    CifFile cifFile();

    // Create CIF parser object
    cifParserP = new CifParser(&cifFile, cifFile.GetVerbose());

    // Parse the CIF file
    cifParserP->Parse(cifFileName, diags);

    // Delete CIF parser object, as it is not needed any more.
    delete(cifParserP);

    // Display diagnostics, if any
    if (!diags.empty())
    {
        cout << "Diagnostics: " << endl << diags << endl;
    }

    // Accessing the data in the CIF file object

    // Get access to block
    Block& block = cifFile.GetBlock(blockName);

    // Get access to the table
    ISTable* isTableP = block.GetTablePtr(tableName);

    // Print out the table
    cout << (*isTableP) << endl;
    
    /*
    ** Accessing "entity_poly" category and printing out its items.
    */

    // Get access to the "entity_poly" table
    ISTable* entityPolyTableP = block.GetTablePtr("entity_poly");

    vector<string> entityPolyItems;

    entityPolyTableP->GetColumnNames(entityPolyItems);

    vector<string> itemValues;

    for (unsigned int itemI = 0; itemI < entityPolyItems.size(); ++itemI)
    {
        entityPolyTableP->GetColumn(itemValues, entityPolyItems[itemI]);

        cout << "Values of " << entityPolyItems[itemI] << " item are: " <<

          itemValues << endl;
    }


    /*
    ** Accessing atom_site category and storing x coordinate in a real numbers
    ** array. 
    */

    // Get access to the "atom_site" table
    ISTable* atomSiteTableP = block.GetTablePtr("atom_site");

    // String vector of values for "cartn_x" item
    vector<string> cartnX;

    atomSiteTableP->GetColumn(cartnX, "cartn_x");

    // Real number vector of values for "cartn_x" item
    vector<double> realCartnX;

    for (unsigned int rowI = 0; rowI < cartnX.size(); ++rowI)
    {
        realCartnX.push_back(atof(cartnX[rowI].c_str()));
    }

    cout << "cartn_x values in atom_site table are: " << realCartnX << endl;

    // Note that we can utilize realCartnX in mathematical expressions, etc.

    

    /*
    ** Example 2: How to update the content of a CIF file.
    */

    // Assume here that we modify the above table in some way (add new rows,
    // columns, change data, etc.). See detailed examples in ISTable
    // examples.

    // Write the modified table to the block.
    block.WriteTable(isTableP);

    // Write out a CIF file with changed table.
    cifFile.Write(cifFileName);

    
    /*
    ** Example 3: How to create a CIF file from scratch.
    */

    // The (absolute or relative) name of the CIF file should be
    // stored in cifFileName
    string cifFileName;


    // Assume here that we have prepared the table to be written to the file
    // and that isTableP points to that table.
    // See other examples of how to create the table.
    // NOTE: ISTables must be allocated from the heap, since their memory
    // ownership is passed to the CifFile object.

    // Create CIF file object.
    // Note that this is only an in-memory object, which will not be
    // serialized to its binary file equivalent.
    CifFile cifFile();

    // Add one block to the CIF file.
    cifFile.AddBlock(blockName);

    // Get the created block.
    Block& block = cifFile.GetBlock(blockName);

    // Write some table into this block of the CIF file.
    block.WriteTable(isTableP);

    // Repeat the above block/table creation as needed

    // Write out the created CIF file object into the CIF file.
    cifFile.Write(cifFileName);

    // ISTable objects must not be destructed by the user code, since they
    // will be destructed during cifFile object destruction