Given Lesson 1's introduction, the program participant should now be able to read in another NetCDF file and plot the results. Complexities may arise owing to dataset format differences. These are addressed below for a specific dataset file.
Try the following data file. It is sea surface temperature (SST) obtained from RSS:
data/sea_surface_temperaturexarraySince this SST data is valid on the same date as the SSS datafile that we just examined (valid on July 17, 2020), there should be some features common to both.
analyzed_sstanalyzed_sst is not in dimensions of [lat,lon] as expected but in [time,lat,lon]. But there is only one time. So, one way to read in the important data is as follows: analysed_sst = nc.variables["analysed_sst"][0,:,:]analyzed_sst is temperature in units of Kelvin so you need to subtract 273.15 to convert to degrees Celciusanalyzed_sst is given to us as -180 degrees to 180 degrees in longitude. Therefore it is simpler to index the data than it was for sea surface salinity. We provide code below to do this.# Read in the data from the netcdf file.
nc = Dataset(infile, "r")
etime = nc.variables["time"][:] # time in seconds since 1981/01/00 00:00
lat = nc.variables["lat"][:] # latitude (degrees), values = [-90, 90]
lon = nc.variables["lon"][:] # longitude (degrees), values = [-180, 180]
analysed_sst = nc.variables["analysed_sst"][0,:,:] # sea_surface_temperature, Kelvin
# Subset for the region of interest.
latlim = np.array([-10.0,10.0]) # in degrees
lonlim = np.array([-20.0,15.0]) # in degrees
latlim = np.double(latlim)
lonlim = np.double(lonlim)
ilat1 = (lat >= latlim[0]) & (lat <= latlim[1])
ilon1 = (lon >= lonlim[0]) & (lon < lonlim[1]);
ilat = ilat1;
ilon = ilon1;
lats = lat[ilat1]
lons = lon[ilon1]
index1 = np.array(np.where(ilat))
index2 = np.array(np.where(ilon))
#print(index1)
#print(index2)
sst_block1 = sst[ilat,:]
sst_block1 = sst_block1[:,ilon]
nlats = len(lats)
nlons = len(lons)
sst_block = np.zeros([nlats,nlons])
sst_block[0:nlats,0:nlons] = sst_block1
# Form a mask for the land.
# This mask uses the bad values to identify land.
mask = np.zeros([nlats,nlons])
igood = (sst_block >= -3) # find good values
mask[igood] = 1
inan = (sst_block < -3) # find bad values
mask[inan] = np.nan # not a number
# Plot the sea surface temperature.
plt.pcolor(lons,lats,sst_block*mask,cmap="coolwarm") # the colormap changes to red/blue
plt.xlabel('Longitude (deg)')
plt.ylabel('Latitude (deg)')
plt.title('Sea Surface Temperature: '+fname) # here we need to insert a date inside the brackets
plt.grid()
plt.colorbar()
#plt.show() # to save the file, we must comment out this line for some reason (ask Dr. Paige)
outfile = "SST_map.png" # define output filename
plt.savefig(outfile,format='png',dpi=200)