Skip to content

Commit

Permalink
Basic unit awareness for netcdf read. Allow check pointing at arbitra…
Browse files Browse the repository at this point in the history
…ry times
  • Loading branch information
Chrismarsh committed Jan 26, 2025
1 parent a557241 commit 7325e34
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,27 @@ void core::config_checkpoint( pt::ptree& value)
}
}

if(!_checkpoint_opts.on_last &&
auto specific_datetime = value.get_optional<std::string>("specific_datetime");
if(specific_datetime)
{
_checkpoint_opts.specific_datetime = boost::posix_time::ptime(boost::posix_time::from_iso_string(*specific_datetime));
SPDLOG_DEBUG("Checkpointing every {} timesteps" , *(_checkpoint_opts.specific_datetime));
}

auto specific_time = value.get_optional<std::string>("specific_time");
if(specific_time)
{
// ptime needs a real date, but we will only ever be checking the minute and hour
auto time = "3000-01-01 " + *specific_time + ":00";
_checkpoint_opts.specific_time = boost::posix_time::ptime(boost::posix_time::time_from_string(time));
SPDLOG_DEBUG("Checkpointing every {} timesteps" , *(_checkpoint_opts.specific_time));
}

if( !_checkpoint_opts.on_last &&
!_checkpoint_opts.frequency &&
!_checkpoint_opts.on_outta_time)
!_checkpoint_opts.on_outta_time &&
!_checkpoint_opts.specific_datetime &&
!_checkpoint_opts.specific_time)
{
SPDLOG_ERROR("Checkpointing is enabled but no `on_*` selection has been specified.\n"
"Please see https://chm.readthedocs.io/en/develop/configuration.html#checkpoint\n");
Expand Down Expand Up @@ -2270,7 +2288,8 @@ void core::run()
if(_checkpoint_opts.should_checkpoint(current_ts,
(max_ts-1) == current_ts,
_hpc_scheduler_info,
_comm_world
_comm_world,
_global->_current_date
)) // -1 because current_ts is 0 indexed
{
SPDLOG_DEBUG("Checkpointing...");
Expand Down
22 changes: 21 additions & 1 deletion src/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,9 @@ class core
boost::optional<size_t> frequency; // frequency of checkpoints


boost::optional<boost::posix_time::ptime> specific_datetime; // at a specific date-time
boost::optional<boost::posix_time::ptime> specific_time; // at a specific time

// used to stop the simulation when we checkpoint when we are outta time
bool checkpoint_request_terminate;

Expand All @@ -542,7 +545,11 @@ class core
* @param is_last_ts
* @return
*/
bool should_checkpoint(size_t current_ts, bool is_last_ts, hpc_scheduler_info& scheduler_info, boost::mpi::communicator& comm_world)
bool should_checkpoint(size_t current_ts,
bool is_last_ts,
hpc_scheduler_info& scheduler_info,
boost::mpi::communicator& comm_world,
const boost::posix_time::ptime& _current_date)
{
if(!do_checkpoint)
return false;
Expand Down Expand Up @@ -576,6 +583,19 @@ class core

}

if(specific_time)
{
if( (_current_date.time_of_day().hours() == specific_time->time_of_day().hours()) &&
(_current_date.time_of_day().minutes() == specific_time->time_of_day().minutes()) )
return true;
}

if(specific_datetime)
{
if(_current_date == *specific_datetime)
return true;
}

return false;
}

Expand Down
22 changes: 22 additions & 0 deletions src/metdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,28 @@ bool metdata::next_nc()

auto stdname = _nc->get_var_standard_name(v);
auto chm_var = CF_name_mapping::standard_names.right.find(stdname)->second;

auto unit = _nc->get_unit(v);

if(stdname == "precipitation_amount" && unit == "m")
{
// auto ud = d * si::metre;
// d = ud.numerical_value_in(si::unit_symbols::mm);
d = d / 1000.0;
}

if(stdname == "air_temperature" && unit == "K")
{
//auto ud = d * si::kelvin;
//d = ud.numerical_value_in(si::degree_Celsius);
d = d + 273.15;
}

if(stdname == "relative_humidity" && unit == "1")
{
d = d * 100.;
}

// SPDLOG_DEBUG("nc var:{} chm_var: {}", v, chm_var);
(*s)[chm_var] = d;

Expand Down
11 changes: 11 additions & 0 deletions src/timeseries/netcdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ bool netcdf::missing_z()
return _missing_z;
}

std::string netcdf::get_unit(const std::string& var)
{
auto v = _data.getVar(var);
auto unitAtt = v.getAtt("unit");

std::string unit;
unitAtt.getValues(unit);

return unit;
}

size_t netcdf::get_ntimesteps()
{
return _datetime_length;
Expand Down
1 change: 1 addition & 0 deletions src/timeseries/netcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ boost::posix_time::time_duration get_dt();
void open_GEM(const std::string &file);
void open(const std::string &file);

std::string get_unit(const std::string& var);
void create(const std::string& file);
size_t get_xsize() const;
size_t get_ysize() const;
Expand Down

0 comments on commit 7325e34

Please sign in to comment.