当我尝试使用xarray导入netCDF4文件时,出现以下错误:
MissingDimensionsError:“名称”具有1个以上的维度,并且名称与其维度之一(“时间”,“名称”)相同。 xarray不允许使用此类变量,因为它们与用于标注尺寸的坐标相冲突。
但是,我可以使用netCDF4 python库成功导入这些数据,并从中获取所需的数据。问题在于该方法非常慢,因此我一直在寻找更快的方法并想尝试xarray。 Here is an example file,以及使我遇到问题的代码。
from netCDF4 import Dataset
#import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#import seaborn as sns
from tkinter import Tk
from tkinter.filedialog import askdirectory
import os
import xarray as xr
#use this function to get a directory name where the files are
def get_dat():
root = Tk()
root.withdraw()
root.focus_force()
root.attributes("-topmost", True) #makes the dialog appear on top
filename = askdirectory() # Open single file
root.destroy()
root.quit()
return filename
directory=get_dat()
#loop through files in directory and read the netCDF4 files
for filename in os.listdir(directory): #loop through files in user's dir
if filename.endswith(".nc"): #all my files are .nc not .nc4
runstart=pd.datetime.now()
#I get the error right here
rootgrp3 = xr.open_dataset(directory+'/'+filename)
#more stuff happens here with the data, but this stuff works