import numpy as np
import h5py
from scipy import interpolate
import netCDF4 as nc
import pandas as pd
import pylab as pl
import matplotlib as mpl
string='GFED4.1s_2015.hdf5'
f = h5py.File(string, 'r')
basis_regions = f['/ancill/basis_regions'][:]
grid_area = f['/ancill/grid_cell_area'][:]
lat=f['lat'][:]
lon=f['lon'][:]
lon_new=np.linspace(-180+0.25/3/2,180-0.25/3/2,1440*3)
lat_new=np.linspace(90-0.25/3/2,-90+0.25/3/2,720*3)
grid_area_newfunc = interpolate.interp2d(lon[0,:], lat[:,0], grid_area , kind='linear')
grid_area_new = grid_area_newfunc(lon_new ,lat_new)
grid_area_new
basis_regions_newfunc = interpolate.interp2d(lon[0,:], lat[:,0], basis_regions , kind='linear')
basis_regions_new = basis_regions_newfunc(lon_new ,lat_new)
basis_regions_new = np.fix(basis_regions_new)
basis_regions_new =np.flipud(basis_regions_new)
file = 'KK10.nc'
dataset = nc.Dataset(file)
time=7901
mask_values=14
data_frame=np.ones((time,mask_values))
data_frame=pd.DataFrame(data_frame)
for t in range(time):
print(t+1)
land_use_frac = dataset.variables['land_use'][t,:,:]
land_use_frac =np.flipud(land_use_frac)
for mask_val in [1]:
mask=(basis_regions_new==mask_val)
print(np.sum(land_use_frac*grid_area_new/9.0*mask))
data_frame.iloc[t,mask_val-1]=np.sum(land_use_frac*grid_area_new/9.0*mask)
data_frame.to_csv('land_use.csv')
|