You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 54

This page contains common usage examples of client library methods that fit common use cases. The examples work with the latest client library version available.

Click "> Expand source" to display each code example.

Discovery examples

1. Get all locations in North East Pacific with Hydrophones deployed
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode':'NEP',     #NEP - North East Pacific
    'deviceCategoryCode':'HYDROPHONE',
    'includeChildren':'true'
}
locations = onc.getLocations(filters)
onc.print(locations)

2. Get all deployments for device WET Labs ECO FLNTU 1087
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'deviceCode':'WLFLNTU1087'    # WET Labs ECO FLNTU 1087
}
deployments = onc.getDeployments(filters)
onc.print(deployments)

3. Get devices deployed at Barkley Canyon - Axis, from June 1, 2016 to May 31, 2017
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode':'BACAX',    #BACAX - Barkley Canyon / Axis
    'dateFrom':'2016-06-01T00:00:00.000Z',
    'dateTo':'2017-05-31T23:59:59.999Z'
}
devices = onc.getDevices(filters)
onc.print(devices)

4. Get all device categories available at Barkley Canyon - Upper Slope
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode':'NCBC'    # NCBC - Barkely Canyon / Upper Slope
}
categories = onc.getDeviceCategories(filters)
onc.print(categories)

5. Get all properties available at Barkley Canyon
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode':'BACAX'    # BACAX - Barkley Canyon / Axis
}
properties = onc.getProperties(filters)
onc.print(properties)

6. Get all data products available with MATLAB .mat extension
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'extension': 'mat'    # mat - MATLAB
}
dataProducts = onc.getDataProducts(filters)
onc.print(dataProducts)

Data product download examples

1. Get all data products available with MATLAB .mat extension
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'extension': 'mat'    # mat - MATLAB
}
dataProducts = onc.getDataProducts(filters)
onc.print(dataProducts)

2. Download Time Series Scalar Data Product in CSV format for ADCP 2 MHZ
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode':'BACAX',
    'deviceCategoryCode':'ADCP2MHZ',
    'dataProductCode':'TSSD',
    'extension':'csv',
    'dateFrom':'2016-07-27T00:00:00.000Z',
    'dateTo':'2016-08-01T00:00:00.000Z',
    'dpo_qualityControl':1,
    'dpo_resample':'none',
    'dpo_dataGaps':0
}
results = onc.orderDataProduct(filters)
onc.print(results)

3. Download 6 months of hydrophone files, 1 hour at a time
# If your data is in the ONC archive, the archive files example 7
# might be noticeably faster for downloading files

from datetime import datetime, timedelta
from dateutil.parser import parse
from time import sleep
from onc.onc import ONC

onc = ONC('YOUR_TOKEN')

# start and end date
dateFrom = parse('2019-01-01T00:00:00.000Z')
dateTo   = parse('2019-06-01T00:00:00.000Z')

# time to add to dateFrom
step = timedelta(hours=1)

# use a loop to download 1 hour at a time
while dateFrom < dateTo:
    txtDate = dateFrom.strftime("%Y-%m-%dT%H:%M:%S.000Z")
    print("\nDownloading data from: {:s}\n".format(txtDate))
    
    # Documentation on "diversion mode":
    # https://wiki.oceannetworks.ca/display/DP/Hydrophone+Data+Acquisition+and+Diversion+Mode
    filters = {
        'dataProductCode'   : 'AD',
        'locationCode'      : 'BACND',
        'deviceCategoryCode': 'HYDROPHONE',
        'dateFrom'          : txtDate,
        'dateTo'            : 'PT1H',
        'extension'         : 'mp3',     # Could be wav
        'dpo_hydrophoneDataDiversionMode': 'OD'
    }
    result = onc.orderDataProduct(filters, includeMetadataFile=False)
    
    dateFrom += step
    sleep(1)

print("\nFinished!")

Near real-time data examples

1. Get the last Thermosalinograph reading from Tswwassen - Duke Point Ferry
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode': 'TWDP',
    'deviceCategoryCode':'TSG',
    'rowLimit': '1',
    'getLatest': 'true'
}
result = onc.getDirectByLocation(filters)
onc.print(result)

2. Get 1 minute of Thermosalinograph readings from Tswwassen - Duke Point Ferry
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode': 'TWDP',
    'deviceCategoryCode':'TSG',
    'dateFrom':'2016-09-01T00:00:00.000Z',
    'dateTo':'2016-09-01T00:01:00.000Z'
}
result = onc.getDirectByLocation(filters)
onc.print(result)

3. Get 10 seconds of readings from a specific device
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'deviceCode': 'AMLMETRECX50348',  # A CTD in Burrard Inlet
    'dateFrom':'2019-06-01T00:00:00.000Z',
    'dateTo':'2019-06-01T00:00:10.000Z'
}
result = onc.getDirectByDevice(filters)
onc.print(result)

3. Get the last raw instrument reading from an AIS Reciever
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode': 'IONA',
    'deviceCategoryCode': 'AISRECEIVER',
    'rowLimit': '1',
    'getLatest': 'true'
}
result = onc.getDirectRawByLocation(filters)
onc.print(result)

4. Get 10 seconds of raw CTD readings from Barkley Canyon Axis
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
     'locationCode':'BACAX',
     'deviceCategoryCode':'CTD',
     'dateFrom':'2017-05-23T00:00:00.000Z',
     'dateTo':'2017-05-23T00:00:10.000Z'
}
result = onc.getDirectRawByLocation(filters)
onc.print(result)

Archived file download examples

1. Get a list of ".ruv" archived files from a radar in one day
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'locationCode': 'VION',
    'deviceCategoryCode': 'OCEANOGRAPHICRADAR',
    'extension': 'ruv',
    'dateFrom': '2018-11-07T00:00:00.000Z',
    'dateTo': '2018-11-08T00:00:00.000Z'
}
results = onc.getListByLocation(filters)
onc.print(results)

2. Get a list of all archived files available in a device in one day
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

filters = {
    'deviceCode':'RDIADCP600WH25471',    # AML CTD Metrec X 50348 in Burrard Inlet
    'dateFrom': '2019-06-07T00:00:00.000Z',
    'dateTo': '2019-06-08T00:00:00.000Z'
}
results = onc.getListByDevice(filters)
onc.print(results)

3. Download a file by its filename
from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

result = onc.getFile('BC_POD2_JB_20181107T000000.000Z.txt')
onc.print(result)

4. List .mp3 files from a hydrophone in the last 2 hours
import datetime

from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

# Get the current ISO8601 timestamp, without milliseconds
now = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + '.000Z'

filters = {
    'locationCode': 'SEVIP',            # Strait of Georgia East
    'deviceCategoryCode': 'HYDROPHONE', # Hydrophones
    'dateFrom': '-PT2H',                # Minus 2 hours from dateTo
    'dateTo': now,
    'extension': 'mp3'                  # "mp3" files, could be "wav"
}

# list available files
result = onc.getListByDevice(filters)
onc.print(result)

5. Download all .mp3 files from a hydrophone in the last 2 hours
import datetime

from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

# Get the current ISO8601 timestamp, without milliseconds
now = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + '.000Z'

filters = {
    'locationCode': 'SEVIP',            # Strait of Georgia East
    'deviceCategoryCode': 'HYDROPHONE', # Hydrophones
    'dateFrom': '-PT2H',                # Minus 2 hours from dateTo
    'dateTo': now,
    'extension': 'mp3'                  # "mp3" files, could be "wav"
}

# download available files (will skip existing files)
result = onc.getDirectFiles(filters)
onc.print(result)

6. Repeatedly download the latest .mp3 files from a hydrophone every 2 hours
import datetime
import time

from onc.onc import ONC
onc = ONC('YOUR_TOKEN_HERE')

while True:
    # Get the current ISO8601 timestamp, without milliseconds
    now = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + '.000Z'

    filters = {
        'locationCode': 'SEVIP',            # Strait of Georgia East
        'deviceCategoryCode': 'HYDROPHONE', # Hydrophones
        'dateFrom': '-PT2H',                # Minus 2 hours from dateTo
        'dateTo': now,
        'extension': 'mp3'                  # "mp3" files, could be "wav"
    }

    # download files found, skip existing files
    result = onc.getDirectFiles(filters)
    
    # wait 2 hours (7200 seconds)
    time.sleep(60 * 60 * 2)

7. Download 6 months of hydrophone files, 1 hour at a time
from datetime import datetime, timedelta
from dateutil.parser import parse
from time import sleep
from onc.onc import ONC

onc = ONC('YOUR_TOKEN')

# start and end date
dateFrom = parse('2019-01-01T00:00:00.000Z')
dateTo   = parse('2019-06-01T00:00:00.000Z')

# time to add to dateFrom
step = timedelta(hours=1)

# use a loop to download 1 hour at a time
while dateFrom < dateTo:
    txtDate = dateFrom.strftime("%Y-%m-%dT%H:%M:%S.000Z")
    print("\nDownloading data from: {:s}\n".format(txtDate))
    
    filters = {
        'locationCode'      : 'BACND',
        'deviceCategoryCode': 'HYDROPHONE',
        'dateFrom'          : txtDate,
        'dateTo'            : 'PT1H',
        'extension'         : 'mp3',     # Could be wav
    }
    result = onc.getDirectFiles(filters)
    
    dateFrom += step
    sleep(1)

print("\nFinished!")
  • No labels