3 from numpy
import concatenate
as cat
11 argv
=['wrfout.nc','ts_smoke.nc']
14 for file_path
in argv
[0:-1]:
15 print('Reading NetCDF file',file_path
)
16 d
= nc4
.Dataset(file_path
,'r')
17 # extract ESMF string times
18 frames
= [''.join(x
) for x
in d
.variables
['Times'][:].astype(str)]
21 times_vars
+= [d
.variables
["Times"][:,:]]
22 pm25_vars
+= [d
.variables
["tr17_1"][:,0,:,:]]
23 lon_vars
+= [d
.variables
["XLONG"][:,:,:]]
24 lat_vars
+= [d
.variables
["XLAT"][:,:,:]]
26 # take the last metadata; should check if same except dim_size_time[0]
27 dtype_times
=d
.variables
["Times"].dtype
28 dtype_vars
=d
.variables
["XLONG"].dtype
29 dim_size_time
= d
.variables
["Times"][:,:].shape
30 dim_name_vars
= d
.variables
["XLONG"].dimensions
31 dim_size_vars
= d
.variables
["XLONG"][:,:,:].shape
33 # concatenate variables
34 times
= cat(times_vars
,0); del times_vars
35 pm25
= cat(pm25_vars
,0); del pm25_vars
36 lon
= cat(lon_vars
,0); del lon_vars
37 lat
= cat(lat_vars
,0); del lat_vars
39 print('Writing NetCDF file',new_path
)
40 new
= nc4
.Dataset(new_path
, mode
="w")
43 new
.createDimension('Time',None)
44 for dim_name
, dim
in d
.dimensions
.items():
45 if dim_name
in ['south_north', 'west_east','DateStrLen']:
46 new
.createDimension(dim_name
, len(dim
))
47 time_dim
= new
.dimensions
['Time']
48 tstr_dim
= new
.dimensions
['DateStrLen']
49 sn_dim
= new
.dimensions
['south_north']
50 we_dim
= new
.dimensions
['west_east']
51 var_dimensions
= (time_dim
, sn_dim
, we_dim
)
54 times_var
= new
.createVariable('Times', dtype_times
, (time_dim
, tstr_dim
))
55 lon_var
= new
.createVariable('XLONG', dtype_vars
, var_dimensions
)
56 lat_var
= new
.createVariable('XLAT', dtype_vars
, var_dimensions
)
57 pm25_var
= new
.createVariable('pm25', dtype_vars
, var_dimensions
)