示例#1
0
def test_to_string():
    CMRparams = {
        'short_name': 'ATL06',
        'version': '002',
        'temporal': '2019-02-20T00:00:00Z,2019-02-28T23:59:59Z',
        'bounding_box': '-55,68,-48,71'
    }
    reqparams = {'page_size': 10, 'page_num': 1}
    params = apifmt.combine_params(CMRparams, reqparams)
    obs = apifmt.to_string(params)
    expected = ('short_name=ATL06&version=002'
                '&temporal=2019-02-20T00:00:00Z,2019-02-28T23:59:59Z'
                '&bounding_box=-55,68,-48,71&page_size=10&page_num=1')
    assert obs == expected
示例#2
0
def test_to_string():
    CMRparams = {
        "short_name": "ATL06",
        "version": "002",
        "temporal": "2019-02-20T00:00:00Z,2019-02-28T23:59:59Z",
        "bounding_box": "-55,68,-48,71",
    }
    reqparams = {"page_size": 2000, "page_num": 1}
    params = apifmt.combine_params(CMRparams, reqparams)
    obs = apifmt.to_string(params)
    expected = (
        "short_name=ATL06&version=002"
        "&temporal=2019-02-20T00:00:00Z,2019-02-28T23:59:59Z"
        "&bounding_box=-55,68,-48,71&page_size=2000&page_num=1"
    )
    assert obs == expected
示例#3
0
    def get_avail(self, CMRparams, reqparams):
        """
        Get a list of available granules for the query object's parameters.
        Generates the `avail` attribute of the granules object.

        Parameters
        ----------
        CMRparams : dictionary
            Dictionary of properly formatted CMR search parameters.
        reqparams : dictionary
            Dictionary of properly formatted parameters required for searching, ordering,
            or downloading from NSIDC.

        Notes
        -----
        This function is used by query.Query.avail_granules(), which automatically
        feeds in the required parameters.

        See Also
        --------
        APIformatting.Parameters
        query.Query.avail_granules
        """

        assert (CMRparams is not None and reqparams
                is not None), "Missing required input parameter dictionaries"

        # if not hasattr(self, 'avail'):
        self.avail = []

        granule_search_url = "https://cmr.earthdata.nasa.gov/search/granules"

        headers = {"Accept": "application/json", "Client-Id": "icepyx"}
        # note we should also check for errors whenever we ping NSIDC-API - make a function to check for errors

        params = apifmt.combine_params(
            CMRparams, {k: reqparams[k]
                        for k in ["page_size"]})

        cmr_search_after = None

        while True:
            if cmr_search_after is not None:
                headers["CMR-Search-After"] = cmr_search_after

            response = requests.get(
                granule_search_url,
                headers=headers,
                params=apifmt.to_string(params),
            )

            try:
                cmr_search_after = response.headers["CMR-Search-After"]
            except KeyError:
                cmr_search_after = None

            try:
                response.raise_for_status()
            except requests.HTTPError as e:
                if (
                        b"errors" in response.content
                ):  # If CMR returns a bad status with extra information, display that
                    raise icepyx.core.exceptions.NsidcQueryError(
                        response.json()["errors"]
                    )  # exception chaining will display original exception too
                else:  # If no 'errors' key, just reraise original exception
                    raise e

            results = json.loads(response.content)
            if not results["feed"]["entry"]:
                assert len(self.avail) == int(
                    response.headers["CMR-Hits"]
                ), "Search failure - unexpected number of results"
                break

            # Collect results
            self.avail.extend(results["feed"]["entry"])

        assert (
            len(self.avail) > 0
        ), "Your search returned no results; try different search parameters"
示例#4
0
    def get_avail(self, CMRparams, reqparams):
        """
        Get a list of available granules for the query object's parameters.
        Generates the `avail` attribute of the granules object.

        Parameters
        ----------
        CMRparams : dictionary
            Dictionary of properly formatted CMR search parameters.
        reqparams : dictionary
            Dictionary of properly formatted parameters required for searching, ordering,
            or downloading from NSIDC.

        Notes
        -----
        This function is used by query.Query.avail_granules(), which automatically
        feeds in the required parameters.

        See Also
        --------
        APIformatting.Parameters
        query.Query.avail_granules
        """

        assert (CMRparams is not None and reqparams
                is not None), "Missing required input parameter dictionaries"

        # if not hasattr(self, 'avail'):
        self.avail = []

        granule_search_url = "https://cmr.earthdata.nasa.gov/search/granules"

        headers = {"Accept": "application/json", "Client-Id": "icepyx"}
        # DevGoal: check the below request/response for errors and show them if they're there; then gather the results
        # note we should also do this whenever we ping NSIDC-API - make a function to check for errors
        while True:
            params = apifmt.combine_params(
                CMRparams,
                {k: reqparams[k]
                 for k in ("page_size", "page_num")})
            response = requests.get(
                granule_search_url,
                headers=headers,
                params=apifmt.to_string(params),
            )

            results = json.loads(response.content)

            # print(results)

            if len(results["feed"]["entry"]) == 0:
                # Out of results, so break out of loop
                break

            # Collect results and increment page_num
            self.avail.extend(results["feed"]["entry"])
            reqparams["page_num"] += 1

        # DevNote: The above calculated page_num is wrong when mod(granule number, page_size)=0.
        # print(reqparams['page_num'])
        reqparams["page_num"] = int(
            np.ceil(len(self.avail) / reqparams["page_size"]))

        assert (
            len(self.avail) > 0
        ), "Your search returned no results; try different search parameters"