# -*- coding: utf-8 -*-
"""
Created on Thu Mar  1 14:32:08 2018

@author: z5030440

Main code to extract shorelines from Landsat imagery
"""

# Preamble
import ee
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from datetime import datetime
import pytz
import pdb

# image processing modules
import skimage.filters as filters 
import skimage.exposure as exposure
import skimage.transform as transform
import sklearn.decomposition as decomposition
import skimage.morphology as morphology
import skimage.measure as measure

# my functions
import functions.utils as utils
import functions.sds as sds

np.seterr(all='ignore') # raise/ignore divisions by 0 and nans
ee.Initialize()

# parameters
plot_bool = False # if you want the plots
prob_high = 99.9 # upper probability to clip and rescale pixel intensity
min_contour_points = 100 # minimum number of points contained in each water line

# select collection
input_col = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')

# location (Narrabeen-Collaroy beach)
rect_narra = [[[151.3473129272461,-33.69035274454718],
              [151.2820816040039,-33.68206818063878], 
              [151.27281188964844,-33.74775138989556],
              [151.3425064086914,-33.75231878701767],
              [151.3473129272461,-33.69035274454718]]];
# Dates
start_date = '2016-01-01'
end_date = '2016-12-31'
# filter by location
flt_col = input_col.filterBounds(ee.Geometry.Polygon(rect_narra))#.filterDate(start_date, end_date)

n_img = flt_col.size().getInfo()
print('Number of images covering Narrabeen:', n_img)
im_all = flt_col.getInfo().get('features')

props = {'cloud_cover_cropped':[],
         'cloud_cover':[],
         'cloud_cover_land':[],
         'date_acquired':[],
         'geom_rmse_model':[],
         'geom_rmse_verify':[],
         'gcp_model':[],
         'gcp_verify':[],
         'quality':[],
         'sun_azimuth':[],
         'sun_elevation':[]}
t = []
# loop through all images
for i in range(n_img):
    
    # find each image in ee database
    im = ee.Image(im_all[i].get('id'))
    im_bands = im_all[i].get('bands')
    im_props = im_all[i]['properties']
    
    # compute cloud cover on cropped image    
    for j in range(len(im_bands)): del im_bands[j]['dimensions']
    qa_band = [im_bands[11]]
    im_qa, crs_qa = sds.load_image(im, rect_narra, qa_band)
    im_qa = im_qa[:,:,0]
    im_cloud = sds.create_cloud_mask(im_qa)
    props['cloud_cover_cropped'].append(100*sum(sum(im_cloud.astype(int)))/(im_cloud.shape[0]*im_cloud.shape[1]))
    
    # extract image metadata
    props['cloud_cover'].append(im_props['CLOUD_COVER'])
    props['cloud_cover_land' ].append(im_props['CLOUD_COVER_LAND'])
    props['date_acquired'].append(im_props['DATE_ACQUIRED'])
    props['geom_rmse_model'].append(im_props['GEOMETRIC_RMSE_MODEL']) 
    props['gcp_model'].append(im_props['GROUND_CONTROL_POINTS_MODEL'])
    props['quality'].append(im_props['IMAGE_QUALITY_OLI'])
    props['sun_azimuth'].append(im_props['SUN_AZIMUTH'])
    props['sun_elevation'].append(im_props['SUN_ELEVATION'])
    
    # try structure as sometimes the geometry cannot be verified
    try:
        props['geom_rmse_verify'].append(im_props['GEOMETRIC_RMSE_VERIFY'])
        props['gcp_verify'].append(im_props['GROUND_CONTROL_POINTS_VERIFY'])
    except:
        props['geom_rmse_verify'].append(np.nan)
        props['gcp_verify'].append(np.nan) 
        
    # record exact time of acquisition
    t.append(im_props['system:time_start'])
    
#%% create pd.DataFrame with datetime index
dt = [];
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
au_tz = pytz.timezone('Australia/Sydney')
for k in range(len(t)): dt.append(datetime.fromtimestamp(t[k]/1000, tz=au_tz))  

df = pd.DataFrame(data = props, index=dt , columns=list(props.keys()))

df.to_pickle('meta_l8.pkl')

#df['cloud_cover_cropped'].groupby(df.index.month).count().plot.bar()

#df_monthly = df['cloud_cover_cropped'].groupby(df.index.month)
