爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 3695|回复: 1

【Py】将nc数据按经纬度分开程序报错

[复制链接]

新浪微博达人勋

发表于 2022-1-24 14:42:06 | 显示全部楼层 |阅读模式
5金钱
我想要将已有的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]的写法,请问问题在哪里?

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2022-1-24 17:34:55 | 显示全部楼层
你没发现你的i和j都没在循环里用到吗
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表