# Makefile for analyse time series tool, with examples:
# Copyright (c) Paul Clark 2008-2012.  Licensed under GPLv3

# Usage:
#   make                           Builds just the analyse tool
#   make graphs                    Creates lots of example PNGs
#   make display                   Creates and displays all the example PNGs
#   make hadcrut3.fourier.display  Creates and displays just one example
#   make clean                     Removes everything non-essential
#   make release                   Make release tarball

# Requires the following Debian packages: g++ gnuplot imagemagick wget 

ANALYSE = ./analyse
HADCRUT3_URL = http://www.cru.uea.ac.uk/cru/data/temperature/hadcrut3vgl.txt

default: analyse merge

clean:
	-@rm -rf *~ *.out *.png *.txt *.tgz analyse merge

release: clean
	tar cfvz analyse-0.9.0.tgz --exclude=CVS *

# Display graphs (note - uses imagemagick package)
display: hadcrut3.plain.display hadcrut3.compress12.display     \
	 hadcrut3.mean12.display hadcrut3.mean132.display       \
	 hadcrut3.mean132-d.display hadcrut3.isolate132.display \
	 hadcrut3.mean12-1970.display hadcrut3.fourier.display  \
	 hadcrut3.trend.display hadcrut3.combined.display

graphs: hadcrut3.plain.png hadcrut3.compress12.png \
        hadcrut3.mean12.png hadcrut3.mean132.png hadcrut3.mean132-d.png \
	hadcrut3.isolate132.png hadcrut3.mean12-1970.png \
	hadcrut3.fourier.png hadcrut3.trend.png hadcrut3.combined.png

# Details to get hadcrut3 data
hadcrut3vgl.txt:
	wget $(HADCRUT3_URL) 

# Rule for 'display' pseudo target
%.display: %.png
	display $< &

# General rule for out->png
%.png: %.out
	cat basic.gp $< | gnuplot > $@

#Plain data
hadcrut3.plain.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 < $< > $@

#Annual average data
hadcrut3.compress12.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 compress 12 < $< > $@

#12-month running mean
hadcrut3.mean12.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 mean 12 < $< > $@

#11-year (132 month) running mean
hadcrut3.mean132.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 mean 132 < $< > $@

#Isolation of 11-year signal
hadcrut3.isolate132.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 isolate 132 mean 12 < $< > $@

#11-year (132 month) running mean derivatives
hadcrut3.mean132-d.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 mean 132 derivative mean 132 < $< > $@

#12-month running mean since 1970
hadcrut3.mean12-1970.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 from 1970 mean 12 < $< > $@

#Fourier transform
hadcrut3.fourier.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 window fourier high-pass 5 low-pass 50 inverse-fourier < $< > $@

#Linear trend
hadcrut3.trend.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	$(ANALYSE) hadcrut3 trend < $< > $@

#Two plots
hadcrut3.combined.out: hadcrut3vgl.txt $(ANALYSE) Makefile
	( $(ANALYSE) hadcrut3 mean 12 < $<; \
	  echo "e"; \
	  $(ANALYSE) hadcrut3 trend < $<; \
        ) > $@

# Override of gnuplot for combined
hadcrut3.combined.png: hadcrut3.combined.out
	cat combined.gp $< | gnuplot > $@

#Program
analyse: analyse.cc 
merge: merge.cc
