I would like to plot a selection of variables from a netCDF file using netCDF4-Python.I am using the following code:
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
prec. Normally your code should work; e.g.evaphas a three dimensional shape in the NetCDF file, but you slice ([:,:]) the first two dimensions out of it, so this should work withpcolor. Could be thatprechas more than two dimensions.