@@ -128,6 +128,53 @@ int BuildFABsFromWRFBdyFile (const std::string &fname,
128
128
amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_ylo,
129
129
amrex::Vector<amrex::Vector<amrex::FArrayBox>>& bdy_data_yhi);
130
130
131
+ template <typename DType>
132
+ void ReadTimeSliceFromNetCDFFile (const std::string& fname,
133
+ const int tidx,
134
+ amrex::Vector<std::string> names,
135
+ amrex::Vector<NDArray<DType> >& arrays,
136
+ amrex::Vector<int >& success)
137
+ {
138
+ amrex::Print () << " Reading time slice " << tidx << " from " << fname << std::endl;
139
+ AMREX_ASSERT (arrays.size () == names.size ());
140
+
141
+ if (amrex::ParallelDescriptor::IOProcessor ())
142
+ {
143
+ auto ncf = ncutils::NCFile::open (fname, NC_CLOBBER | NC_NETCDF4);
144
+
145
+ int ntimes = ncf.dim (" Time" ).len ();
146
+ AMREX_ALWAYS_ASSERT ((tidx >= 0 ) && (tidx < ntimes));
147
+
148
+ for (auto n=0 ; n<arrays.size (); ++n)
149
+ {
150
+ std::string vname_to_write = names[n];
151
+ std::string vname_to_read = names[n];
152
+ if (vname_to_read.substr (0 ,2 ) == " R_" ) {
153
+ vname_to_read = names[n+4 ]; // This allows us to read "T" instead -- we will over-write this later
154
+ }
155
+
156
+ success[n] = ncf.has_var (vname_to_read);
157
+
158
+ if (success[n] == 1 )
159
+ {
160
+ std::vector<std::string> dimnames = ncf.var (vname_to_read).dimnames ();
161
+ AMREX_ALWAYS_ASSERT (dimnames[0 ] == " Time" );
162
+
163
+ std::vector<size_t > count = ncf.var (vname_to_read).shape ();
164
+ std::vector<size_t > start (count.size (), 0 );
165
+ start[0 ] = tidx;
166
+ count[0 ] = 1 ;
167
+
168
+ arrays[n] = NDArray<DType>(vname_to_read, count);
169
+ DType* dataPtr = arrays[n].get_data ();
170
+
171
+ ncf.var (vname_to_read).get (dataPtr, start, count);
172
+ } // has_var
173
+ }
174
+ ncf.close ();
175
+ }
176
+ }
177
+
131
178
template <typename DType>
132
179
void ReadNetCDFFile (const std::string& fname, amrex::Vector<std::string> names,
133
180
amrex::Vector<NDArray<DType> >& arrays, amrex::Vector<int >& success)
@@ -172,19 +219,29 @@ void ReadNetCDFFile (const std::string& fname, amrex::Vector<std::string> names,
172
219
173
220
if (success[n] == 1 ) {
174
221
222
+ std::vector<std::string> dimnames = ncf.var (vname_to_read).dimnames ();
223
+ AMREX_ALWAYS_ASSERT (dimnames[0 ] == " Time" );
224
+
175
225
std::vector<size_t > shape = ncf.var (vname_to_read).shape ();
176
226
arrays[n] = NDArray<DType>(vname_to_read,shape);
177
227
DType* dataPtr = arrays[n].get_data ();
178
228
179
229
std::vector<size_t > start (shape.size (), 0 );
180
230
181
- // auto numPts = arrays[n].ndim();
182
- // amrex::Print() << "NetCDF Variable name = " << vname_to_read << std::endl;
183
- // amrex::Print() << "numPts read from NetCDF file/var = " << numPts << std::endl;
184
- // amrex::Print() << "Dims of the variable = (";
185
- // for (auto &dim:shape)
186
- // amrex::Print() << dim << ", " ;
187
- // amrex::Print() << ")" << std::endl;
231
+ #if 0
232
+ auto numPts = arrays[n].ndim();
233
+ amrex::Print() << "NetCDF Variable name = " << vname_to_read << std::endl;
234
+ amrex::Print() << "numPts read from NetCDF file/var = " << numPts << std::endl;
235
+ amrex::Print() << "Dims in var = " << ncf.var(vname_to_read).ndim() << std::endl;
236
+ amrex::Print() << "Dim names = (";
237
+ for (auto &dim:dimnames)
238
+ amrex::Print() << dim << ", " ;
239
+ amrex::Print() << ")" << std::endl;
240
+ amrex::Print() << "Dims of the variable = (";
241
+ for (auto &dim:shape)
242
+ amrex::Print() << dim << ", " ;
243
+ amrex::Print() << ")" << std::endl;
244
+ #endif
188
245
189
246
ncf.var (vname_to_read).get (dataPtr, start, shape);
190
247
0 commit comments