1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
import netCDF4 as nc import numpy as np import os import time import multiprocessing as mp
def Gumble_func(data,N,length): if np.isnan(data).any(): return np.nan datasort = np.sort(data, axis=0)[::-1] data_select_sorts = datasort[0:length - 1] mu = np.mean(data_select_sorts) sum_mm = 0 for data_select_sort in data_select_sorts: sum_mm = sum_mm + (data_select_sort - mu) ** 2 sigma = np.sqrt(sum_mm / (len(data_select_sorts) - 1)) alpha = np.pi / (np.sqrt(6) * sigma) u = mu - 0.57721 / alpha for i in np.arange(1, 99999, 0.1): if 1 / N >= 1 - np.exp(-np.exp(-alpha * (i - u))): Recurrence_period_value = i break return Recurrence_period_value
def cal_N_year_appear_single(YEARLY_RAINFALL, i, j, N=10): rainfall_history = YEARLY_RAINFALL[:,i,j].flatten() if np.isnan(rainfall_history).any(): return (i,j,np.nan) return (i,j,Gumble_func(rainfall_history, N, rainfall_history.shape[0]))
def cal_N_year_appear(YEARLY_RAINFALL, N=10): yearly_rainfall_N = np.empty(YEARLY_RAINFALL.shape[1:])
idx = np.where(~np.isnan(YEARLY_RAINFALL[0,:,:])) print('Number of non-nan elements: ', len(idx[0])) with mp.Pool(processes=6) as pool: results = pool.starmap(cal_N_year_appear_single, [(YEARLY_RAINFALL, i, j, N) for i, j in zip(idx[0], idx[1])]) pool.close() for i, j, result in results: yearly_rainfall_N[i,j] = result return yearly_rainfall_N
def main(): dir = 'nc_crop/'
fileList = os.listdir(dir)
print('Files in directory: ', len(fileList))
yearly_rainfall_sum = []
src = None
for file in fileList[:3]: print('Processing file: ', file) data = nc.Dataset(dir + file) print('Variables in file: ', data.variables) print('Dimensions in file: ', data.dimensions) print('Shape of variable: ', data.variables['time'].shape) print('Data type of variable: ', data.variables['time'].dtype) print('Values of variable: ', data.variables['time'][:]) pre = np.array(data.variables['pre'][:]) print('Shape of pre: ', pre.shape) year = int(file.split('_')[-1].split('.')[0]) print('Year: ', year) monthly_days = [31, 28 if year % 4 != 0 else 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] pre_sum = np.dot(pre.transpose((1,2,0)), monthly_days) print('Shape of pre_sum: ', pre_sum.shape) yearly_rainfall_sum.append(pre_sum) print('-----------------------------------') src = data
yearly_rainfall_sum = np.array(yearly_rainfall_sum)
print('Shape of yearly_rainfall_sum: ', yearly_rainfall_sum.shape) yearly_rainfall_sum_mean = np.mean(yearly_rainfall_sum, axis=0) print('Shape of yearly_rainfall_sum_mean: ', yearly_rainfall_sum_mean.shape)
print('开始计算10年一遇降雨量...') yearly_rainfall_10 = cal_N_year_appear(yearly_rainfall_sum, 10) print('重现期为10年的平均降水量: ', yearly_rainfall_10.mean()) print('开始计算20年一遇降雨量...') yearly_rainfall_20 = cal_N_year_appear(yearly_rainfall_sum, 20) print('重现期为20年的平均降水量: ', yearly_rainfall_20.mean()) print('开始计算50年一遇降雨量...') yearly_rainfall_50 = cal_N_year_appear(yearly_rainfall_sum, 50) print('重现期为50年的平均降水量: ', yearly_rainfall_50.mean()) print('开始计算100年一遇降雨量...') yearly_rainfall_100 = cal_N_year_appear(yearly_rainfall_sum, 100) print('重现期为100年的平均降水量: ', yearly_rainfall_100.mean())
out = nc.Dataset('yearly_rainfall_%s.nc'%time.time(), 'w', format='NETCDF4') out.createDimension('latitude', src.dimensions['latitude'].size, ) out.createDimension('longitude', src.dimensions['longitude'].size) lat=out.createVariable('latitude', 'f4', ('latitude',)) out.variables['latitude'][:] = src.variables['latitude'][:] long=out.createVariable('longitude', 'f4', ('longitude',)) out.variables['longitude'][:] = src.variables['longitude'][:] lat.units = 'degreesN' long.units = 'degreesE' out.createVariable('yearly_rainfall_sum', 'f8', ('longitude','latitude')) out.variables['yearly_rainfall_sum'][:] = yearly_rainfall_sum_mean out.createVariable('yearly_rainfall_10', 'f8', ('longitude','latitude')) out.variables['yearly_rainfall_10'][:] = yearly_rainfall_10 out.createVariable('yearly_rainfall_20', 'f8', ('longitude','latitude')) out.variables['yearly_rainfall_20'][:] = yearly_rainfall_20 out.createVariable('yearly_rainfall_50', 'f8', ('longitude','latitude')) out.variables['yearly_rainfall_50'][:] = yearly_rainfall_50 out.createVariable('yearly_rainfall_100', 'f8', ('longitude','latitude')) out.variables['yearly_rainfall_100'][:] = yearly_rainfall_100 out.close() print('File written successfully.')
if __name__ == '__main__': print('Start processing...') main() print('Processing finished.')
|