<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
Step1:  DRB Vizer json services
10/30/2015. Emilio Mayorga. 
Run with Emilio's "IOOS_test1" conda envioronment (recent versions of most packages)
http
Step2:  Service end point
For vizer instance that currently serves only the CRB and outlying areas, but will be expanded to the DRB to support the William Penn project needs.
Step3:  Meta info (metadata) requests
siso = "stationary in-situ observations" (eg, a river gage, weather stationg, moored buoy, etc)
Step4:  Examine all stations (siso assets) by first importing into a Pandas Dataframe
Step5:  Summaries (station counts) by platform_type and provider
Step6:  Request and examine one station
Step7:  Meta info request
Step8:  Data request
Step9:  Create dtutc column with parsed datetime. Also, it's safer to rename the "value" column to something unlikely to conflict with pandas method names.
<ASSISTANT_TASK:>
Python Code:
import json
import requests
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import datetime
import time
import calendar
import pytz
#from matplotlib.dates import date2num, num2date
utc_tz = pytz.utc
def epochsec_to_dt(epochsec):
     Return the datetime object for epoch seconds epochsec
    
    dtnaive_dt = datetime.datetime.utcfromtimestamp(epochsec)
    dtutc_dt   = dtnaive_dt.replace(tzinfo=pytz.utc)
    return dtutc_dt
def get_measurement_byvarid(metaresult, var_id):
    return [e for e in metaresult['measurements'] if e['var_id'] == var_id][0]
Explanation: DRB Vizer json services
10/30/2015. Emilio Mayorga. 
Run with Emilio's "IOOS_test1" conda envioronment (recent versions of most packages)
http://www.wikiwatershed-vs.org examples:
- meta requests
  - http://www.wikiwatershed-vs.org/services/get_asset_info.php?opt=meta&asset_type=siso
  - http://www.wikiwatershed-vs.org/services/get_asset_info.php?opt=meta&asset_type=siso&asset_id=CRBCZO_WCC019
- data & recent_values requests
  - http://www.wikiwatershed-vs.org/services/get_asset_info.php?opt=data&asset_type=siso&asset_id=CRBCZO_WCC019&var_id=all&units_mode=v1
  - http://www.wikiwatershed-vs.org/services/get_asset_info.php?opt=data&asset_type=siso&asset_id=CRBCZO_WCC019&var_id=H1_WaterTemp
  - http://www.wikiwatershed-vs.org/services/get_asset_info.php?opt=recent_values&asset_type=siso&asset_id=CRBCZO_WCC019&var_id=H1_WaterTemp&units_mode=v2
- plot request
  - http://www.wikiwatershed-vs.org/services/get_asset_info.php?opt=plot&asset_type=siso&asset_id=USGS_01474500&var_id=H1_WaterTemp&range=30d&y_axis_mode=global
Import packages and set up utility functions
End of explanation
vz_gai_url = "http://www.wikiwatershed-vs.org/services/get_asset_info.php"
Explanation: Service end point
For vizer instance that currently serves only the CRB and outlying areas, but will be expanded to the DRB to support the William Penn project needs.
End of explanation
meta_r = requests.get(vz_gai_url, params={'asset_type':'siso', 'opt':'meta'})
meta = meta_r.json()
meta.keys(), meta['success']
type(meta['result']), len(meta['result'])
# siso_id is the unique identifier (string type) for the station
siso_id_lst = [e['siso_id'] for e in meta['result']]
# Examine the response for the first station (index 0) in the returned list
meta['result'][0]['siso_id']
meta['result'][0]
Explanation: Meta info (metadata) requests
siso = "stationary in-situ observations" (eg, a river gage, weather stationg, moored buoy, etc)
End of explanation
stations_rec = []
for sta in meta['result']:
    sta_rec = {key:sta[key] for key in ['siso_id', 'name', 'lat', 'lon', 
                                        'platform_type', 'provider']}
    stations_rec.append(sta_rec)
stations_df = pd.DataFrame.from_records(stations_rec)
stations_df.set_index('siso_id', inplace=True, verify_integrity=True)
stations_df.index.name = 'siso_id'
print len(stations_df)
stations_df.head(10)
Explanation: Examine all stations (siso assets) by first importing into a Pandas Dataframe
End of explanation
stations_df.platform_type.value_counts()
stations_df.provider.value_counts()
Explanation: Summaries (station counts) by platform_type and provider
End of explanation
# USGS Schuylkill River at Philadelphia
siso_id = 'USGS_01474500'
Explanation: Request and examine one station
End of explanation
# (asset_id, a more generic descriptor for the unique id of any asset)
meta_r = requests.get(vz_gai_url, params={'asset_type':'siso', 'opt':'meta', 
                                          'asset_id':siso_id})
# use [0] to pull out the dict from the single-element list
metaresult = meta_r.json()['result'][0]  # ideally, should first test for success
# var_id is the unique identifier for a "measurement" (or variable)
[(d['var_id'], d['depth']) for d in metaresult['measurements']]
metaresult['name']
Explanation: Meta info request
End of explanation
data_r = requests.get(vz_gai_url, params={'asset_type':'siso', 'opt':'data', 'units_mode': 'v1',
                                          'asset_id':siso_id, 'var_id':'all'})
data = data_r.json()
data['success'], len(data['result']), data['result'][0].keys(), len(data['result'][0]['data'])
# Mapping of var_id string to 'result' list element index
var_ids = {e['var_id']:i for i,e in enumerate(data['result'])}
var_ids
var_id = 'H1_Discharge'
get_measurement_byvarid(metaresult, var_id)
data['result'][var_ids[var_id]]['data'][-10:]
# Pull out data time series for one variable based on var_id
# returns a list of dicts
data_lst = data['result'][var_ids[var_id]]['data']
data_df = pd.DataFrame.from_records(data_lst)
data_df.head()
Explanation: Data request
End of explanation
data_df['dtutc'] = data_df.time.map(lambda es: epochsec_to_dt(es))
data_df.set_index('dtutc', inplace=True, verify_integrity=True)
data_df.index.name = 'dtutc'
data_df = data_df.rename(columns={'value':var_id})
data_df.info()
data_df.head()
data_df.describe()
var_info = get_measurement_byvarid(metaresult, var_id)
title = "%s (%s) at %s" % (var_info['name'], var_id, metaresult['name'])
data_df[var_id].plot(title=title, figsize=[11,5])
plt.ylabel(var_info['units']);
Explanation: Create dtutc column with parsed datetime. Also, it's safer to rename the "value" column to something unlikely to conflict with pandas method names.
End of explanation
<END_TASK>