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.
geetools_VH/oldcodes/time_coverage.py

137 lines
4.2 KiB
Python

7 years ago
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 20 16:15:51 2018
@author: z5030440
"""
import scipy.io as sio
import os
import ee
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import pickle
import pdb
import pytz
# 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
#plt.rcParams['axes.grid'] = True
au_tz = pytz.timezone('Australia/Sydney')
# load quadbike dates and convert from datenum to datetime
suffix = '.mat'
dir_name = os.getcwd()
file_name = 'data\quadbike_dates'
file_path = os.path.join(dir_name, file_name + suffix)
quad_dates = sio.loadmat(file_path)['dates']
dt_quad = []
for i in range(quad_dates.shape[0]):
dt_quad.append(datetime(quad_dates[i,0], quad_dates[i,1], quad_dates[i,2], tzinfo=au_tz))
# load satellite datetimes (in UTC) and convert to AEST time
input_col = ee.ImageCollection('LANDSAT/LC08/C01/T1_RT_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]]];
flt_col = input_col.filterBounds(ee.Geometry.Polygon(rect_narra))
n_img = flt_col.size().getInfo()
print('Number of images covering Narrabeen:', n_img)
im_all = flt_col.getInfo().get('features')
# extract datetimes from image metadata
dt_sat = [_['properties']['system:time_start'] for _ in im_all]
dt_sat = [datetime.fromtimestamp(_/1000, tz=pytz.utc) for _ in dt_sat]
dt_sat = [_.astimezone(au_tz) for _ in dt_sat]
# calculate days difference
diff_days = [ [(x - _).days for _ in dt_quad] for x in dt_sat]
day_thresh = 15
idx = [utils.find_indices(_, lambda e: abs(e) < day_thresh) for _ in diff_days]
dt_diff = []
idx_nogt = []
for i in range(n_img):
if not idx[i]:
idx_nogt.append(i)
continue
dt_diff.append({"sat dt": dt_sat[i],
"quad dt": [dt_quad[_] for _ in idx[i]],
"days diff": [diff_days[i][_] for _ in idx[i]] })
with open('idx_nogt.pkl', 'wb') as f:
pickle.dump(idx_nogt, f)
#%%
dates_sat = mdates.date2num(dt_sat)
dates_quad = mdates.date2num(dt_quad)
plt.figure()
plt.plot_date(dates_sat, np.zeros((n_img,1)))
plt.plot_date(dates_quad, np.ones((len(dates_quad),1)))
plt.show()
data = pd.read_pickle('data_2016.pkl')
dt_sat = [_.astimezone(au_tz) for _ in data['dt']]
[ (_ - dt_sat[0]).days for _ in dt_quad]
dn_sat = []
for i in range(len(dt_sat)): dn_sat.append(dt_sat[i].toordinal())
dn_sat = np.array(dn_sat)
dn_sur = []
for i in range(len(dt_survey)): dn_sur.append(dt_survey[i].toordinal())
dn_sur = np.array(dn_sur)
distances = np.zeros((len(dn_sat),4)).astype('int32')
indexes = np.zeros((len(dn_sat),2)).astype('int32')
for i in range(len(dn_sat)):
distances[i,0] = np.sort(abs(dn_sat[i] - dn_sur))[0]
distances[i,1] = np.sort(abs(dn_sat[i] - dn_sur))[1]
distances[i,2] = dt_sat[i].year
distances[i,3] = dt_sat[i].month
indexes[i,0] = np.where(abs(dn_sat[i] - dn_sur) == np.sort(abs(dn_sat[i] - dn_sur))[0])[0][0]
indexes[i,1] = np.where(abs(dn_sat[i] - dn_sur) == np.sort(abs(dn_sat[i] - dn_sur))[1])[0][0]
years = [2013, 2014, 2015, 2016]
months = mdates.MonthLocator()
days = mdates.DayLocator()
month_fmt = mdates.DateFormatter('%b')
f, ax = plt.subplots(4, 1)
for i, ca in enumerate(ax):
ca.xaxis.set_major_locator(months)
ca.xaxis.set_major_formatter(month_fmt)
ca.xaxis.set_minor_locator(days)
ca.set_ylabel(str(years[i]))
for j in range(len(dt_sat)):
if dt_sat[j].year == years[i]:
ca.plot(dt_sat[j],0, 'bo', markerfacecolor='b')
#f.subplots_adjust(hspace=0)
#plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
plt.plot(dt_survey, np.zeros([len(dt_survey),1]), 'bo')
plt.plot(dt_sat, np.ones([len(dt_sat),1]), 'ro')
plt.yticks([])
plt.show()