Merge branch 'fixf'
[wrf-fire-matlab.git] / smoke / ts_smoke.py
blob141f478856e0673e18cd1213ff52412c197a84cd
1 import netCDF4 as nc4
2 import numpy as np
3 from numpy import concatenate as cat
4 import sys, pickle
6 times_vars = []
7 pm25_vars = []
8 lon_vars = []
9 lat_vars = []
10 argv = sys.argv
11 #argv=['wrfout.nc','ts_smoke.nc']
12 new_path = argv[-1]
14 nframes = 0
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)]
20 for i in frames:
21 nframes += 1
22 print(nframes,i)
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")
46 # create dimensions
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)
59 # create variables
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)
65 # copy values
66 times_var[:]=times
67 lon_var[:]=lon
68 lat_var[:]=lat
69 pm25_var[:]=pm25
71 new.close()