Skip to content

Documentation for Granule Results

Bases: CustomDict

Dictionary-like object to represent a granule from CMR.

Source code in earthaccess/results.py
def __init__(
    self,
    collection: Dict[str, Any],
    fields: Optional[List[str]] = None,
    cloud_hosted: bool = False,
):
    super().__init__(collection)
    self.cloud_hosted = cloud_hosted
    # TODO: maybe add area, start date and all that as an instance value
    self["size"] = self.size()
    self.uuid = str(uuid.uuid4())
    self.render_dict: Any
    if fields is None:
        self.render_dict = self
    elif fields[0] == "basic":
        self.render_dict = self._filter_fields_(self._basic_umm_fields_)
    else:
        self.render_dict = self._filter_fields_(fields)

__repr__()

Returns:

Type Description
str

A basic representation of a data granule.

Source code in earthaccess/results.py
def __repr__(self) -> str:
    """
    Returns:
        A basic representation of a data granule.
    """
    data_links = [link for link in self.data_links()]
    rep_str = f"""
    Collection: {self['umm']['CollectionReference']}
    Spatial coverage: {self['umm']['SpatialExtent']}
    Temporal coverage: {self['umm']['TemporalExtent']}
    Size(MB): {self.size()}
    Data: {data_links}\n\n
    """.strip().replace("  ", "")
    return rep_str

Returns the data links from a granule.

Parameters:

Name Type Description Default
access Optional[str]

direct or external. Direct means in-region access for cloud-hosted collections.

None
in_region bool

True if we are running in us-west-2. It is meant for the store class.

False

Returns:

Type Description
List[str]

The data links for the requested access type.

Source code in earthaccess/results.py
def data_links(
    self, access: Optional[str] = None, in_region: bool = False
) -> List[str]:
    """Returns the data links from a granule.

    Parameters:
        access: direct or external.
            Direct means in-region access for cloud-hosted collections.
        in_region: True if we are running in us-west-2.
            It is meant for the store class.

    Returns:
        The data links for the requested access type.
    """
    https_links = self._filter_related_links("GET DATA")
    s3_links = self._filter_related_links("GET DATA VIA DIRECT ACCESS")
    if in_region:
        # we are in us-west-2
        if self.cloud_hosted and access in (None, "direct"):
            # this is a cloud collection, and we didn't specify the access type
            # default to S3 links
            if len(s3_links) == 0 and len(https_links) > 0:
                # This is guessing the S3 links for some cloud collections that for
                # some reason only offered HTTPS links
                return self._derive_s3_link(https_links)
            else:
                # we have the s3 links so we return those
                return s3_links
        else:
            # Even though we are in us-west-2, the user wants the HTTPS links used in-region.
            # They are S3 signed links from TEA.
            # <https://github.com/asfadmin/thin-egress-app>
            return https_links
    else:
        # we are not in-region
        if access == "direct":
            # maybe the user wants to collect S3 links and use them later
            # from the cloud
            return s3_links
        else:
            # we are not in us-west-2, even cloud collections have HTTPS links
            return https_links

Returns:

Type Description
List[str]

The data visualization links, usually the browse images.

Source code in earthaccess/results.py
def dataviz_links(self) -> List[str]:
    """
    Returns:
        The data visualization links, usually the browse images.
    """
    links = self._filter_related_links("GET RELATED VISUALIZATION")
    return links

size()

Returns:

Type Description
float

The total size for the granule in MB.

Source code in earthaccess/results.py
def size(self) -> float:
    """
    Returns:
        The total size for the granule in MB.
    """
    try:
        data_granule = self["umm"]["DataGranule"]
        total_size = sum(
            [
                float(s["Size"])
                for s in data_granule["ArchiveAndDistributionInformation"]
                if "ArchiveAndDistributionInformation" in data_granule
            ]
        )
    except Exception:
        try:
            data_granule = self["umm"]["DataGranule"]
            total_size = sum(
                [
                    float(s["SizeInBytes"])
                    for s in data_granule["ArchiveAndDistributionInformation"]
                    if "ArchiveAndDistributionInformation" in data_granule
                ]
            ) / (1024 * 1024)
        except Exception:
            total_size = 0
    return total_size