Quiver plot of a netcdf file

Hello,
I am trying to use matplotlib to create a quiver plot of a NetCDF file with
the extension .nc. The Netcdf file is a series of arrays that contain
information about the stress tensors on a globe.

I am struggling to import the file into python and having the quiver data
show up.
To import the file I have been using:
“ncdump file.nc”

Any thoughts would be appreciated.
Thanks,
Jon

···

--
View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23986313.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

I am struggling to import the file into python and having the quiver data
show up.
To import the file I have been using:
“ncdump file.nc”

scipy.io has netcdf reading - it just uses a copy of
http://pypi.python.org/pypi/pupynere/

Best,

Matthew

In addition to scipy.io, there are some pure python netcdf readers, including pupynere (might be easier to install a pure python module?). Also, you have ScientificPython.io.netCDF, in case this is more convenient for you.

ncdump is almost certainly not the way to go, it is best suited for human inspection of the file contents. If you absolutely want to use it, consider using the C source creator in ncdump to import it (Cython or similar?).

Good luck,

Paul.

···

On Thu, Jun 11, 2009 at 8:09 PM, JPKay <kay1829@…2648…> wrote:

Hello,

I am trying to use matplotlib to create a quiver plot of a NetCDF file with

the extension .nc. The Netcdf file is a series of arrays that contain

information about the stress tensors on a globe.

I am struggling to import the file into python and having the quiver data

show up.

To import the file I have been using:

“ncdump file.nc

Any thoughts would be appreciated.

Thanks,

Jon

View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23986313.html

Sent from the matplotlib - users mailing list archive at Nabble.com.


Crystal Reports - New Free Runtime and 30 Day Trial

Check out the new simplified licensing option that enables unlimited

royalty-free distribution of the report engine for externally facing

server and web deployment.

http://p.sf.net/sfu/businessobjects


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

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

I was under the impression that ScientificPython.io.netCDF was outdated. I
have no preference on ncdump and if there is an easier way to import the
data, then I am all for it.
Right now the pupynere website is down so I can't check the site for help.
Thanks for the responses.

Paul Anton Letnes wrote:

···

In addition to scipy.io, there are some pure python netcdf readers,
including pupynere (might be easier to install a pure python module?).
Also,
you have ScientificPython.io.netCDF, in case this is more convenient for
you.
ncdump is
almost certainly not the way to go, it is best suited for human
inspection of the file contents. If you absolutely want to use it,
consider
using the C source creator in ncdump to import it (Cython or similar?).

Good luck,
Paul.

On Thu, Jun 11, 2009 at 8:09 PM, JPKay <kay1829@...2648...> wrote:

Hello,
I am trying to use matplotlib to create a quiver plot of a NetCDF file
with
the extension .nc. The Netcdf file is a series of arrays that contain
information about the stress tensors on a globe.

I am struggling to import the file into python and having the quiver data
show up.
To import the file I have been using:
“ncdump file.nc”

Any thoughts would be appreciated.
Thanks,
Jon
--
View this message in context:
http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23986313.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23988479.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I recommend you to check netcdf4-python (http://code.google.com/p/netcdf4-python/)
You can load both netcdf3 and 4 files. Additionally, there are date conversion utilities which help to do transforms between numbers and dates and vice versa.

···

On Thu, Jun 11, 2009 at 1:09 PM, JPKay <kay1829@…2648…> wrote:

Hello,

I am trying to use matplotlib to create a quiver plot of a NetCDF file with

the extension .nc. The Netcdf file is a series of arrays that contain

information about the stress tensors on a globe.

I am struggling to import the file into python and having the quiver data

show up.

To import the file I have been using:

“ncdump file.nc

Any thoughts would be appreciated.

Thanks,

Jon

View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23986313.html

Sent from the matplotlib - users mailing list archive at Nabble.com.

Gökhan SEVER wrote:

···

On Thu, Jun 11, 2009 at 1:09 PM, JPKay <kay1829@…2648… > <mailto:kay1829@…2648…>> wrote:

    Hello,
    I am trying to use matplotlib to create a quiver plot of a NetCDF
    file with
    the extension .nc. The Netcdf file is a series of arrays that contain
    information about the stress tensors on a globe.

    I am struggling to import the file into python and having the quiver
    data
    show up.
    To import the file I have been using:
    “ncdump file.nc <http://file.nc>”

    Any thoughts would be appreciated.
    Thanks,
    Jon
    --
    View this message in context:
    http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23986313.html
    Sent from the matplotlib - users mailing list archive at Nabble.com.

I recommend you to check netcdf4-python (http://code.google.com/p/netcdf4-python/)
You can load both netcdf3 and 4 files. Additionally, there are date conversion utilities which help to do transforms between numbers and dates and vice versa.

Caution: it's great, and I use it, but it is not trivial to build and install. I think the OP would do best to start with pupynere. The web site for download looks like it is working: http://pypi.python.org/pypi/pupynere/

Eric

Eric Firing wrote:

Gökhan SEVER wrote:
  

    Hello,
    I am trying to use matplotlib to create a quiver plot of a NetCDF
    file with
    the extension .nc. The Netcdf file is a series of arrays that contain
    information about the stress tensors on a globe.

    I am struggling to import the file into python and having the quiver
    data
    show up.
    To import the file I have been using:
    “ncdump file.nc <http://file.nc>”

    Any thoughts would be appreciated.
    Thanks,
    Jon
    --
    View this message in context:
    http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23986313.html
    Sent from the matplotlib - users mailing list archive at Nabble.com.

I recommend you to check netcdf4-python (http://code.google.com/p/netcdf4-python/)
You can load both netcdf3 and 4 files. Additionally, there are date conversion utilities which help to do transforms between numbers and dates and vice versa.
    
Caution: it's great, and I use it, but it is not trivial to build and install. I think the OP would do best to start with pupynere. The web site for download looks like it is working: http://pypi.python.org/pypi/pupynere/

Eric

Note that if the OP is using basemap, an interface to pupynere is already included (see http://matplotlib.sourceforge.net/basemap/doc/html/api/basemap_api.html#mpl_toolkits.basemap.NetCDFFile).

-Jeff

···

On Thu, Jun 11, 2009 at 1:09 PM, JPKay <kay1829@…2648… >> <mailto:kay1829@…2648…>> wrote:

The suggestion to install matplotlib.basemap seems to be the right direction
to go. However, I have been unsuccessful in importing the file by this
method. This is what I have been trying.

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
mpl_toolkits.basemap.NetCDFFile(output.nc, mode='r', maskandscale=True,
cache=None, mmap=True, username=None, password=None, verbose=False)

I am getting the following error as a result.

Traceback (most recent call last):
  File "/Users/interpott/Documents/trial.py", line 6, in <module>
    mpl_toolkits.basemap.NetCDFFile(output.nc, mode='r', maskandscale=True,
cache=None, mmap=True, username=None, password=None, verbose=False)
NameError: name 'mpl_toolkits' is not defined

How do I force the location of the Netcdf file? and Why am I getting that
mpl_toolkits are not defined?
Thanks for the help,

Jon

Jeff Whitaker wrote:

···

Eric Firing wrote:

Gökhan SEVER wrote:
  

On Thu, Jun 11, 2009 at 1:09 PM, JPKay <kay1829@…2648… >>> <mailto:kay1829@…2648…>> wrote:

    Hello,
    I am trying to use matplotlib to create a quiver plot of a NetCDF
    file with
    the extension .nc. The Netcdf file is a series of arrays that
contain
    information about the stress tensors on a globe.

    I am struggling to import the file into python and having the quiver
    data
    show up.
    To import the file I have been using:
    “ncdump file.nc <http://file.nc>”

    Any thoughts would be appreciated.
    Thanks,
    Jon
    --
    View this message in context:
   
http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p23986313.html
    Sent from the matplotlib - users mailing list archive at Nabble.com.

I recommend you to check netcdf4-python
(http://code.google.com/p/netcdf4-python/)
You can load both netcdf3 and 4 files. Additionally, there are date
conversion utilities which help to do transforms between numbers and
dates and vice versa.
    
Caution: it's great, and I use it, but it is not trivial to build and
install. I think the OP would do best to start with pupynere. The web
site for download looks like it is working:
http://pypi.python.org/pypi/pupynere/

Eric

Note that if the OP is using basemap, an interface to pupynere is
already included (see
http://matplotlib.sourceforge.net/basemap/doc/html/api/basemap_api.html#mpl_toolkits.basemap.NetCDFFile).

-Jeff

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p24003499.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

from mpl_toolkits.basemap import Basemap

You have not so far imported mpl_toolkits into the namespace, only
Basemap. You could do:

import mpl_toolkits.basemap

as another import line, or:

from mpl_toolkits.basemap import Basemap, NetCDFFile

followed by:

ncfile = NetCDFFile(output.nc, ...)

Best,

Matthew

···

On Fri, Jun 12, 2009 at 10:52 AM, JPKay<kay1829@...2648...> wrote:

Hello,

First let me say thank you for all of the help it is very appreciated. Your
suggestion to use the command import "import mpl_toolkits.basemap" has
worked, but now a new problem has popped up.

Does the Netcdf file need to be in the same directory as the script I am
running to retrieve the file? I have moved the netcdf file to the same
location as the script and I get the following error.

Traceback (most recent call last):
  File "/Users/interpott/Documents/trial.py", line 7, in <module>
    mpl_toolkits.basemap.NetCDFFile(output.nc, mode='r', maskandscale=True,
cache=None, mmap=True, username=None, password=None, verbose=False)
NameError: name 'output' is not defined

Any thoughts?

Thanks,

Jon

Matthew Brett wrote:

···

Hi,

On Fri, Jun 12, 2009 at 10:52 AM, JPKay<kay1829@...2648...> wrote:

from mpl_toolkits.basemap import Basemap

You have not so far imported mpl_toolkits into the namespace, only
Basemap. You could do:

import mpl_toolkits.basemap

as another import line, or:

from mpl_toolkits.basemap import Basemap, NetCDFFile

followed by:

ncfile = NetCDFFile(output.nc, ...)

Best,

Matthew

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p24005790.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

I think you mean:

mpl_toolkits.basemap.NetCDFFile("output.nc", mode='r', maskandscale=True,
cache=None, mmap=True, username=None, password=None, verbose=False)

Note quotes round filename... Sorry, I missed those out in my previous mail.

Best,

Matthew

Hello,

Now that I have correctly imported the NetCDF file and set up the kind of
projection I am interested in having the data displayed over I am having
trouble plotting my data with the quiver function. I am interested in
plotting the principal vectors of the stress field onto a Mercator
projection.
My netcdf file is telling me the following after "ncdump -h"
dimensions:
  nsr_delta = 20 ;
  time = 24 ;
  latitude = 37 ;
  longitude = 73 ;
variables:
  float nsr_delta(nsr_delta) ;
    nsr_delta:units = "" ;
    nsr_delta:long_name = "NSR Surface Delta (mu/(eta*omega))" ;
  float time(time) ;
    time:units = "degrees" ;
    time:long_name = "degrees after periapse" ;
  float latitude(latitude) ;
    latitude:units = "degrees_north" ;
    latitude:long_name = "latitude" ;
  float longitude(longitude) ;
    longitude:units = "degrees_east" ;
    longitude:long_name = "longitude" ;
  float Ttt_D(time, latitude, longitude) ;
    Ttt_D:units = "Pa" ;
    Ttt_D:long_name = "north-south component stress of Diurnal stresses" ;
  float Tpt_D(time, latitude, longitude) ;
    Tpt_D:units = "Pa" ;
    Tpt_D:long_name = "shear component of Diurnal stresses" ;
  float Tpp_D(time, latitude, longitude) ;
    Tpp_D:units = "Pa" ;
    Tpp_D:long_name = "east-west component of Diurnal stresses" ;
  float Ttt_N(nsr_delta, latitude, longitude) ;
    Ttt_N:units = "Pa" ;
    Ttt_N:long_name = "north-south component of NSR stresses" ;
  float Tpt_N(nsr_delta, latitude, longitude) ;
    Tpt_N:units = "Pa" ;
    Tpt_N:long_name = "shear component of NSR stresses" ;
  float Tpp_N(nsr_delta, latitude, longitude) ;
    Tpp_N:units = "Pa" ;
    Tpp_N:long_name = "east-west component of NSR stresses" ;

// global attributes:
    :description = "Testing pySatStress on a regular grid" ;
    :history = "Created: Mon Jun 15 12:46:13 2009 using pySatStress" ;
    :Conventions = "COARDS" ;
However, when I am strugglingto use the quiver_demo.py as a guide to making
my quiver plot.
Thanks for any help you can offer.
Jon

···

--
View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p24041837.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

JPKay wrote:

Hello,

Now that I have correctly imported the NetCDF file and set up the kind of
projection I am interested in having the data displayed over I am having
trouble plotting my data with the quiver function. I am interested in
plotting the principal vectors of the stress field onto a Mercator
projection. My netcdf file is telling me the following after "ncdump -h"
dimensions:
  nsr_delta = 20 ;
  time = 24 ;
  latitude = 37 ;
  longitude = 73 ;
variables:
  float nsr_delta(nsr_delta) ;
    nsr_delta:units = "" ;
    nsr_delta:long_name = "NSR Surface Delta (mu/(eta*omega))" ;
  float time(time) ;
    time:units = "degrees" ;
    time:long_name = "degrees after periapse" ;
  float latitude(latitude) ;
    latitude:units = "degrees_north" ;
    latitude:long_name = "latitude" ;
  float longitude(longitude) ;
    longitude:units = "degrees_east" ;
    longitude:long_name = "longitude" ;
  float Ttt_D(time, latitude, longitude) ;
    Ttt_D:units = "Pa" ;
    Ttt_D:long_name = "north-south component stress of Diurnal stresses" ;
  float Tpt_D(time, latitude, longitude) ;
    Tpt_D:units = "Pa" ;
    Tpt_D:long_name = "shear component of Diurnal stresses" ;
  float Tpp_D(time, latitude, longitude) ;
    Tpp_D:units = "Pa" ;
    Tpp_D:long_name = "east-west component of Diurnal stresses" ;
  float Ttt_N(nsr_delta, latitude, longitude) ;
    Ttt_N:units = "Pa" ;
    Ttt_N:long_name = "north-south component of NSR stresses" ;
  float Tpt_N(nsr_delta, latitude, longitude) ;
    Tpt_N:units = "Pa" ;
    Tpt_N:long_name = "shear component of NSR stresses" ;
  float Tpp_N(nsr_delta, latitude, longitude) ;
    Tpp_N:units = "Pa" ;
    Tpp_N:long_name = "east-west component of NSR stresses" ;

// global attributes:
    :description = "Testing pySatStress on a regular grid" ;
    :history = "Created: Mon Jun 15 12:46:13 2009 using pySatStress" ;
    :Conventions = "COARDS" ;
However, when I am strugglingto use the quiver_demo.py as a guide to making
my quiver plot. Thanks for any help you can offer. Jon

Jon: Have you looked the quiver_demo.py in the basemap examples? If so, what problems did you have adopting that code?

As always, a simple script demonstrating your problem is most helpful.

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

Thanks for the suggestion to look at the demo, I had missed it in my initial
look through. The quiver_demo seems to be broken up into three sections. The
first section initializes the information for the the two quiver plots
(sections two and three). This seems like the logical place to start for
importing the array information. The information in the quiver demo seems to
make sense, but I do not understand what needs to be modified and what does
not. Is there any location that contains more documentation about the quiver
function, or another demo that I could follow? Anyway, thanks for the help.
Jon

I have done my best to comment out the first section.

Section 1:
# read in data.
file = open('fcover.dat','r')
#I assume this section is not needed because because the netcdf file has
already been imported.
ul=[];vl=[];pl=[]
#this seems to initiate the functions that are going to be imported and
plotted
nlons=73; nlats=73
#Not sure what this does
dellat = 2.5; dellon = 5.
#degrees that are displayed?
for line in file.readlines():
   l = line.replace('\n','').split()
   ul.append(float(l[0]))
   vl.append(float(l[1]))
   pl.append(float(l[2]))
u = np.reshape(np.array(ul,np.float32),(nlats,nlons))
v = np.reshape(np.array(vl,np.float32),(nlats,nlons))
p = np.reshape(np.array(pl,np.float32),(nlats,nlons))
lats1 = -90.+dellat*np.arange(nlats)
lons1 = -180.+dellon*np.arange(nlons)
lons, lats = np.meshgrid(lons1, lats1)

# read in data.
file = open('fcover.dat','r')
ul=[];vl=[];pl=[]
nlons=73; nlats=73
dellat = 2.5; dellon = 5.
for line in file.readlines():
   l = line.replace('\n','').split()
   ul.append(float(l[0]))
   vl.append(float(l[1]))
   pl.append(float(l[2]))
u = np.reshape(np.array(ul,np.float32),(nlats,nlons))
v = np.reshape(np.array(vl,np.float32),(nlats,nlons))
p = np.reshape(np.array(pl,np.float32),(nlats,nlons))
#equations calculating the magnitude of the vectors?
lats1 = -90.+dellat*np.arange(nlats)
lons1 = -180.+dellon*np.arange(nlons)
lons, lats = np.meshgrid(lons1, lats1)

···

--
View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p24045770.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Okay, so in the "variables" section, what you've got is a list of things
which vary as a function of the "dimensions" listed in the previous section
(time, nsr_delta, lat, lon). I don't remember which of the NetCDF file
reading libraries you're using, but using Jeff's older netCDF3 interface,
you could do something like:

nc_in = netCDF3.Dataset("outputfile.nc")

And then that nc_in thing contains all the data that's contained in the file
outputfile.nc, and you can read it into numpy arrays:

Ttt_D = nc_in.variables['Ttt_D'][:,:,:]
Tpt_D = nc_in.variables['Tpt_D'][:,:,:]
Tpp_D = nc_in.variables['Tpp_D'][:,:,:]

What that does is stick the north-south, shear, and east-west components of
the Diurnal stresses into the three arrays. The [:,:,:] bit says "give me
the data for all values of time, latitude and longitude".

In order to turn those tensor components into the principal components, you
need to, for each (t,lat,lon) set, diagonalize the matrices composed of the
stress values:

[ [ Ttt(t,lat,lon), Tpt(t,lat,lon) ],
  [ Tpt(t,lat,lon), Tpp(t,lat,lon) ] ]

which you can do with np.eig()

The (unit length) eigenvectors it returns will tell you what direction the
principal components point, and the corresponding eigenvalues will tell you
their magnitudes... which then have to get fed in to quiver().

Hopefully that helps at least a little?

JPKay wrote:

···

Hello,

Now that I have correctly imported the NetCDF file and set up the kind of
projection I am interested in having the data displayed over I am having
trouble plotting my data with the quiver function. I am interested in
plotting the principal vectors of the stress field onto a Mercator
projection.
My netcdf file is telling me the following after "ncdump -h"
dimensions:
  nsr_delta = 20 ;
  time = 24 ;
  latitude = 37 ;
  longitude = 73 ;
variables:
  float nsr_delta(nsr_delta) ;
    nsr_delta:units = "" ;
    nsr_delta:long_name = "NSR Surface Delta (mu/(eta*omega))" ;
  float time(time) ;
    time:units = "degrees" ;
    time:long_name = "degrees after periapse" ;
  float latitude(latitude) ;
    latitude:units = "degrees_north" ;
    latitude:long_name = "latitude" ;
  float longitude(longitude) ;
    longitude:units = "degrees_east" ;
    longitude:long_name = "longitude" ;
  float Ttt_D(time, latitude, longitude) ;
    Ttt_D:units = "Pa" ;
    Ttt_D:long_name = "north-south component stress of Diurnal stresses" ;
  float Tpt_D(time, latitude, longitude) ;
    Tpt_D:units = "Pa" ;
    Tpt_D:long_name = "shear component of Diurnal stresses" ;
  float Tpp_D(time, latitude, longitude) ;
    Tpp_D:units = "Pa" ;
    Tpp_D:long_name = "east-west component of Diurnal stresses" ;
  float Ttt_N(nsr_delta, latitude, longitude) ;
    Ttt_N:units = "Pa" ;
    Ttt_N:long_name = "north-south component of NSR stresses" ;
  float Tpt_N(nsr_delta, latitude, longitude) ;
    Tpt_N:units = "Pa" ;
    Tpt_N:long_name = "shear component of NSR stresses" ;
  float Tpp_N(nsr_delta, latitude, longitude) ;
    Tpp_N:units = "Pa" ;
    Tpp_N:long_name = "east-west component of NSR stresses" ;

// global attributes:
    :description = "Testing pySatStress on a regular grid" ;
    :history = "Created: Mon Jun 15 12:46:13 2009 using pySatStress" ;
    :Conventions = "COARDS" ;
However, when I am strugglingto use the quiver_demo.py as a guide to
making my quiver plot.
Thanks for any help you can offer.
Jon

--
View this message in context: http://www.nabble.com/Quiver-plot-of-a-netcdf-file-tp23986313p24046177.html
Sent from the matplotlib - users mailing list archive at Nabble.com.