Skip to content

Fix NLDAS data exploration notebook #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions examples/NLDAS_Data_Exploration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
"- Downloads data file(s) from NASA\n",
"- Show attribute statistics and visualizations\n",
"- Do viz-related data cleaning\n",
"- Show (corrected) attribute statistics and visualizations"
"- Show (corrected) attribute statistics and visualizations\n",
"\n",
"### Setup Instructions:\n",
"1. Create *.netrc* file in home dir according to [GES DISC site instructions](https://disc.gsfc.nasa.gov/information/howto?title=How%20to%20Download%20Data%20Files%20from%20HTTP%20Service%20with%20wget)\n",
"2. Create environment, install notebook pkgs, enable extension:\n",
"```\n",
"conda env create -n elm python=2.7 # 2.7 needed for pynio\n",
"source activate elm\n",
"conda install -c conda-forge pycurl lxml holoviews\n",
"jupyter nbextension enable --py widgetsnbextension # This should report \"OK\"\n",
"```"
]
},
{
Expand Down Expand Up @@ -179,9 +189,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": []
}
Expand All @@ -202,7 +210,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"version": "2.7.14"
}
},
"nbformat": 4,
Expand Down
56 changes: 36 additions & 20 deletions examples/example_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,54 @@

import requests
from six.moves.urllib.parse import urlparse
from six.moves import range
from six.moves import range, input
from lxml import etree, html
from ipywidgets import widgets, Layout
from IPython.display import display, Javascript
from pydap.cas.urs import setup_session


session = setup_session(
os.environ.get('NLDAS_USERNAME') or raw_input('NLDAS Username: '),
os.environ.get('NLDAS_PASSWORD') or getpass.getpass('Password: ')
)

def get_request(url):
import pycurl
from io import BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
return buffer.getvalue()
PYCURL = True

if not PYCURL:
from pydap.cas.urs import setup_session
session = setup_session(
os.environ.get('NLDAS_USERNAME') or input('NLDAS Username: '),
os.environ.get('NLDAS_PASSWORD') or getpass.getpass('Password: ')
)

def get_request(url, outfpath=None):
global PYCURL
if PYCURL:
# outfpath must be set
import pycurl
from io import BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.COOKIEJAR, '/tmp/cookie.jar')
c.setopt(c.NETRC, True)
c.setopt(c.FOLLOWLOCATION, True)
#c.setopt(c.REMOTE_NAME, outfpath)
c.perform()
c.close()
return buffer.getvalue()
resp = requests.get(url)
return resp.text

def dl_file(url):
data_fpath = urlparse(url).path.lstrip(os.sep)
data_dpath = os.path.dirname(data_fpath)
if not os.path.exists(data_fpath):
resp = session.get(url)
if not os.path.isdir(data_dpath):
os.makedirs(data_dpath)
with open(data_fpath, 'w') as outfp:
outfp.write(resp.content)
if PYCURL:
with open(data_fpath, 'w') as outfp:
outfp.write(get_request(url))
else:
resp = session.get(url)
with open(data_fpath, 'w') as outfp:
outfp.write(resp.content)
return data_fpath

def dups_to_indexes(field_names):
Expand Down