简体   繁体   English

Python,如何切片netcdf文件。

[英]Python, how to slice a netcdf file.

I am trying to slice a variable from a netcdf file and plot it but I am running into problems. 我正在尝试从netcdf文件中切出一个变量并将其绘制出来,但是我遇到了问题。

This is from my code: 这是从我的代码:

import numpy as np
from netCDF4 import Dataset
Raw=  "filename.nc"
data = Dataset(Raw)
u=data.variables['u'][:,:,:,:]
print u.shape 
U=u([0,0,[200:500],[1:300]]) 

#The print statement yields (2, 17, 900, 2600) as u's dimensions. #print语句的尺寸为(2,17,900,2600)。

#U Is the slice of the dataset I am interested inn. #U是我感兴趣的旅馆的数据集的一部分。 A small subset of the 4-dimensional vector. 4维向量的一小部分。 This last line of code gives me a syntax error and I cannot figure out why. 这最后一行代码给了我一个语法错误,我不知道为什么。

Trying to pick out a single value from the array ( u(0,0,0,1)) gives me an Type error: TypeError: 'MaskedArray' The program's aim is to perform simple algebra on a subset of this subset and to plot this data. 尝试从数组(u(0,0,0,1))中选择一个值会给我一个Type错误:TypeError:'MaskedArray'该程序的目的是对该子集的子集执行简单的代数并作图这个数据。 Any help is appreciated. 任何帮助表示赞赏。

I think the comment by Spencer Hill is correct. 我认为斯宾塞·希尔的评论是正确的。 Without seeing the full error message, I can't be sure, but I'm pretty sure that the TypeError results from you (through the use of parenthesis) trying to call the array as a function. 没有看到完整的错误消息,我不能确定,但​​是我很确定TypeError是您(通过使用括号)尝试将数组作为函数调用而产生的。 Try: 尝试:

U=u[0,0,200:500,1:300]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM