Reputation: 41
I've been searching for a pathetically long time for this, so I would appreciate any help or hint I can get.
I'm trying to plot some sea ice freeboard data (netCDF, Gridded total freeboard) on the Antarctic sea, but the data that should plot nicely around Antarctica lies at the bottom of my image. NetCDF and matplotlib are fairly new to me so maybe the error could be e.g. with handling the dimensions or the projection.
from scipy.io.netcdf import netcdf_file as Dataset
import numpy as np
import matplotlib.pyplot as plt
FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,0]
lon = FB.variables['lon'][0,:]
masked_fb = np.ma.masked_where(np.isnan(f), f)
mtx_lon, mtx_lat = np.meshgrid(lon, lat)
m = Basemap(projection='spstere',boundinglat=-50, lon_0=180., resolution='l')
m.bluemarble()
plt.figure()
m.pcolormesh(mtx_lon, mtx_lat, masked_fb, latlon=True)
plt.show()
ncdump gives:
dimensions:
x = 79 ;
y = 83 ;
variables:
float lat(y, x) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude coordinate" ;
lat:units = "degrees_north" ;
float lon(y, x) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude coordinate" ;
lon:units = "degrees_east" ;
float f(y, x) ;
f:long_name = "total_freeboard" ;
f:units = "mm" ;
f:coordinates = "lat lon" ;
One weird thing I noticed is that min lat is -5156.6201 but I didn't know how to count how many of them there are...
Edit: Formated the code to fit the common way, like Neil advised.
Upvotes: 1
Views: 1161
Reputation: 41
Okay, I got help from matplotlib and thought I should share this here if someone else has sometimes similar problems. The problem was with meshgrid. Since the latitudes and longitudes in the netCDF file were already in 2D the meshgrid was unnecessary. The solution that worked for me was:
from scipy.io.netcdf import netcdf_file as Dataset
import numpy as np
import matplotlib.pyplot as plt
FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,:]
lon = FB.variables['lon'][:,:]
masked_fb = np.ma.masked_where(np.isnan(f), f)
m = Basemap(projection='spstere',boundinglat=-50, lon_0=180., resolution='l')
m.bluemarble()
plt.figure()
m.pcolormesh(lon, lat, masked_fb, latlon=True)
plt.show()
Upvotes: 1
Reputation: 3453
First, it's common practice to read in the netcdf module as
from scipy.io.netcdf import netcdf_file as Dataset
You can then read in the file and access variables as
FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,:]
lon = FB.variables['lon'][:,:]
Are you sure that lat[:,0]
and lon[0,:]
is reading in the grid coordinates correctly? ncdump indicates they are 2D variables and I suspect that the issue is creating a meshgrid
from lat[:,0]
and lon[0,:]
.
Upvotes: 0