在python中使用Netcdf4从Netcdf文件检索float32时要浮动的ValueError字符串

时间:2015-03-04 23:25:57

标签: python numpy netcdf

我在windows7机器上使用python 2.7中的netcdf4。我已将numpy recarrays加载到我创建的netcdf文件中,并随后多次检索数据。然后,由于某些未知原因,当我尝试检索数据时,我得到一个ValueError无法将字符串转换为float:

用于检索数据的代码是:

def getNetCDFGroupVarData(NCfilename, GroupPath, Variable):
"""  ==============================================================
TITLE:      getNetCDFGroupVarData    

DESCR:      for a valid variable on the specified path in a NetCDF file
            returns a data vector 

ARGS:       NCfilename : netcdf4 file path and name
            GroupPath : group path 
            Variable : variable name 

RETURN:     VarData: vector of variable data

DEPEND:     netCDF4.Dataset     

=======================================================================  
"""
# get rootgroup and group from which to return attributes
if os.path.isfile(NCfilename):
    RG = Dataset(NCfilename, 'a')
    G = giveListEndGroup(RG,GroupPath)

    # retrieve variable data from group
    keyVar = G.variables.keys()
    print(keyVar)
    kvlen = len(keyVar)
    var = unicode(Variable) 
    if kvlen > 0 :
        print('variable name: ',var)
        V =  G.variables[var]
        print V.dtype
        print V.shape
        print V.dimensions
        VarData = V[:]          #====== Error raised here ==============
    else:
        print('no keys found')
        VarData = None

    RG.close()
    return VarData

调用此函数时得到的打印输出和错误堆栈是:

[u'time', u'SECONDS', u'NANOSECONDS', u'Rg', u'Ts1', u'Ts2', u'Ts3', u'V_log', u'T_log']
('variable name: ', u'time')
float64
(88872,)
(u'time',)
variable:  time  does not exist
Unexpected error: <type 'exceptions.ValueError'>
Traceback (most recent call last):
  File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\Panel_NCTS_structure.py", line 69, in tree_path_changed
    pub.sendMessage('NetcdfTS.group.specified', arg1=pathlist )
  File "C:\Python27\lib\site-packages\pubsub\core\kwargs\publisher.py", line 27, in sendMessage
    topicObj.publish(**kwargs)
  File "C:\Python27\lib\site-packages\pubsub\core\kwargs\publishermixin.py", line 24, in publish
    self._publish(msgKwargs)
  File "C:\Python27\lib\site-packages\pubsub\core\topicobj.py", line 376, in _publish
    self.__sendMessage(data, self, iterState)
  File "C:\Python27\lib\site-packages\pubsub\core\topicobj.py", line 397, in __sendMessage
    self._mix_callListener(listener, data, iterState)
  File "C:\Python27\lib\site-packages\pubsub\core\kwargs\publishermixin.py", line 64, in _mix_callListener
    listener(iterState.filteredArgs, self, msgKwargs)
  File "C:\Python27\lib\site-packages\pubsub\core\kwargs\listenerimpl.py", line 43, in __call__
    cb(**kwargs)
  File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\NetcdfTimeSeries.py", line 70, in listner_group
    atime = self.GetSelectedVariableData(pathlist, u'time')
  File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\NetcdfTimeSeries.py", line 307, in GetSelectedVariableData
    VarData = MNU.getNetCDFGroupVarData(self.filename, GroupPathList, variable )
  File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\MyNetcdfUtil.py", line 304, in getNetCDFGroupVarData
    VarData = V[:]  
  File "netCDF4.pyx", line 2949, in netCDF4.Variable.__getitem__ (netCDF4.c:36472)
  File "netCDF4.pyx", line 2969, in netCDF4.Variable._toma (netCDF4.c:36814)
ValueError: could not convert string to float: 

当我使用其他netcdf实用程序(即panolpy)时,我可以访问数据。 有没有人知道为什么netcdf4会抛出这个重复 - 或者更糟 - 它如何在netcdf文件中的float32字段中插入一个字符串?

1 个答案:

答案 0 :(得分:1)

从回溯中,问题发生在&#34; _toma&#34; Netcdf4函数,用于将数据转换为掩码数组。使用其他实用程序(例如NCDUMP)读取文件时,访问数据时没有问题。

目前我认为问题出现了,因为我有一个未分配的&#39; missing_value&#39;变量的属性。显然,如果没有&#39; missing_value&#39;属性Netcdf4默认为适合dtype的缺失值。在我的实施中,&#39; missing_value&#39;通过wxpyhton网格控件公开属性以进行编辑。当网格中编辑的属性写回netcdf文件时,空网格单元返回None对象或wx.emptyString,Netcdf4尝试将其插入到netcdf文件中的float类型中。