REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_ReadFromInitNetcdf.cpp
Go to the documentation of this file.
1#include "REMORA_NCFile.H"
2#include "AMReX_FArrayBox.H"
3#include "REMORA_DataStruct.H"
4
5using namespace amrex;
6
7#ifdef REMORA_USE_NETCDF
8/**
9 * @param lev level of data to read
10 * @param domain simulation domain
11 * @param fname file name to read from
12 * @param NC_temp_fab container for temperature data
13 * @param NC_salt_fab container for salinity data
14 * @param NC_u_fab container for u velocity data
15 * @param NC_v_fab container for v velocity data
16 */
17void
19 const Box& domain,
20 const std::string& fname,
21 FArrayBox& NC_temp_fab, FArrayBox& NC_salt_fab,
22 FArrayBox& NC_xvel_fab, FArrayBox& NC_yvel_fab)
23{
24 amrex::Print() << "Loading initial solution data from NetCDF file " << fname << std::endl;
25
26 Vector<FArrayBox*> NC_fabs;
27 Vector<std::string> NC_names;
28 Vector<enum NC_Data_Dims_Type> NC_dim_types;
29
30 NC_fabs.push_back(&NC_temp_fab); NC_names.push_back("temp"); NC_dim_types.push_back(NC_Data_Dims_Type::Time_BT_SN_WE); // 0
31 NC_fabs.push_back(&NC_salt_fab); NC_names.push_back("salt"); NC_dim_types.push_back(NC_Data_Dims_Type::Time_BT_SN_WE); // 1
32 NC_fabs.push_back(&NC_xvel_fab); NC_names.push_back("u"); NC_dim_types.push_back(NC_Data_Dims_Type::Time_BT_SN_WE); // 2
33 NC_fabs.push_back(&NC_yvel_fab); NC_names.push_back("v"); NC_dim_types.push_back(NC_Data_Dims_Type::Time_BT_SN_WE); // 3
34
35 // Read the netcdf file and fill these FABs
36 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs);
37}
38
39/**
40 * @param lev level of data to read
41 * @param domain simulation domain
42 * @param fname file name to read from
43 * @param NC_zeta_fab container for sea surface height data
44 */
45void
47 const Box& domain,
48 const std::string& fname,
49 FArrayBox& NC_zeta_fab)
50{
51 amrex::Print() << "Loading initial sea surface height from NetCDF file " << fname << std::endl;
52
53 Vector<FArrayBox*> NC_fabs;
54 Vector<std::string> NC_names;
55 Vector<enum NC_Data_Dims_Type> NC_dim_types;
56
57 NC_fabs.push_back(&NC_zeta_fab ) ; NC_names.push_back("zeta") ; NC_dim_types.push_back(NC_Data_Dims_Type::Time_SN_WE); // 0
58
59 // Read the netcdf file and fill these FABs
60 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs);
61}
62
63/**
64 * @param lev level of data to read
65 * @param domain simulation domain
66 * @param fname file name to read from
67 * @param NC_h_fab container for bathymetry data
68 */
69void
71 const Box& domain,
72 const std::string& fname,
73 FArrayBox& NC_h_fab)
74{
75 amrex::Print() << "Loading initial bathymetry from NetCDF file " << fname << std::endl;
76
77 Vector<FArrayBox*> NC_fabs;
78 Vector<std::string> NC_names;
79 Vector<enum NC_Data_Dims_Type> NC_dim_types;
80
81 NC_fabs.push_back(&NC_h_fab ) ; NC_names.push_back("h") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 0
82
83 // Read the netcdf file and fill these FABs
84 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs);
85}
86
87/**
88 * @param lev level of data to read
89 * @param domain simulation domain
90 * @param fname file name to read from
91 * @param NC_pm_fab container for pm data
92 * @param NC_pn_fab container for pn data
93 * @param NC_xr_fab container for x_rho data
94 * @param NC_yr_fab container for y_rho data
95 * @param NC_xu_fab container for x_u data
96 * @param NC_yu_fab container for y_u data
97 * @param NC_xv_fab container for x_v data
98 * @param NC_yv_fab container for y_v data
99 * @param NC_xp_fab container for x_p data
100 * @param NC_yp_fab container for y_p data
101 */
102void
104 const Box& domain,
105 const std::string& fname,
106 FArrayBox& NC_pm_fab, FArrayBox& NC_pn_fab,
107 FArrayBox& NC_xr_fab, FArrayBox& NC_yr_fab,
108 FArrayBox& NC_xu_fab, FArrayBox& NC_yu_fab,
109 FArrayBox& NC_xv_fab, FArrayBox& NC_yv_fab,
110 FArrayBox& NC_xp_fab, FArrayBox& NC_yp_fab)
111{
112 amrex::Print() << "Loading grid variables from NetCDF file " << fname << std::endl;
113
114 Vector<FArrayBox*> NC_fabs;
115 Vector<std::string> NC_names;
116 Vector<enum NC_Data_Dims_Type> NC_dim_types;
117
118 NC_fabs.push_back(&NC_pm_fab) ; NC_names.push_back("pm") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 1
119 NC_fabs.push_back(&NC_pn_fab) ; NC_names.push_back("pn") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 2
120 NC_fabs.push_back(&NC_xr_fab) ; NC_names.push_back("x_rho") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 3
121 NC_fabs.push_back(&NC_yr_fab) ; NC_names.push_back("y_rho") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 4
122 NC_fabs.push_back(&NC_xu_fab) ; NC_names.push_back("x_u") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 5
123 NC_fabs.push_back(&NC_yu_fab) ; NC_names.push_back("y_u") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 6
124 NC_fabs.push_back(&NC_xv_fab) ; NC_names.push_back("x_v") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 7
125 NC_fabs.push_back(&NC_yv_fab) ; NC_names.push_back("y_v") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 8
126 NC_fabs.push_back(&NC_xp_fab) ; NC_names.push_back("x_psi") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 9
127 NC_fabs.push_back(&NC_yp_fab) ; NC_names.push_back("y_psi") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 10
128
129 // Read the netcdf file and fill these FABs
130 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs);
131}
132
133/**
134 * @param domain simulation domain at nc_hires_grid_level
135 * @param fname file name to read from
136 * @param NC_h_fab container for bathymetry data
137 * @param ngrow number of grow cells to read in, if not default
138 */
139void
141 const std::string& fname,
142 FArrayBox& NC_h_fab, IntVect ngrow)
143{
144 amrex::Print() << "Loading high resolution bathymetry from NetCDF file " << fname << std::endl;
145
146 Vector<FArrayBox*> NC_fabs;
147 Vector<std::string> NC_names;
148 Vector<enum NC_Data_Dims_Type> NC_dim_types;
149
150 NC_fabs.push_back(&NC_h_fab ) ; NC_names.push_back("h") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 0
151
152 // Read the netcdf file and fill these FABs
153 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs, false, 0, ngrow);
154}
155
156/**
157 * @param lev level of data to read
158 * @param domain simulation domain
159 * @param fname file name to read from
160 * @param NC_pm_fab container for pm data
161 * @param NC_pn_fab container for pn data
162 * @param ngrow number of grow cells to read in, if not default
163 */
164void
166 const std::string& fname,
167 FArrayBox& NC_pm_fab, FArrayBox& NC_pn_fab,
168 IntVect ngrow)
169{
170 amrex::Print() << "Loading high resolution grid variables from NetCDF file " << fname << std::endl;
171
172 Vector<FArrayBox*> NC_fabs;
173 Vector<std::string> NC_names;
174 Vector<enum NC_Data_Dims_Type> NC_dim_types;
175
176 NC_fabs.push_back(&NC_pm_fab) ; NC_names.push_back("pm") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 0
177 NC_fabs.push_back(&NC_pn_fab) ; NC_names.push_back("pn") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 1
178 // Read the netcdf file and fill these FABs
179 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs, false, 0, ngrow);
180}
181
182/**
183 * @param lev level of data to read
184 * @param domain simulation domain
185 * @param fname file name to read from
186 * @param NC_fcor_fab container for Coriolis parameter data
187 */
188void
190 const Box& domain,
191 const std::string& fname,
192 FArrayBox& NC_fcor_fab)
193{
194 amrex::Print() << "Loading initial coriolis from NetCDF file " << fname << std::endl;
195
196 Vector<FArrayBox*> NC_fabs;
197 Vector<std::string> NC_names;
198 Vector<enum NC_Data_Dims_Type> NC_dim_types;
199
200 NC_fabs.push_back(&NC_fcor_fab ) ; NC_names.push_back("f") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 0
201
202 // Read the netcdf file and fill these FABs
203 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs);
204}
205
206/**
207 * @param lev level of data to read
208 * @param domain simulation domain
209 * @param fname file name to read from
210 * @param NC_mskr_fab container for rho-point land/sea mask data
211 * @param NC_msku_fab container for u-point land/sea mask data
212 * @param NC_mskv_fab container for v-point land/sea mask data
213 */
214void
216 const Box& domain,
217 const std::string& fname,
218 FArrayBox& NC_mskr_fab,
219 FArrayBox& NC_msku_fab,
220 FArrayBox& NC_mskv_fab)
221{
222 amrex::Print() << "Loading masks from NetCDF file " << fname << std::endl;
223
224 Vector<FArrayBox*> NC_fabs;
225 Vector<std::string> NC_names;
226 Vector<enum NC_Data_Dims_Type> NC_dim_types;
227
228 NC_fabs.push_back(&NC_mskr_fab ) ; NC_names.push_back("mask_rho") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 0
229 NC_fabs.push_back(&NC_msku_fab ) ; NC_names.push_back("mask_u") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 1
230 NC_fabs.push_back(&NC_mskv_fab ) ; NC_names.push_back("mask_v") ; NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE); // 2
231
232 // Read the netcdf file and fill these FABs
233 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs);
234}
235
236/**
237 * @param lev level of data to read
238 * @param domain simulation domain
239 * @param fname file name to read from
240 * @param do_m2_clim_nudg whether to do 2d momentum climatology nudging
241 * @param do_m3_clim_nudg whether to do 3d momentum climatology nudging
242 * @param do_temp_clim_nudg whether to do temperature climatology nudging
243 * @param do_salt_clim_nudg whether to do salinity climatology nudging
244 * @param NC_M2NC_fab container for 2d momentum climatology data
245 * @param NC_M3NC_fab container for 3d momentum climatology data
246 * @param NC_TempNC_fab container for temperature climatology data
247 * @param NC_SaltNC_fab container for salinity climatology data
248 */
249void
251 const Box& domain,
252 const std::string& fname,
253 bool do_m2_clim_nudg,
254 bool do_m3_clim_nudg,
255 bool do_temp_clim_nudg,
256 bool do_salt_clim_nudg,
257 FArrayBox& NC_M2NC_fab,
258 FArrayBox& NC_M3NC_fab,
259 FArrayBox& NC_TempNC_fab,
260 FArrayBox& NC_SaltNC_fab)
261{
262 amrex::Print() << "Loading nudging coefficients from NetCDF file " << fname << std::endl;
263
264 Vector<FArrayBox*> NC_fabs;
265 Vector<std::string> NC_names;
266 Vector<enum NC_Data_Dims_Type> NC_dim_types;
267
268 if (do_m3_clim_nudg) {
269 NC_fabs.push_back(&NC_M3NC_fab ); NC_names.push_back("M3_NudgeCoef"); NC_dim_types.push_back(NC_Data_Dims_Type::BT_SN_WE);
270 }
271 if (do_m2_clim_nudg) {
272 NC_fabs.push_back(&NC_M2NC_fab ); NC_names.push_back("M2_NudgeCoef"); NC_dim_types.push_back(NC_Data_Dims_Type::SN_WE);
273 }
274 if (do_temp_clim_nudg) {
275 NC_fabs.push_back(&NC_TempNC_fab ); NC_names.push_back("temp_NudgeCoef"); NC_dim_types.push_back(NC_Data_Dims_Type::BT_SN_WE);
276 }
277 if (do_salt_clim_nudg) {
278 NC_fabs.push_back(&NC_SaltNC_fab ); NC_names.push_back("salt_NudgeCoef"); NC_dim_types.push_back(NC_Data_Dims_Type::BT_SN_WE);
279 }
280 // Read the netcdf file and fill these FABs
281 BuildFABsFromNetCDFFile<FArrayBox,Real>(domain, fname, NC_names, NC_dim_types, NC_fabs);
282}
283
284/**
285 * @param lev level of data to read
286 * @param fnames file name(s) to read from
287 * @param field_name field name to read
288 * @param vec_dat vector to fill data
289 */
290//template <typename DType>
291void read_vec_from_netcdf (int /*lev*/, const amrex::Vector<std::string>& fnames, const std::string& field_name, amrex::Vector<int>& vec_dat)
292{
293 AMREX_ALWAYS_ASSERT(!fnames.empty());
294 const std::string& fname = fnames[0];
295
296 amrex::Print() << "Reading " << field_name << " from NetCDF file" << std::endl;
297
298 // get x-positions and put in array
299 using ARRAY = NDArray<int>;
300 amrex::Vector<ARRAY> array_dat(1);
301 ReadNetCDFFile(fname, {field_name}, array_dat); // filled only on proc 0
302 if (amrex::ParallelDescriptor::IOProcessor())
303 {
304 int n = array_dat[0].get_vshape()[0];
305 for (int i(0); i < n; i++)
306 {
307 vec_dat.push_back((*(array_dat[0].get_data() + i)));
308 }
309 }
310 int nvals = vec_dat.size();
311 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
312 amrex::ParallelDescriptor::Bcast(&nvals,1,ioproc);
313 if (!(amrex::ParallelDescriptor::IOProcessor())) {
314 vec_dat.resize(nvals);
315 }
316 amrex::ParallelDescriptor::Bcast(vec_dat.data(), vec_dat.size(), ioproc);
317}
318
319#endif // ROMSX_USE_NETCDF
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 read_clim_nudg_coeff_from_netcdf(int, const Box &domain, const std::string &fname, bool do_m2_clim_nudg, bool do_m3_clim_nudg, bool do_temp_clim_nudg, bool do_salt_clim_nudg, FArrayBox &NC_M2NC_fab, FArrayBox &NC_M3NC_fab, FArrayBox &NC_TempNC_fab, FArrayBox &NC_SaltNC_fab)
helper function to read climatology nudging from netcdf
void read_bathymetry_from_netcdf(int, const Box &domain, const std::string &fname, FArrayBox &NC_h_fab)
helper function to read bathymetry from netcdf
void read_vec_from_netcdf(int, const amrex::Vector< std::string > &fnames, const std::string &field_name, amrex::Vector< int > &vec_dat)
helper function to read in vector of data from netcdf
void read_grid_vars_from_netcdf(int, const Box &domain, const std::string &fname, FArrayBox &NC_pm_fab, FArrayBox &NC_pn_fab, FArrayBox &NC_xr_fab, FArrayBox &NC_yr_fab, FArrayBox &NC_xu_fab, FArrayBox &NC_yu_fab, FArrayBox &NC_xv_fab, FArrayBox &NC_yv_fab, FArrayBox &NC_xp_fab, FArrayBox &NC_yp_fab)
helper function to read grid variables from netcdf
void read_bathymetry_full_domain_from_netcdf(const Box &domain, const std::string &fname, FArrayBox &NC_h_fab, IntVect ngrow)
helper function to read full-domain high resolution bathymetry from netcdf
void read_coriolis_from_netcdf(int, const Box &domain, const std::string &fname, FArrayBox &NC_fcor_fab)
helper function to read coriolis factor from netcdf
void read_masks_from_netcdf(int, const Box &domain, const std::string &fname, FArrayBox &NC_mskr_fab, FArrayBox &NC_msku_fab, FArrayBox &NC_mskv_fab)
helper function for reading in land-sea masks from netcdf
void read_zeta_from_netcdf(int, const Box &domain, const std::string &fname, FArrayBox &NC_zeta_fab)
helper function to read sea surface height from netcdf
void read_data_from_netcdf(int, const Box &domain, const std::string &fname, FArrayBox &NC_temp_fab, FArrayBox &NC_salt_fab, FArrayBox &NC_xvel_fab, FArrayBox &NC_yvel_fab)
helper function for reading in initial state data from netcdf
void read_grid_vars_full_domain_from_netcdf(const Box &domain, const std::string &fname, FArrayBox &NC_pm_fab, FArrayBox &NC_pn_fab, IntVect ngrow)
helper function to read full-domain high resolution grid variables from netcdf
NDArray is the datatype designed to hold any data, including scalars, multidimensional arrays,...