REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_NCFile.cpp
Go to the documentation of this file.
1#include <string>
2
3#include "REMORA_NCFile.H"
5
6
7/**
8 * @param fname Name of NetCDF file
9 * @param var_name Name of variable
10 * @param attr_name Name of attribute to read
11 * @returns attribute value
12 */
13std::string ReadNetCDFVarAttrStr (const std::string& fname,
14 const std::string& var_name,
15 const std::string& attr_name)
16{
17 std::string attr_val;
20 if (amrex::ParallelDescriptor::IOProcessor())
21 {
22 if (!ncf.has_var(var_name)) {
23 amrex::Print() << "Trying to read attribute " << attr_name << " from variable " << var_name << " that does not exist!" << std::endl;
24 }
25 if (!ncf.var(var_name).has_attr(attr_name)) {
26 amrex::Print() << "Trying to read attribute " << attr_name << " that does not exist from variable " << var_name << "!" << std::endl;
27 }
28 attr_val = ncf.var(var_name).get_attr(attr_name);
29 }
30 ncf.close();
31 return attr_val;
32}
33
34/**
35 * @param fname Name of NetCDF file
36 * @param var_name Name of variable
37 * @param attr_name Name of attribute to read
38 * @returns whether attribute was found
39 */
40bool QueryNetCDFVarAttrStr (const std::string& fname,
41 const std::string& var_name,
42 const std::string& attr_name)
43{
44 int has_var = 0;
47 if (amrex::ParallelDescriptor::IOProcessor())
48 {
49 if (!ncf.has_var(var_name)) {
50 amrex::Print() << "Trying to check for attribute " << attr_name << " from variable " << var_name << " that does not exist!" << std::endl;
51 }
52 has_var = ncf.var(var_name).has_attr(attr_name) ? 1 : 0;
53 }
54 ncf.close();
55 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
56 amrex::ParallelDescriptor::Bcast(&has_var, 1, ioproc);
57 return (has_var != 0);
58}
59
60/**
61 * @param fname Name of NetCDF file
62 * @param var_names Names of the variables to test for
63 * @returns whether every named variable is present in the file
64 *
65 * Use this before calling BuildFABsFromNetCDFFile() on optional variables:
66 * ReadNetCDFFile() calls ncf.var(name) unconditionally, so a missing variable
67 * is a hard failure rather than a recoverable one. The answer is broadcast so
68 * every rank agrees before any collective read is attempted.
69 */
70bool QueryNetCDFHasVars (const std::string& fname,
71 const amrex::Vector<std::string>& var_names)
72{
73 int has_all = 1;
76 if (amrex::ParallelDescriptor::IOProcessor())
77 {
78 for (const auto& var_name : var_names) {
79 if (!ncf.has_var(var_name)) { has_all = 0; break; }
80 }
81 }
82 ncf.close();
83 amrex::ParallelDescriptor::Bcast(&has_all, 1, amrex::ParallelDescriptor::IOProcessorNumber());
84 return (has_all != 0);
85}
mf_h setVal(geomdata.ProbHi(2))
bool QueryNetCDFHasVars(const std::string &fname, const amrex::Vector< std::string > &var_names)
Helper function for testing whether a file carries every named variable.
bool QueryNetCDFVarAttrStr(const std::string &fname, const std::string &var_name, const std::string &attr_name)
Helper function for testing for the presence of a single variable attribute.
std::string ReadNetCDFVarAttrStr(const std::string &fname, const std::string &var_name, const std::string &attr_name)
Helper function for reading a single variable attribute.
static NCFile open(const std::string &name, const int cmode=NC_NOWRITE, MPI_Comm comm=MPI_COMM_WORLD, MPI_Info info=MPI_INFO_NULL)
Open an existing file.