コード例 #1
0
def mask_sub(sub_id: str, dir: str, model: str, api: API) -> None:
    """ mask_sub masks a given subscription  """
    count = 0
    while True:
        print(f"Page: {count + 1}")
        products = api.get_products(sub_id=sub_id, page=count, page_size=500)
        mask_products(products, dir, model)
        count += 1

        if not products:
            break
コード例 #2
0
def get_subscription_products_info(subscription_id: int,
                                   api_object: API) -> list:
    products = []
    page_count = 0
    while True:
        product_page = api_object.get_products(sub_id=subscription_id,
                                               page=page_count,
                                               page_size=100)
        page_count += 1
        if not product_page:
            break
        for product in product_page:
            products.append(product)
    return products
コード例 #3
0
def download_hyp3_products(hyp3_api_object: API, 
                           destination_path: str, 
                           start_date: datetime.date = None, 
                           end_date: datetime.date = None, 
                           flight_direction: str = None, 
                           path: int = None) -> int:
    """
    Takes a Hyp3 API object and a destination path.
    Calls pick_hyp3_subscription() and downloads all products associated with the selected subscription. 
    Returns subscription id.
    preconditions: -must already be logged into hyp3
                   -destination_path must be valid
    """
    assert type(hyp3_api_object) == API, 'Error: hyp3_api_object must be an asf_hyp3.API object.'
    assert type(destination_path) == str, 'Error: destination_path must be a string'
    assert os.path.exists(destination_path), 'Error: desitination_path must be valid'
    if start_date:
        assert type(start_date) == datetime.date, 'Error: start_date must be a datetime.date'
    if end_date:
        assert type(end_date) == datetime.date, 'Error:, end_date must be a datetime.date'
    if flight_direction:
        assert type(flight_direction) == str, 'Error: flight_direction must be a string.'
    if path:
        assert type(path) == int, 'Error: path must be an integer.'
                    
    subscriptions = get_hyp3_subscriptions(hyp3_api_object)
    subscription_id = pick_hyp3_subscription(subscriptions)
    if subscription_id:
        products = []
        page_count = 0
        product_count = 1
        while True:
            product_page = hyp3_api_object.get_products(
                sub_id=subscription_id, page=page_count, page_size=100)
            page_count += 1
            if not product_page:
                break
            for product in product_page:
                products.append(product)
        if date_range_valid(start_date, end_date): 
            products = filter_date_range(products, start_date, end_date)
        if flight_direction: # must check this because both None and incorrect flight_directions 
                             # will return False and it shouldn't exit if flight_direction is None
            if flight_direction_valid(flight_direction):
                products = product_filter(products, flight_direction=flight_direction)
            else:
                print('Aborting download_hyp3_products() due to invalid flight_direction.')
                sys.exit(1)
        if path:
            products = product_filter(products, path=path) 
        if path_exists(destination_path):
            print(f"\n{len(products)} products are associated with the selected date range, flight direction, and path for Subscription ID: {subscription_id}")
            for p in products:
                print(f"\nProduct Number {product_count}:")
                product_count += 1
                url = p['url']
                _match = re.match(
                    r'https://hyp3-download.asf.alaska.edu/asf/data/(.*).zip', url)
                product = _match.group(1)
                filename = f"{destination_path}/{product}"
                # if not already present, we need to download and unzip products
                if not os.path.exists(filename):
                    print(
                        f"\n{product} is not present.\nDownloading from {url}")
                    r = requests.get(url, stream=True)
                    download(filename, r)
                    print(f"\n")
                    os.rename(filename, f"{filename}.zip")
                    filename = f"{filename}.zip"
                    asf_unzip(destination_path, filename)
                    os.remove(filename)
                    print(f"\nDone.")
                else:
                    print(f"{filename} already exists.")
        return subscription_id