from pyrise import products
from pyrise.products import PRODUCT_ID
from pyrise.downloads import download_browse_product

obsid = 'PSP_003092_0985_RED'

from urllib.request import urlretrieve

"""Download a browse product from HiRISE website.

By default, store it in current path.

Parameters
----------
obsid : str
    HiRISE observation ID
kind : {'RED','COLOR'}, optional
    String indicating the kind of required browse product. Default: 'RED'
annotated : bool, optional
    Switch to control if the annotated version is required. Default: True
saveroot : str, pathlib.Path, optional
    Path in where the browse product is being stored. Default: '.'
overwrite : bool, optional
    Boolean switch to control if an existing path should be overwritten. Default: False
"""
kind='RED'
pid = PRODUCT_ID(f"{obsid}_{kind}")
url = pid.browse_url
print("URL:", url)
savepath = 'test.jpg'
print("Downloading\n", url, '\nto\n', savepath)
try:
    urlretrieve(url, str(savepath))
except HTTPError as e:
    print(e)

!open test.jpg

download_browse_product(obsid, annotated=False)

!open {_}

"""
Explanation: Table of Contents
<p><div class="lev1 toc-item"><a href="#Download-obsid-browse-image" data-toc-modified-id="Download-obsid-browse-image-1"><span class="toc-item-num">1&nbsp;&nbsp;</span>Download obsid browse image</a></div><div class="lev1 toc-item"><a href="#Download-list-of-obsid-browse-images" data-toc-modified-id="Download-list-of-obsid-browse-images-2"><span class="toc-item-num">2&nbsp;&nbsp;</span>Download list of obsid browse images</a></div><div class="lev1 toc-item"><a href="#Create-presentation-from-browse-images" data-toc-modified-id="Create-presentation-from-browse-images-3"><span class="toc-item-num">3&nbsp;&nbsp;</span>Create presentation from browse images</a></div>

# Download obsid browse image
End of explanation
"""


def download_browse_list(obsids, **kwargs):
    """Download browse products for a list of obsids.
    
    Parameters
    ----------
    obsids : iterable
        Container with HiRISE observation ID strings
        
    Example
    -------
    download_browse_list(['PSP_003092_0985', 'ESP_020146_0950'])
    """
    for obsid in obsids:
        download_browse_product(obsid, **kwargs)

download_browse_list(['PSP_003092_0985', 'ESP_020146_0950'], kind='COLOR')

"""
Explanation: Download list of obsid browse images
End of explanation
"""


from pptx import Presentation
from scipy.misc import imread

def create_browse_presentation(obsids, **kwargs):
    prs = Presentation()

    pic_left  = int(prs.slide_width * 0.0)
    pic_top   = int(prs.slide_height * 0.0)
    
    blank_slide_layout = prs.slide_layouts[6]

    for obsid in obsids[0:]:
        imgpath = str(download_browse_product(obsid, **kwargs))
        slide = prs.slides.add_slide(blank_slide_layout)
        img = imread(imgpath)
        ratio = img.shape[0] / img.shape[1]
        if ratio < 1:
            pic_width = int(prs.slide_width)
            pic_height = int(pic_width * img.shape[0] / img.shape[1])
        else:
            pic_height = int(prs.slide_height)
            pic_width = int(pic_height / ratio)
        pic = slide.shapes.add_picture(imgpath, pic_left, pic_top, pic_width, pic_height)

    prs.save('obsid_browse_images.pptx')

obsids = ['PSP_003092_0985', 'ESP_020146_0950']

create_browse_presentation(obsids)

"""
Explanation: Create presentation from browse images
End of explanation
"""


