@@ -1314,7 +1314,6 @@ def write(
13141314 ext_file_action = ExtFileAction .copy_relative_paths ,
13151315 netcdf = None ,
13161316 ):
1317- from ..version import __version__
13181317 """
13191318 Writes out model's package files.
13201319
@@ -1360,7 +1359,10 @@ def write(
13601359 if write_netcdf :
13611360 # set data storage to write ascii for netcdf
13621361 pp ._set_netcdf_storage ()
1363-
1362+ if pp .package_type .startswith ("dis" ):
1363+ crs = pp .crs .get_data ()
1364+ if crs is not None and self .modelgrid .crs is None :
1365+ self .modelgrid .crs = crs [0 ][1 ]
13641366 if (
13651367 self .simulation_data .verbosity_level .value
13661368 >= VerbosityLevel .normal .value
@@ -1373,35 +1375,11 @@ def write(
13731375
13741376 # write netcdf file
13751377 if write_netcdf and netcdf .lower () != "nofile" :
1376- mesh = netcdf
1377- if mesh .upper () == "STRUCTURED" :
1378- mesh = None
1379-
1380- ds = self .modelgrid .dataset (
1381- modeltime = self .modeltime ,
1382- mesh = mesh ,
1383- )
1384-
1385- nc_info = self .netcdf_info (mesh = mesh )
1386- nc_info ["attrs" ]["title" ] = f"{ self .name .upper ()} input"
1387- nc_info ["attrs" ]["source" ] = f"flopy { __version__ } "
1388- # :history = "first created 2025/8/21 9:46:2.909" ;
1389- # :Conventions = "CF-1.11 UGRID-1.0" ;
1390- ds = self .update_dataset (ds , netcdf_info = nc_info , mesh = mesh )
1391-
1392- # write dataset to netcdf
1393- fname = self .name_file .nc_filerecord .get_data ()[0 ][0 ]
1394- ds .to_netcdf (
1395- os .path .join (self .model_ws , fname ),
1396- format = "NETCDF4" ,
1397- engine = "netcdf4"
1398- )
1399-
1378+ self ._write_netcdf (mesh = netcdf )
14001379 if nc_fname is not None :
14011380 self .name_file .nc_filerecord = None
14021381
14031382
1404-
14051383 def get_grid_type (self ):
14061384 """
14071385 Return the type of grid used by model 'model_name' in simulation
@@ -2329,12 +2307,142 @@ def update_dataset(self, dataset, netcdf_info=None, mesh=None, update_data=True)
23292307 else :
23302308 nc_info = netcdf_info
23312309
2310+ if (
2311+ self .simulation .simulation_data .verbosity_level .value
2312+ >= VerbosityLevel .normal .value
2313+ ):
2314+ print (f" updating model dataset..." )
2315+
23322316 for a in nc_info ["attrs" ]:
23332317 dataset .attrs [a ] = nc_info ["attrs" ][a ]
23342318
23352319 # add all packages and update data
23362320 for p in self .packagelist :
23372321 # add package var to dataset
2322+ if (
2323+ self .simulation .simulation_data .verbosity_level .value
2324+ >= VerbosityLevel .normal .value
2325+ ):
2326+ print (f" updating dataset for package { p ._get_pname ()} ..." )
23382327 dataset = p .update_dataset (dataset , mesh = mesh , update_data = update_data )
23392328
23402329 return dataset
2330+
2331+ def _write_netcdf (self , mesh = None ):
2332+ import datetime
2333+
2334+ from ..version import __version__
2335+ if mesh is not None and mesh .upper () == "STRUCTURED" :
2336+ mesh = None
2337+
2338+ encode = {}
2339+ for pp in self .packagelist :
2340+ if pp .package_type == "ncf" :
2341+ encode ["shuffle" ] = pp .shuffle .get_data ()
2342+ encode ["deflate" ] = pp .deflate .get_data ()
2343+ encode ["chunk_time" ] = pp .chunk_time .get_data ()
2344+ encode ["chunk_face" ] = pp .chunk_face .get_data ()
2345+ encode ["chunk_x" ] = pp .chunk_x .get_data ()
2346+ encode ["chunk_y" ] = pp .chunk_y .get_data ()
2347+ encode ["chunk_z" ] = pp .chunk_z .get_data ()
2348+ wkt = pp .wkt .get_data ()
2349+ if wkt is not None :
2350+ wkt = wkt [0 ][1 ]
2351+ encode ["wkt" ] = wkt
2352+
2353+ if (
2354+ self .simulation .simulation_data .verbosity_level .value
2355+ >= VerbosityLevel .normal .value
2356+ ):
2357+ print (f" creating model dataset..." )
2358+
2359+ ds = self .modelgrid .dataset (
2360+ modeltime = self .modeltime ,
2361+ mesh = mesh ,
2362+ encoding = encode ,
2363+ )
2364+
2365+ dt = datetime .datetime .now ()
2366+ timestamp = dt .strftime ("%m/%d/%Y %H:%M:%S" )
2367+
2368+ nc_info = self .netcdf_info (mesh = mesh )
2369+ nc_info ["attrs" ]["title" ] = f"{ self .name .upper ()} input"
2370+ nc_info ["attrs" ]["source" ] = f"flopy { __version__ } "
2371+ nc_info ["attrs" ]["history" ] = f"first created { timestamp } "
2372+ if mesh is None :
2373+ nc_info ["attrs" ]["Conventions" ] = "CF-1.11"
2374+ elif mesh .upper () is "LAYERED" :
2375+ nc_info ["attrs" ]["Conventions" ] = "CF-1.11 UGRID-1.0"
2376+
2377+ ds = self .update_dataset (
2378+ ds ,
2379+ netcdf_info = nc_info ,
2380+ mesh = mesh ,
2381+ )
2382+
2383+ chunk = False
2384+ chunk_t = False
2385+ if mesh is None :
2386+ if (
2387+ "chunk_x" in encode
2388+ and encode ["chunk_x" ] is not None
2389+ and "chunk_y" in encode
2390+ and encode ["chunk_y" ] is not None
2391+ and "chunk_z" in encode
2392+ and encode ["chunk_z" ] is not None
2393+ ):
2394+ chunk = True
2395+ elif mesh .upper () == "LAYERED" :
2396+ if "chunk_face" in encode and encode ["chunk_face" ] is not None :
2397+ chunk = True
2398+ if "chunk_time" in encode and encode ["chunk_time" ] is not None :
2399+ chunk_t = True
2400+
2401+ base_encode = {}
2402+ if "deflate" in encode and encode ["deflate" ] is not None :
2403+ base_encode ["zlib" ] = True
2404+ base_encode ["complevel" ] = encode ["deflate" ]
2405+ if "shuffle" in encode and encode ["deflate" ] is not None :
2406+ base_encode ["shuffle" ] = True
2407+
2408+ encoding = {}
2409+ chunk_dims = {'time' , 'nmesh_face' , 'z' , 'y' , 'x' }
2410+ for varname , da in ds .data_vars .items ():
2411+ dims = ds .data_vars [varname ].dims
2412+ codes = dict (base_encode )
2413+ if (
2414+ not set (dims ).issubset (chunk_dims )
2415+ or not chunk or not chunk_t
2416+ ):
2417+ encoding [varname ] = codes
2418+ continue
2419+ chunksizes = []
2420+ if "time" in dims :
2421+ chunksizes .append (encode ["chunk_time" ])
2422+ if mesh is None :
2423+ if "z" in dims :
2424+ chunksizes .append (encode ["chunk_z" ])
2425+ if "y" in dims :
2426+ chunksizes .append (encode ["chunk_y" ])
2427+ if "x" in dims :
2428+ chunksizes .append (encode ["chunk_x" ])
2429+ elif mesh .upper () == "LAYERED" and "nmesh_face" in dims :
2430+ chunksizes .append (encode ["chunk_face" ])
2431+ if len (chunksizes ) > 0 :
2432+ codes ["chunksizes" ] = chunksizes
2433+ encoding [varname ] = codes
2434+
2435+ fname = self .name_file .nc_filerecord .get_data ()[0 ][0 ]
2436+
2437+ if (
2438+ self .simulation .simulation_data .verbosity_level .value
2439+ >= VerbosityLevel .normal .value
2440+ ):
2441+ print (f" writing NetCDF file { fname } ..." )
2442+ # write dataset to netcdf
2443+ ds .to_netcdf (
2444+ os .path .join (self .model_ws , fname ),
2445+ format = "NETCDF4" ,
2446+ engine = "netcdf4" ,
2447+ encoding = encoding ,
2448+ )
0 commit comments