简体   繁体   English

netCDF4-Python绘制变量

[英]netCDF4-Python Plotting variables

在此处输入图片说明 I would like to plot a selection of variables from a netCDF file using netCDF4-Python.I am using the following code: 我想使用netCDF4-Python从netCDF文件中绘制一系列变量,我正在使用以下代码:

import numpy as np
import pandas as pd
from pylab import *
from netCDF4 import Dataset
pals = Dataset('pals_amplero_2003_2006_ecmwf_v1.nc4', "a",format='NETCDF4')
print (pals.variables.keys())
print (pals.variables['Rainf'])
print (pals.variables['Evap'])
print (pals.variables['time'])
evap = pals.variables['Evap'][:,:]
rain = pals.variables['Rainf'][:,:]
subplot(2,1,1)
pcolor(evap)
subplot(2,1,2)
pcolor(rain)

Unfortunately, it outputs an error message (more detailed in the enclosed document): 不幸的是,它输出一条错误消息(在随附的文档中有更详细的说明):

ValueError: too many values to unpack (expected 2)

For info the following is the output of the print commands 有关信息,以下是打印命令的输出

odict_keys(['DelIntercept', 'DelSWE', 'DelSoilMoist', 'Evap', 'Qs', 'Qsb',      'Qsm', 'Rainf', 'Snowf', 'lat', 'lon', 'nlevs', 'time', 'timestp', 'M_fieldcap', 'M_sat', 'M_wilt', 'SoilDepth', 'CanopInt', 'Conds', 'ECanop', 'ESoil', 'RootMoist', 'SubSnow', 'TVeg', 'DelColdCont', 'DelSoilHeat', 'LWnet', 'LWup', 'Qf', 'Qg', 'Qh', 'Qle', 'SWnet', 'Fdepth', 'HFLUXRF', 'IceFrac', 'MFLUXRF', 'SAlbedo', 'SnowDepth', 'SnowFrac', 'Tdepth', 'WSN', 'AvgSurfT', 'HLICE', 'HLML', 'SWE', 'SnowT', 'SoilMoist', 'SoilTemp', 'TLBOT', 'TLICE', 'TLMNW', 'TLSF', 'TLWML', 'icetemp', 'snowdens', 'Albedo', 'BaresoilT', 'RH2m', 'RadT', 'T2m', 'VegT', 'Bgain', 'Biomstr', 'Biomstr2', 'Bloss', 'biomass', 'lai', 'Ag', 'An', 'CO2flux', 'Rd', 'Reco', 'Rsoil_str'])
<class 'netCDF4._netCDF4.Variable'>
float32 Rainf(time, y, x)
units: mm/day
long_name: Rainfall rate
associate: time y x
missing_value: 1e+20
time_representation: average over past model timestep
unlimited dimensions: time
current shape = (70084, 1, 1)
filling on, default _FillValue of 9.969209968386869e+36 used

<class 'netCDF4._netCDF4.Variable'>
float32 Evap(time, y, x)
units: mm/day
long_name: Total evapotranspiration
associate: time y x
missing_value: 1e+20
time_representation: average over past model timestep
unlimited dimensions: time
current shape = (70084, 1, 1)
filling on, default _FillValue of 9.969209968386869e+36 used

<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
units: seconds since 2003-01-01 00:00:00
long_name: Time in seconds
Time_label: Start of output interval
unlimited dimensions: time
current shape = (70084,)
filling on, default _FillValue of 9.969209968386869e+36 used

Finally see the problem. 终于看到问题了。 Take this example (a random NetCDF file): 请看以下示例(随机的NetCDF文件):

ncdump -h th.xz.nc

Gives (amongst other things): 给予(除其他事项外):

float th(time, z, x, y) ;

So the variable th has 4 dimensions. 因此变量th有4个维度。 If you read/plot this in a similar way as in your code: 如果您以类似于代码中的方式读取/绘制此图:

import netCDF4 as nc4
import matplotlib.pylab as pl

nc = nc4.Dataset('th.xz.nc')
th = nc.variables['th'][:,:]

pl.figure()
pl.pcolor(th)

It gives the same error: 它给出了相同的错误:

ValueError: too many values to unpack (expected 2) ValueError:太多值无法解包(预期2)

Why? 为什么? The slicing with [:,:] (or [:] , or [:,:,:] ) will still give you more than two dimensions: 使用[:,:] (或[:][:,:,:] )进行切片仍会为您提供两个以上的维度:

print(th.shape)

(9, 32, 32, 1) (9,32,32,1)

while pcolor expects a two-dimensional array. pcolor需要一个二维数组。 The solution is simple; 解决方案很简单; you need to select which 2D slice you want to show, for example pcolor(evap[0,:,:]) ( xy slice) or pcolor(evap[:,0,:]) ( time-x slice), or ... 您需要选择要显示的2D切片,例如pcolor(evap[0,:,:])xy slice)或pcolor(evap[:,0,:])time-x slice)或。 ..

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

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