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);
173 const std::string& var_name,
180 nsy = nc_arrays[iv].get_vshape()[0];
181 nsx = nc_arrays[iv].get_vshape()[1];
184 nsz = nc_arrays[iv].get_vshape()[0];
185 nsy = nc_arrays[iv].get_vshape()[1];
186 nsx = nc_arrays[iv].get_vshape()[2];
190 nsy = nc_arrays[iv].get_vshape()[1];
191 nsx = nc_arrays[iv].get_vshape()[2];
194 nsz = nc_arrays[iv].get_vshape()[1];
195 nsy = nc_arrays[iv].get_vshape()[2];
196 nsx = nc_arrays[iv].get_vshape()[3];
199 nsz = nc_arrays[iv].get_vshape()[1];
201 nsx = nc_arrays[iv].get_vshape()[2];
205 nsx = nc_arrays[iv].get_vshape()[1];
207 nsz = nc_arrays[iv].get_vshape()[0];
209 nsx = nc_arrays[iv].get_vshape()[1];
211 amrex::Abort(
"Dont know this NC_Data_Dims_Type");
216 if (var_name ==
"u" || var_name ==
"ubar" || var_name ==
"mask_u" || var_name ==
"x_u" ||
217 var_name ==
"y_u" || var_name ==
"sustr")
219 my_box.setSmall(amrex::IntVect(0,-1,0));
220 my_box.setBig(amrex::IntVect(nsx-1,nsy-2,nsz-1));
221 my_box.setType(amrex::IndexType(amrex::IntVect(1,0,0)));
223 else if (var_name ==
"v" || var_name ==
"vbar" || var_name ==
"mask_v" || var_name ==
"x_v" ||
224 var_name ==
"y_v" || var_name ==
"svstr")
226 my_box.setSmall(amrex::IntVect(-1,0,0));
227 my_box.setBig(amrex::IntVect(nsx-2,nsy-1,nsz-1));
228 my_box.setType(amrex::IndexType(amrex::IntVect(0,1,0)));
230 else if (var_name ==
"mask_psi" || var_name ==
"x_psi" || var_name ==
"y_psi")
232 my_box.setSmall(amrex::IntVect(0,0,0));
233 my_box.setBig(amrex::IntVect(nsx-1,nsy-1,nsz-1));
234 my_box.setType(amrex::IndexType(amrex::IntVect(1,1,0)));
239 my_box.setSmall(amrex::IntVect(0,0,0));
240 my_box.setBig(amrex::IntVect(nsx-1,nsy-1,nsz-1));
244 my_box.setSmall(amrex::IntVect(-1,-1,0));
245 my_box.setBig(amrex::IntVect(nsx-2,nsy-2,nsz-1));
252 temp.resize(my_box,1,amrex::The_Pinned_Arena());
254 temp.resize(my_box,1);
256 amrex::Array4<DType> fab_arr = temp.array();
258 int ioff = temp.box().smallEnd()[0];
259 int joff = temp.box().smallEnd()[1];
261 auto num_pts_in_box = my_box.numPts();
262 auto num_pts_in_src = nsx*nsy*nsz;
265 AMREX_ALWAYS_ASSERT(num_pts_in_box >= num_pts_in_src);
267 for (
int n(0); n < num_pts_in_src; ++n) {
268 int nplane = nsx*nsy;
270 int j = (n - k*nplane) / nsx + joff;
271 int i = n - k*nplane - (j-joff) * nsx + ioff;
272 fab_arr(i,j,k,0) =
static_cast<DType
>(*(nc_arrays[iv].get_data()+n));
289 const std::string &fname,
290 amrex::Vector<std::string> nc_var_names,
291 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types,
292 amrex::Vector<FAB*> fab_vars,
293 bool one_time=
false,
int fill_time=0)
295 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
297 amrex::Vector<NDArray<amrex::Real>> nc_arrays(nc_var_names.size());
300 ReadNetCDFFile(fname, nc_var_names, nc_arrays, one_time, fill_time);
302 for (
int iv = 0; iv < nc_var_names.size(); iv++)
305 if (amrex::ParallelDescriptor::IOProcessor()) {
306 fill_fab_from_arrays<FAB,DType>(iv, nc_arrays, nc_var_names[iv], NC_dim_types[iv], tmp);
309 int ncomp = tmp.nComp();
310 amrex::Box box = tmp.box();
312 amrex::ParallelDescriptor::Bcast(&box, 1, ioproc);
313 amrex::ParallelDescriptor::Bcast(&ncomp, 1, ioproc);
315 if (!amrex::ParallelDescriptor::IOProcessor()) {
317 tmp.resize(box,ncomp,amrex::The_Pinned_Arena());
319 tmp.resize(box,ncomp);
323 amrex::ParallelDescriptor::Bcast(tmp.dataPtr(), tmp.size(), ioproc);
326 amrex::Box fab_bx = tmp.box();
327 amrex::Dim3 dom_lb = lbound(domain);
328 fab_bx += amrex::IntVect(dom_lb.x,dom_lb.y,dom_lb.z);
330 fab_vars[iv]->resize(fab_bx,1);
332 amrex::Gpu::copy(amrex::Gpu::hostToDevice,
333 tmp.dataPtr(), tmp.dataPtr() + tmp.size(),
334 fab_vars[iv]->dataPtr());
337 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)
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)
Helper function for reading data from NetCDF file into a provided FAB.