1 #ifndef ERF_NCWPSFILE_H_
2 #define ERF_NCWPSFILE_H_
8 #include "AMReX_FArrayBox.H"
9 #include "AMReX_IArrayBox.H"
50 template <
typename DataType>
53 using DType =
typename std::remove_const<DataType>::type;
56 explicit NDArray (
const std::string vname,
const std::vector<size_t>& vshape)
59 std::default_delete<
DType[]>())} {}
82 int isize =
static_cast<int>(
shape.size());
83 for (
auto i=0; i<isize; ++i)
num *=
shape[i];
95 std::shared_ptr<DType[]>
data;
99 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xlo,
100 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_xhi,
101 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_ylo,
102 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_yhi);
104 template<
typename DType>
107 amrex::Vector<std::string> names,
109 amrex::Vector<int>& success)
111 amrex::Print() <<
"Reading time slice " << tidx <<
" from " << fname << std::endl;
112 AMREX_ASSERT(arrays.size() == names.size());
114 if (amrex::ParallelDescriptor::IOProcessor())
118 int ntimes = ncf.dim(
"Time").len();
121 for (
auto n=0; n<arrays.size(); ++n)
123 std::string vname_to_write = names[n];
124 std::string vname_to_read = names[n];
125 if (vname_to_read.substr(0,2) ==
"R_") {
126 vname_to_read = names[n+4];
129 success[n] = ncf.has_var(vname_to_read);
133 std::vector<std::string> dimnames = ncf.var(vname_to_read).dimnames();
136 std::vector<size_t> count = ncf.var(vname_to_read).shape();
137 std::vector<size_t> start(count.size(), 0);
142 DType* dataPtr = arrays[n].get_data();
144 ncf.var(vname_to_read).get(dataPtr, start, count);
151 template<
typename DType>
153 amrex::Vector<
NDArray<DType> >& arrays, amrex::Vector<int>& success)
155 AMREX_ASSERT(arrays.size() == names.size());
157 if (amrex::ParallelDescriptor::IOProcessor())
176 for (
auto n=0; n<arrays.size(); ++n)
178 std::string vname_to_write = names[n];
179 std::string vname_to_read = names[n];
181 if (vname_to_read.substr(0,2) ==
"R_") {
182 vname_to_read = names[n+4];
185 success[n] = ncf.has_var(vname_to_read);
186 if (success[n] == 0) {
187 amrex::Print() <<
" Skipping " << vname_to_read << std::endl;
189 amrex::Print() <<
" Reading " << vname_to_read << std::endl;
192 if (success[n] == 1) {
194 std::vector<std::string> dimnames = ncf.var(vname_to_read).dimnames();
197 std::vector<size_t> shape = ncf.var(vname_to_read).shape();
199 DType* dataPtr = arrays[n].get_data();
201 std::vector<size_t> start(shape.size(), 0);
204 auto numPts = arrays[n].ndim();
205 amrex::Print() <<
"NetCDF Variable name = " << vname_to_read << std::endl;
206 amrex::Print() <<
"numPts read from NetCDF file/var = " << numPts << std::endl;
207 amrex::Print() <<
"Dims in var = " << ncf.var(vname_to_read).ndim() << std::endl;
208 amrex::Print() <<
"Dim names = (";
209 for (
auto &dim:dimnames)
210 amrex::Print() << dim <<
", " ;
211 amrex::Print() <<
")" << std::endl;
212 amrex::Print() <<
"Dims of the variable = (";
213 for (
auto &dim:shape)
214 amrex::Print() << dim <<
", " ;
215 amrex::Print() <<
")" << std::endl;
218 ncf.var(vname_to_read).get(dataPtr, start, shape);
236 template<
class FAB,
typename DType>
239 const amrex::Box& domain,
241 const std::string& var_name,
247 ns1 = nc_arrays[iv].get_vshape()[1];
253 ns2 = nc_arrays[iv].get_vshape()[1];
254 ns3 = nc_arrays[iv].get_vshape()[2];
257 ns1 = nc_arrays[iv].get_vshape()[1];
258 ns2 = nc_arrays[iv].get_vshape()[2];
259 ns3 = nc_arrays[iv].get_vshape()[3];
262 ns1 = nc_arrays[iv].get_vshape()[1];
263 ns2 = nc_arrays[iv].get_vshape()[2];
264 ns3 = nc_arrays[iv].get_vshape()[3];
267 ns1 = nc_arrays[iv].get_vshape()[1];
273 amrex::Abort(
"Dont know this NC_Data_Dims_Type");
280 int khi = std::min(domain.bigEnd(2), ns1-1);
283 amrex::Box in_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,ns1-1));
284 amrex::Box my_box(amrex::IntVect(0,0,0), amrex::IntVect(ns3-1,ns2-1,
khi));
286 if (var_name ==
"PH" || var_name ==
"PHB") {
287 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
288 my_box.setBig(2,
khi+1);
290 else if (var_name ==
"U" || var_name ==
"UU" || var_name ==
"MAPFAC_U") {
291 my_box.setType(amrex::IndexType(amrex::IntVect(1,0,0)));
293 else if (var_name ==
"V" || var_name ==
"VV" || var_name ==
"MAPFAC_V") {
294 my_box.setType(amrex::IndexType(amrex::IntVect(0,1,0)));
296 else if (var_name ==
"W" || var_name ==
"WW") {
297 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
298 my_box.setBig(2,
khi+1);
300 else if (var_name ==
"TSLB" || var_name ==
"SMOIS" || var_name ==
"SH2O" || var_name ==
"ZS" || var_name ==
"DZS") {
302 my_box.setType(amrex::IndexType(amrex::IntVect(0,0,1)));
305 amrex::Arena* Arena_Used = amrex::The_Arena();
308 Arena_Used = amrex::The_Pinned_Arena();
310 temp.resize(my_box,1, Arena_Used);
311 amrex::Array4<DType> fab_arr = temp.array();
313 int ioff = temp.box().smallEnd()[0];
314 int joff = temp.box().smallEnd()[1];
315 int kmax = temp.box().bigEnd()[2];
318 auto num_pts = in_box.numPts();
320 for (
int n(0); n < num_pts; ++n) {
321 int k = n / (ns2*ns3);
322 if (k > kmax)
continue;
323 int j = (n - k*(ns2*ns3)) / ns3 + joff;
324 int i = n - k*(ns2*ns3) - (j-joff) * ns3 + ioff;
325 fab_arr(i,j,k,0) =
static_cast<DType
>(*(nc_arrays[iv].get_data()+n));
337 template<
class FAB,
typename DType>
340 const std::string &fname,
341 amrex::Vector<std::string> nc_var_names,
342 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types,
343 amrex::Vector<FAB*> fab_vars,
344 amrex::Vector<int>& success)
346 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
348 amrex::Vector<NDArray<float>> nc_arrays(nc_var_names.size());
350 if (amrex::ParallelDescriptor::IOProcessor())
355 amrex::ParallelDescriptor::Bcast(success.dataPtr(), success.size(), ioproc);
357 for (
int iv = 0; iv < nc_var_names.size(); iv++)
359 if (success[iv] == 1) {
361 if (amrex::ParallelDescriptor::IOProcessor()) {
362 fill_fab_from_arrays<FAB,DType>(iv, domain, nc_arrays, nc_var_names[iv], NC_dim_types[iv], tmp);
365 int ncomp = tmp.nComp();
366 amrex::Box box = tmp.box();
368 amrex::ParallelDescriptor::Bcast(&box, 1, ioproc);
369 amrex::ParallelDescriptor::Bcast(&ncomp, 1, ioproc);
371 if (!amrex::ParallelDescriptor::IOProcessor()) {
373 tmp.resize(box,ncomp,amrex::The_Pinned_Arena());
375 tmp.resize(box,ncomp);
379 amrex::ParallelDescriptor::Bcast(tmp.dataPtr(), tmp.size(), ioproc);
382 amrex::Box fab_bx = tmp.box();
383 amrex::Dim3 dom_lb = lbound(domain);
384 fab_bx += amrex::IntVect(dom_lb.x,dom_lb.y,dom_lb.z);
386 fab_vars[iv]->resize(fab_bx,1);
388 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
389 tmp.dataPtr(), tmp.dataPtr() + tmp.size(),
390 fab_vars[iv]->dataPtr());
393 fab_vars[iv]->copy(tmp,tmp.box(),0,fab_bx,0,1);
@ num
Definition: ERF_DataStruct.H:24
const int khi
Definition: ERF_InitCustomPert_Bubble.H:21
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
void ReadNetCDFFile(const std::string &fname, amrex::Vector< std::string > names, amrex::Vector< NDArray< DType > > &arrays, amrex::Vector< int > &success)
Definition: ERF_NCWpsFile.H:152
NC_Data_Dims_Type
Definition: ERF_NCWpsFile.H:29
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:238
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:339
amrex::Vector< amrex::FArrayBox > PlaneVector
Definition: ERF_NCWpsFile.H:13
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:105
static NCFile open(const std::string &name, const int cmode=NC_NOWRITE)
Definition: ERF_NCInterface.cpp:707
Definition: ERF_NCWpsFile.H:52
std::string name
Definition: ERF_NCWpsFile.H:93
std::shared_ptr< DType[]> data
Definition: ERF_NCWpsFile.H:95
size_t ndim()
Definition: ERF_NCWpsFile.H:80
NDArray()
Definition: ERF_NCWpsFile.H:62
std::string get_vname()
Definition: ERF_NCWpsFile.H:70
void set_vshape(std::vector< size_t > vshape)
Definition: ERF_NCWpsFile.H:88
NDArray(const std::string vname, const std::vector< size_t > &vshape)
Definition: ERF_NCWpsFile.H:56
typename std::remove_const< DataType >::type DType
Definition: ERF_NCWpsFile.H:53
std::vector< size_t > shape
Definition: ERF_NCWpsFile.H:94
decltype(auto) get_data()
Definition: ERF_NCWpsFile.H:65
std::vector< size_t > get_vshape()
Definition: ERF_NCWpsFile.H:75