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