Skip to content

Commit

Permalink
cache catalog to load faster
Browse files Browse the repository at this point in the history
  • Loading branch information
trngbich committed Mar 4, 2021
1 parent 137b3bf commit e1dbdee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions Modules/WaPOR/WaporAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import json
import pandas as pd
import datetime
import pickle
import os

class __WaPOR_API_class(object):
def __init__(self):
Expand All @@ -22,28 +24,36 @@ def __init__(self):
self.path_query=r'https://io.apps.fao.org/gismgr/api/v1/query/'
self.path_jobs=r'https://io.apps.fao.org/gismgr/api/v1/catalog/workspaces/WAPOR/jobs/'
self.workspaces={2: 'WAPOR_2'}
self.version=2
self.version=2
self.cached_catalog={2: os.path.join(os.path.dirname(__file__),'catalog_2.pickle')}

def getCatalog(self,level=None,cubeInfo=True):
def getCatalog(self,level=None,cubeInfo=True,cached=True):
'''
Get catalog from workspace
'''

print('Loading WaPOR catalog...')

try:
df=self._query_catalog(level)
except:
print('ERROR: The data with specified level version is not available in this version')
if cubeInfo:
cubes_measure=[]
cubes_dimension=[]
for cube_code in df['code'].values:
cubes_measure.append(self._query_cubeMeasures(cube_code))
cubes_dimension.append(self._query_cubeDimensions(cube_code))
df['measure']=cubes_measure
df['dimension']=cubes_dimension
self.catalog=df
if cached:
catalog_pickle=self.cached_catalog[self.version]
with open(catalog_pickle, 'rb') as handle:
self.catalog=pickle.load(handle)
else:
try:
df=self._query_catalog(level)
except:
print('ERROR: The data with specified level version is not available in this version')
if cubeInfo:
cubes_measure=[]
cubes_dimension=[]
for cube_code in df['code'].values:
cubes_measure.append(self._query_cubeMeasures(cube_code,
version=self.version))
cubes_dimension.append(self._query_cubeDimensions(cube_code,
version=self.version))
df['measure']=cubes_measure
df['dimension']=cubes_dimension
self.catalog=df
print('Loading WaPOR catalog...Done')
return self.catalog

def _query_catalog(self,level):
Expand Down Expand Up @@ -88,14 +98,14 @@ def getCubeInfo(self,cube_code):
except:
print('ERROR: Data for specified cube code and version is not available')

def _query_cubeMeasures(self,cube_code):
def _query_cubeMeasures(self,cube_code,version=1):
request_url = r'{0}{1}/cubes/{2}/measures?overview=false&paged=false'.format(self.path_catalog,
self.workspaces[self.version],cube_code)
resp = requests.get(request_url)
cube_measures = resp.json()['response'][0]
return cube_measures

def _query_cubeDimensions(self,cube_code):
def _query_cubeDimensions(self,cube_code,version=1):
request_url = r'{0}{1}/cubes/{2}/dimensions?overview=false&paged=false'.format(self.path_catalog,
self.workspaces[self.version],cube_code)
resp = requests.get(request_url)
Expand Down
Binary file added Modules/WaPOR/catalog_2.pickle
Binary file not shown.

0 comments on commit e1dbdee

Please sign in to comment.