writing time
[wrf-fire-matlab.git] / smoke / ts_smoke.py
blob03372863c5b15c47b622e9e329fb24a4467924e5
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 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)]
19 for i in frames:
20 print(i)
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")
42 # create dimensions
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)
53 # create variables
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)
59 # copy values
60 times_var[:]=times
61 lon_var[:]=lon
62 lat_var[:]=lat
63 pm25[:]=pm25
65 new.close()