[31]:
#Read netCDF file from wrf model.
#There are two netCDF format files (wrfout1 and wrfout2).
#The wrfpython is an useful tool for get variables in netCDF file derived from wrf model.
import netCDF4 as nc4
from wrf import getvar, ALL_TIMES
#Read single file and get 2-m Temperature
wrfout_1 = nc4.Dataset("wrfout1")
wrfout_2 = nc4.Dataset("wrfout2")
allvar = [ eachvar for eachvar in wrfout_1.variables ]
print(allvar)
#GET T2 use netCDF4 self and wrfpython
wrfout_1_T2_netCDF4 = wrfout_1["T2"][0]
print(wrfout_1_T2_netCDF4.shape)
wrfout_1_T2_wrfpython = getvar(wrfout_1,"T2")
print(wrfout_1_T2_wrfpython.shape)
diff = wrfout_1_T2_netCDF4-wrfout_1_T2_wrfpython
print(diff.sum())
#IF we want to get 10m wind speed
wrfout_1_u10_netCDF4 = wrfout_1["U10"][0]
wrfout_1_v10_netCDF4 = wrfout_1["V10"][0]
wrfout_1_10mws_netCDF4 = (wrfout_1_u10_netCDF4**2 + wrfout_1_v10_netCDF4**2)**0.5
wrfout_1_10mws_wrfpython = getvar(wrfout_1,"wspd10")
print()
#Read all files one time
wrflist = [wrfout_1, wrfout_2]
wrfout_T2 = getvar(wrflist,"T2", timeidx=ALL_TIMES,method="cat")
print((wrfout_T2[0] - wrfout_1_T2).data.sum(), (wrfout_T2[1] - wrfout_2_T2).data.sum())
['Times', 'XLAT', 'XLONG', 'LU_INDEX', 'ZNU', 'ZNW', 'ZS', 'DZS', 'VAR_SSO', 'U', 'V', 'W', 'PH', 'PHB', 'T', 'HFX_FORCE', 'LH_FORCE', 'TSK_FORCE', 'HFX_FORCE_TEND', 'LH_FORCE_TEND', 'TSK_FORCE_TEND', 'MU', 'MUB', 'NEST_POS', 'P', 'PB', 'FNM', 'FNP', 'RDNW', 'RDN', 'DNW', 'DN', 'CFN', 'CFN1', 'THIS_IS_AN_IDEAL_RUN', 'P_HYD', 'Q2', 'T2', 'TH2', 'PSFC', 'U10', 'V10', 'RDX', 'RDY', 'RESM', 'ZETATOP', 'CF1', 'CF2', 'CF3', 'ITIMESTEP', 'XTIME', 'QVAPOR', 'QCLOUD', 'QRAIN', 'QICE', 'QSNOW', 'QGRAUP', 'TSLB', 'SMOIS', 'SH2O', 'SMCREL', 'SEAICE', 'XICEM', 'SFROFF', 'UDROFF', 'IVGTYP', 'ISLTYP', 'VEGFRA', 'GRDFLX', 'ACGRDFLX', 'SNOW', 'SNOWH', 'CANWAT', 'SSTSK', 'LAI', 'VAR', 'MAPFAC_M', 'MAPFAC_U', 'MAPFAC_V', 'MAPFAC_MX', 'MAPFAC_MY', 'MAPFAC_UX', 'MAPFAC_UY', 'MAPFAC_VX', 'MF_VX_INV', 'MAPFAC_VY', 'F', 'HGT', 'TSK', 'P_TOP', 'T00', 'P00', 'TLP', 'TISO', 'TLP_STRAT', 'P_STRAT', 'RAINC', 'RAINSH', 'RAINNC', 'CLDFRA', 'SWDOWN', 'GSW', 'GLW', 'SWNORM', 'OLR', 'XLAT_U', 'XLONG_U', 'XLAT_V', 'XLONG_V', 'ALBEDO', 'ALBBCK', 'EMISS', 'NOAHRES', 'TMN', 'XLAND', 'UST', 'PBLH', 'HFX', 'QFX', 'LH', 'ACHFX', 'ACLHF', 'SNOWC', 'SR', 'SAVE_TOPO_FROM_REAL', 'LANDMASK', 'SST']
(450, 450)
(450, 450)
0.0
0.0001241928
0.0 0.0
[4]:
#Read netCDF file from wrf model.
#There are two netCDF format files (wrfout1 and wrfout2).
#However, the data read read by xarray can not apply to wrfpython.
#Thus, some variables need to be calculated can not derive.
import xarray as xr
wrfout_1 = xr.open_dataset("wrfout1")
wrfout_2 = xr.open_dataset("wrfout2")
[18]:
#Read netCDF file from wrf model.
#There are two netCDF format files (wrfout1 and wrfout2).
#However, the data read read by xarray can not apply to wrfpython.
#Thus, some variables need to be calculated can not derive.
from scipy.io import netcdf_file
wrfout_1 = netcdf_file("wrfout1","r")
wrfout_2 = netcdf_file("wrfout2","r")
wrfout_1_T2 = getvar(wrfout_1,"T2")
wrfout_1_T2
wrfout_1.close()
wrfout_2.close()