int main(int argc, char* argv[])
{
    namespace js = anu_am::json;

    char* scalarPath = argv[1];
    char* fieldPath  = argv[2];

    if (argc < 3)
    {
        std::cerr << "Usage:" << argv[0] << " SCALARS FIELD [OUTPUT]"
                  << std::endl;
        return 1;
    }

    // Read the data for this process.
    NCFileInfo const info = readFileInfo(fieldPath);
    Variable const var = findVolumeVariable(info);

    std::vector<size_t> dims = readDimensions(info);
    CubicalComplex complex(dims.at(0), dims.at(1), dims.at(2));
    Vertices vertices(dims.at(0), dims.at(1), dims.at(2));

    assert(dims == readDimensions(scalarPath));

    Scalars::DataPtr scalarData = readVolumeData<Value>(scalarPath);
    Scalars scalars(complex, scalarData);

    Field::DataPtr fieldData = readVolumeData<FieldItem>(fieldPath);
    Field field = Field(dims.at(0), dims.at(1), dims.at(2), fieldData);

    // Process the data.
    std::map<Cell, Boundary> chains = chainComplex(complex, field);

    std::vector<Cell> const sources =
        criticalCellsSorted(complex, scalars, field);
    size_t const n = sources.size();

    SimpleComplex const simple = simpleChainComplex(
        complex, scalars, chains, sources);

    std::vector<Pairing<Cell> > const pairs = persistencePairing(simple);

    // Generate metadata
    std::string const parentID = guessDatasetID(fieldPath, info.attributes());
    std::string const thisID   = derivedID(parentID, "persistence", "PP");

    std::string const outfile =
        argc > 3 ? argv[3] : (stripTimestamp(thisID) + ".txt");

    js::Array const predecessors = js::Array
        (parentID)
        (guessDatasetID(scalarPath, readFileInfo(scalarPath).attributes()));

    js::Object const description = js::Object
        ("id"          , thisID)
        ("process"     , "Critical Cell Persistence Pairs")
        ("sourcefile"  , __FILE__)
        ("revision"    , js::Object("id", GIT_REVISION)("date", GIT_TIMESTAMP))
        ("parent"      , parentID)
        ("predecessors", predecessors)
        ("parameters"  , js::Object());

    // Write data
    std::stringstream tmp;

    tmp << "# Persistence pairs for " << scalarPath
        << std::endl
        << "#   format: <birth> <death> <dimension> <creator xyz> <destructor xyz>"
        << std::endl;

    for (size_t i = 0; i < pairs.size(); ++i)
    {
        size_t const j = pairs.at(i).partner;

        if (j > i)
        {
            Cell const v = sources.at(i);
            Cell const w = j >= n ? sources.at(i) : sources.at(j);

            tmp << std::fixed << std::setprecision(6);

            tmp << std::setw(12) << cellValue(v, scalars, vertices) << " "
                << std::setw(12);

            if (w == v)
                tmp << "inf";
            else
                tmp << cellValue(w, scalars, vertices);

            tmp << "    "
                << complex.cellDimension(v) << "    "
                << complex.cellPosition(v) << "    ";

            if (w == v)
                tmp << "   -      -      -  ";
            else
                tmp << complex.cellPosition(w);

            tmp << std::endl;
        }
    }

    std::ofstream ofs(outfile.c_str());
    ofs << tmp.str();

    // Write metadata
    ofs << "#" << std::endl
        << "# Metadata:" << std::endl;

    Attributes const attr = inheritableAttributes(info.attributes());
    for (size_t i = 0; i < attr.size(); ++i)
    {
        std::string const key = attr.keyAt(i);
        ofs << "#" << std::endl
            << "#+ " << key << std::endl;
        if (key == "dataset_id")
            ofs << "#= " << thisID << std::endl;
        else if (key == "zdim_total")
            ofs << "#= " << dims.at(2) << std::endl;
        else if (key == "number_of_files")
            ofs << "#= 1" << std::endl;
        else if (key == "zdim_range")
            ofs << "#= 0, " << dims.at(2)-1 << std::endl;
        else
            printWithPrefix(ofs, attr(key).valuesAsString(), "#= ");
    }

    ofs << "#" << std::endl
        << "#+ " << "history_"+thisID << std::endl;
    ofs << js::toString(description, 2, "#= ") << std::endl;
}
Ejemplo n.º 2
0
int main(int argc, char* argv[])
{
    char* infile = argv[1];

    if (argc < 2)
    {
        std::cerr << "Usage:" << argv[0] << " INPUT" << std::endl;
        return 1;
    }

    FileBuffer data(infile);
    NCFile<FileBuffer> file(data);

    std::vector<Dimension> const dims  = file.dimensions();
    std::vector<Variable>  const vars  = file.variables();
    Attributes const attrs = file.attributes();
    size_t i;

    std::cout << "netcdf " << stripname(infile) << " {" << std::endl;

    std::cout << "dimensions:" << std::endl;
    for (i = 0; i < dims.size(); ++i)
    {
        Dimension d = dims.at(i);
        std::cout << "\t" << d.name << " = " << d.size
                  << " ;" << std::endl;
    }
    std::cout << std::endl;

    std::cout << "variables:" << std::endl;
    for (i = 0; i < vars.size(); ++i)
    {
        Variable v = vars.at(i);
        std::cout << "\t" << tname(v.type()) << " " << v.name()
                  << "(" << toString(v.dimensionNames())
                  << ") ;" << std::endl;

        Attributes const attrs = v.attributes();
        for (size_t j = 0; j < attrs.size(); ++j)
        {
            Attribute a = attrs.at(j);
            std::cout << "\t\t" << v.name() << ":" << attrs.keyAt(j) << " = "
                      << formatString(a.valuesAsString())
                      << " ;" << std::endl;
        }
    }
    std::cout << std::endl;

    std::cout << "// global attributes:" << std::endl;
    for (i = 0; i < attrs.size(); ++i)
    {
        Attribute a = attrs.at(i);
        std::cout << "\t\t:" << attrs.keyAt(i) << " = "
                  << formatString(a.valuesAsString())
                  << " ;" << std::endl;
    }
    std::cout << std::endl;

    std::cout << "data:" << std::endl;
    for (i = 0; i < vars.size(); ++i)
    {
        Variable v = vars.at(i);
        std::cout << std::endl << " " << v.name() << " =" << std::endl;
        std::cout << "  " << file.valueAsString(v, 0, 0, 0)
                  << ", " << file.valueAsString(v, 1, 0, 0)
                  << ", " << file.valueAsString(v, 2, 0, 0)
                  << ", " << file.valueAsString(v, 3, 0, 0)
                  << ", " << file.valueAsString(v, 4, 0, 0)
                  << ", ... ;"
                  << std::endl;
    }

    std::cout << "}" << std::endl;
}