我想要将已有的nc格式文件按照经纬度,每个格点分为一个csv文件。
数据格式为:(其中ncl0为time,ncl1为y坐标,ncl2为x坐标)
netcdf SICflt { dimensions: ncl0 = 511 ; ncl1 = 448 ; ncl2 = 304 ; variables: float SIC(ncl0, ncl1, ncl2) ; SIC:_FillValue = -1.f ; } ==================================================================== 程序为: import netCDF4 as nc
import pandas as pd
import numpy as np
import xarray as xr
filename = '/Users/xhh/Downloads/SIC/NHSICmonthly/SICflt.nc'
f = nc.Dataset(filename)
SIC = f.variables['SIC']
x = f.dimensions['ncl1']
y = f.dimensions['ncl2']
time = f.dimensions['ncl0']
for i in range(len(x)):
for j in range(len(y)):
SICxy = SIC[time,y,x]
dataframe = pd.DataFrame({'time':[time],'SIC':[SICxy]})
dataframe.to_csv('SIC_{}_{}.csv'.format(x, y))
================================================================ 报错内容是: Traceback (most recent call last): File "/Users/xhh/opt/anaconda3/lib/python3.8/site-packages/netCDF4/utils.py", line 295, in _StartCountStride e = int(e) TypeError: int() argument must be a string, a bytes-like object or a number, not 'netCDF4._netCDF4.Dimension'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/Users/xhh/Downloads/NHSICmonthly/SICgrid.py", line 17, in <module> SICxy = SIC[time,y,x] File "netCDF4/_netCDF4.pyx", line 4384, in netCDF4._netCDF4.Variable.__getitem__ File "/Users/xhh/opt/anaconda3/lib/python3.8/site-packages/netCDF4/utils.py", line 298, in _StartCountStride raise IndexError(IndexErrorMsg)
IndexError: only integers, slices (`:`), ellipsis (`...`), and 1-d integer or boolean arrays are valid indices ===================================================================== 我在netcdf的使用手册上看到了 variable[d1,d2,d3]的写法,请问问题在哪里?
|