<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Geospatial Selections and ROVCTD data
Connect to a remote database, select data using GeoDjango's spatial lookup, and make some simple plots
Executing this Notebook requires a personal STOQS server. Follow the steps to build your own development system &mdash; this will take a few hours and depends on a good connection to the Internet.  Once your server is up log into it (after a cd ~/Vagrants/stoqsvm) and activate your virtual environment with the usual commands
Step1:  Count all of the the ROV dives whose Measurements are near MARS
Step2:  Near surface ROV location data is notoriously noisy (because of fundamental inaccuracies of USBL navigation systems). Let's remove near surface Measurment values from our selection. Count all of the dives near MARS and whose Measurments are deeper than 800 m.
Step3:  Let's plot the measurement points of dives on a map of Monterey Bay to confirm that the selection is in the right spot.
Step4:  (The major cluster is around the MARS site, but there are a few spurious navigation points even for the deep dive data.)
Let's plot CTD profiles for these dives.
<ASSISTANT_TASK:>
Python Code:
db = 'stoqs_rovctd_mb'
from django.contrib.gis.geos import fromstr
from django.contrib.gis.measure import D
mars = fromstr('POINT(-122.18681000 36.71137000)')
near_mars = Measurement.objects.using(db).filter(geom__distance_lt=(mars, D(km=.1)))
Explanation: Geospatial Selections and ROVCTD data
Connect to a remote database, select data using GeoDjango's spatial lookup, and make some simple plots
Executing this Notebook requires a personal STOQS server. Follow the steps to build your own development system &mdash; this will take a few hours and depends on a good connection to the Internet.  Once your server is up log into it (after a cd ~/Vagrants/stoqsvm) and activate your virtual environment with the usual commands:
vagrant ssh -- -X
cd /vagrant/dev/stoqsgit
source venv-stoqs/bin/activate
Connect to your Institution's STOQS database server using read-only credentials. (Note: firewalls typically limit unprivileged access to such resources.)
cd stoqs
ln -s mbari_campaigns.py campaigns.py
export DATABASE_URL=postgis://everyone:guest@kraken.shore.mbari.org:5433/stoqs
Launch Jupyter Notebook on your system with:
cd contrib/notebooks
../../manage.py shell_plus --notebook
navigate to this file and open it. You will then be able to execute the cells and experiment with this notebook.
For reference please see for GeoDjango Spatial Lookups and the STOQS schema diagram.
Define our database and a GeoDjango query set for data within 0.1 km of the MARS site.
End of explanation
mars_dives = Activity.objects.using(db).filter(instantpoint__measurement__in=near_mars
                                              ).distinct()
print(mars_dives.count())
Explanation: Count all of the the ROV dives whose Measurements are near MARS
End of explanation
deep_mars_dives = Activity.objects.using(db
                                        ).filter(instantpoint__measurement__in=near_mars,
                                                 instantpoint__measurement__depth__gt=800
                                                ).distinct()
print(deep_mars_dives.count())
Explanation: Near surface ROV location data is notoriously noisy (because of fundamental inaccuracies of USBL navigation systems). Let's remove near surface Measurment values from our selection. Count all of the dives near MARS and whose Measurments are deeper than 800 m.
End of explanation
%%time
%matplotlib inline
import pylab as plt
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='cyl', resolution='l',
            llcrnrlon=-122.7, llcrnrlat=36.5,
            urcrnrlon=-121.7, urcrnrlat=37.0)
m.arcgisimage(server='http://services.arcgisonline.com/ArcGIS', service='Ocean_Basemap')
for dive in deep_mars_dives:
    points = Measurement.objects.using(db).filter(instantpoint__activity=dive,
                                                  instantpoint__measurement__depth__gt=800
                                                 ).values_list('geom', flat=True)
    m.scatter(
        [geom.x for geom in points],
        [geom.y for geom in points])
Explanation: Let's plot the measurement points of dives on a map of Monterey Bay to confirm that the selection is in the right spot.
End of explanation
%%time
# A Python dictionary comprehension for all the Parameters and axis labels we want to plot
parms = {p.name: '{} ({})'.format(p.long_name, p.units) for 
                 p in Parameter.objects.using(db).filter(name__in=
                        ('t', 's', 'o2', 'sigmat', 'spice', 'light'))}
plt.rcParams['figure.figsize'] = (18.0, 8.0)
fig, ax = plt.subplots(1, len(parms), sharey=True)
ax[0].invert_yaxis()
ax[0].set_ylabel('Depth (m)')
dive_names = []
for dive in deep_mars_dives.order_by('startdate'):
    dive_names.append(dive.name)
    # Use select_related() to improve query performance for the depth lookup
    # Need to also order by time
    mps = MeasuredParameter.objects.using(db
                                ).filter(measurement__instantpoint__activity=dive
                                ).select_related('measurement'
                                ).order_by('measurement__instantpoint__timevalue')
    depth = [mp.measurement.depth for mp in mps.filter(parameter__name='t')]
    for i, (p, label) in enumerate(parms.items()):
        ax[i].set_xlabel(label)
        try:
            ax[i].plot(mps.filter(parameter__name=p).values_list(
                    'datavalue', flat=True), depth)
        except ValueError:
            pass
from IPython.display import display, HTML
display(HTML('<p>All dives at MARS site: ' + ' '.join(dive_names) + '<p>'))
Explanation: (The major cluster is around the MARS site, but there are a few spurious navigation points even for the deep dive data.)
Let's plot CTD profiles for these dives.
End of explanation
<END_TASK>