forked from kilianv/CoastSat_WRL
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
6.1 KiB
Python
179 lines
6.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Tue Mar 20 16:15:51 2018
|
|
|
|
@author: z5030440
|
|
"""
|
|
|
|
import os
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import pdb
|
|
import ee
|
|
|
|
import matplotlib.dates as mdates
|
|
import matplotlib.cm as cm
|
|
from datetime import datetime, timedelta
|
|
import pickle
|
|
import pytz
|
|
import scipy.io as sio
|
|
|
|
# image processing modules
|
|
import skimage.filters as filters
|
|
import skimage.exposure as exposure
|
|
import skimage.transform as transform
|
|
import skimage.morphology as morphology
|
|
import skimage.measure as measure
|
|
import sklearn.decomposition as decomposition
|
|
from scipy import spatial
|
|
# my functions
|
|
import functions.utils as utils
|
|
import functions.sds as sds
|
|
|
|
np.seterr(all='ignore') # raise/ignore divisions by 0 and nans
|
|
plt.rcParams['axes.grid'] = True
|
|
plt.rcParams['figure.max_open_warning'] = 100
|
|
|
|
au_tz = pytz.timezone('Australia/Sydney')
|
|
|
|
# load quadbike dates and convert from datenum to datetime
|
|
filename = 'data\quadbike\survey_dates.mat'
|
|
filepath = os.path.join(os.getcwd(), filename)
|
|
dates_quad = sio.loadmat(filepath)['dates'] # matrix containing year, month, day
|
|
dates_quad = [datetime(dates_quad[i,0], dates_quad[i,1], dates_quad[i,2],
|
|
tzinfo=au_tz) for i in range(dates_quad.shape[0])]
|
|
|
|
# load timestamps from satellite images
|
|
satname = 'L8'
|
|
sitename = 'NARRA'
|
|
filepath = os.path.join(os.getcwd(), 'data', satname, sitename)
|
|
with open(os.path.join(filepath, sitename + '_output' + '.pkl'), 'rb') as f:
|
|
output = pickle.load(f)
|
|
dates_l8 = output['t']
|
|
# convert to AEST
|
|
dates_l8 = [_.astimezone(au_tz) for _ in dates_l8]
|
|
|
|
# load wave data
|
|
filename = 'data\wave\SydneyProcessed.mat'
|
|
filepath = os.path.join(os.getcwd(), filename)
|
|
wave_data = sio.loadmat(filepath)
|
|
idx = utils.find_indices(wave_data['dates'][:,0], lambda e: e >= dates_l8[0].year and e <= dates_l8[-1].year)
|
|
hsig = np.array([wave_data['Hsig'][i][0] for i in idx])
|
|
wdir = np.array([wave_data['Wdir'][i][0] for i in idx])
|
|
dates_wave = [datetime(wave_data['dates'][i,0], wave_data['dates'][i,1],
|
|
wave_data['dates'][i,2], wave_data['dates'][i,3],
|
|
wave_data['dates'][i,4], wave_data['dates'][i,5],
|
|
tzinfo=au_tz) for i in idx]
|
|
#%% make a plot of all the dates
|
|
f = plt.figure()
|
|
months = mdates.MonthLocator()
|
|
month_fmt = mdates.DateFormatter('%b %Y')
|
|
days = mdates.DayLocator()
|
|
years = [2013,2014,2015,2016]
|
|
for k in range(len(years)):
|
|
sel_year = years[k]
|
|
ax = plt.subplot(4,1,k+1)
|
|
idx_year = utils.find_indices(dates_wave, lambda e : e.year >= sel_year and e.year <= sel_year)
|
|
plt.plot([dates_wave[i] for i in idx_year], [hsig[i] for i in idx_year], 'k-', linewidth=0.5)
|
|
hsigmax = np.nanmax([hsig[i] for i in idx_year])
|
|
cbool = True
|
|
for j in range(len(dates_quad)):
|
|
if dates_quad[j].year == sel_year:
|
|
if cbool:
|
|
plt.plot([dates_quad[j], dates_quad[j]], [0, hsigmax], color=[255/255,140/255,0], label='survey')
|
|
cbool = False
|
|
else:
|
|
plt.plot([dates_quad[j], dates_quad[j]], [0, hsigmax], color=[255/255,140/255,0])
|
|
cbool = True
|
|
for j in range(len(dates_l8)):
|
|
if dates_l8[j].year == sel_year:
|
|
if cbool:
|
|
plt.plot([dates_l8[j], dates_l8[j]], [0, hsigmax], color=[0,191/255,255/255], label='landsat8')
|
|
cbool = False
|
|
else:
|
|
plt.plot([dates_l8[j], dates_l8[j]], [0, hsigmax], color=[0,191/255,255/255])
|
|
if k == 3:
|
|
plt.legend()
|
|
plt.xlim((datetime(sel_year,1,1), datetime(sel_year,12,31, tzinfo=au_tz)))
|
|
plt.ylim((0, hsigmax))
|
|
plt.ylabel('Hs [m]')
|
|
ax.xaxis.set_major_locator = months
|
|
ax.xaxis.set_major_formatter(month_fmt)
|
|
f.subplots_adjust(hspace=0.2)
|
|
plt.draw()
|
|
#%%
|
|
|
|
# calculate days difference
|
|
diff_days = [ [(x - _).days for _ in dates_quad] for x in dates_l8]
|
|
max_diff = 5
|
|
idx_closest = [utils.find_indices(_, lambda e: abs(e) <= max_diff) for _ in diff_days]
|
|
dates_diff = []
|
|
for i in range(len(idx_closest)):
|
|
if not idx_closest[i]:
|
|
continue
|
|
elif len(idx_closest[i]) > 1:
|
|
idx_best = np.argmin(np.abs([diff_days[i][_] for _ in idx_closest[i]]))
|
|
dates_temp = [dates_quad[_] for _ in idx_closest[i]]
|
|
days_temp = [diff_days[i][_] for _ in idx_closest[i]]
|
|
dates_diff.append({"date sat": dates_l8[i],
|
|
"date quad": dates_temp[idx_best],
|
|
"days diff": days_temp[idx_best]})
|
|
else:
|
|
dates_diff.append({"date sat": dates_l8[i],
|
|
"date quad": dates_quad[idx_closest[i][0]],
|
|
"days diff": diff_days[i][idx_closest[i][0]]
|
|
})
|
|
|
|
np.mean([ np.abs(_['days diff']) for _ in dates_diff])
|
|
|
|
#%%
|
|
|
|
# load quadbike .mat files
|
|
foldername = 'data\quadbike\surveys3D'
|
|
folderpath = os.path.join(os.getcwd(), foldername)
|
|
filenames = os.listdir(folderpath)
|
|
|
|
# load the satellite shorelines
|
|
sl = output['shorelines']
|
|
|
|
dates_quad = [datetime(int(_[6:10]), int(_[11:13]), int(_[14:16]), tzinfo= au_tz) for _ in filenames]
|
|
# for each satellite shoreline, load the corresponding 3D survey
|
|
for i in range(len(dates_diff)):
|
|
idx_closest = np.argmin(np.abs(np.array([(dates_diff[i]['date sat'] - _).days for _ in dates_quad])))
|
|
survey3d = sio.loadmat(os.path.join(folderpath, filenames[idx_closest]))
|
|
|
|
plt.figure()
|
|
plt.axis('equal')
|
|
plt.scatter(survey3d['x'], survey3d['y'], s=10, c=survey3d['z'], marker='o', cmap=cm.get_cmap('jet'),
|
|
label='quad data')
|
|
plt.plot(sl[i][:,0], sl[i][:,1], 'ko')
|
|
plt.draw()
|
|
|
|
import statsmodels.api as sm
|
|
lowess = sm.nonparametric.lowess
|
|
|
|
|
|
|
|
# For the 1D case:
|
|
x = sl[i][:,0]
|
|
y = sl[i][:,1]
|
|
x0 = x
|
|
f_hat = lo.lowess(x, y, x)
|
|
fig,ax = plt.subplots(1)
|
|
ax.scatter(x,y)
|
|
ax.plot(x0,f_hat,'ro')
|
|
plt.show()
|
|
|
|
# 2D case (and more...)
|
|
x = np.random.randn(2, 100)
|
|
f = -1 * np.sin(x[0]) + 0.5 * np.cos(x[1]) + 0.2*np.random.randn(100)
|
|
x0 = np.mgrid[-1:1:.1, -1:1:.1]
|
|
x0 = np.vstack([x0[0].ravel(), x0[1].ravel()])
|
|
f_hat = lo.lowess(x, f, x0, kernel=lo.tri_cube)
|
|
from mpl_toolkits.mplot3d import Axes3D
|
|
fig = plt.figure()
|
|
ax = fig.add_subplot(111, projection='3d')
|
|
ax.scatter(x[0], x[1], f)
|
|
ax.scatter(x0[0], x0[1], f_hat, color='r')
|
|
|