Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Merge branch 'CLIMATE-388' of https://github.com/MichaelArthurAnderso…
Browse files Browse the repository at this point in the history
  • Loading branch information
lewismc committed Jan 17, 2018
2 parents a9fbf74 + 410d8dc commit 47940c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
71 changes: 35 additions & 36 deletions ocw-ui/backend/rcmed_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
# limitations under the License.
#

''' Services for interacting with NASA JPL's Regional Climate Model Evaluation Database. '''

import ocw.data_source.rcmed as rcmed
""" Services for interacting with NASA JPL's Regional Climate Model Evaluation Database. """

import requests
from bottle import Bottle, request, response

import requests
import ocw.data_source.rcmed as rcmed

rcmed_app = Bottle()

@rcmed_app.route('/datasets/')

@rcmed_app.get('/datasets/')
def get_observation_dataset_data():
''' Return a list of dataset information from JPL's RCMED.
""" Return a list of dataset information from JPL's RCMED.
**Example Return JSON Format**
Expand All @@ -42,20 +42,21 @@ def get_observation_dataset_data():
},
...
]
'''
r = requests.get('http://rcmes.jpl.nasa.gov/query-api/datasets.php')
"""
result = requests.get('http://rcmes.jpl.nasa.gov/query-api/datasets.php')

if (request.query.callback):
return "%s(%s)" % (request.query.callback, r.text)
return r.text
if hasattr(request.query, 'callback'):
return "%s(%s)" % (request.query.callback, result.text)
return result.text

@rcmed_app.route('/parameters/')
def get_dataset_parameters():
''' Return dataset specific parameter information from JPL's RCMED.

@rcmed_app.get('/parameters/dataset/<name>')
def get_dataset_parameters(name):
""" Return dataset specific parameter information from JPL's RCMED.
**Example Call Format**
.. sourcecode:: javascript
.. sourcecode:: text
/parameters/?dataset=<dataset's short name>
Expand All @@ -73,17 +74,18 @@ def get_dataset_parameters():
}
]
'''
url = 'http://rcmes.jpl.nasa.gov/query-api/parameters.php?dataset=' + request.query.dataset
r = requests.get(url)
"""

url = 'http://rcmes.jpl.nasa.gov/query-api/parameters.php?dataset=' + name
result = requests.get(url)

if (request.query.callback):
return "%s(%s)" % (request.query.callback, r.text)
return r.text
if hasattr(request.query, 'callback'):
return "%s(%s)" % (request.query.callback, result.text)
return result.text


def extract_bounds(parameter):
''' This will take a parameter dictionary and return the spatial and temporal bounds.
""" This will take a parameter dictionary and return the spatial and temporal bounds.
:param parameter: Single parameter that is returned from rcmed.get_parameters_metadata().
:type parameter: dictionary:
Expand All @@ -97,11 +99,9 @@ def extract_bounds(parameter):
"lon_max": 179.75,
"lon_min": -179.75
}
'''
bounds_data = {}
bounds_data['start_date'] = str(parameter['start_date'])
bounds_data['end_date'] = str(parameter['end_date'])
spatial_bounds = parameter['bounding_box'].replace('(','').replace(')','')
"""
bounds_data = {'start_date': str(parameter['start_date']), 'end_date': str(parameter['end_date'])}
spatial_bounds = parameter['bounding_box'].replace('(', '').replace(')', '')
spatial_bounds = spatial_bounds.split(',')
# spatial_bounds is in format:
# [<lat_max>, <lon_max>, <lat_min>, <lon_max>, <lat_min>, <lon_min>, <lat_max>, <lon_min>]
Expand All @@ -110,18 +110,18 @@ def extract_bounds(parameter):
bounds_data['lat_min'] = float(spatial_bounds[2])
bounds_data['lon_max'] = float(spatial_bounds[1])
bounds_data['lon_min'] = float(spatial_bounds[5])
param_id =str(parameter['parameter_id'])
param_id = str(parameter['parameter_id'])
return param_id, bounds_data


@rcmed_app.route('/parameters/bounds/')
@rcmed_app.route('/parameters/bounds')
@rcmed_app.get('/parameters/bounds/')
@rcmed_app.get('/parameters/bounds')
def get_parameters_bounds():
''' Return temporal and spatial bounds metadata for all of JPL's RCMED parameters.
""" Return temporal and spatial bounds metadata for all of JPL's RCMED parameters.
**Example Call Format**
.. sourcecode:: javascript
.. sourcecode:: text
/parameters/bounds/
Expand All @@ -148,19 +148,18 @@ def get_parameters_bounds():
}
}
'''
"""
parameter_bounds = {}
raw_parameters = rcmed.get_parameters_metadata()
for parameter in raw_parameters:
if parameter['bounding_box'] != None:
if parameter['bounding_box'] is not None:
param_id, bounds_data = extract_bounds(parameter)
parameter_bounds[param_id] = bounds_data


return parameter_bounds


@rcmed_app.hook('after_request')
def enable_cors():
''' Allow Cross-Origin Resource Sharing for all URLs. '''
""" Allow Cross-Origin Resource Sharing for all URLs. """
response.headers['Access-Control-Allow-Origin'] = '*'
4 changes: 2 additions & 2 deletions ocw-ui/frontend/app/scripts/controllers/rcmedselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ angular.module('ocwUiApp')
};

$scope.dataSelectUpdated = function() {
var urlString = $rootScope.baseURL + '/rcmed/parameters/?dataset=' +
var urlString = $rootScope.baseURL + '/rcmed/parameters/dataset/' +
$scope.datasetSelection["shortname"] +
"&callback=JSON_CALLBACK";
"?callback=JSON_CALLBACK";
$http.jsonp(urlString)
.success(function(data) {
$scope.retrievedObsParams = data;
Expand Down
8 changes: 4 additions & 4 deletions ocw-ui/frontend/test/spec/controllers/rcmedselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ describe('Controller: RcmedSelectionCtrl', function () {
scope.datasetSelection = {shortname: 'TRMM'}

// Test return with only single parameter
$httpBackend.expectJSONP($rootScope.baseURL + '/rcmed/parameters/?dataset=' +
$httpBackend.expectJSONP($rootScope.baseURL + '/rcmed/parameters/dataset/' +
scope.datasetSelection['shortname'] +
'&callback=JSON_CALLBACK').
'?callback=JSON_CALLBACK').
respond(200, ['pcp']);
scope.dataSelectUpdated();
$httpBackend.flush();

expect(scope.parameterSelection).toEqual('pcp');

// Test return with multiple parameters
$httpBackend.expectJSONP($rootScope.baseURL + '/rcmed/parameters/?dataset=' +
$httpBackend.expectJSONP($rootScope.baseURL + '/rcmed/parameters/dataset/' +
scope.datasetSelection['shortname'] +
'&callback=JSON_CALLBACK').
'?callback=JSON_CALLBACK').
respond(200, ['pcp', 'pcp2']);
scope.dataSelectUpdated();
$httpBackend.flush();
Expand Down

0 comments on commit 47940c4

Please sign in to comment.