3
votes

I am new to using python for scientific data so apologies in advance if anything is unclear. I have a netCDF4 file with multiple variables including latitude, longitude and density. I am trying to plot the variable density on a matplotlib basemap using only density values from coordinates between 35-40 N and 100-110 W.

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

in: f = nc.Dataset('C:\\Users\\mdc\\data\\density.nc', 'r')

in: f.variables['latitude'].shape
out:(120000,)

(the variables longitude and density have the same shape)

I am stuck trying to find a way to extract only the latitude and longitude coordinate pairs (and their associated density values) that fit the criteria of [35 < lat < 40 & -110 < lon < -100]. Any advice on how to do this would be appreciated.

I have tried extracting each of the relevant variables and compiling them into a 2d-array but I have not figured out how to select only the data I need.

lats = f.variables['latitude'][:]
lons = f.variables['longitude'][:]
dens = f.variables['density'][:]
combined = np.vstack((lats,lons,dens))

in: combined
out: array([[ -4.14770737e+01,  -3.89834557e+01,  -3.86000137e+01, ...,
      4.34283943e+01,   4.37634315e+01,   4.40338402e+01],
   [  1.75510895e+02,   1.74857147e+02,   1.74742798e+02, ...,
      7.83558655e+01,   7.81687775e+01,   7.80410919e+01],
   [  7.79418945e-02,   7.38342285e-01,   9.94934082e-01, ...,
      5.60119629e-01,  -1.60522461e-02,   5.52429199e-01]], dtype=float32)

As for plotting I am trying to plot the coordinate pairs by different colors, rather than sizes, according to their density value.

m = Basemap(projection='robin', resolution='i', lat_0 = 37, lon_0 = -105)
m.drawcoastlines()
for lats,lons,dens in zip(lats,lons,dens):
    x,y = m(lats,lons)
    size = dens*3
    m.plot(x,y, 'r', markersize=size)
plt.show()
1
What's the data selection problem? (Don't answer in comments, make it clear in the OP(Original Post). The colors part is easier -- use scatter rather than plot and start by passing the density array in the c kwarg: matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scattercphlewis
Could you post a link to a dataset?Daniel Watkins

1 Answers

0
votes

The data selection, using pandas (can't install netCDF here, sorry, and pandas is satisfactory):

import pandas as pd
tinyd = pd.DataFrame(np.array(
  [[ -4.14770737e+01,  -3.89834557e+01,  -3.86000137e+01,
      4.34283943e+01,   4.37634315e+01,   4.40338402e+01],
   [  1.75510895e+02,   1.74857147e+02,   1.74742798e+02,
      7.83558655e+01,   7.81687775e+01,   7.80410919e+01],
   [  7.79418945e-02,   7.38342285e-01,   9.94934082e-01,
      5.60119629e-01,  -1.60522461e-02,   5.52429199e-01]]).T,
      columns=['lat','lon','den'])

mask =  (tinyd.lat > -39) & (tinyd.lat < 44) & \
        (tinyd.lon > 80) & (tinyd.lon < 175)

toplot = tinyd[mask]
print(toplot)
         lat         lon       den
1 -38.983456  174.857147  0.738342
2 -38.600014  174.742798  0.994934
plt.scatter(toplot.lat, toplot.lon, s=90, c=toplot.den)
plt.colorbar()

enter image description here

plotting on top of Basemap is the same, and you can specify a different colormap, etc.