python气象学习笔记(一):处理逐日.nc数据(包括合并,重新写入,读取,画图)

一、打开.nc文件

import xarray as xr
import os
from netCDF4 import Dataset
import netCDF4 as nc
import numpy as np

这里加载的库有:xarray、os、netCDF4、numpy

file_paths = r'D:\NOAA_file2023\01'
#写入.nc文件路径

数据为31天的逐日降雨数据,文件格式为.nc

file_path = [] ##定义一个空列表来存储该文件里的.nc数据路径
for file_name in os.listdir(file_paths):
    file_path.append(r'D:/NOAA_file2023/01/'+file_name)
file_path ##输出文件路径

输出结果:
[‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230101.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230102.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230103.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230104.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230105.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230106.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230107.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230108.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230109.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230110.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230111.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230112.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230113.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230114.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230115.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230116.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230117.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230118.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230119.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230120.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230121.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230122.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230123.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230124.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230125.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230126.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230127.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230128.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230129.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230130.nc’,
‘D:/NOAA_file2023/01/CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_20230131.nc’]

二、合并.nc文件

cmorph_new = [] ##建立一个空列表,存储逐日降雨数据
for i in range(len(file_path)):
    cmorph = xr.open_dataset(file_path[i])['cmorph']
    cmorph_new.append(cmorph) ##存储每日的降雨数据
da = xr.concat(cmorph_new,dim='time') ##将数据以时间维度来进行拼接
#print(da)
path_new = r"D:/nc_file/" ##设置新路径
#print(da.variable)
da.to_netcdf(path_new + 'CMORPH_6.nc') ##对拼接好的文件进行存储
da.close() ##关闭文件

三、打开合并文件,进行绘图

import netCDF4 as nc
import matplotlib.pyplot as plt

导入绘图库matplotlib

path_file_new = r'D:\nc_file\CMORPH_6.nc' ##写入文件路径
data_new = nc.Dataset(path_file_new) ##打开.nc文件
#data_new.variables['cmorph'][0]表示第一天的降雨量,同理,[10]表示第10天的降雨量
##设置12×8,分辨率为800的画布
plt.figure(figsize=(12, 8),dpi = 800)

##将前4天的降雨分布情况进行绘制
plt.subplot(2,2,1)
plt.contourf(data_new.variables['cmorph'][0])
plt.colorbar(label='cmorph',orientation="horizontal")

plt.subplot(2,2,2)
plt.contourf(data_new.variables['cmorph'][1])
plt.colorbar(label='cmorph',orientation="horizontal")

plt.subplot(2,2,3)
plt.contourf(data_new.variables['cmorph'][2])
plt.colorbar(label='cmorph',orientation="horizontal")

plt.subplot(2,2,4)
plt.contourf(data_new.variables['cmorph'][3])
plt.colorbar(label='cmorph',orientation="horizontal")

plt.show()

作者:剑拭锋芒

物联沃分享整理
物联沃-IOTWORD物联网 » python气象学习笔记(一):处理逐日.nc数据(包括合并,重新写入,读取,画图)

发表回复