簡體   English   中英

如何從 python 中的文件夾中讀取多個 NetCDF 文件

[英]How to read multiple NetCDF files from a folder in python

我正在嘗試制作多年平均溫度(1979-2014)的 plot,我遇到的唯一問題是嘗試從文件夾中讀取多個 NetCDF(.nc)文件。 目前我的程序將 plot 單個文件,但我不明白如何讓它讀取文件夾中的所有文件(每年一個)。 我想找到所有年份的平均值。 我遺漏了 plot 數據,因為這很好,我需要的唯一幫助是遍歷單個文件夾中的所有文件

import numpy as np
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
import matplotlib.pyplot as plt

q=Dataset('/Users/marjoryrogers/Desktop/July_Temp/MERRA300.prod.assim.tavgM_2d_rad_Nx.201407.SUB-4.nc','r',format='NETCDF4')
q.variables 

#jan_temp = q.variables['ts'] # units here, degrees
july_temp = q.variables['ts']

lats = q.variables['latitude']
lons = q.variables['longitude']

#jan_temp.shape
july_temp.shape

lats[:], lons[:]


#q=Dataset('Users/marjoryrogers/Desktop/Spring 2015/Hydroclimatology/MERRA301.prod.assim.tavgM_2d_rad_Nx.200805.SUB.nc', 'r', format='NETCDF4')

july_temp = q.variables['ts']
#jan_july = np.concatenate((may_temp, jun_temp), axis=0)
#jan_july.shape

#aver_temp = np.mean(jan_temp, axis=0)# average temperature
aver_temp2 = np.mean(july_temp, axis=0)

netcdf4-python庫有一個類 ,可以讀取多個netcdf文件,使具有記錄維度的變量顯示為單個大變量。

import netCDF4 as nc

# read multiple files (wildcard)
vn = nc.MFDataset('data_y*.nc') 

# read multiple files (file list)
vn = nc.MFDataset(['data_y1997','data_y1998','data_y1999'])

# Variable from multiple files.
airv = vn.variables['ts']

讀取通配符時出現錯誤

vn = nc.MFDataset('./Documents/STAR/gvf/GVF*.nc')

主數據集。/Documents/STAR/gvf/GVF-WKL-REG_v2r3_j01_s20220601_e20220607_c202206080857410.nc 沒有聚合維度如何解決

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM