4 #ifndef ERF_NCWPSFILE_H_
5 #define ERF_NCWPSFILE_H_
11 #include "AMReX_FArrayBox.H"
12 #include "AMReX_IArrayBox.H"
53 template <
typename DataType>
56 using DType =
typename std::remove_const<DataType>::type;
59 explicit NDArray (
const std::string vname,
const std::vector<size_t>& vshape)
62 std::default_delete<
DType[]>())} {}
85 int isize =
static_cast<int>(
shape.size());
86 for (
auto i=0; i<isize; ++i)
num *=
shape[i];
98 std::shared_ptr<DType[]>
data;
102 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xlo,
103 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xhi,
104 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_ylo,
105 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_yhi);
107 template<
typename DType>
110 amrex::Vector<std::string> names,
112 amrex::Vector<int>& success)
114 amrex::Print() <<
"Reading time slice " << tidx <<
" from " << fname << std::endl;
115 AMREX_ASSERT(arrays.size() == names.size());
117 if (amrex::ParallelDescriptor::IOProcessor())
119 auto ncf = ncutils::NCFile::open(fname, NC_CLOBBER | NC_NETCDF4);
121 int ntimes = ncf.dim(
"Time").len();
124 for (
auto n=0; n<arrays.size(); ++n)
126 std::string vname_to_write = names[n];
127 std::string vname_to_read = names[n];
128 if (vname_to_read.substr(0,2) ==
"R_") {
129 vname_to_read = names[n+4];
132 success[n] = ncf.has_var(vname_to_read);
136 std::vector<std::string> dimnames = ncf.var(vname_to_read).dimnames();
139 std::vector<size_t> count = ncf.var(vname_to_read).shape();
140 std::vector<size_t> start(count.size(), 0);
145 DType* dataPtr = arrays[n].get_data();
147 ncf.var(vname_to_read).get(dataPtr, start, count);
154 template<
typename DType>
156 amrex::Vector<
NDArray<DType> >& arrays, amrex::Vector<int>& success)
158 AMREX_ASSERT(arrays.size() == names.size());
160 if (amrex::ParallelDescriptor::IOProcessor())
162 auto ncf = ncutils::NCFile::open(fname, NC_CLOBBER | NC_NETCDF4);
179 for (
auto n=0; n<arrays.size(); ++n)
181 std::string vname_to_write = names[n];
182 std::string vname_to_read = names[n];
184 if (vname_to_read.substr(0,2) ==
"R_") {
185 vname_to_read = names[n+4];
188 success[n] = ncf.has_var(vname_to_read);
189 if (success[n] == 0) {
190 amrex::Print() <<
" Skipping " << vname_to_read << std::endl;
192 amrex::Print() <<
" Reading " << vname_to_read << std::endl;
195 if (success[n] == 1) {
197 std::vector<std::string> dimnames = ncf.var(vname_to_read).dimnames();
200 std::vector<size_t> shape = ncf.var(vname_to_read).shape();
202 DType* dataPtr = arrays[n].get_data();
204 std::vector<size_t> start(shape.size(), 0);
207 auto numPts = arrays[n].ndim();
208 amrex::Print() <<
"NetCDF Variable name = " << vname_to_read << std::endl;
209 amrex::Print() <<
"numPts read from NetCDF file/var = " << numPts << std::endl;
210 amrex::Print() <<
"Dims in var = " << ncf.var(vname_to_read).ndim() << std::endl;
211 amrex::Print() <<
"Dim names = (";
212 for (
auto &dim:dimnames)
213 amrex::Print() << dim <<
", " ;
214 amrex::Print() <<
")" << std::endl;
215 amrex::Print() <<
"Dims of the variable = (";
216 for (
auto &dim:shape)
217 amrex::Print() << dim <<
", " ;
218 amrex::Print() <<
")" << std::endl;
221 ncf.var(vname_to_read).get(dataPtr, start, shape);
240 template<
class FAB,
typename DType>
243 const amrex::Box& domain,
245 const std::string& var_name,
251 ns1 = nc_arrays[iv].get_vshape()[1];
257 ns2 = nc_arrays[iv].get_vshape()[1];
258 ns3 = nc_arrays[iv].get_vshape()[2];
261 ns1 = nc_arrays[iv].get_vshape()[1];
262 ns2 = nc_arrays[iv].get_vshape()[2];
263 ns3 = nc_arrays[iv].get_vshape()[3];
266 ns1 = nc_arrays[iv].get_vshape()[1];
267 ns2 = nc_arrays[iv].get_vshape()[2];
268 ns3 = nc_arrays[iv].get_vshape()[3];
271 ns1 = nc_arrays[iv].get_vshape()[1];
277 amrex::Abort(
"Dont know this NC_Data_Dims_Type");
284 int khi = std::min(domain.bigEnd(2), ns1-1);
287 amrex::Box in_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,ns1-1));
288 amrex::Box my_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,
khi));
290 if (var_name ==
"PH" || var_name ==
"PHB") {
291 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
292 my_box.setBig(2,
khi+1);
294 else if (var_name ==
"U" || var_name ==
"UU" || var_name ==
"MAPFAC_U") {
295 my_box.setType(amrex::IndexType(amrex::IntVect(1,0,0)));
297 else if (var_name ==
"V" || var_name ==
"VV" || var_name ==
"MAPFAC_V") {
298 my_box.setType(amrex::IndexType(amrex::IntVect(0,1,0)));
300 else if (var_name ==
"W" || var_name ==
"WW") {
301 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
302 my_box.setBig(2,
khi+1);
304 else if (var_name ==
"TSLB" || var_name ==
"SMOIS" || var_name ==
"SH2O" || var_name ==
"ZS" || var_name ==
"DZS") {
306 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
309 amrex::Arena*
Arena_Used = amrex::The_Arena();
315 amrex::Array4<DType> fab_arr = temp.array();
317 int ioff = temp.box().smallEnd()[0];
318 int joff = temp.box().smallEnd()[1];
319 int kmax = temp.box().bigEnd()[2];
322 auto num_pts = in_box.numPts();
324 for (
int n(0); n < num_pts; ++n) {
325 int k = n / (ns2*ns3);
326 if (k > kmax)
continue;
327 int j = (n - k*(ns2*ns3)) / ns3 + joff;
328 int i = n - k*(ns2*ns3) - (j-joff) * ns3 + ioff;
329 fab_arr(i,j,k,0) =
static_cast<DType
>(*(nc_arrays[iv].get_data()+n));
343 template<
class FAB,
typename DType>
346 const std::string &fname,
347 amrex::Vector<std::string> nc_var_names,
348 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types,
349 amrex::Vector<FAB*> fab_vars,
350 amrex::Vector<int>& success)
352 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
354 amrex::Vector<NDArray<float>> nc_arrays(nc_var_names.size());
356 if (amrex::ParallelDescriptor::IOProcessor())
361 amrex::ParallelDescriptor::Bcast(success.dataPtr(), success.size(), ioproc);
363 for (
int iv = 0; iv < nc_var_names.size(); iv++)
365 if (success[iv] == 1) {
367 if (amrex::ParallelDescriptor::IOProcessor()) {
368 fill_fab_from_arrays<FAB,DType>(iv, domain, nc_arrays, nc_var_names[iv], NC_dim_types[iv],
tmp);
371 int ncomp =
tmp.nComp();
372 amrex::Box box =
tmp.box();
374 amrex::ParallelDescriptor::Bcast(&box, 1, ioproc);
375 amrex::ParallelDescriptor::Bcast(&ncomp, 1, ioproc);
377 if (!amrex::ParallelDescriptor::IOProcessor()) {
379 tmp.resize(box,ncomp,amrex::The_Pinned_Arena());
381 tmp.resize(box,ncomp);
385 amrex::ParallelDescriptor::Bcast(
tmp.dataPtr(),
tmp.size(), ioproc);
388 amrex::Box fab_bx =
tmp.box();
389 amrex::Dim3 dom_lb = lbound(domain);
390 fab_bx += amrex::IntVect(dom_lb.x,dom_lb.y,dom_lb.z);
392 fab_vars[iv]->resize(fab_bx,1);
394 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
395 tmp.dataPtr(),
tmp.dataPtr() +
tmp.size(),
396 fab_vars[iv]->dataPtr());
399 fab_vars[iv]->copy(
tmp,
tmp.box(),0,fab_bx,0,1);
@ num
Definition: ERF_DataStruct.H:27
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:155
NC_Data_Dims_Type
Definition: ERF_NCWpsFile.H:32
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:242
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:345
amrex::Vector< amrex::FArrayBox > PlaneVector
Definition: ERF_NCWpsFile.H:16
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:108
@ tmp
Definition: ERF_AdvanceWSM6.cpp:114
Definition: ERF_NCWpsFile.H:55
std::string name
Definition: ERF_NCWpsFile.H:96
std::shared_ptr< DType[]> data
Definition: ERF_NCWpsFile.H:98
size_t ndim()
Definition: ERF_NCWpsFile.H:83
NDArray()
Definition: ERF_NCWpsFile.H:65
std::string get_vname()
Definition: ERF_NCWpsFile.H:73
void set_vshape(std::vector< size_t > vshape)
Definition: ERF_NCWpsFile.H:91
NDArray(const std::string vname, const std::vector< size_t > &vshape)
Definition: ERF_NCWpsFile.H:59
typename std::remove_const< DataType >::type DType
Definition: ERF_NCWpsFile.H:56
std::vector< size_t > shape
Definition: ERF_NCWpsFile.H:97
decltype(auto) get_data()
Definition: ERF_NCWpsFile.H:68
std::vector< size_t > get_vshape()
Definition: ERF_NCWpsFile.H:78