16 #include "oops/util/abor1_cpp.h" 17 #include "oops/util/datetime_f.h" 18 #include "oops/util/Duration.h" 19 #include "oops/util/Logger.h" 48 const std::size_t & Nlocs,
const std::size_t & Nobs,
49 const std::size_t & Nrecs,
const std::size_t & Nvars) {
59 oops::Log::trace() << __func__ <<
" fname_: " <<
fname_ <<
" fmode_: " <<
fmode_ << std::endl;
66 retval_ = nc_open(
fname_.c_str(), NC_NOWRITE, &
ncid_);
67 }
else if (
fmode_ ==
"w") {
68 retval_ = nc_create(
fname_.c_str(), NC_NOCLOBBER|NC_NETCDF4, &
ncid_);
69 }
else if (
fmode_ ==
"W") {
70 retval_ = nc_create(
fname_.c_str(), NC_CLOBBER|NC_NETCDF4, &
ncid_);
73 oops::Log::error() << __func__ <<
": Must use one of: 'r', 'w', 'W'" << std::endl;
74 ABORT(
"Unrecognized file mode for NetcdfIO constructor");
78 if (retval_ != NC_NOERR) {
80 <<
"' in mode: " <<
fmode_ << std::endl;
81 ABORT(
"Unable to open file");
150 oops::Log::trace() << __func__ <<
" fname_: " <<
fname_ << std::endl;
170 oops::Log::trace() << __func__ <<
" VarName: " << VarName << std::endl;
172 std::string ErrorMsg;
174 ErrorMsg =
"NetcdfIO::ReadVar: Netcdf dataset not found: " + VarName;
177 ErrorMsg =
"NetcdfIO::ReadVar: Unable to read dataset: " + VarName;
184 oops::Log::trace() << __func__ <<
" VarName: " << VarName << std::endl;
186 std::string ErrorMsg;
188 ErrorMsg =
"NetcdfIO::ReadVar: Netcdf dataset not found: " + VarName;
191 ErrorMsg =
"NetcdfIO::ReadVar: Unable to read dataset: " + VarName;
198 oops::Log::trace() << __func__ <<
" VarName: " << VarName << std::endl;
200 std::string ErrorMsg;
202 ErrorMsg =
"NetcdfIO::ReadVar: Netcdf dataset not found: " + VarName;
205 ErrorMsg =
"NetcdfIO::ReadVar: Unable to read dataset: " + VarName;
224 oops::Log::trace() << __func__ <<
" VarName: " << VarName << std::endl;
226 std::string ErrorMsg;
228 if (nc_inq_varid(
ncid_, VarName.c_str(), &
nc_varid_) != NC_NOERR) {
230 ErrorMsg =
"NetcdfIO::WriteVar: Unable to create variable dataset: " + VarName;
234 ErrorMsg =
"NetcdfIO::WriteVar: Unable to write dataset: " + VarName;
241 oops::Log::trace() << __func__ <<
" VarName: " << VarName << std::endl;
243 std::string ErrorMsg;
245 if (nc_inq_varid(
ncid_, VarName.c_str(), &
nc_varid_) != NC_NOERR) {
247 ErrorMsg =
"NetcdfIO::WriteVar: Unable to create variable dataset: " + VarName;
251 ErrorMsg =
"NetcdfIO::WriteVar: Unable to write dataset: " + VarName;
258 oops::Log::trace() << __func__ <<
" VarName: " << VarName << std::endl;
260 std::string ErrorMsg;
262 if (nc_inq_varid(
ncid_, VarName.c_str(), &
nc_varid_) != NC_NOERR) {
264 ErrorMsg =
"NetcdfIO::WriteVar: Unable to create variable dataset: " + VarName;
268 ErrorMsg =
"NetcdfIO::WriteVar: Unable to write dataset: " + VarName;
304 std::string ErrorMsg;
306 oops::Log::trace() << __func__ << std::endl;
311 ErrorMsg =
"NetcdfIO::ReadDateTime: Unable to read attribute: date_time";
312 CheckNcCall(nc_get_att_int(
ncid_, NC_GLOBAL,
"date_time", &dtvals_), ErrorMsg);
314 util::DateTime refdt_;
315 datetime_setints_f(&refdt_, dtvals_/100, dtvals_%100);
322 ErrorMsg =
"NetcdfIO::ReadDateTime: Unable to find time variable: time OR Obs_Time";
327 std::unique_ptr<float[]> OffsetTime;
330 ErrorMsg =
"NetcdfIO::ReadDateTime: Unable to find dimension of time variable";
333 ErrorMsg =
"NetcdfIO::ReadDateTime: Unable to find size of dimension of time variable";
336 OffsetTime.reset(
new float[vsize_]);
337 ErrorMsg =
"NetcdfIO::ReadDateTime: Unable to read time variable: ";
342 std::unique_ptr<util::DateTime> dt_(
new util::DateTime[vsize_]);
343 for (std::size_t
i = 0;
i < vsize_; ++
i) {
344 dt_.get()[
i] = refdt_ + util::Duration(static_cast<int>(OffsetTime.get()[
i] * 3600));
345 dt_.get()[
i].toYYYYMMDDhhmmss(Year, Month, Day, Hour, Minute, Second);
347 VarDate[
i] = Year*10000 + Month*100 + Day;
348 VarTime[
i] = Hour*10000 + Minute*100 + Second;
361 os <<
"Netcdf: In " << __FILE__ <<
" @ " << __LINE__ << std::endl;
379 if (RetCode != NC_NOERR) {
std::size_t nobs_
number of unique observations
l_size ! loop over number of fields ke do je do i
void print(std::ostream &os) const
print method for stream output
std::size_t nlocs_
number of unique locations
bool have_nlocs_
This data member is a flag that indicates the existence of the "nlocs" dimension in the opened netcdf...
bool have_nrecs_
This data member is a flag that indicates the existence of the "nrecs" dimension in the opened netcdf...
std::string fname_
file name
int nobs_id_
This data member holds the netcdf id of the "nobs" dimension in the opened netcdf file...
void ReadDateTime(int *VarDate, int *VarTime)
Read and format the date, time values.
int nvars_id_
This data member holds the netcdf id of the "nvars" dimension in the opened netcdf file...
int nlocs_id_
This data member holds the netcdf id of the "nlocs" dimension in the opened netcdf file...
bool have_nobs_
This data member is a flag that indicates the existence of the "nobs" dimension in the opened netcdf ...
int nc_varid_
This data member holds the netcdf id of the current dataset (variable) in the opened netcdf file...
void CheckNcCall(int, std::string &)
check results of netcdf call
bool have_nchans_
This data member is a flag that indicates the existence of the "nchans" dimension in the opened netcd...
std::string fmode_
file mode ("r" -> read, "w" -> overwrite, "W" -> create and write)
bool have_nvars_
This data member is a flag that indicates the existence of the "nvars" dimension in the opened netcdf...
void ReadVar(const std::string &VarName, int *VarData)
Read data from netcdf file to memory.
int nrecs_id_
This data member holds the netcdf id of the "nrecs" dimension in the opened netcdf file...
std::size_t nrecs_
number of unique records
void WriteVar(const std::string &VarName, int *VarData)
Write data from memory to netcdf file.
int nchans_id_
This data member holds the netcdf id of the "nchans" dimension in the opened netcdf file...
std::size_t nvars_
number of unique variables
NetcdfIO(const std::string &FileName, const std::string &FileMode, const std::size_t &Nlocs, const std::size_t &Nobs, const std::size_t &Nrecs, const std::size_t &Nvars)