1 #ifndef ERF_NCWPSFILE_H_
2 #define ERF_NCWPSFILE_H_
9 #include "AMReX_FArrayBox.H"
10 #include "AMReX_IArrayBox.H"
49 template <
typename DataType>
52 using DType =
typename std::remove_const<DataType>::type;
55 explicit NDArray (
const std::string vname,
const std::vector<size_t>& vshape)
69 ref_counted.fetch_add(1, std::memory_order_relaxed);
78 ref_counted.fetch_add(1, std::memory_order_relaxed);
84 ref_counted.fetch_sub(1, std::memory_order_acq_rel);
90 ref_counted.fetch_add(1, std::memory_order_relaxed);
107 int isize =
static_cast<int>(
shape.size());
108 for (
auto i=0; i<isize; ++i) num *=
shape[i];
126 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xlo,
127 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xhi,
128 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_ylo,
129 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_yhi);
133 getEpochTime (
const std::string& dateTime,
const std::string& dateTimeFormat)
137 std::istringstream ss{ dateTime };
141 memset(&tmTime, 0,
sizeof(tmTime));
145 strptime(dateTime.c_str(), dateTimeFormat.c_str(), &tmTime);
149 auto epoch = timegm(&tmTime);
156 template<
typename DType>
160 AMREX_ASSERT(arrays.size() == names.size());
162 if (amrex::ParallelDescriptor::IOProcessor())
180 for (
auto n=0; n<arrays.size(); ++n) {
182 std::string vname_to_write = names[n];
183 std::string vname_to_read = names[n];
184 if (vname_to_read.substr(0,2) ==
"R_") {
185 vname_to_read = names[n+4];
193 std::vector<size_t> shape = ncf.var(vname_to_read).shape();
195 DType* dataPtr = arrays[n].get_data();
197 std::vector<size_t> start(shape.size(), 0);
207 ncf.var(vname_to_read).get(dataPtr, start, shape);
223 template<
class FAB,
typename DType>
226 amrex::Real& Latitude,
227 amrex::Real& Longitude,
228 std::string& Lat_var_name,
229 std::string& Lon_var_name,
231 const std::string& var_name,
237 ns1 = nc_arrays[iv].get_vshape()[1];
243 ns2 = nc_arrays[iv].get_vshape()[1];
244 ns3 = nc_arrays[iv].get_vshape()[2];
247 ns1 = nc_arrays[iv].get_vshape()[1];
248 ns2 = nc_arrays[iv].get_vshape()[2];
249 ns3 = nc_arrays[iv].get_vshape()[3];
252 amrex::Abort(
"Dont know this NC_Data_Dims_Type");
256 amrex::Box my_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,ns1-1));
259 if (var_name ==
"U" || var_name ==
"UU" ||
260 var_name ==
"MAPFAC_U" || var_name ==
"MAPFAC_UY") my_box.setType(amrex::IndexType(amrex::IntVect(1,0,0)));
261 if (var_name ==
"V" || var_name ==
"VV" ||
262 var_name ==
"MAPFAC_V" || var_name ==
"MAPFAC_VY") my_box.setType(amrex::IndexType(amrex::IntVect(0,1,0)));
263 if (var_name ==
"W" || var_name ==
"WW") my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
265 amrex::Arena* Arena_Used = amrex::The_Arena();
268 Arena_Used = amrex::The_Pinned_Arena();
270 temp.resize(my_box,1, Arena_Used);
271 amrex::Array4<DType> fab_arr = temp.array();
273 int ioff = temp.box().smallEnd()[0];
274 int joff = temp.box().smallEnd()[1];
276 auto num_pts = my_box.numPts();
281 for (
int n(0); n < num_pts; ++n) {
282 int k = n / (ns2*ns3);
283 int j = (n - k*(ns2*ns3)) / ns3 + joff;
284 int i = n - k*(ns2*ns3) - (j-joff) * ns3 + ioff;
285 fab_arr(i,j,k,0) =
static_cast<DType
>(*(nc_arrays[iv].get_data()+n));
288 if (var_name == Lat_var_name) Latitude = fab_arr(0,0,0);
289 if (var_name == Lon_var_name) Longitude = fab_arr(0,0,0);
300 template<
class FAB,
typename DType>
303 amrex::Real& Latitude,
304 amrex::Real& Longitude,
305 std::string& Lat_var_name,
306 std::string& Lon_var_name,
307 const std::string &fname,
308 amrex::Vector<std::string> nc_var_names,
309 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types,
310 amrex::Vector<FAB*> fab_vars)
312 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
314 amrex::Vector<NDArray<float>> nc_arrays(nc_var_names.size());
316 if (amrex::ParallelDescriptor::IOProcessor())
321 for (
int iv = 0; iv < nc_var_names.size(); iv++)
324 if (amrex::ParallelDescriptor::IOProcessor()) {
325 fill_fab_from_arrays<FAB,DType>(iv, Latitude, Longitude,
326 Lat_var_name, Lon_var_name,
327 nc_arrays, nc_var_names[iv],
328 NC_dim_types[iv], tmp);
331 int ncomp = tmp.nComp();
332 amrex::Box box = tmp.box();
334 amrex::ParallelDescriptor::Bcast(&box, 1, ioproc);
335 amrex::ParallelDescriptor::Bcast(&ncomp, 1, ioproc);
337 if (!amrex::ParallelDescriptor::IOProcessor()) {
339 tmp.resize(box,ncomp,amrex::The_Pinned_Arena());
341 tmp.resize(box,ncomp);
345 amrex::ParallelDescriptor::Bcast(tmp.dataPtr(), tmp.size(), ioproc);
348 amrex::Box fab_bx = tmp.box();
349 amrex::Dim3 dom_lb = lbound(domain);
350 fab_bx += amrex::IntVect(dom_lb.x,dom_lb.y,dom_lb.z);
352 fab_vars[iv]->resize(fab_bx,1);
354 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
355 tmp.dataPtr(), tmp.dataPtr() + tmp.size(),
356 fab_vars[iv]->dataPtr());
359 fab_vars[iv]->copy(tmp,tmp.box(),0,fab_bx,0,1);
NC_Data_Dims_Type
Definition: ERF_NCWpsFile.H:30
AMREX_FORCE_INLINE std::time_t getEpochTime(const std::string &dateTime, const std::string &dateTimeFormat)
Definition: ERF_NCWpsFile.H:133
amrex::Vector< amrex::FArrayBox > PlaneVector
Definition: ERF_NCWpsFile.H:13
void fill_fab_from_arrays(int iv, amrex::Real &Latitude, amrex::Real &Longitude, std::string &Lat_var_name, std::string &Lon_var_name, amrex::Vector< NDArray< float >> &nc_arrays, const std::string &var_name, NC_Data_Dims_Type &NC_dim_type, FAB &temp)
Definition: ERF_NCWpsFile.H:225
void ReadNetCDFFile(const std::string &fname, amrex::Vector< std::string > names, amrex::Vector< NDArray< DType > > &arrays)
Definition: ERF_NCWpsFile.H:157
int BuildFABsFromWRFBdyFile(const std::string &fname, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &bdy_data_xlo, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &bdy_data_xhi, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &bdy_data_ylo, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &bdy_data_yhi)
void BuildFABsFromNetCDFFile(const amrex::Box &domain, amrex::Real &Latitude, amrex::Real &Longitude, std::string &Lat_var_name, std::string &Lon_var_name, const std::string &fname, amrex::Vector< std::string > nc_var_names, amrex::Vector< enum NC_Data_Dims_Type > NC_dim_types, amrex::Vector< FAB * > fab_vars)
Definition: ERF_NCWpsFile.H:302
static NCFile open(const std::string &name, const int cmode=NC_NOWRITE)
Definition: ERF_NCInterface.cpp:674
Definition: ERF_NCWpsFile.H:51
bool owned
Definition: ERF_NCWpsFile.H:121
std::string name
Definition: ERF_NCWpsFile.H:118
size_t ndim()
Definition: ERF_NCWpsFile.H:105
NDArray()
Definition: ERF_NCWpsFile.H:61
std::string get_vname()
Definition: ERF_NCWpsFile.H:95
NDArray(const NDArray &array)
Definition: ERF_NCWpsFile.H:64
NDArray & operator=(const NDArray &array)
Definition: ERF_NCWpsFile.H:73
void set_vshape(std::vector< size_t > vshape)
Definition: ERF_NCWpsFile.H:113
NDArray(const std::string vname, const std::vector< size_t > &vshape)
Definition: ERF_NCWpsFile.H:55
std::atomic< size_t > ref_counted
Definition: ERF_NCWpsFile.H:120
typename std::remove_const< DataType >::type DType
Definition: ERF_NCWpsFile.H:52
std::vector< size_t > shape
Definition: ERF_NCWpsFile.H:119
DType * data
Definition: ERF_NCWpsFile.H:122
decltype(auto) get_data()
Definition: ERF_NCWpsFile.H:89
std::vector< size_t > get_vshape()
Definition: ERF_NCWpsFile.H:100
~NDArray()
Definition: ERF_NCWpsFile.H:83