4 #ifndef ERF_NCWPSFILE_H_
5 #define ERF_NCWPSFILE_H_
12 #include "AMReX_FArrayBox.H"
13 #include "AMReX_IArrayBox.H"
54 template <
typename DataType>
57 using DType =
typename std::remove_const<DataType>::type;
60 explicit NDArray (
const std::string vname,
const std::vector<size_t>& vshape)
63 std::default_delete<
DType[]>())} {}
86 int isize =
static_cast<int>(
shape.size());
87 for (
auto i=0; i<isize; ++i)
num *=
shape[i];
99 std::shared_ptr<DType[]>
data;
103 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xlo,
104 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xhi,
105 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_ylo,
106 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_yhi);
108 template<
typename DType>
111 amrex::Vector<std::string> names,
113 amrex::Vector<int>& success)
115 amrex::Print() <<
"Reading time slice " << tidx <<
" from " << fname << std::endl;
116 AMREX_ASSERT(arrays.size() == names.size());
118 if (amrex::ParallelDescriptor::IOProcessor())
120 auto ncf = ncutils::NCFile::open(fname, NC_CLOBBER | NC_NETCDF4);
122 int ntimes = ncf.dim(
"Time").len();
125 for (
auto n=0; n<arrays.size(); ++n)
127 std::string vname_to_write = names[n];
128 std::string vname_to_read = names[n];
129 if (vname_to_read.substr(0,2) ==
"R_") {
130 vname_to_read = names[n+4];
133 success[n] = ncf.has_var(vname_to_read);
137 std::vector<std::string> dimnames = ncf.var(vname_to_read).dimnames();
140 std::vector<size_t> count = ncf.var(vname_to_read).shape();
141 std::vector<size_t> start(count.size(), 0);
146 DType* dataPtr = arrays[n].get_data();
148 ncf.var(vname_to_read).get(dataPtr, start, count);
155 template<
typename DType>
157 amrex::Vector<
NDArray<DType> >& arrays, amrex::Vector<int>& success)
159 AMREX_ASSERT(arrays.size() == names.size());
161 if (amrex::ParallelDescriptor::IOProcessor())
163 auto prev_fpe_except = amrex::disableFPExcept(amrex::FPExcept::all);
165 auto ncf = ncutils::NCFile::open(fname, NC_CLOBBER | NC_NETCDF4);
182 for (
auto n=0; n<arrays.size(); ++n)
184 std::string vname_to_write = names[n];
185 std::string vname_to_read = names[n];
187 if (vname_to_read.substr(0,2) ==
"R_") {
188 vname_to_read = names[n+4];
191 success[n] = ncf.has_var(vname_to_read);
192 if (success[n] == 0) {
193 amrex::Print() <<
" Skipping " << vname_to_read << std::endl;
195 amrex::Print() <<
" Reading " << vname_to_read << std::endl;
198 if (success[n] == 1) {
200 std::vector<std::string> dimnames = ncf.var(vname_to_read).dimnames();
203 std::vector<size_t> shape = ncf.var(vname_to_read).shape();
205 DType* dataPtr = arrays[n].get_data();
207 std::vector<size_t> start(shape.size(), 0);
210 auto numPts = arrays[n].ndim();
211 amrex::Print() <<
"NetCDF Variable name = " << vname_to_read << std::endl;
212 amrex::Print() <<
"numPts read from NetCDF file/var = " << numPts << std::endl;
213 amrex::Print() <<
"Dims in var = " << ncf.var(vname_to_read).ndim() << std::endl;
214 amrex::Print() <<
"Dim names = (";
215 for (
auto &dim:dimnames)
216 amrex::Print() << dim <<
", " ;
217 amrex::Print() <<
")" << std::endl;
218 amrex::Print() <<
"Dims of the variable = (";
219 for (
auto &dim:shape)
220 amrex::Print() << dim <<
", " ;
221 amrex::Print() <<
")" << std::endl;
224 ncf.var(vname_to_read).get(dataPtr, start, shape);
230 std::feclearexcept(FE_ALL_EXCEPT);
231 amrex::setFPExcept(prev_fpe_except);
246 template<
class FAB,
typename DType>
249 const amrex::Box& domain,
251 const std::string& var_name,
257 ns1 = nc_arrays[iv].get_vshape()[1];
263 ns2 = nc_arrays[iv].get_vshape()[1];
264 ns3 = nc_arrays[iv].get_vshape()[2];
267 ns1 = nc_arrays[iv].get_vshape()[1];
268 ns2 = nc_arrays[iv].get_vshape()[2];
269 ns3 = nc_arrays[iv].get_vshape()[3];
272 ns1 = nc_arrays[iv].get_vshape()[1];
273 ns2 = nc_arrays[iv].get_vshape()[2];
274 ns3 = nc_arrays[iv].get_vshape()[3];
277 ns1 = nc_arrays[iv].get_vshape()[1];
283 amrex::Abort(
"Dont know this NC_Data_Dims_Type");
290 int khi = std::min(domain.bigEnd(2), ns1-1);
293 amrex::Box in_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,ns1-1));
294 amrex::Box my_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,
khi));
296 if (var_name ==
"PH" || var_name ==
"PHB") {
297 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
298 my_box.setBig(2,
khi+1);
300 else if (var_name ==
"U" || var_name ==
"UU" || var_name ==
"MAPFAC_U") {
301 my_box.setType(amrex::IndexType(amrex::IntVect(1,0,0)));
303 else if (var_name ==
"V" || var_name ==
"VV" || var_name ==
"MAPFAC_V") {
304 my_box.setType(amrex::IndexType(amrex::IntVect(0,1,0)));
306 else if (var_name ==
"W" || var_name ==
"WW") {
307 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
308 my_box.setBig(2,
khi+1);
310 else if (var_name ==
"TSLB" || var_name ==
"SMOIS" || var_name ==
"SH2O" || var_name ==
"ZS" || var_name ==
"DZS") {
312 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
315 amrex::Arena*
Arena_Used = amrex::The_Arena();
321 amrex::Array4<DType> fab_arr = temp.array();
323 int ioff = temp.box().smallEnd()[0];
324 int joff = temp.box().smallEnd()[1];
325 int kmax = temp.box().bigEnd()[2];
328 auto num_pts = in_box.numPts();
330 for (
int n(0); n < num_pts; ++n) {
331 int k = n / (ns2*ns3);
332 if (k > kmax)
continue;
333 int j = (n - k*(ns2*ns3)) / ns3 + joff;
334 int i = n - k*(ns2*ns3) - (j-joff) * ns3 + ioff;
335 fab_arr(i,j,k,0) =
static_cast<DType
>(*(nc_arrays[iv].get_data()+n));
349 template<
class FAB,
typename DType>
352 const std::string &fname,
353 amrex::Vector<std::string> nc_var_names,
354 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types,
355 amrex::Vector<FAB*> fab_vars,
356 amrex::Vector<int>& success)
358 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
360 amrex::Vector<NDArray<float>> nc_arrays(nc_var_names.size());
362 if (amrex::ParallelDescriptor::IOProcessor())
367 amrex::ParallelDescriptor::Bcast(success.dataPtr(), success.size(), ioproc);
369 for (
int iv = 0; iv < nc_var_names.size(); iv++)
371 if (success[iv] == 1) {
373 if (amrex::ParallelDescriptor::IOProcessor()) {
374 fill_fab_from_arrays<FAB,DType>(iv, domain, nc_arrays, nc_var_names[iv], NC_dim_types[iv],
tmp);
377 int ncomp =
tmp.nComp();
378 amrex::Box box =
tmp.box();
380 amrex::ParallelDescriptor::Bcast(&box, 1, ioproc);
381 amrex::ParallelDescriptor::Bcast(&ncomp, 1, ioproc);
383 if (!amrex::ParallelDescriptor::IOProcessor()) {
385 tmp.resize(box,ncomp,amrex::The_Pinned_Arena());
387 tmp.resize(box,ncomp);
391 amrex::ParallelDescriptor::Bcast(
tmp.dataPtr(),
tmp.size(), ioproc);
394 amrex::Box fab_bx =
tmp.box();
395 amrex::Dim3 dom_lb = lbound(domain);
396 fab_bx += amrex::IntVect(dom_lb.x,dom_lb.y,dom_lb.z);
398 fab_vars[iv]->resize(fab_bx,1);
400 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
401 tmp.dataPtr(),
tmp.dataPtr() +
tmp.size(),
402 fab_vars[iv]->dataPtr());
405 fab_vars[iv]->copy(
tmp,
tmp.box(),0,fab_bx,0,1);
@ num
Definition: ERF_DataStruct.H:29
const int khi
Definition: ERF_InitCustomPert_Bubble.H:21
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
Arena * Arena_Used
Definition: ERF_Morrison_Advance_F.H:23
void ReadNetCDFFile(const std::string &fname, amrex::Vector< std::string > names, amrex::Vector< NDArray< DType > > &arrays, amrex::Vector< int > &success)
Definition: ERF_NCWpsFile.H:156
NC_Data_Dims_Type
Definition: ERF_NCWpsFile.H:33
void fill_fab_from_arrays(int iv, const amrex::Box &domain, amrex::Vector< NDArray< float >> &nc_arrays, const std::string &var_name, NC_Data_Dims_Type &NC_dim_type, FAB &temp)
Definition: ERF_NCWpsFile.H:248
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, amrex::Vector< int > &success)
Definition: ERF_NCWpsFile.H:351
amrex::Vector< amrex::FArrayBox > PlaneVector
Definition: ERF_NCWpsFile.H:17
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 ReadTimeSliceFromNetCDFFile(const std::string &fname, const int tidx, amrex::Vector< std::string > names, amrex::Vector< NDArray< DType > > &arrays, amrex::Vector< int > &success)
Definition: ERF_NCWpsFile.H:109
@ tmp
Definition: ERF_AdvanceWSM6.cpp:116
Definition: ERF_NCWpsFile.H:56
std::string name
Definition: ERF_NCWpsFile.H:97
std::shared_ptr< DType[]> data
Definition: ERF_NCWpsFile.H:99
size_t ndim()
Definition: ERF_NCWpsFile.H:84
NDArray()
Definition: ERF_NCWpsFile.H:66
std::string get_vname()
Definition: ERF_NCWpsFile.H:74
void set_vshape(std::vector< size_t > vshape)
Definition: ERF_NCWpsFile.H:92
NDArray(const std::string vname, const std::vector< size_t > &vshape)
Definition: ERF_NCWpsFile.H:60
typename std::remove_const< DataType >::type DType
Definition: ERF_NCWpsFile.H:57
std::vector< size_t > shape
Definition: ERF_NCWpsFile.H:98
decltype(auto) get_data()
Definition: ERF_NCWpsFile.H:69
std::vector< size_t > get_vshape()
Definition: ERF_NCWpsFile.H:79