91void ReadNetCDFFile (
const std::string& fname, amrex::Vector<std::string> names,
92 amrex::Vector<
NDArray<DType> >& arrays,
bool one_time=
false,
int fill_time=0)
94 AMREX_ASSERT(arrays.size() == names.size());
97 ncmpi_begin_indep_data(ncf.ncid);
98 if (amrex::ParallelDescriptor::IOProcessor())
115 for (
auto n=0; n<arrays.size(); ++n) {
117 std::string vname_to_write = names[n];
118 std::string vname_to_read = names[n];
120 if (!ncf.has_var(vname_to_read)) {
121 amrex::Print() <<
"Variable name " << vname_to_read <<
" not found!" << std::endl;
127 std::vector<MPI_Offset> shape = ncf.var(vname_to_read).shape();
129 DType* dataPtr = arrays[n].get_data();
131 std::vector<MPI_Offset> start(shape.size(), 0);
134 start[0] = fill_time;
146 ncf.var(vname_to_read).get(dataPtr, start, shape);
189 const std::string& var_name,
191 FAB& temp, amrex::IntVect custom_ngrow)
196 nsy = nc_arrays[iv].get_vshape()[0];
197 nsx = nc_arrays[iv].get_vshape()[1];
200 nsz = nc_arrays[iv].get_vshape()[0];
201 nsy = nc_arrays[iv].get_vshape()[1];
202 nsx = nc_arrays[iv].get_vshape()[2];
206 nsy = nc_arrays[iv].get_vshape()[1];
207 nsx = nc_arrays[iv].get_vshape()[2];
210 nsz = nc_arrays[iv].get_vshape()[1];
211 nsy = nc_arrays[iv].get_vshape()[2];
212 nsx = nc_arrays[iv].get_vshape()[3];
215 nsz = nc_arrays[iv].get_vshape()[1];
217 nsx = nc_arrays[iv].get_vshape()[2];
221 nsx = nc_arrays[iv].get_vshape()[1];
223 nsz = nc_arrays[iv].get_vshape()[0];
225 nsx = nc_arrays[iv].get_vshape()[1];
227 amrex::Abort(
"Dont know this NC_Data_Dims_Type");
232 if (var_name ==
"u" || var_name ==
"ubar" || var_name ==
"mask_u" || var_name ==
"x_u" ||
233 var_name ==
"y_u" || var_name ==
"sustr")
235 my_box.setSmall(amrex::IntVect(0,-1,0));
236 my_box.setBig(amrex::IntVect(nsx-1,nsy-2,nsz-1));
237 my_box.setType(amrex::IndexType(amrex::IntVect(1,0,0)));
239 else if (var_name ==
"v" || var_name ==
"vbar" || var_name ==
"mask_v" || var_name ==
"x_v" ||
240 var_name ==
"y_v" || var_name ==
"svstr")
242 my_box.setSmall(amrex::IntVect(-1,0,0));
243 my_box.setBig(amrex::IntVect(nsx-2,nsy-1,nsz-1));
244 my_box.setType(amrex::IndexType(amrex::IntVect(0,1,0)));
246 else if (var_name ==
"mask_psi" || var_name ==
"x_psi" || var_name ==
"y_psi" ||
247 var_name ==
"lon_psi" || var_name ==
"lat_psi")
249 my_box.setSmall(amrex::IntVect(0,0,0));
250 my_box.setBig(amrex::IntVect(nsx-1,nsy-1,nsz-1));
251 my_box.setType(amrex::IndexType(amrex::IntVect(1,1,0)));
256 my_box.setSmall(amrex::IntVect(0,0,0));
257 my_box.setBig(amrex::IntVect(nsx-1,nsy-1,nsz-1));
261 my_box.setSmall(amrex::IntVect(-1,-1,0));
262 my_box.setBig(amrex::IntVect(nsx-2,nsy-2,nsz-1));
265 my_box.grow(custom_ngrow - amrex::IntVect(1,1,0));
271 temp.resize(my_box,1,amrex::The_Pinned_Arena());
273 temp.resize(my_box,1);
275 amrex::Array4<DType> fab_arr = temp.array();
277 int ioff = temp.box().smallEnd()[0];
278 int joff = temp.box().smallEnd()[1];
280 auto num_pts_in_box = my_box.numPts();
281 auto num_pts_in_src = nsx*nsy*nsz;
286 for (
int n(0); n < num_pts_in_src; ++n) {
287 int nplane = nsx*nsy;
289 int j = (n - k*nplane) / nsx + joff;
290 int i = n - k*nplane - (j-joff) * nsx + ioff;
291 fab_arr(i,j,k,0) =
static_cast<DType
>(*(nc_arrays[iv].get_data()+n));
309 const std::string &fname,
310 amrex::Vector<std::string> nc_var_names,
311 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types,
312 amrex::Vector<FAB*> fab_vars,
313 bool one_time=
false,
int fill_time=0,
314 amrex::IntVect custom_ngrow=amrex::IntVect(1,1,0))
316 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
318 amrex::Vector<NDArray<amrex::Real>> nc_arrays(nc_var_names.size());
321 ReadNetCDFFile(fname, nc_var_names, nc_arrays, one_time, fill_time);
323 for (
int iv = 0; iv < nc_var_names.size(); iv++)
326 if (amrex::ParallelDescriptor::IOProcessor()) {
327 fill_fab_from_arrays<FAB,DType>(iv, nc_arrays, nc_var_names[iv], NC_dim_types[iv], tmp, custom_ngrow);
330 int ncomp = tmp.nComp();
331 amrex::Box box = tmp.box();
333 amrex::ParallelDescriptor::Bcast(&box, 1, ioproc);
334 amrex::ParallelDescriptor::Bcast(&ncomp, 1, ioproc);
336 if (!amrex::ParallelDescriptor::IOProcessor()) {
338 tmp.resize(box,ncomp,amrex::The_Pinned_Arena());
340 tmp.resize(box,ncomp);
344 amrex::ParallelDescriptor::Bcast(tmp.dataPtr(), tmp.size(), ioproc);
347 amrex::Box fab_bx = tmp.box();
348 amrex::Dim3 dom_lb = lbound(domain);
349 fab_bx += amrex::IntVect(dom_lb.x,dom_lb.y,dom_lb.z);
351 fab_vars[iv]->resize(fab_bx,1);
353 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
354 tmp.dataPtr(), tmp.dataPtr() + tmp.size(),
355 fab_vars[iv]->dataPtr());
358 fab_vars[iv]->copy(tmp,tmp.box(),0,fab_bx,0,1);
void ReadNetCDFFile(const std::string &fname, amrex::Vector< std::string > names, amrex::Vector< NDArray< DType > > &arrays, bool one_time=false, int fill_time=0)
Read in data from netcdf file and save to data arrays.
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, bool one_time=false, int fill_time=0, amrex::IntVect custom_ngrow=amrex::IntVect(1, 1, 0))
Function to read NetCDF variables and fill the corresponding Array4's.
void fill_fab_from_arrays(int iv, amrex::Vector< NDArray< amrex::Real > > &nc_arrays, const std::string &var_name, NC_Data_Dims_Type &NC_dim_type, FAB &temp, amrex::IntVect custom_ngrow)
Helper function for reading data from NetCDF file into a provided FAB.