Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
add Gridmet_current for ONHM
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcd-mscb committed Jul 16, 2020
1 parent f8acf22 commit cbb5df0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions gridmetetl/Gridmet_current.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import urllib3
import xmltodict
from datetime import datetime, timedelta
import sys

def getxml(url):
http = urllib3.PoolManager()

response = http.request('GET', url)
try:
data = xmltodict.parse(response.data)
except:
print("Failed to parse XML from response (%s)" % traceback.format_exc())
return data


# "http://thredds.northwestknowledge.net:8080/thredds/ncss/grid/agg_met_tmmx_1979_CurrentYear_CONUS.nc/dataset.xml"
# http://thredds.northwestknowledge.net:8080/thredds/ncss/grid/agg_met_tmmn_1979_CurrentYear_CONUS.nc/dataset.xml
# http://thredds.northwestknowledge.net:8080/thredds/ncss/grid/agg_met_pr_1979_CurrentYear_CONUS.nc/dataset.xml
# http://thredds.northwestknowledge.net:8080/thredds/ncss/grid/agg_met_rmin_1979_CurrentYear_CONUS.nc/dataset.xml
# http://thredds.northwestknowledge.net:8080/thredds/ncss/grid/agg_met_rmax_1979_CurrentYear_CONUS.nc/dataset.xml
# http://thredds.northwestknowledge.net:8080/thredds/ncss/grid/agg_met_vs_1979_CurrentYear_CONUS.nc/dataset.xml

serverURL = 'http://thredds.northwestknowledge.net:8080/thredds/ncss/grid'

data_packets = ['agg_met_tmmn_1979_CurrentYear_CONUS.nc', 'agg_met_pr_1979_CurrentYear_CONUS.nc',
'agg_met_rmin_1979_CurrentYear_CONUS.nc', 'agg_met_rmax_1979_CurrentYear_CONUS.nc',
'agg_met_vs_1979_CurrentYear_CONUS.nc']
urlsuffix = 'dataset.xml'

now = datetime.today().date()
yesterday = now - timedelta(days=1)

for data in data_packets:
masterURL = serverURL + '/' + data + '/' + urlsuffix
datadef = getxml(masterURL)['gridDataset']['TimeSpan']['end']
gm_date = datetime.strptime(datadef[:10],'%Y-%m-%d').date()
if gm_date != yesterday:
print(f'Gridmet data {data} is not available:\n' +
'process exiting')
sys.exit(1)

0 comments on commit cbb5df0

Please sign in to comment.