Exemple #1
0
    def granule_download(self, query_string, path=''):
        ''' Granule Download service submits a job to subset and download. Upon a successful request,\
            token will be returned which can be used to check status.

            :param query_string: data collection query json as a string.
            :type query_string: :mod:`string`

            :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.
        '''
        params = urlencode({'query': query_string})
        headers = {
            "Content-type": "application/x-www-form-urlencoded",
            "Accept": "*"
        }
        connection = HTTPSConnection("podaac-tools.jpl.nasa.gov")
        connection.request("POST", "/l2ss-services/l2ss/subset/submit", params,
                           headers)
        response = connection.getresponse()
        data = response.read().decode('utf-8')
        result = json.loads(data)
        token = result['token']
        connection.close()

        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"
                )
            if status == "partial error":
                raise Exception(
                    "The job was done but with some errors, please submit the job again"
                )
            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)
Exemple #2
0
    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. \
        NOTE : At present PODAAC's granule subsetting service is only \
        restricted to Level2 granules.

        :param input_file_path: path to a json file which contains the \
        the request that you want to send to PO.DAAC
        :type input_file_path: :mod:`string`

        :param path: path to a directory where you want the subsetted \
        dataset to be stored.
        :type path: :mod:`string`

        :returns: a string token which can be used to determine the status\
        of a subset task.

        '''
        data = open(input_file_path, 'r+')
        input_data = json.load(data)
        input_string = json.dumps(input_data)

        # submit subset request
        params = urlencode({'query': input_string})
        headers = {
            "Content-type": "application/x-www-form-urlencoded",
            "Accept": "*"
        }
        conn = HTTPSConnection("podaac.jpl.nasa.gov")
        conn.request("POST", "/ws/subset/granule?request=submit", params,
                     headers)
        response = conn.getresponse()

        data = response.read().decode('utf-8')
        result = json.loads(data)
        token = result['token']
        conn.close()

        flag = 0
        while flag == 0:
            url = url = self.URL + "subset/status?token=" + token
            subset_response = requests.get(url, headers=HEADERS).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 during subset job, post your issue to the PO.DAAC forum https://podaac.jpl.nasa.gov/forum/"
                )
            time.sleep(1)

        download_url = subset_response_json['resultURLs'][0]
        split = download_url.split('/')
        length = len(split)
        zip_file_name = split[length - 1]
        if path == '':
            zip_path = os.path.join(os.path.dirname(__file__), zip_file_name)
        else:
            zip_path = path + '/' + zip_file_name
        response = urlretrieve(download_url, zip_path)
        zip_content = zipfile.ZipFile(zip_path)
        for i in zip_content.infolist():
            granule_name = i.filename
        zip_content.extractall(path=path)
        print(
            "Podaacpy completed granule subset task for granule '%s'. Granule available at '%s'."
            % (granule_name, path))
        os.remove(zip_path)
        return granule_name