【发布时间】:2019-08-02 05:12:41
【问题描述】:
当我尝试使用 xarray 导入 netCDF4 文件时,出现以下错误:
MissingDimensionsError: 'name' 有多个维度且名称与其维度之一相同('time'、'name')。 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
【问题讨论】:
-
我认为这不太可能在 xarray 中工作。正如您收到的错误所示,您有一个与二维坐标共享名称的维度。虽然 netCDF 支持这一点,但 xarray 不支持。最简单的解决方法可能是在使用 xarray 打开之前重命名 netcdf 文件中的维度(或坐标)。
-
我担心是这样。那是我读到的,但我希望有一个解决方法。唯一的问题是我有 383 个这些文件,而且我不知道如何快速重命名它们。当您对 383 个文件(大约 3 小时)进行 20 秒的读取时,补偿时间会变得很昂贵。
标签: python python-xarray netcdf4