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;
18 auto ncf = ncutils::NCFile::open(fname, NC_NOCLOBBER);
19 ncmpi_begin_indep_data(ncf.ncid);
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 bool has_var;
45 auto ncf = ncutils::NCFile::open(fname, NC_NOCLOBBER);
46 ncmpi_begin_indep_data(ncf.ncid);
47 if (amrex::ParallelDescriptor::IOProcessor())
48 {
49 has_var = ncf.var(var_name).has_attr(attr_name);
50 }
51 ncf.close();
52 return has_var;
53}
54
55/**
56 * @param fname Name of NetCDF file
57 * @param var_names Names of the variables to test for
58 * @returns whether every named variable is present in the file
59 *
60 * Use this before calling BuildFABsFromNetCDFFile() on optional variables:
61 * ReadNetCDFFile() calls ncf.var(name) unconditionally, so a missing variable
62 * is a hard failure rather than a recoverable one. The answer is broadcast so
63 * every rank agrees before any collective read is attempted.
64 */
65bool QueryNetCDFHasVars (const std::string& fname,
66 const amrex::Vector<std::string>& var_names)
67{
68 int has_all = 1;
69 auto ncf = ncutils::NCFile::open(fname, NC_NOCLOBBER);
70 ncmpi_begin_indep_data(ncf.ncid);
71 if (amrex::ParallelDescriptor::IOProcessor())
72 {
73 for (const auto& var_name : var_names) {
74 if (!ncf.has_var(var_name)) { has_all = 0; break; }
75 }
76 }
77 ncf.close();
78 amrex::ParallelDescriptor::Bcast(&has_all, 1, amrex::ParallelDescriptor::IOProcessorNumber());
79 return (has_all != 0);
80}
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.