# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 12:46:04 2018

@author: z5030440
"""

# Preamble

import ee
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import pandas as pd
from datetime import datetime
import pickle
import pdb
import pytz
from pylab import ginput

# 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()

#%% Select images

# parameters
plot_bool = True # 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
output_epsg = 28356 # GDA94 / MGA Zone 56
cloud_threshold = 0.8

# select collection
input_col = ee.ImageCollection('LANDSAT/LC08/C01/T1_RT_TOA')
satname = 'L8'
# 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]]];
               
#rect_narra = [[[151.301454, -33.700754],
#               [151.311453, -33.702075], 
#               [151.307237, -33.739761],
#               [151.294220, -33.736329],
#               [151.301454, -33.700754]]];
# location (Oldbar NSW)
rect_narra = [[[152.578395, -31.841216],
               [152.777281, -31.842523], 
               [152.738086, -32.028773],
               [152.557812, -32.004663],
               [152.578395, -31.841216]]];

# Dates
start_date = '2018-01-18'
end_date = '2018-01-20'
# 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')

# find each image in ee database
im = ee.Image(im_all[0].get('id')) 
# load image as np.array
im_pan, im_ms, im_cloud, crs, meta = sds.read_eeimage(im, rect_narra, satname, plot_bool)

# rescale intensities
im_ms = sds.rescale_image_intensity(im_ms, im_cloud, prob_high, plot_bool)
im_pan = sds.rescale_image_intensity(im_pan, im_cloud, prob_high, plot_bool)

# pansharpen rgb image
im_ms_ps = sds.pansharpen(im_ms[:,:,[0,1,2]], im_pan, im_cloud, plot_bool)

plt.figure()
plt.imshow(im_ms_ps[:,:,[2,1,0]])
plt.show()

pts = ginput(n=20, timeout=1000, show_clicks=True)
points = np.array(pts)
plt.plot(points[:,0], points[:,1], 'ko')
plt.show()

pts_coords = sds.convert_pix2world(points[:,[1,0]], crs['crs_15m'])
pts = sds.convert_epsg(pts_coords, crs['epsg_code'], output_epsg)

with open('olddata/oldbar_beach.pkl', 'wb') as f:
    pickle.dump(pts, f)


#pts_wgs84 = sds.convert_epsg(pts_coords, crs['epsg_code'], 4326)
#
#import simplekml
#kml = simplekml.Kml()
#kml.new(name='test', coords=pts_wgs84)
#kml.save("test.kml")