惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
有赞技术团队
有赞技术团队
量子位
N
Netflix TechBlog - Medium
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
V2EX
IT之家
IT之家
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

遐说

2026第一场日出日落-黄山 [徒步]宁波九龙爱心线 [徒步]仙居公盂徒步+丽人谷溯溪(多图) 文献引用批量标注蓝色-Zotero/EndNote [出差]内蒙草原风光 [徒步]浙东小九寨 [徒步]西湖小猫线 微软Office365家庭版拼车5年180 2024浏阳跨年烟火 一路向北-广西 落日余晖——武汉东湖 博士论文盲审通过 青海出差 我开盒了我自己 hexo-butterfly+artalk无法显示文章PV浏览次数 记某站字体混淆解析方案 记第一次进藏 2024川西大环线 2024武汉东湖樱花园 zotero批量修改条目语言解决”等”和”et al”混排问题 torch_geometric_temporal安装报错找不到to_dense_adj 武汉冻雨&乡村春节 云南镇雄县凉水村滑坡:卫星影像解析 博客聚合平台RSS订阅-BlogFinder&博友圈&十年之约 腾讯云CDN正式取消免费10G流量包 2023跨年闲逛武汉 观感|小米SU7技术发布会 【留痕】解密微信聊天记录工具:年度报告、聊天导出一键搞定! 小米电脑管家 vs. MIUI+: 手机与PC协同体验大比拼! Waline私有化部署(Docker+VPS)
Python读取NetCDF文件-裁剪&计算
Dorad · 2024-05-12 · via 遐说

依据前 30 年的年降雨数据,按 Gumbel 分布估算 10 年一遇、20 年一遇、50 年一遇和 100 年一遇的极端降雨工况下的降雨分布,并生成 nc 文件。

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.')