我想使用python xarray 'open_mfdataset'连接目录中的50个netCDF文件。它只适用于一小部分netCDF文件,但不能连接全部50个文件。在脚本“ demo.py”中从“ toolbox.py”调用函数时出现此错误:
文件“”,第1行,在 runfile('data_dir / demo.py',wdir ='')
文件 “ anaconda3 / lib / python3.6 / site-packages / spyder_kernels / customize / spydercustomize.py”, 运行文件中的第827行 execfile(文件名,命名空间)
文件 “ anaconda3 / lib / python3.6 / site-packages / spyder_kernels / customize / spydercustomize.py”, 第110行,在execfile中 exec(compile(f.read(),文件名,'exec'),命名空间)
文件“ data_dir / demo.py”,第15行,在 data_files = concat(data_dir,chunks,file_name)
文件“ Python / toolbox.py”,第53行,在 康卡特 data_vars ='minimal',parallel = False).chunk(块)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / backends / api.py”, open_mfdataset中的第719行 ids = ids)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / combine.py”, _auto_combine中的第553行 data_vars = data_vars,coords = coords)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / combine.py”, _combine_nd中的第475行 compat = compat)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / combine.py”, _auto_combine_all_along_first_dim中的第493行 data_vars,坐标)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / combine.py”, _auto_combine_1d中的第514行 merged = merge(concatenated,compat = compat)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / merge.py”, 合并中的第532行 变量,coord_names,dims = merge_core(dict_like_objects,compat,join)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / merge.py”, 第445行,在merge_core中 aligned = deep_align(强制,join = join,copy = False,indexs =索引)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / alignment.py”, 第217行,在deep_align中 exclude = exclude)
文件 “ anaconda3 / lib / python3.6 / site-packages / xarray / core / alignment.py”, 第133行,对齐 索引=细木工(matching_indexes)
文件 “ anaconda3 / lib / python3.6 / site-packages / pandas / core / indexes / base.py”, 第2223行,位于或 返回self.union(other)
文件 “ anaconda3 / lib / python3.6 / site-packages / pandas / core / indexes / datetimes.py”, 493行,在工会中 结果= Index.union(this,other)
文件 “ anaconda3 / lib / python3.6 / site-packages / pandas / core / indexes / base.py”, 2332行,在工会中 indexer = self.get_indexer(other)
文件 “ anaconda3 / lib / python3.6 / site-packages / pandas / core / indexes / base.py”, 第2740行,在get_indexer中 引发InvalidIndexError('重新索引仅在唯一时有效'
InvalidIndexError:重新索引仅对唯一值索引有效 对象
我尝试查看单个netCDF文件,并尝试了不同选择的netCDF文件进行串联。我认为可能是因为有多个相同的索引对象吗?但我不太了解。
def concat(data_dir,chunks,file_name):
# data_dir = directory where IMOS glider .nc files exist
# chunks = output variable 'chunks' from function 'find_chunks' - this is a single value
# file_name = specify string for name of file (not including extension e.g. '.zarr')
# concatenate IMOS glider netCDF files in data_dir
data_files = xr.open_mfdataset(data_dir + '*.nc', concat_dim='TIME',data_vars='minimal',parallel=False).chunk(chunks)
print('Data concatenated..')
# I then save the concatenated dataset as a netCDF and .zarr. This only works for smaller slections of netCDF files.
return data_files
我希望可以从函数中返回并保存为netCDF和.zarr文件的级联数据集。