0

enter image description hereI 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
4
  • You don't show where/how your read prec. Normally your code should work; e.g. evap has a three dimensional shape in the NetCDF file, but you slice ([:,:]) the first two dimensions out of it, so this should work with pcolor. Could be that prec has more than two dimensions. Commented Nov 17, 2017 at 5:46
  • Thank you for your answer! Actually, you pointed out an error in my code there is no variable "prec". There is only "evap" and "rain" and both of them have 3 dimensions that I slice using [:,:] I just don't understand the error! Commented Nov 17, 2017 at 14:25
  • Me neither. Can you add the full error message (at least including which line causes the error)? Commented Nov 18, 2017 at 7:50
  • Ok! I re-edit my question now with the full error message. Thank you! Commented Nov 20, 2017 at 15:11

1 Answer 1

1

Finally see the problem. Take this example (a random NetCDF file):

ncdump -h th.xz.nc

Gives (amongst other things):

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

So the variable th has 4 dimensions. 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)

Why? The slicing with [:,:] (or [:], or [:,:,:]) will still give you more than two dimensions:

print(th.shape)

(9, 32, 32, 1)

while pcolor expects a two-dimensional array. The solution is simple; you need to select which 2D slice you want to show, for example pcolor(evap[0,:,:]) (x-y slice) or pcolor(evap[:,0,:]) (time-x slice), or ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.