SHELL=cmd

###############################
# Load environment variables

include .env
export $(shell sed 's/=.*//' .env)
CURRENT_DIR = $(shell pwd)


###############################
# Create python virtual environment

.PHONY: venv-init
venv-init: ##@environment Setup virtual environment
	conda env create -f environment.yml --prefix=.venv python=3.6

.PHONY: venv-activate
venv-activate: ##@environment Activates the virtual environment
	activate $(CURRENT_DIR)/.venv

.PHONY: venv-update
venv-update: ##@environment Updates to latest packages
	conda update ipykernel && conda update --prefix .venv --all

.PHONY: venv-requirements-install
venv-requirements-install: ##@environment Ensures environment.yml packages are installed
	conda env update

# The environment.yml file should really be created by hand, but
# this provides a good starting point.
.PHONY: venv-requirements-export
venv-requirements-export: ##@environment Exports current environment to environment.yml
	conda env export --file environment.yml

# To install new packages: conda install --prefix .venv PACKAGE


###############################
# Get data from network drive
.PHONY: push-data pull-data

push-data: ##@data Copies data from ./data/ to data backup directory
	rclone copy ./data/ $(DATA_BACKUP_DIR) --exclude "*.las" --progress

pull-data: ##@data Copies data from data backup directory to ./data/
	# We probably don't want to pull the raw LIDAR .las files, so lets exclude them
	rclone copy $(DATA_BACKUP_DIR) ./data/ --exclude "*.las" --progress


###############################
# Process data
.PHONY: process-mat

impacts: ./data/interim/impacts_forecasted_foreshore_slope_sto06.csv ./data/interim/impacts_forecasted_mean_slope_sto06.csv ./data/interim/impacts_observed.csv ##@products makes obsered and forecasted impacts

# Calculates beach orientations at each profile
./data/raw/processed_shorelines/orientations.mat: ./data/raw/processed_shorelines/profiles.mat
	$(MATLAB_PATH) -nosplash -r "cd $(CURRENT_DIR); run('./src/data/beach_orientations.m'); quit"

# # Produces a .csv of sites where our beach cross-sections are located
# ./data/interim/sites.csv ./data/interim/profiles.csv: ./data/raw/processed_shorelines/profiles.mat
# 	activate ./.venv && python ./src/data/parse_mat.py create-sites-and-profiles-csv \
# 	--profiles-mat "./data/raw/processed_shorelines/profiles.mat" \
# 	--profiles-output-file "./data/interim/profiles.csv" \
# 	--sites-output-file "./data/interim/sites.csv"

# Produces a .csv of sites where our beach cross-sections are located
./data/interim/sites.csv ./data/interim/profiles.csv: ./data/raw/processed_shorelines/profiles.mat
	activate ./.venv && python ./src/cli.py create-sites-and-profiles-csv \
	--profiles-mat "./data/raw/processed_shorelines/profiles.mat" \
	--profiles-output-file "./data/interim/profiles.csv" \
	--sites-output-file "./data/interim/sites.csv"

# Produces a .csv of waves for each site
./data/interim/waves.csv: ./data/interim/sites.csv ./data/raw/processed_shorelines/waves.mat
	activate ./.venv && python ./src/cli.py create-waves-csv \
	--waves-mat "./data/raw/processed_shorelines/waves.mat" \
	--sites-csv "./data/interim/sites.csv" \
	--output-file "./data/interim/waves.csv"

# Produces a .csv of tides for each site
./data/interim/tides.csv: ./data/interim/sites.csv ./data/raw/processed_shorelines/tides.mat
	activate ./.venv && python ./src/cli.py create-tides-csv \
	--tides-mat "./data/raw/processed_shorelines/tides.mat" \
	--sites-csv "./data/interim/sites.csv" \
	--output-file "./data/interim/tides.csv"

# Creates a .shp of our sites to load into QGis
./data/interim/sites.shp: ./data/interim/sites.csv
	activate ./.venv && python ./src/cli.py sites-csv-to-shp \
	--input-csv "./data/interim/sites.csv" \
	--output-shp "./data/interim/sites.shp"

# # Creates a .csv of our dune toe and crest profile features from .shp file
# ./data/interim/profile_features.csv: ./data/raw/profile_features/dune_crests.shp ./data/raw/profile_features/dune_toes.shp ./data/interim/sites.csv ./data/interim/profiles.csv
# 	activate ./.venv && python ./src/cli.py create-profile-features \
# 	--dune-crest-shp "./data/raw/profile_features/dune_crests.shp" \
# 	--dune-toe-shp "./data/raw/profile_features/dune_toes.shp" \
# 	--sites-csv "./data/interim/sites.csv" \
# 	--profiles-csv "./data/interim/profiles.csv" \
# 	--output-csv "./data/interim/profile_features.csv"

# Create a .csv of our dune toe and crest profile features from Tom Beuzen's .mat file
./data/interim/profile_features.csv: ./data/raw/profile_features_tom_beuzen/*.mat ./data/interim/sites.csv
	activate ./.venv && python ./src/cli.py create-profile-features \
	--crest-mat "./data/raw/profile_features_tom_beuzen/J16_DuneCrest.mat" \
	--toe-mat "./data/raw/profile_features_tom_beuzen/J16_DuneToe.mat" \
	--sites-csv "./data/interim/sites.csv" \
	--output-file "./data/interim/profile_features.csv"

# Creates a forecast of twl using sto06 and prestorm time varying prestorm foreshore slope
./data/interim/twl_foreshore_slope_sto06.csv: ./data/interim/waves.csv ./data/interim/tides.csv ./data/interim/profiles.csv ./data/interim/sites.csv ./data/interim/profile_features.csv
	activate ./.venv && python ./src/cli.py create-twl-forecast \
	--waves-csv "./data/interim/waves.csv" \
	--tides-csv "./data/interim/tides.csv" \
	--profiles-csv "./data/interim/profiles.csv" \
	--profile-features-csv "./data/interim/profile_features.csv" \
	--runup-function "sto06" \
	--slope "foreshore" \
	--output-file "./data/interim/twl_foreshore_slope_sto06.csv"

./data/interim/twl_mean_slope_sto06.csv: ./data/interim/waves.csv ./data/interim/tides.csv ./data/interim/profiles.csv ./data/interim/sites.csv ./data/interim/profile_features.csv
	activate ./.venv && python ./src/cli.py create-twl-forecast \
	--waves-csv "./data/interim/waves.csv" \
	--tides-csv "./data/interim/tides.csv" \
	--profiles-csv "./data/interim/profiles.csv" \
	--profile-features-csv "./data/interim/profile_features.csv" \
	--runup-function "sto06" \
	--slope "mean" \
	--output-file "./data/interim/twl_mean_slope_sto06.csv"

./data/interim/twl_poststorm_mean_slope_sto06.csv: ./data/interim/waves.csv ./data/interim/tides.csv ./data/interim/profiles.csv ./data/interim/sites.csv ./data/interim/profile_features.csv
	activate ./.venv && python ./src/cli.py create-twl-forecast \
	--waves-csv "./data/interim/waves.csv" \
	--tides-csv "./data/interim/tides.csv" \
	--profiles-csv "./data/interim/profiles.csv" \
	--profile-features-csv "./data/interim/profile_features.csv" \
	--runup-function "sto06" \
	--slope "mean" \
	--profile-type "poststorm" \
	--output-file "./data/interim/twl_poststorm_mean_slope_sto06.csv"

./data/interim/impacts_observed.csv: ./data/interim/profiles.csv ./data/interim/profile_features.csv
	activate ./.venv && python ./src/cli.py create-observed-impacts \
	--profiles-csv "./data/interim/profiles.csv" \
	--profile-features-csv "./data/interim/profile_features.csv" \
	--output-file "./data/interim/impacts_observed.csv"

./data/interim/impacts_forecasted_mean_slope_sto06.csv: ./data/interim/profile_features.csv ./data/interim/twl_mean_slope_sto06.csv
	activate ./.venv && python ./src/cli.py create-forecasted-impacts \
	--profile-features-csv "./data/interim/profile_features.csv" \
	--forecasted-twl-csv "./data/interim/twl_mean_slope_sto06.csv" \
	--output-file "./data/interim/impacts_forecasted_mean_slope_sto06.csv"

./data/interim/impacts_forecasted_foreshore_slope_sto06.csv: ./data/interim/profile_features.csv ./data/interim/twl_foreshore_slope_sto06.csv
	activate ./.venv && python ./src/cli.py create-forecasted-impacts \
	--profile-features-csv "./data/interim/profile_features.csv" \
	--forecasted-twl-csv "./data/interim/twl_foreshore_slope_sto06.csv" \
	--output-file "./data/interim/impacts_forecasted_foreshore_slope_sto06.csv"

./data/interim/impacts_forecasted_poststorm_mean_slope_sto06.csv: ./data/interim/profile_features.csv ./data/interim/twl_foreshore_slope_sto06.csv
	activate ./.venv && python ./src/cli.py create-forecasted-impacts \
	--profile-features-csv "./data/interim/profile_features.csv" \
	--forecasted-twl-csv "./data/interim/twl_poststorm_mean_slope_sto06.csv" \
	--output-file "./data/interim/impacts_forecasted_poststorm_mean_slope_sto06.csv"


###############################
# Misc commands
format: ./src/*.py ##@misc Check python file formatting
	activate ./.venv && black --line-length 120 "src/"


###############################
# Help command

.DEFAULT_GOAL := help
.PHONY: help

# Refer to https://gist.github.com/prwhite/8168133

#COLORS
GREEN  := $(shell tput -Txterm setaf 2)
WHITE  := $(shell tput -Txterm setaf 7)
YELLOW := $(shell tput -Txterm setaf 3)
RESET  := $(shell tput -Txterm sgr0)

# Add the following 'help' target to your Makefile
# And add help text after each target name starting with '\#\#'
# A category can be added with @category
HELP_FUN = \
	%help; \
	while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
	print "usage: make [target]\n\n"; \
	for (sort keys %help) { \
	print "${WHITE}$$_:${RESET}\n"; \
	for (@{$$help{$$_}}) { \
	$$sep = " " x (32 - length $$_->[0]); \
	print "  ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \
	}; \
	print "\n"; }

help: ##@other Show this help.
	@perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)