2014-07-18 39 views
-1

我用Python寫一個腳本處理的NetCDF文件,但我在創建變量面臨着一些問題,這裏是代碼的一部分:的Python的NetCDF IO錯誤:創建NetCDF:創建NetCDF:無效的尺寸ID或名字

stepnumber_var = ofl.createVariable("step_number", "i",("step_number",)) 
stepnumber_var.standard_name = "step_number" 

atomNumber_var = ofl.createVariable("atom_number", "i", ("atom_number",)) 
atomNumber_var.standard_name = "atom__number" 

,但給我這個錯誤:

Traceback (most recent call last): 
    File "sub_avg.py", line 141, in <module> 
    atomNumber_var = ofl.createVariable("atom_number", "i", ("atom_number",)) 
IOError: netcdf: NetCDF: Invalid dimension ID or name 

我的問題是,爲什麼第一個變量是沒有任何問題的產生和第二不起作用?

感謝

這是完整的代碼

from array import array 
import os 
import sys 
import math 
import string as st 
import numpy as N 
from Scientific.IO.NetCDF import NetCDFFile as S 

if len(sys.argv) < 2: 
    sys.exit("No input file found. \nPlease privide NetCDF trajectory input file") 
####################### 
## Open NetCDF file ### 
####################### 
infl = S(sys.argv[1], 'r') 

file = sys.argv[1] 
title,ext = file.split(".") 

           #for v in infl.variables: # Lists the variables in file 
           # print(v)   

################################################################################# 
# Variable "configurations" has the structure [step_number, atom_number, x y z] # 
################################################################################# 

varShape = infl.variables['configuration'].shape  # This gets the shape of the variable, i.e. the dimension in terms of elements 

nSteps = varShape[0]         
nAtoms = varShape[1] 


coordX_atom = N.zeros((nSteps,nAtoms)) 
coordY_atom = N.zeros((nSteps,nAtoms)) 
coordZ_atom = N.zeros((nSteps,nAtoms)) 

sumX = [0] * nAtoms 
sumY = [0] * nAtoms 
sumZ = [0] * nAtoms 

###################################################### 
# 1) Calculate the average structure fron trajectory # 
###################################################### 

for i in range(0, 3): 
    for j in range(0, 3): 
     coordX_atom[i][j] = infl.variables["configuration"][i,j,0] 
     coordY_atom[i][j] = infl.variables["configuration"][i,j,1] 
     coordZ_atom[i][j] = infl.variables["configuration"][i,j,2] 

     sumX[j] = sumX[j] + coordX_atom[i][j] 
     sumY[j] = sumY[j] + coordY_atom[i][j] 
     sumZ[j] = sumZ[j] + coordZ_atom[i][j] 

avgX = [0] * nAtoms 
avgY = [0] * nAtoms 
avgZ = [0] * nAtoms 

for j in range(0, 3): 
    avgX[j] = sumX[j]/nSteps 
     avgY[j] = sumY[j]/nSteps 
     avgZ[j] = sumZ[j]/nSteps 

############################################################## 
# 2) Subtract average structure to each atom and for each frame # 
############################################################## 

for i in range(0, 3): 
    for j in range(0, 3): 
       coordX_atom[i][j] = infl.variables["configuration"][i,j,0] - avgX[j] 
       coordY_atom[i][j] = infl.variables["configuration"][i,j,1] - avgY[j] 
       coordZ_atom[i][j] = infl.variables["configuration"][i,j,2] - avgZ[j] 

####################################### 
# 3) Write new NetCDF trajectory file #      
####################################### 

ofl = S(title + "_subAVG.nc", "a") 
############################################################ 
# Get information of variables contained in the NetCDF input file 
############################################################# 

i = 0 
for v in infl.variables:  
    varNames = [v for v in infl.variables] 
    i += 1 
############################################# 
# Respectively get, elements names in variable, dimension of elements and lenght of the array variableNames 
############################################## 
for v in infl.variables["box_size"].dimensions: 
    boxSizeNames = [v for v in infl.variables["box_size"].dimensions] 
for v in infl.variables["box_size"].shape: 
    boxSizeShape = [v for v in infl.variables["box_size"].shape] 
boxSizeLenght = boxSizeNames.__len__() 

print boxSizeLenght 

for v in infl.variables["step"].dimensions: 
    stepNames = [v for v in infl.variables["step"].dimensions] 
for v in infl.variables["step"].shape: 
    stepShape = [v for v in infl.variables["box_size"].shape] 
stepLenght = stepNames.__len__() 
print stepLenght 

for v in infl.variables["configuration"].dimensions: 
    configurationNames = [v for v in infl.variables["configuration"].dimensions] 
for v in infl.variables["configuration"].shape: 
    configurationShape = [v for v in infl.variables["configuration"].shape] 
configurationLenght = configurationNames.__len__() 
print configurationLenght 

for v in infl.variables["description"].dimensions: 
    descriptionNames = [v for v in infl.variables["description"].dimensions] 
for v in infl.variables["description"].shape: 
    descriptionShape = [v for v in infl.variables["description"].shape] 
descriptionLenght = descriptionNames.__len__() 
print descriptionLenght 

for v in infl.variables["time"].dimensions: 
    timeNames = [v for v in infl.variables["time"].dimensions] 
for v in infl.variables["time"].shape: 
    timeShape = [v for v in infl.variables["time"].shape] 
timeLenght = timeNames.__len__() 
print timeLenght 

#Get Box size 

xBox = infl.variables["box_size"][0,0] 
yBox = infl.variables["box_size"][0,1] 
zBox = infl.variables["box_size"][0,2] 

# Get description lenght 
description_lenghtLenght = infl.variables["description"][:] 

############################################################ 
# Create Dimensions 
############################################################ 

stepnumber_var = ofl.createVariable("step_number", "i",("step_number",)) 
stepnumber_var.standard_name = "step_number" 

atomNumber_var = ofl.createVariable("atom_number", "i", ("atom_number",)) 
atomNumber_var.standard_name = "atom__number" 


# 
#xyz_var = ofl.createVariable("xyz", "f",("xyz",)) 
#xyz_var.units = "nanometers" 
#xyz_var.standard_name = "xyz" 
# 
#configuration_var = ofl.createVariable("configuration", "f", ("step_number", "atom_number", "xyz")) 
#configuration_var.units = "nanometers" 
#configuration_var.standard_name = "configuration" 
# 
#print configuration_var.shape 
#step_var = ofl.createVariable("box_size_lenght", 3) 
#configuration_var = ofl.createVariable("atom_number", nAtoms) 
#description_var = ofl.createVariable("xyz", 3) 
#time_var = ofl.createVariable(description_lenght, description_lenghtLenght) 
# 
#a = infl.variables["step_number"].dimensions.keys() 
#print a 

謝謝!

+1

你願意發佈更多的代碼嗎?您的小片段中沒有足夠的信息可用,除非您可能尚未創建atom_number維度。謝謝! –

+0

已發佈,您將在代碼末尾附近找到行,謝謝! –

+0

尺寸「atom_number」是否可能在輸入文件中不存在?如果沒有,您可能需要創建它。另外,如果你正在向netCDF文件添加新的東西,你應該以「w」模式打開它,而不是「r」模式(在第14行左右)。您嘗試閱讀的文件有多大? –

回答

2

這可能是一個圖書館試圖成爲「有用」的案例(詳見我的文章末尾,但我無法確認)。爲了解決這個問題,你應該明確地創建atom_number和STEP_NUMBER尺寸,使用下面的創建變量(假設我是正確認識和n步nAtoms)前:

ofl.createDimension(「STEP_NUMBER」,n步) 氧氟沙星.createDimension( 「atom_number」,nAtoms)

如果你是新來的netCDF,我可能會建議看無論是netcdf4-Python包,

http://unidata.github.io/netcdf4-python/

在SciPy的發現的netCDF包

http://docs.scipy.org/doc/scipy/reference/io.html

可能是什麼回事:它看起來像問題是,當你創建變量STEP_NUMBER,圖書館正試圖通過創建具有無限長度的STEP_NUMBER尺寸很有幫助。但是,netcdf-3文件中只能有一個無限維度,所以有用的「技巧」不起作用。

+0

謝謝肖恩,那也是我的想法! 無論如何,每次我需要刪除創建的ofl文件,否則會給我一個錯誤(而不是重寫l文件)。 –

+0

這很有趣...我會認爲「a」的標誌可以解決這個問題。您是否在使用olf.close()寫入文件後關閉文件?如果是這樣,你可能想提交一個錯誤報告給開發人員在https://bitbucket.org/khinsen/scientificpython/issues/new –

+0

嗨肖恩,是的,我想過,是的,我正在關閉文件,我不'不知道爲什麼,但我需要刪除ofl文件才能使程序正常工作。我會發送一個錯誤報告! –

0

atomNumber_var.standard_name = 「atom__number」

的atom__number有兩個 「__」,而不是一個 「_」。我不確定這是否是您的問題,但可能需要注意。

我也建議讓你的netcdf文件步驟更清晰。我喜歡把它們分成三步。我使用了一個使用ocean sst的科學數據的例子。您還有一個創建維度的部分,但實際上並沒有這樣做。這是更正確地創建可變部分。

  1. 創建維度

  2. 創建變量

  3. 填寫變量

    from netCDF4 import Dataset 
    ncfile = Dataset('temp.nc','w') 
    lonsdim = latdata.shape #Set dimension lengths 
    latsdim = londata.shape 
    ############### 
    #Create Dimensions 
    ############### 
    latdim = ncfile.createDimension('latitude', latsdim) 
    londim = ncfile.createDimension('longitude', lonsdim) 
    ############### 
    #Create Variables 
    ################# The variables contain the dimensions previously set 
    latitude = ncfile.createVariable('latitude','f8',('latitude')) 
    longitude = ncfile.createVariable('longitude','f8',('longitude')) 
    oceantemp = ncfile.createVariable('SST','f4' ('latitude','longitude'),fill_value=-99999.0) 
    ############### 
    Fill Variables 
    ################ 
    latitude[:] = latdata  #lat data to fill in 
    longitude[:] = londata  #lon data to fill in 
    oceantemp[:,:] = sst[:,:] #some variable previous calculated 
    

我希望這是有幫助的。