This notebook graphs temperature and salinity against time, as recorded in a dataset in the RIDDC ERDDAP server. I believe this is at one specific location (can be confirmed by checking the map at the ERDDAP server).
# installs for google colab
%pip install eofs
%pip install netCDF4
%pip install cartopy
%pip install shapely --no-binary shapely;%pip install cartopy
import requests
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
#To use cartopy in a Colab notebook, I need a particular sequence of installs (as shown at this notebook: https://colab.research.google.com/github/adamlamee/CODINGinK12/blob/master/notebooks/quakes.ipynb#scrollTo=3LkZkXvnMAr4)
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from matplotlib.axes import Axes
from cartopy.mpl.geoaxes import GeoAxes
GeoAxes._pcolormesh_patched = Axes.pcolormesh
import calendar
import datetime---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 8
6 import matplotlib.pyplot as plt
7 #To use cartopy in a Colab notebook, I need a particular sequence of installs (as shown at this notebook: https://colab.research.google.com/github/adamlamee/CODINGinK12/blob/master/notebooks/quakes.ipynb#scrollTo=3LkZkXvnMAr4)
----> 8 import cartopy.crs as ccrs
9 import cartopy.feature as cfeature
10 from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/cartopy/__init__.py:106
102 pass
104 # Commonly used sub-modules. Imported here to provide end-user
105 # convenience.
--> 106 import cartopy.crs # noqa: E402 module-level imports
107 import cartopy.feature # noqa: E402,F401 (unused import)
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/cartopy/crs.py:1722
1718 return self._y_limits
1721 # Define a specific instance of a Mercator projection, the Google mercator.
-> 1722 Mercator.GOOGLE = Mercator(min_latitude=-85.0511287798066,
1723 max_latitude=85.0511287798066,
1724 globe=Globe(ellipse=None,
1725 semimajor_axis=WGS84_SEMIMAJOR_AXIS,
1726 semiminor_axis=WGS84_SEMIMAJOR_AXIS,
1727 nadgrids='@null'))
1728 # Deprecated form
1729 GOOGLE_MERCATOR = Mercator.GOOGLE
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/cartopy/crs.py:1676, in Mercator.__init__(self, central_longitude, min_latitude, max_latitude, globe, latitude_true_scale, false_easting, false_northing, scale_factor)
1673 else:
1674 proj4_params.append(('k_0', scale_factor))
-> 1676 super().__init__(proj4_params, globe=globe)
1678 # Need to have x/y limits defined for the initial hash which
1679 # gets used within transform_points for caching
1680 self._x_limits = self._y_limits = None
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/cartopy/crs.py:699, in Projection.__init__(self, *args, **kwargs)
697 x0, x1, y0, y1 = self.bounds
698 self.threshold = min(x1 - x0, y1 - y0) / 100.
--> 699 elif self.is_geographic:
700 # If the projection is geographic without an area of use, assume
701 # the bounds are the full globe.
702 self.bounds = (-180, 180, -90, 90)
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/pyproj/crs/crs.py:1475, in CRS.is_geographic(self)
1462 @property
1463 def is_geographic(self) -> bool:
1464 """
1465 This checks if the CRS is geographic.
1466 It will check if it has a geographic CRS
(...) 1473 True if the CRS is in geographic (lon/lat) coordinates.
1474 """
-> 1475 return self._crs.is_geographic
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/pyproj/_crs.pyx:3042, in pyproj._crs._CRS.is_geographic.__get__()
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/pyproj/_crs.pyx:3024, in pyproj._crs._CRS._is_crs_property()
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/pyproj/_crs.pyx:2587, in pyproj._crs._CRS.source_crs.__get__()
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/pyproj/_crs.pyx:2361, in pyproj._crs._CRS.__init__()
File ~/Documents/riddc-jbook/.venv/lib/python3.12/site-packages/pyproj/_compat.pyx:9, in pyproj._compat.cstrencode()
AttributeError: 'NoneType' object has no attribute 'encode'#making the url, opening the dataset:
url = 'https://pricaimcit.services.brown.edu/erddap/tabledap/erdGtsppBest.nc?station_id%2Clongitude%2Clatitude%2Ctime%2Cdepth%2Ctemperature%2Csalinity&longitude=-71.321&latitude=41.579&time%3E=1985-03-31T13%3A15%3A00Z&time%3C=2020-03-30T10%3A08%3A00Z'
r = requests.get(url, allow_redirects=True)
open('test.nc', 'wb').write(r.content)
nc = NetCDFFile('test.nc')
nc
#defining dataset variables:
time = nc.variables['time'][:]
time = nc.variables['time'][:]
mapvar = nc.variables['temperature'][:]
salinity = nc.variables['salinity'][:]
depth = nc.variables['depth'][:]
lat = nc.variables['latitude'][:]
#making a list of times for graphing:
timearray = []
for i in time:
timearray.append(datetime.fromtimestamp(i))
plt.plot(timearray, mapvar)
plt.xlabel('Date')
plt.ylabel('Sea Surface Temperature (Celsius)')
plt.title('Temp over time')
plt.show()
plt.plot(timearray, salinity)
plt.xlabel('Date')
plt.ylabel('Sea Surface Salinity')
plt.title('Salinity over time')
plt.show()