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

Commit

Permalink
Merge pull request #86 from Omkar20895/master
Browse files Browse the repository at this point in the history
Adding extract_subset_granule
  • Loading branch information
Omkar20895 committed Oct 23, 2016
2 parents 124e499 + e1d7ddd commit d3bdfc7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
45 changes: 38 additions & 7 deletions podaac/podaac.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

import requests
from future.moves.urllib.parse import urlparse, urlencode
from future.moves.urllib.request import urlopen
from future.moves.urllib.request import urlopen, urlretrieve
from future.moves.urllib.error import HTTPError
from future.moves.http.client import HTTPConnection
import os
import zipfile
import json
import time
import xml.etree.ElementTree as ET

URL = 'http://podaac.jpl.nasa.gov/ws/'
Expand Down Expand Up @@ -470,7 +472,7 @@ def granule_preview(self, dataset_id='', image_variable='', path=''):

return image

def granule_subset(self, input_file_path):
def granule_subset(self, input_file_path, path=''):
'''Subset Granule service allows users to Submit subset jobs. \
Use of this service should be preceded by a Granule Search in \
order to identify and generate a list of granules to be subsetted.
Expand All @@ -479,8 +481,12 @@ def granule_subset(self, input_file_path):
the request that you want to send to PO.DAAC
:type input_file_path: :mod:`string`
:returns: a token on successful request reception. This can be \
further used to check the status of the request.
:param path: path to a directory where you want the subsetted \
dataset to be stored.
:type path: :mod:`string`
:returns: a zip file downloaded and extracted in the destination\
directory path provided.
'''
data = open(input_file_path, 'r+')
Expand All @@ -501,7 +507,32 @@ def granule_subset(self, input_file_path):
token = result['token']
conn.close()

return token
flag = 0
while(flag == 0):
url = url = self.URL + "subset/status?token=" + token
subset_response = requests.get(url).text
subset_response_json = json.loads(subset_response)
status = subset_response_json['status']
if (status == "done"):
flag = 1
if (status == "error"):
raise Exception(
"Unexpected error occured for the subset job you have requested")
time.sleep(1)

print("Done! downloading the dataset zip .....")
download_url = subset_response_json['resultURLs'][0]
split = download_url.split('/')
length = len(split)
zip_file_name = split[length-1]
if path == '':
path = os.path.join(os.path.dirname(__file__), zip_file_name)
else:
path = path + zip_file_name
response = urlretrieve(download_url, path)
zip_content = zipfile.ZipFile(path)
zip_content.extractall()
os.remove(path)

def subset_status(self, token=''):
'''Subset Granule Status service allows users to check the status \
Expand Down Expand Up @@ -562,10 +593,10 @@ def extract_l4_granule(self, dataset_id='', path=''):
else:
path = path + '/' + granule_name
data = urlopen(url)
granule = open(path, 'wb')
granule.write(data.read())
if data.info()['content-type'] == 'text/plain':
raise Exception("Unexpected Error Occured")
granule = open(path, 'wb')
granule.write(data.read())

except Exception:
raise
Expand Down
12 changes: 8 additions & 4 deletions podaac/tests/podaac_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,14 @@ def test_granule_preview(self):

# test case for the function granule_subset
def test_granule_subset(self):
path = os.path.dirname(os.path.abspath(__file__)) + "/test.json"
subset_token = self.podaac.granule_subset(input_file_path=path)

assert subset_token != ''
path1 = os.path.dirname(os.path.abspath(__file__)) + "/test.json"
path2 = os.path.abspath(__file__)
self.podaac.granule_subset(input_file_path=path1, path=path2)

assert os.path.isfile(
'./subsetted-ascat_20160409_113000_metopa_49153_eps_o_250_2401_ovw.l2.nc') == True
os.remove(
'./subsetted-ascat_20160409_113000_metopa_49153_eps_o_250_2401_ovw.l2.nc')

# test case for the function subset_status
def test_subset_status(self):
Expand Down

0 comments on commit d3bdfc7

Please sign in to comment.