You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 5 Next »
This example creates a request for a Time Series Scalar Data data product for the Barkley Canyon / Axis (POD 1) 150 kHz ADCP and returns information about the request. The returns a Request Id, which can be used to run the data product.
import requests import json url = 'http://dmas.uvic.ca/api/dataProductDelivery' parameters = {'method':'request', 'token':'YOUR_TOKEN_HERE', # replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile 'locationCode':'BACAX', # Barkley Canyon / Axis (POD 1) 'deviceCategoryCode':'ADCP2MHZ', # 150 kHz Acoustic Doppler Current Profiler 'dataProductCode':'TSSD', # Time Series Scalar Data 'extension':'csv', # Comma Separated spreadsheet file 'begin':'2016-07-27T00:00:00.000Z', 'end':'2016-08-01T00:00:00.000Z', 'dpo_qualityControl':1, 'dpo_resample':'none', 'dpo_dataGaps':0} response = requests.get(url,params=parameters) if (response.ok): requestInfo = json.loads(str(response.content,'utf-8')) # convert the json response to an object print('Request Id: {}'.format(requestInfo['dpRequestId'])) # Print the Request Id if ('numFiles' in requestInfo.keys()): print('File Count: {}'.format(requestInfo['numFiles'])) # Print the Estimated File Size if ('fileSize' in requestInfo.keys()): print('File Size: {}'.format(requestInfo['fileSize'])) # Print the Estimated File Size if 'downloadTimes' in requestInfo.keys(): print('Estimated download time:') for e in sorted(requestInfo['downloadTimes'].items(),key=lambda t: t[1]): print(' {} - {} sec'.format(e[0],'{:0.2f}'.format(e[1]))) if 'estimatedFileSize' in requestInfo.keys(): print('Estimated File Size: {}'.format(requestInfo['estimatedFileSize'])) if 'estimatedProcessingTime' in requestInfo.keys(): print('Estimated Processing Time: {}'.format(requestInfo['estimatedProcessingTime'])) else: if(response.status_code == 400): error = json.loads(str(response.content,'utf-8')) print(error) # json response contains a list of errors, with an errorMessage and parameter else: print ('Error {} - {}'.format(response.status_code,response.reason))
options = weboptions('Timeout',60); try requestInfo = webread('http://dmas.uvic.ca/api/dataProductDelivery',... 'method','request',... 'token','YOUR_TOKEN_HERE',... %replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile 'locationCode','BACAX',... %Barkley Canyon / Axis (POD 1) 'deviceCategoryCode','ADCP2MHZ',... %150 kHz Acoustic Doppler Current Profiler 'dataProductCode','TSSD',... %Time Series Scalar Data 'extension','csv',... %Comma Separated spreadsheet file 'begin','2016-07-27T00:00:00.000Z',... 'end','2016-08-01T00:00:00.000Z',... 'dpo_qualityControl',1,... 'dpo_resample','none',... 'dpo_dataGaps',0,... options); disp(requestInfo) catch ME if (strcmp(ME.identifier,'MATLAB:webservices:HTTP400StatusCodeError')) disp(ME.message); else disp(ME); end end
library(httr) r <- GET("http://dmas.uvic.ca/api/dataProductDelivery", query = list(method="request", token="YOUR_TOKEN_HERE", #>replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile locationCode="BACAX", #>Barkley Canyon / Axis (POD 1) deviceCategoryCode="ADCP2MHZ", #>150 kHz Acoustic Doppler Current Profiler dataProductCode="TSSD", #>Time Series Scalar Data extension="csv", #>Comma Separated spreadsheet file begin="2016-07-27T00:00:00.000Z", end="2016-08-01T00:00:00.000Z", dpo_qualityControl=1, dpo_resample="none", dpo_dataGaps=0)) if (http_error(r)) { if (r$status_code == 400){ error = content(r) str(error) } else { str(http_status(r)$message) } } else { requestInfo = content(r) str(requestInfo) }
This example runs a data product request using a Request Id returned from the 'Request Data Product' example
import requests import json url = 'http://dmas.uvic.ca/api/dataProductDelivery' parameters = {'method':'run', 'token':'YOUR_TOKEN_HERE', # replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile 'dpRequestId':YOUR_REQUEST_ID_HERE} # replace YOUR_REQUEST_ID_HERE with a requestId number returned from the request method response = requests.get(url,params=parameters) if (response.ok): runs = json.loads(str(response.content,'utf-8')) # convert the json response to an object for runId in runs['dpRunIds']: print('Run Id: {}'.format(runId)) # Print each of the Run Ids else: if(response.status_code == 400): error = json.loads(str(response.content,'utf-8')) print(error) # json response contains a list of errors, with an errorMessage and parameter else: print ('Error {} - {}'.format(response.status_code,response.reason))
options = weboptions('Timeout',60); try runs = webread('http://dmas.uvic.ca/api/dataProductDelivery',... 'method','run',... 'token','YOUR_TOKEN_HERE',... %replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile 'dpRequestId',YOUR_REQUEST_ID_HERE,... %replace YOUR_REQUEST_ID_HERE with a requestId number returned from the request method options); for i=1:numel(runs) run = runs(i); disp(sprintf('Run Id: %s',run)); end catch ME if (strcmp(ME.identifier,'MATLAB:webservices:HTTP400StatusCodeError')) disp(ME.message); else disp(ME); end end
library(httr) r <- GET("http://dmas.uvic.ca/api/dataProductDelivery", query = list(method="run", token="YOUR_TOKEN_HERE", #>replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile dpRequestId=YOUR_REQUEST_ID_HERE)) #>replace YOUR_REQUEST_ID_HERE with a requestId number returned from the request method if (http_error(r)) { if (r$status_code == 400){ error = content(r) str(error) } else { str(http_status(r)$message) } } else { runs = content(r) for (run in runs){ cat(sprintf("Run Id: %s",run)) } }
This example downloads all of the files generated by a data product request using a Run Id returned from the 'Run Data Product Request' example
import requests import json import os from contextlib import closing import errno url = 'http://dmas.uvic.ca/api/dataProductDelivery' parameters = {'method':'download', 'token':'YOUR_TOKEN_HERE', # replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile. 'dpRunId':YOUR_RUN_ID, # replace YOUR_RUN_ID with the dpRunId returned from the 'run' method. 'index':1} # for run requests that contain more than one file, change the index number to the index of the file you would like to download. # If the index number does not exist an HTTP 410 and a message will be returned. outPath='c:/temp' # replace with the file location you would like the file to be downloaded to. with closing(requests.get(url,params=parameters,stream=True)) as streamResponse: if streamResponse.status_code == 200: #OK if 'Content-Disposition' in streamResponse.headers.keys(): content = streamResponse.headers['Content-Disposition'] filename = content.split('filename=')[1] else: print('Error: Invalid Header') streamResponse.close() sys.exit(-1) filePath = '{}/{}'.format(outPath,filename) try: if (not os.path.isfile(filePath)): #Create the directory structure if it doesn't already exist try: os.makedirs(outPath) except OSError as exc: if exc.errno == errno.EEXIST and os.path.isdir(outPath): pass else: raise print ("Downloading '{}'".format(filename)) with open(filePath,'wb') as handle: try: for block in streamResponse.iter_content(1024): handle.write(block) except KeyboardInterrupt: print('Process interupted: Deleting {}'.format(filePath)) handle.close() streamResponse.close() os.remove(filePath) sys.exit(-1) else: print (" Skipping '{}': File Already Exists".format(filename)) except: msg = 'Error streaming response.' print(msg) else: if(streamResponse.status_code in [202,204,400,404,410]): payload = json.loads(str(streamResponse.content))# for 3.x use str(streamResponse.content,'utf-8') if len(payload) >= 1: msg = payload['message'] print('HTTP {} - {}: {}'.format(streamResponse.status_code,streamResponse.reason,msg)) else: print ('Error {} - {}'.format(streamResponse.status_code,streamResponse.reason)) streamResponse.close()
options = weboptions('Timeout',60); try runs = webread('http://dmas.uvic.ca/api/dataProductDelivery',... 'method','run',... 'token','YOUR_TOKEN_HERE',... %replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile 'dpRequestId',YOUR_REQUEST_ID_HERE,... %replace YOUR_REQUEST_ID_HERE with a requestId number returned from the request method options); for i=1:numel(runs) run = runs(i); disp(sprintf('Run Id: %s',run)); end catch ME if (strcmp(ME.identifier,'MATLAB:webservices:HTTP400StatusCodeError')) disp(ME.message); else disp(ME); end end
library(httr) r <- GET("http://dmas.uvic.ca/api/dataProductDelivery", query = list(method="run", token="YOUR_TOKEN_HERE", #>replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at http://dmas.uvic.ca/Profile dpRequestId=YOUR_REQUEST_ID_HERE)) #>replace YOUR_REQUEST_ID_HERE with a requestId number returned from the request method if (http_error(r)) { if (r$status_code == 400){ error = content(r) str(error) } else { str(http_status(r)$message) } } else { runs = content(r) for (run in runs){ cat(sprintf("Run Id: %s",run)) } }
- No labels