fraymio / modis-tools

Tools for working with the MODIS API and MODIS data.
Apache License 2.0
23 stars 12 forks source link

Download Some of the images. #35

Closed MohammadJavadSoltani closed 11 months ago

MohammadJavadSoltani commented 1 year ago

Describe the bug Hello, Firstly I sincerely appreciate your previous assistance. I want to extract size and link of the images using modis-tools and then download some of them. Is it possible? I recognized when I use for granule in granules I can't use granules again and it's like it becomes empty. After all, I could extract all urls, image names and also sizes, but they are string and in _get_location function we can't have url.host. I would appreciate any assistant. Thank you all.

Screenshots image

After That: image

Desktop (please complete the following information):

iamchrisearle commented 1 year ago

Hi @MohammadJavadSoltani , were you able to solve this?

A few notes here to help out. The granules object is a generator which gets consumed when you download them. See more here: https://wiki.python.org/moin/Generators

To download from a url, the download_from_url method is expecting a type AnyUrl input, not a string. You can generate the urls using the get_urls_from_granule method to ensure they're in the correct format. Here's a short example that both makes a copy of the granules as a list, so that it will no longer behave as a generator, thus not being consumed. As well as a url fetcher and downloading implementation after fetching the urls in their expected type:

from modis_tools.auth import ModisSession
from modis_tools.granule_handler import GranuleHandler
from modis_tools.resources import CollectionApi, GranuleApi
import anyur
username = "secret"  # Update this line
password = "supersecret" # Update this line

# Authenticate a session
session = ModisSession(username=username, password=password)

# Query the MODIS catalog for collection
collection_client = CollectionApi(session=session)
collections = collection_client.query(short_name="VNP03DNB", version="2")

# Query the selected collection for granules
granule_client = GranuleApi.from_collection(collections[0], session=session)

lsn_bbox = (-94.064941, 28.960089, -89.055176, 32.971804)
lsn_granules = granule_client.query(
    start_date="2018-01-01", end_date="2018-01-02", bounding_box=lsn_bbox
)

# convert the generator to a list to avoid it being consumed (optional)
lsn_granules_copy = list(lsn_granules)

path = "/Users/YOUR_PATH"
# since lsn_granules_copy is a list now, we can subset, you can adapt this to your needs
# set the ext to your needs as well
urls = GranuleHandler.get_url_from_granule(lsn_granules_copy[0], ext=("nc"))
# check the type with type(urls) to see that it is AnyUrl, not a string!

GranuleHandler.download_from_urls(urls, session, path)

Alternatively, if you want to leave the granules as a generator, you could do something like

lsn_granules = granule_client.query(
    start_date="2018-01-01", end_date="2018-01-02", bounding_box=lsn_bbox
)

for granule in lsn_granules:
    url = GranuleHandler.get_url_from_granule(granule, ext=("nc"))
    GranuleHandler.download_from_urls(url, session, path)
iamchrisearle commented 11 months ago

This issue has become stale and is being closed.