将多个月的nc数据文件合成一个(月平均)

将多个月的nc数据文件合成一个(月平均)

最近在处理nc数据,希望可以获得多年的月平均数据。但是ERA这个产品从网站上下载到的是每个月的日数据,所以我希望可以把他合成一个,以每个月月平均数据存储的nc文件。

首先是引入要用的库:

from netCDF4 import Dataset   #nc库功能不用说,读nc文件肯定要用
import numpy as np
from pandas import Series
import netCDF4 as nc
import tkinter as tk
from tkinter import filedialog #这两个是用来读取文件夹的

定义一个nc文件类:

class ncfile:
    def __init__(self,file_name):
        #用于读取数据
        NCfile=nc.Dataset(file_name)
        #获取维度的值,一般有时间、经纬度、各个数值
        self.time=NCfile.variables['time'][:].data
        self.latitude=NCfile.variables['latitude'][:].data
        self.longitude=NCfile.variables['longitude'][:].data
        self.e=NCfile.variables['e'][:].data
        self.sro=NCfile.variables['sro'][:].data
        self.ssro=NCfile.variables['ssro'][:].data
        self.tp=NCfile.variables['tp'][:].data       

定义几个要用的全局变量:

n=0 #这个用来记录读入的文件数
'''
下面是用来存储原始数据的,其实像time、latitude、longitude这种是不需要单独
记录的,只是为了在后面偷个懒,就顺便打上了,但是这里其实占用了一部分内存,不太好
'''
times=[]
latitudes=[]
longitudes=[]
es=[]
sros=[]
ssros=[]
tps=[] 

#下面的是用来存储最后要写入的数据
times_after=[]
latitudes_after=[]
longitudes_after=[]
es_after=[]
sros_after=[]
ssros_after=[]
tps_after=[] 

然后定义读入文件的函数:

def read_files():
    #读取存放要合并文件的文件夹
    root=tk.Tk()
    root.withdraw()
    Folderpath=filedialog.askdirectory()    
    filelist = os.listdir(Folderpath)
    
    global n
    n=len(filelist)
    
    for i in range(n):
        temp=ncfile(str(Folderpath)+"/"+filelist[i])
        times.append(temp.time)
        latitudes.append(temp.latitude)
        longitudes.append(temp.longitude)
        es.append(temp.e)
        sros.append(temp.sro)
        ssros.append(temp.ssro)
        tps.append(temp.tp)

处理数据,得到每个月月平均值的矩阵:

read_files()
for i in range(n):
    tps_after.append(np.mean(tps[i],axis=0))
    es_after.append(np.mean(es[i],axis=0))
    sros_after.append(np.mean(sros[i],axis=0))
    ssros_after.append(np.mean(ssros[i],axis=0))

最后创建新的nc文件并写入数据:

#写入部分
#创建新文件
new_NC = nc.Dataset("test-result.nc", 'w', format='NETCDF4')

'''
定义维度,后一个参数表示维度的长度,因为是合并的同一个产品的数据,所以是统一
的,注意维度的长度一定要和读入的数据匹配
'''
new_NC.createDimension('time', n)
new_NC.createDimension('latitude', len(latitudes[0]))
new_NC.createDimension('longitude', len(longitudes[0]))
new_NC.createDimension('e', len(es[0]))
new_NC.createDimension('sro', len(sros[0]))
new_NC.createDimension('ssro', len(ssros[0]))
new_NC.createDimension('tp', len(tps[0]))

#定义变量,这里需要规定变量的类型,以及限制它的维度
#可以看到,四个与数据相关的变量,其由另外三个基本维度约束
new_NC.createVariable('time', 'u4',("time"))
new_NC.createVariable('latitude', 'f', ("latitude"))
new_NC.createVariable('longitude', 'f', ("longitude"))
new_NC.createVariable('e', 'f', ("time","latitude","longitude"))
new_NC.createVariable('sro', 'f',("time","latitude","longitude"))
new_NC.createVariable('ssro', 'f',("time","latitude","longitude"))
new_NC.createVariable('tp', 'f',("time","latitude","longitude"))

#向变量中填充数据
new_NC.variables['latitude'][:] = latitudes[0]
new_NC.variables['longitude'][:] = longitudes[0]

new_NC.variables['e'][:]=np.array(es_after)
new_NC.variables['sro'][:]=np.array(sros_after)
new_NC.variables['ssro'][:]=np.array(ssros_after)
new_NC.variables['tp'][:]=np.array(tps_after)

#最后记得关闭文件
new_NC.close()

新人,还在学习和探索中,有问题请各位大佬指正。

上一篇:thinkphp 获取两坐标之间的距离


下一篇:基于dotspatial的拓扑检查