Decorative image frame

ZSYXY Meteorological workshop

摸鱼今天只想摸鱼

ZSYXY Meteorological workshop

Use CDO in Python

在Python中调用CDO批量处理文件(非cdo-python库)。

实际上可以利用CDO的chain方式连锁处理,代码会更简洁。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os
#批量文件路径
filePath = '/CMIP6/hist/ta/CESM2/r1i1p1f1/'
#获取文件名
filename = os.listdir(filePath)
#将全部文件进行筛选,进保留文件名以ta开头的文件
filename = list(filter(lambda x:x.startswith('ta'), filename))
#对文件循环,注意每条语句的输入和输出,根据需要修改和开启/关闭指令
for i in range(len(filename)):
# os.system('cdo sellevel,50000 {}{} {}tmp1.nc'.format(filePath,filename[i],filePath))
os.system('cdo remapbil,r144x73 {}{} {}no{}.nc'.format(filePath,filename[i],filePath,i))
# os.system('cdo sellonlatbox,-180,180,0,90 {}tmp2.nc {}no{}.nc'.format(filePath,filePath,i))
# os.system('cdo delete,month=2,day=29 {}tmp3.nc {}no{}.nc'.format(filePath,filePath,i))
#删掉中间文件
os.system('rm -f {}tmp*'.format(filePath))
#合并
os.system('cdo -b 32 mergetime {}no* {}ta.nc'.format(filePath,filePath))
#删掉中间文件
os.system('rm -f {}no*'.format(filePath))

可使用的CDO指令可以参考:

Bash for Download

利用Bash脚本下载资料(默认用于NOAA网站相关资料集)。使用时修改https内容。格式化函数${i}使用方法类似Python语言的format函数。

1
2
3
4
for (( i = 1948; i < 2023; i++))
do
wget --no-check-certificate https://downloads.psl.noaa.gov/Datasets/ncep.reanalysis.dailyavgs/surface_gauss/air.2m.gauss.${i}.nc
done