Can't plot NCEP reanalysis data

Hi all,
I’ve been looking for solution on this for days, and seems like nothing works.

I wrote this code to read TRMM data and it works, but somehow not working when I use the same script to read NCEP reanalysis data…which later I found out it worked for netCDF files with only 1 ‘level’ (Zsize=1), not multiple ‘levels’ (Zsize more than 1).

I’m stuck at where went wrong, and I tried everything and lost of track what the error massages were.

This is what I wrote to read and plot NCEP reanalysis data. (this omega data has Xsize=144, Ysize=73, Zsize=12, Tsize=792, Esize=1)

···

import netCDF4 as nc

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

import numpy as np

f = nc.Dataset(‘D:/data/omega.mon.mean.nc’,‘r’)

omg = f.variables[‘omega’][0]

lon = f.variables[‘lon’][:]

lat = f.variables[‘lat’][:]

times = f.variables[‘time’][:]

**# Set up a map **

map = Basemap(projection=‘cyl’,llcrnrlat=0.,urcrnrlat=10.,llcrnrlon=97.,urcrnrlon=110.,resolution=‘i’)

x,y=map(*np.meshgrid(lon,lat))

map.drawcoastlines()

map.drawcountries()

map.drawparallels(np.arange(-90.,90.,3),labels=[1,0,0,0],fontsize=10)

map.drawmeridians(np.arange(-180.,180.,3),labels=[0,0,0,1],fontsize=10)

#contour data

clevs=np.arange(0.,1.,0.1) # contour interval

cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)

**cb = map.colorbar(cs,‘bottom’,size=‘2%’,pad=“5%”) #plot the colorbar **

cb.set_label(‘m/s’)

plt.title(‘Omega-test’)


plt.show()

f.close()


Above code gave error: Input z must be a 2D array…

(that’s at cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)…)

Hopefully anyone can help.

Thanks for your time reading this.

Fadzil

Hi Fadzil,

I am not sure if I fully understand your question. Are you simply trying to write a general script that plots contours for atmospheric netcdf data? At the very least, your error message has a very simple explanation in that the third argument of contourf (in this case, pcpr or omg?) must be a 2D array with shape (Xsize, Ysize). For data with one vertical level, it would be reasonable to expect the script to work, but if you have multiple vertical levels and don’t select a specific one in your code, then you can’t use contourf, simple as that.

Does that help at all?

Alex

···

On Tue, Mar 4, 2014 at 8:22 PM, Fadzil Mnor <fadzilmnor84@…287…> wrote:

Hi all,
I’ve been looking for solution on this for days, and seems like nothing works.

I wrote this code to read TRMM data and it works, but somehow not working when I use the same script to read NCEP reanalysis data…which later I found out it worked for netCDF files with only 1 ‘level’ (Zsize=1), not multiple ‘levels’ (Zsize more than 1).

I’m stuck at where went wrong, and I tried everything and lost of track what the error massages were.

This is what I wrote to read and plot NCEP reanalysis data. (this omega data has Xsize=144, Ysize=73, Zsize=12, Tsize=792, Esize=1)


import netCDF4 as nc

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

import numpy as np

f = nc.Dataset(‘D:/data/omega.mon.mean.nc’,‘r’)

omg = f.variables[‘omega’][0]

lon = f.variables[‘lon’][:]

lat = f.variables[‘lat’][:]

times = f.variables[‘time’][:]

**# Set up a map **

map = Basemap(projection=‘cyl’,llcrnrlat=0.,urcrnrlat=10.,llcrnrlon=97.,urcrnrlon=110.,resolution=‘i’)

x,y=map(*np.meshgrid(lon,lat))

map.drawcoastlines()

map.drawcountries()

map.drawparallels(np.arange(-90.,90.,3),labels=[1,0,0,0],fontsize=10)

map.drawmeridians(np.arange(-180.,180.,3),labels=[0,0,0,1],fontsize=10)

#contour data

clevs=np.arange(0.,1.,0.1) # contour interval

cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)

**cb = map.colorbar(cs,‘bottom’,size=‘2%’,pad=“5%”) #plot the colorbar **

cb.set_label(‘m/s’)

plt.title(‘Omega-test’)


plt.show()

f.close()


Above code gave error: Input z must be a 2D array…

(that’s at cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)…)

Hopefully anyone can help.

Thanks for your time reading this.

Fadzil


Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.

With Perforce, you get hassle-free workflows. Merge that actually works.

Faster operations. Version large binaries. Built-in WAN optimization and the

freedom to use Git, Perforce or both. Make the move to Perforce.

http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Alex Goodman
Graduate Research Assistant

Department of Atmospheric Science

Colorado State University

Hi all, thanks for your time.
Paul, yes, it should be ‘omg’, I did some copy-and-paste error while writing the email and test the code at the same time.

Alex, yes, that’s the problem that i wasn’t sure how to do. But thanks, I’ll try to figure it out how and try in a bit.

Cheers,

···

Postgraduate Student
Room 1U09 - Dept of Meteorology
University of Reading, Earley Gate
Reading
RG6 6BB, UK

On Wed, Mar 5, 2014 at 3:40 AM, Alex Goodman <alex.goodman@…4442…> wrote:

Hi Fadzil,

I am not sure if I fully understand your question. Are you simply trying to write a general script that plots contours for atmospheric netcdf data? At the very least, your error message has a very simple explanation in that the third argument of contourf (in this case, pcpr or omg?) must be a 2D array with shape (Xsize, Ysize). For data with one vertical level, it would be reasonable to expect the script to work, but if you have multiple vertical levels and don’t select a specific one in your code, then you can’t use contourf, simple as that.

Does that help at all?

Alex

On Tue, Mar 4, 2014 at 8:22 PM, Fadzil Mnor <fadzilmnor84@…287…> wrote:

Hi all,
I’ve been looking for solution on this for days, and seems like nothing works.

I wrote this code to read TRMM data and it works, but somehow not working when I use the same script to read NCEP reanalysis data…which later I found out it worked for netCDF files with only 1 ‘level’ (Zsize=1), not multiple ‘levels’ (Zsize more than 1).

I’m stuck at where went wrong, and I tried everything and lost of track what the error massages were.

This is what I wrote to read and plot NCEP reanalysis data. (this omega data has Xsize=144, Ysize=73, Zsize=12, Tsize=792, Esize=1)


import netCDF4 as nc

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

import numpy as np

f = nc.Dataset(‘D:/data/omega.mon.mean.nc’,‘r’)

omg = f.variables[‘omega’][0]

lon = f.variables[‘lon’][:]

lat = f.variables[‘lat’][:]

times = f.variables[‘time’][:]

**# Set up a map **

map = Basemap(projection=‘cyl’,llcrnrlat=0.,urcrnrlat=10.,llcrnrlon=97.,urcrnrlon=110.,resolution=‘i’)

x,y=map(*np.meshgrid(lon,lat))

map.drawcoastlines()

map.drawcountries()

map.drawparallels(np.arange(-90.,90.,3),labels=[1,0,0,0],fontsize=10)

map.drawmeridians(np.arange(-180.,180.,3),labels=[0,0,0,1],fontsize=10)

#contour data

clevs=np.arange(0.,1.,0.1) # contour interval

cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)

**cb = map.colorbar(cs,‘bottom’,size=‘2%’,pad=“5%”) #plot the colorbar **

cb.set_label(‘m/s’)

plt.title(‘Omega-test’)


plt.show()

f.close()


Above code gave error: Input z must be a 2D array…

(that’s at cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)…)

Hopefully anyone can help.

Thanks for your time reading this.

Fadzil


Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.

With Perforce, you get hassle-free workflows. Merge that actually works.

Faster operations. Version large binaries. Built-in WAN optimization and the

freedom to use Git, Perforce or both. Make the move to Perforce.

http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Alex Goodman
Graduate Research Assistant

Department of Atmospheric Science

Colorado State University

Fadzil,

Questions like this that involve code are great to ask on
stackoverflow, tagging with "netcdf", "matplotlib" and "python".

-Rich

···

On Wed, Mar 5, 2014 at 6:57 AM, Fadzil Mnor <fadzilmnor84@...287...> wrote:

Hi all, thanks for your time.
Paul, yes, it should be 'omg', I did some copy-and-paste error while writing
the email and test the code at the same time.
Alex, yes, that's the problem that i wasn't sure how to do. But thanks, I'll
try to figure it out how and try in a bit.

Cheers,

Postgraduate Student Room 1U09 - Dept of Meteorology University of Reading,
Earley Gate Reading RG6 6BB, UK

On Wed, Mar 5, 2014 at 3:40 AM, Alex Goodman <alex.goodman@...4442...> > wrote:

Hi Fadzil,

I am not sure if I fully understand your question. Are you simply trying
to write a general script that plots contours for atmospheric netcdf data?
At the very least, your error message has a very simple explanation in that
the third argument of contourf (in this case, pcpr or omg?) must be a 2D
array with shape (Xsize, Ysize). For data with one vertical level, it would
be reasonable to expect the script to work, but if you have multiple
vertical levels and don't select a specific one in your code, then you can't
use contourf, simple as that.

Does that help at all?
Alex

On Tue, Mar 4, 2014 at 8:22 PM, Fadzil Mnor <fadzilmnor84@...287...> >> wrote:

Hi all,
I've been looking for solution on this for days, and seems like nothing
works.
I wrote this code to read TRMM data and it works, but somehow not working
when I use the same script to read NCEP reanalysis data...which later I
found out it worked for netCDF files with only 1 'level' (Zsize=1), not
multiple 'levels' (Zsize more than 1).
I'm stuck at where went wrong, and I tried everything and lost of track
what the error massages were.
This is what I wrote to read and plot NCEP reanalysis data. (this omega
data has Xsize=144, Ysize=73, Zsize=12, Tsize=792, Esize=1)

-------------------------------------------------------------------------------------------------------------------------------------------
import netCDF4 as nc
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

f = nc.Dataset('D:/data/omega.mon.mean.nc','r')
omg = f.variables['omega'][0]
lon = f.variables['lon'][:]
lat = f.variables['lat'][:]
times = f.variables['time'][:]

# Set up a map
map =
Basemap(projection='cyl',llcrnrlat=0.,urcrnrlat=10.,llcrnrlon=97.,urcrnrlon=110.,resolution='i')
x,y=map(*np.meshgrid(lon,lat))
map.drawcoastlines()
map.drawcountries()
map.drawparallels(np.arange(-90.,90.,3),labels=[1,0,0,0],fontsize=10)
map.drawmeridians(np.arange(-180.,180.,3),labels=[0,0,0,1],fontsize=10)

#contour data
clevs=np.arange(0.,1.,0.1) # contour interval
cs = map.contourf(x,y,pcpr,clevs,extent='both')
cb = map.colorbar(cs,'bottom',size='2%',pad="5%") #plot the colorbar

cb.set_label('m/s')
plt.title('Omega-test')

plt.show()

f.close()

-----------------------------------------------------------------------------------------------------------------------------------------

Above code gave error: Input z must be a 2D array....
(that's at cs = map.contourf(x,y,pcpr,clevs,extent='both')...)

Hopefully anyone can help.
Thanks for your time reading this.

Fadzil

------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to
Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works.
Faster operations. Version large binaries. Built-in WAN optimization and
the
freedom to use Git, Perforce or both. Make the move to Perforce.

http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Alex Goodman
Graduate Research Assistant
Department of Atmospheric Science
Colorado State University

------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to
Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works.
Faster operations. Version large binaries. Built-in WAN optimization and
the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Dr. Richard P. Signell (508) 457-2229
USGS, 384 Woods Hole Rd.
Woods Hole, MA 02543-1598

The issue I see is one of ensuring that your inputs into contourf() are of the expected shapes. If you want a generalized script, then you will need to do various steps that can prepare 3d data into a 2d by assuming a particular level. The fun part of all of this is that different data sources organize their data differently. You might have data defined as (time, lat, lon, z), or you might have (lon, lat, time), or maybe (z, lat, lon). If your data sources are CF-compliant, then you might be able to take advantage of tools such as ncview that tries to be very intelligent about all of these fun issues.

Cheers!

Ben Root

···

On Tue, Mar 4, 2014 at 10:22 PM, Fadzil Mnor <fadzilmnor84@…287…> wrote:

Hi all,
I’ve been looking for solution on this for days, and seems like nothing works.

I wrote this code to read TRMM data and it works, but somehow not working when I use the same script to read NCEP reanalysis data…which later I found out it worked for netCDF files with only 1 ‘level’ (Zsize=1), not multiple ‘levels’ (Zsize more than 1).

I’m stuck at where went wrong, and I tried everything and lost of track what the error massages were.

This is what I wrote to read and plot NCEP reanalysis data. (this omega data has Xsize=144, Ysize=73, Zsize=12, Tsize=792, Esize=1)


import netCDF4 as nc

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

import numpy as np

f = nc.Dataset(‘D:/data/omega.mon.mean.nc’,‘r’)

omg = f.variables[‘omega’][0]

lon = f.variables[‘lon’][:]

lat = f.variables[‘lat’][:]

times = f.variables[‘time’][:]

**# Set up a map **

map = Basemap(projection=‘cyl’,llcrnrlat=0.,urcrnrlat=10.,llcrnrlon=97.,urcrnrlon=110.,resolution=‘i’)

x,y=map(*np.meshgrid(lon,lat))

map.drawcoastlines()

map.drawcountries()

map.drawparallels(np.arange(-90.,90.,3),labels=[1,0,0,0],fontsize=10)

map.drawmeridians(np.arange(-180.,180.,3),labels=[0,0,0,1],fontsize=10)

#contour data

clevs=np.arange(0.,1.,0.1) # contour interval

cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)

**cb = map.colorbar(cs,‘bottom’,size=‘2%’,pad=“5%”) #plot the colorbar **

cb.set_label(‘m/s’)

plt.title(‘Omega-test’)


plt.show()

f.close()


Above code gave error: Input z must be a 2D array…

(that’s at cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)…)

Hopefully anyone can help.

Thanks for your time reading this.

Fadzil


Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.

With Perforce, you get hassle-free workflows. Merge that actually works.

Faster operations. Version large binaries. Built-in WAN optimization and the

freedom to use Git, Perforce or both. Make the move to Perforce.

http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi all,
Signel, yeah, I should have post it on stackoverflow. Will do that next time.

Benjamin, I’ve been using GrADS for the last 3 years, and just recently using python for more functionality (which came with a price,of course-learning curve).

Seems like I 've found the solution. I should have declared the ‘level’ early , as suggested by Alex, for eg:

···

Postgraduate Student
Room 1U09 - Dept of Meteorology
University of Reading, Earley Gate
Reading
RG6 6BB, UK

On Wed, Mar 5, 2014 at 3:36 PM, Benjamin Root <ben.root@…1304…> wrote:

The issue I see is one of ensuring that your inputs into contourf() are of the expected shapes. If you want a generalized script, then you will need to do various steps that can prepare 3d data into a 2d by assuming a particular level. The fun part of all of this is that different data sources organize their data differently. You might have data defined as (time, lat, lon, z), or you might have (lon, lat, time), or maybe (z, lat, lon). If your data sources are CF-compliant, then you might be able to take advantage of tools such as ncview that tries to be very intelligent about all of these fun issues.

Cheers!

Ben Root

On Tue, Mar 4, 2014 at 10:22 PM, Fadzil Mnor <fadzilmnor84@…287…> wrote:

Hi all,
I’ve been looking for solution on this for days, and seems like nothing works.

I wrote this code to read TRMM data and it works, but somehow not working when I use the same script to read NCEP reanalysis data…which later I found out it worked for netCDF files with only 1 ‘level’ (Zsize=1), not multiple ‘levels’ (Zsize more than 1).

I’m stuck at where went wrong, and I tried everything and lost of track what the error massages were.

This is what I wrote to read and plot NCEP reanalysis data. (this omega data has Xsize=144, Ysize=73, Zsize=12, Tsize=792, Esize=1)


import netCDF4 as nc

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

import numpy as np

f = nc.Dataset(‘D:/data/omega.mon.mean.nc’,‘r’)

omg = f.variables[‘omega’][0]

lon = f.variables[‘lon’][:]

lat = f.variables[‘lat’][:]

times = f.variables[‘time’][:]

**# Set up a map **

map = Basemap(projection=‘cyl’,llcrnrlat=0.,urcrnrlat=10.,llcrnrlon=97.,urcrnrlon=110.,resolution=‘i’)

x,y=map(*np.meshgrid(lon,lat))

map.drawcoastlines()

map.drawcountries()

map.drawparallels(np.arange(-90.,90.,3),labels=[1,0,0,0],fontsize=10)

map.drawmeridians(np.arange(-180.,180.,3),labels=[0,0,0,1],fontsize=10)

#contour data

clevs=np.arange(0.,1.,0.1) # contour interval

cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)

**cb = map.colorbar(cs,‘bottom’,size=‘2%’,pad=“5%”) #plot the colorbar **

cb.set_label(‘m/s’)

plt.title(‘Omega-test’)


plt.show()

f.close()


Above code gave error: Input z must be a 2D array…

(that’s at cs = map.contourf(x,y,pcpr,clevs,extent=‘both’)…)

Hopefully anyone can help.

Thanks for your time reading this.

Fadzil


Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.

With Perforce, you get hassle-free workflows. Merge that actually works.

Faster operations. Version large binaries. Built-in WAN optimization and the

freedom to use Git, Perforce or both. Make the move to Perforce.

http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users