简体   繁体   中英

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.

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.

#U Is the slice of the dataset I am interested inn. A small subset of the 4-dimensional vector. 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. 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. Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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