21 #include "pism/util/Diagnostic.hh"
22 #include "pism/util/Time.hh"
23 #include "pism/util/error_handling.hh"
24 #include "pism/util/io/io_helpers.hh"
25 #include "pism/util/Logger.hh"
26 #include "pism/util/pism_utilities.hh"
27 #include "pism/util/Context.hh"
33 m_sys(grid->ctx()->unit_system()),
34 m_config(grid->ctx()->config()),
35 m_fill_value(m_config->get_number(
"output.fill_value")) {
61 out =
m_vars.at(0)[
"output_units"],
62 in =
m_vars.at(0)[
"units"];
71 out =
m_vars.at(0)[
"output_units"],
72 in =
m_vars.at(0)[
"units"];
113 "variable metadata index %d is out of bounds",
126 for (
const auto &v :
m_vars) {
132 std::vector<std::string> names;
133 for (
const auto &v :
m_vars) {
134 names.push_back(v.get_name());
136 std::string all_names =
join(names,
",");
138 m_grid->ctx()->log()->message(3,
"- Computing %s...\n", all_names.c_str());
140 m_grid->ctx()->log()->message(3,
"- Done computing %s.\n", all_names.c_str());
147 m_config(grid->ctx()->config()),
148 m_sys(grid->ctx()->unit_system()),
149 m_time_name(grid->ctx()->config()->get_string(
"time.dimension_name")),
150 m_variable(name, m_sys),
151 m_dimension(m_time_name, m_sys),
152 m_time_bounds(m_time_name +
"_bounds", m_sys) {
159 m_variable[
"ancillary_variables"] = name +
"_aux";
172 const std::string &output_units) {
175 if (not
m_config->get_flag(
"output.use_MKS")) {
186 :
TSDiagnostic(grid, name), m_accumulator(0.0), m_v_previous(0.0), m_v_previous_set(false) {
211 const double t_s = (*m_requested_times)[
k - 1];
212 const double t_e = (*m_requested_times)[
k];
225 static const double epsilon = 1e-4;
248 const double t_s = (*m_requested_times)[
k - 1];
249 const double t_e = (*m_requested_times)[
k];
256 total_change =
m_accumulator + change * (t_e - t0) / (t1 - t0);
257 const double dt = t_e - t_s;
259 rate = total_change / dt;
264 rate = change / (t1 - t0);
297 static const double epsilon = 1e-4;
299 if (fabs(t1 - t0) < epsilon) {
309 const double v = this->
compute();
321 static const double epsilon = 1e-4;
323 if (fabs(t1 - t0) < epsilon) {
348 if (last_time <
m_time.front()) {
353 auto time_name =
m_config->get_string(
"time.dimension_name");
376 std::shared_ptr<std::vector<double>> requested_times) {
void write_state(const File &output) const
virtual void write_state_impl(const File &output) const
virtual void reset_impl()
void define(const File &file, io::Type default_type) const
const units::System::Ptr m_sys
the unit system
double to_internal(double x) const
virtual std::shared_ptr< array::Array > compute_impl() const =0
std::vector< SpatialVariableMetadata > m_vars
metadata corresponding to NetCDF variables
virtual void define_state_impl(const File &output) const
void init(const File &input, unsigned int time)
virtual void update_impl(double dt)
Diagnostic(std::shared_ptr< const Grid > g)
double to_external(double x) const
void define_state(const File &output) const
virtual void define_impl(const File &file, io::Type default_type) const
Define NetCDF variables corresponding to a diagnostic quantity.
unsigned int n_variables() const
Get the number of NetCDF variables corresponding to a diagnostic quantity.
std::shared_ptr< const Grid > m_grid
the grid
virtual void init_impl(const File &input, unsigned int time)
std::shared_ptr< array::Array > compute() const
Compute a diagnostic quantity and return a pointer to a newly-allocated Array.
SpatialVariableMetadata & metadata(unsigned int N=0)
Get a metadata object corresponding to variable number N.
std::string filename() const
unsigned int dimension_length(const std::string &name) const
Get the length of a dimension.
std::vector< double > read_dimension(const std::string &name) const
Get dimension data (a coordinate variable).
High-level PISM I/O class.
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
std::vector< double > m_values
void set_units(const std::string &units, const std::string &output_units)
std::shared_ptr< std::vector< double > > m_requested_times
requested times
VariableMetadata m_dimension
unsigned int m_current_time
index into m_times
unsigned int m_start
starting index used when flushing the buffer
VariableMetadata m_time_bounds
std::vector< double > m_bounds
size_t m_buffer_size
size of the buffer used to store data
VariableMetadata m_variable
TSDiagnostic(std::shared_ptr< const Grid > g, const std::string &name)
std::string m_output_filename
const Config::ConstPtr m_config
Configuration flags and parameters.
std::vector< double > m_time
virtual double compute()=0
void init(const File &output_file, std::shared_ptr< std::vector< double > > requested_times)
const VariableMetadata & metadata() const
virtual void update_impl(double t0, double t1)=0
std::shared_ptr< const Grid > m_grid
the grid
void update(double t0, double t1)
PISM's scalar time-series diagnostics.
void update_impl(double t0, double t1)
TSFluxDiagnostic(std::shared_ptr< const Grid > g, const std::string &name)
double m_accumulator
accumulator of changes (used to compute rates of change)
void update_impl(double t0, double t1)
TSRateDiagnostic(std::shared_ptr< const Grid > g, const std::string &name)
void evaluate(double t0, double t1, double change)
double m_v_previous
last two values, used to compute the change during a time step
Scalar diagnostic reporting the rate of change of a quantity modeled by PISM.
void evaluate(double t0, double t1, double v)
TSSnapshotDiagnostic(std::shared_ptr< const Grid > g, const std::string &name)
void update_impl(double t0, double t1)
#define PISM_ERROR_LOCATION
@ PISM_READWRITE
open an existing file for reading and writing
void define_time_bounds(const VariableMetadata &var, const std::string &dimension_name, const std::string &bounds_name, const File &file, io::Type nctype)
void write_time_bounds(const File &file, const VariableMetadata &metadata, size_t t_start, const std::vector< double > &data)
void define_spatial_variable(const SpatialVariableMetadata &metadata, const Grid &grid, const File &file, io::Type default_type)
Define a NetCDF variable corresponding to a VariableMetadata object.
void write_timeseries(const File &file, const VariableMetadata &metadata, size_t t_start, const std::vector< double > &data)
Write a time-series data to a file.
void define_timeseries(const VariableMetadata &var, const std::string &dimension_name, const File &file, io::Type nctype)
Define a NetCDF variable corresponding to a time-series.
double convert(System::Ptr system, double input, const std::string &spec1, const std::string &spec2)
Convert a quantity from unit1 to unit2.
std::string join(const std::vector< std::string > &strings, const std::string &separator)
Concatenate strings, inserting separator between elements.
double vector_max(const std::vector< double > &input)