我在打开
.nc
文件并将其转换为 .csv
文件时遇到问题,但我仍然无法读取它们(指第一部分)。我看到了 this 链接也看到了 this 链接,但我不知道如何打开它们。我编写了一段代码,但遇到了一个错误,我将在下面发布该错误。详细说明错误,它能够找到文件但无法打开它们。
#from netCDF4 import Dataset # use scipy instead
from scipy.io import netcdf #### <--- This is the library to import.
import os
# Open file in a netCDF reader
directory = './'
#wrf_file_name = directory+'filename'
wrf_file_name = [f for f in sorted(os.listdir('.')) if f.endswith('.nc')]
nc = netcdf.netcdf_file(wrf_file_name,'r')
#Look at the variables available
nc.variables
#Look at the dimensions
nc.dimensions
错误是:
Error: LAKE00000002-GloboLakes-L3S-LSWT-v4.0-fv01.0.nc is not a valid NetCDF 3 file
我发现https://docs.xarray.dev/en/stable/index.html是最好的工具 将 nc 文件直接读取到 pandas 数据框中。
import xarray as xr
import pandas as pd # only needed if you want to do pandas stuff
dataset = xr.open_dataset('<your_data>.nc')
然后,如果您想将其转储到 csv 文件,例如:
df.to_csv("<your_data>.csv")