將多個月的nc資料檔案合併成一個(月平均)
阿新 • • 發佈:2021-02-05
將多個月的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()
新人,還在學習和探索中,有問題請各位大佬指正。