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>
227 const std::string& var_name,
233 ns1 = nc_arrays[iv].get_vshape()[1];
239 ns2 = nc_arrays[iv].get_vshape()[1];
240 ns3 = nc_arrays[iv].get_vshape()[2];
243 ns1 = nc_arrays[iv].get_vshape()[1];
244 ns2 = nc_arrays[iv].get_vshape()[2];
245 ns3 = nc_arrays[iv].get_vshape()[3];
248 amrex::Abort(
"Dont know this NC_Data_Dims_Type");
252 amrex::Box my_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,ns1-1));
254 if (var_name ==
"PH" || var_name ==
"PHB") {
255 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
256 }
else if (var_name ==
"U" || var_name ==
"UU" ||
257 var_name ==
"MAPFAC_U" || var_name ==
"MAPFAC_UY")
259 my_box.setType(amrex::IndexType(amrex::IntVect(1,0,0)));
260 }
else if (var_name ==
"V" || var_name ==
"VV" ||
261 var_name ==
"MAPFAC_V" || var_name ==
"MAPFAC_VY")
263 my_box.setType(amrex::IndexType(amrex::IntVect(0,1,0)));
264 }
else if (var_name ==
"W" || var_name ==
"WW") {
265 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
268 amrex::Arena* Arena_Used = amrex::The_Arena();
271 Arena_Used = amrex::The_Pinned_Arena();
273 temp.resize(my_box,1, Arena_Used);
274 amrex::Array4<DType> fab_arr = temp.array();
276 int ioff = temp.box().smallEnd()[0];
277 int joff = temp.box().smallEnd()[1];
279 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));
297 template<
class FAB,
typename DType>
300 const std::string &fname,
301 amrex::Vector<std::string> nc_var_names,
302 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types,
303 amrex::Vector<FAB*> fab_vars)
305 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
307 amrex::Vector<NDArray<float>> nc_arrays(nc_var_names.size());
309 if (amrex::ParallelDescriptor::IOProcessor())
314 for (
int iv = 0; iv < nc_var_names.size(); iv++)
317 if (amrex::ParallelDescriptor::IOProcessor()) {
318 fill_fab_from_arrays<FAB,DType>(iv, nc_arrays, nc_var_names[iv], NC_dim_types[iv], tmp);
321 int ncomp = tmp.nComp();
322 amrex::Box box = tmp.box();
324 amrex::ParallelDescriptor::Bcast(&box, 1, ioproc);
325 amrex::ParallelDescriptor::Bcast(&ncomp, 1, ioproc);
327 if (!amrex::ParallelDescriptor::IOProcessor()) {
329 tmp.resize(box,ncomp,amrex::The_Pinned_Arena());
331 tmp.resize(box,ncomp);
335 amrex::ParallelDescriptor::Bcast(tmp.dataPtr(), tmp.size(), ioproc);
338 amrex::Box fab_bx = tmp.box();
339 amrex::Dim3 dom_lb = lbound(domain);
340 fab_bx += amrex::IntVect(dom_lb.x,dom_lb.y,dom_lb.z);
342 fab_vars[iv]->resize(fab_bx,1);
344 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
345 tmp.dataPtr(), tmp.dataPtr() + tmp.size(),
346 fab_vars[iv]->dataPtr());
349 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
void fill_fab_from_arrays(int iv, 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
amrex::Vector< amrex::FArrayBox > PlaneVector
Definition: ERF_NCWpsFile.H:13
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, 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:299
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