#
# makefile
#
# Written by Taiji Yamada <taiji@aihara.co.jp>
#
# usage:
#	% make
#	% make all clean
#	% make all distclean
#
CXXFLAGS=-O3 -W -Wall -std=c++17
EXES=\
recurrence-modified_bernoulli_map	\
recurrence-lorenz_attractor	\
recurrence-rossler_attractor	\

ALL=$(EXES)	\
$(EXES:%=v%.dat) $(EXES:%=v%.dat.ld) $(EXES:%=v%.dat.ud) $(EXES:%=v%.eps) $(EXES:%=v%.pdf) $(EXES:%=v%.svg)	\
$(EXES:%=p%.dat) $(EXES:%=p%.dat.ld) $(EXES:%=p%.dat.ud) $(EXES:%=p%.eps) $(EXES:%=p%.pdf) $(EXES:%=p%.svg)	\

all: $(ALL)

v%.dat: % makefile
	./$< -t 500 -i 500 -C 2> $@.lub | dd of=$@ 2> /dev/null

p%.dat: % makefile
	./$< -t 2000 -i 2000 -C -P 2> $@.lub | dd of=$@ 2> /dev/null

vrecurrence-lorenz_attractor.dat: recurrence-lorenz_attractor makefile
	./$< -t 500 -i 500 -d .064 -C 2> $@.lub | dd of=$@ 2> /dev/null

precurrence-lorenz_attractor.dat: recurrence-lorenz_attractor makefile
	./$< -t 2000 -i 2000 -d .016 -C -P 2> $@.lub | dd of=$@ 2> /dev/null

vrecurrence-rossler_attractor.dat: recurrence-rossler_attractor makefile
	./$< -t 500 -i 500 -d .2 -C 2> $@.lub | dd of=$@ 2> /dev/null

precurrence-rossler_attractor.dat: recurrence-rossler_attractor makefile
	./$< -t 2000 -i 2000 -d .05 -C -P 2> $@.lub | dd of=$@ 2> /dev/null

#
v%.dat.ld: % makefile
	./$< -t 500 -i 500 -L -C | dd of=$@ 2> /dev/null

p%.dat.ld: % makefile
	./$< -t 2000 -i 2000 -L -C -P | dd of=$@ 2> /dev/null

vrecurrence-lorenz_attractor.dat.ld: recurrence-lorenz_attractor makefile
	./$< -t 500 -i 500 -d .064 -L -C | dd of=$@ 2> /dev/null

precurrence-lorenz_attractor.dat.ld: recurrence-lorenz_attractor makefile
	./$< -t 2000 -i 2000 -d .016 -L -C -P | dd of=$@ 2> /dev/null

vrecurrence-rossler_attractor.dat.ld: recurrence-rossler_attractor makefile
	./$< -t 500 -i 500 -d .2 -L -C | dd of=$@ 2> /dev/null

precurrence-rossler_attractor.dat.ld: recurrence-rossler_attractor makefile
	./$< -t 2000 -i 2000 -d .05 -L -C -P | dd of=$@ 2> /dev/null

#
v%.dat.ud: % makefile
	./$< -t 500 -i 500 -U -C | dd of=$@ 2> /dev/null

p%.dat.ud: % makefile
	./$< -t 2000 -i 2000 -U -C -P | dd of=$@ 2> /dev/null

vrecurrence-lorenz_attractor.dat.ud: recurrence-lorenz_attractor makefile
	./$< -t 500 -i 500 -d .064 -U -C | dd of=$@ 2> /dev/null

precurrence-lorenz_attractor.dat.ud: recurrence-lorenz_attractor makefile
	./$< -t 2000 -i 2000 -d .016 -U -C -P | dd of=$@ 2> /dev/null

vrecurrence-rossler_attractor.dat.ud: recurrence-rossler_attractor makefile
	./$< -t 500 -i 500 -d .2 -U -C | dd of=$@ 2> /dev/null

precurrence-rossler_attractor.dat.ud: recurrence-rossler_attractor makefile
	./$< -t 2000 -i 2000 -d .05 -U -C -P | dd of=$@ 2> /dev/null

vrecurrence-modified_bernoulli_map.%: XYRANGE=[0:500]
precurrence-modified_bernoulli_map.%: XYRANGE=[0:2000]

vrecurrence-lorenz_attractor.%: XYRANGE=[0:500]
precurrence-lorenz_attractor.%: XYRANGE=[0:2000]

vrecurrence-rossler_attractor.%: XYRANGE=[0:500]
precurrence-rossler_attractor.%: XYRANGE=[0:2000]

YMARGIN=.1125

v%.eps p%.eps: SERIF=Times-Roman
v%.eps p%.eps: SLANT=Times-Italic

v%.pdf p%.pdf: SERIF="Times New Roman"
v%.pdf p%.pdf: SLANT=:Italic

v%.svg p%.svg: SERIF="Times New Roman"
v%.svg p%.svg: SLANT=:Italic

#
v%.eps p%.eps: SCALE=1.25
v%.eps p%.eps: XSIZE=5
v%.eps p%.eps: YSIZE=3.5
v%.eps p%.eps: VSIZE=3.09
# 3.09 (inch) = 4451 (pt) * 0.05 (scale) * 1/72

v%.pdf p%.pdf: SCALE=1.25
v%.pdf p%.pdf: XSIZE=5
v%.pdf p%.pdf: YSIZE=3.5
v%.pdf p%.pdf: VSIZE=3.09
# 3.09 (inch) = 4451 (pt) * 0.05 (scale) * 1/72

v%.svg p%.svg: SCALE=1.2
v%.svg p%.svg: XSIZE=600
v%.svg p%.svg: YSIZE=480
v%.svg p%.svg: VSIZE=404.4
# see generated .svg

#
define VPLOT_EPS
lb=system("sed -ne '/^#minimum:	\\(.*\\)$$/{;s/^#minimum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
ub=system("sed -ne '/^#maximum:	\\(.*\\)$$/{;s/^#maximum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
set palette model HSV functions gray*4/6,1,1;
set xlabel '{/$(SERIF) time {/$(SLANT) i}}';
set ylabel '{/$(SERIF) time {/$(SLANT) j}}';
set size square;
set xrange $(XYRANGE);
set yrange $(XYRANGE);
set cblabel '{/$(SERIF) inter-point distance {/$(SLANT) d_{ij}}}';

set tmargin at screen $(YMARGIN)+($(VSIZE)/$(SCALE))/$(YSIZE);
set bmargin at screen $(YMARGIN);
set lmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE);
set rmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2+($(VSIZE)/$(SCALE)))/$(XSIZE);

set multiplot;

set colorbox vertical default;
set cbrange [0:ub];
plot input.".ld" with dots palette notitle;

set colorbox horizontal user origin (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE),.725+$(YMARGIN) size $(VSIZE)/$(SCALE)/$(XSIZE),.035;
set cblabel offset 0,7.5;
set cbtics offset 0,4,0;
set cbrange [lb:ub]; set log cb;
plot input.".ud" with dots palette notitle;

unset multiplot;

endef
export VPLOT_EPS

v%.eps: v%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$VPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$VPLOT_EPS"
	perl -pe "s/\/Pnt {.*} def/\/Pnt {stroke gsave 0 setlinecap 10 setlinewidth M 10 0 V stroke grestore} def/" -i '$@'

#
define VPLOT_PDF
lb=system("sed -ne '/^#minimum:	\\(.*\\)$$/{;s/^#minimum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
ub=system("sed -ne '/^#maximum:	\\(.*\\)$$/{;s/^#maximum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
set palette model HSV functions gray*4/6,1,1;
set xlabel '{/$(SERIF) time {/$(SLANT) i}}';
set ylabel '{/$(SERIF) time {/$(SLANT) j}}';
set size square;
set xrange $(XYRANGE);
set yrange $(XYRANGE);
set cblabel '{/$(SERIF) inter-point distance {/$(SLANT) d_{ij}}}';

set tmargin at screen $(YMARGIN)+($(VSIZE)/$(SCALE))/$(YSIZE);
set bmargin at screen $(YMARGIN);
set lmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE);
set rmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2+($(VSIZE)/$(SCALE)))/$(XSIZE);

set multiplot;

set colorbox vertical default;
set cbrange [0:ub];
plot input.".ld" with dots palette notitle;

set colorbox horizontal user origin (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE),.725+$(YMARGIN) size $(VSIZE)/$(SCALE)/$(XSIZE),.035;
set cblabel offset 0,7.5;
set cbtics offset 0,4,0;
set cbrange [lb:ub]; set log cb;
plot input.".ud" with dots palette notitle;

unset multiplot;

endef
export VPLOT_PDF

v%.pdf: v%.dat
	GNUTERM='pdf enhanced fontscale .425 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$VPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$VPLOT_PDF"

#
define VPLOT_SVG
lb=system("sed -ne '/^#minimum:	\\(.*\\)$$/{;s/^#minimum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
ub=system("sed -ne '/^#maximum:	\\(.*\\)$$/{;s/^#maximum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
set palette model HSV functions gray*4/6,1,1;
set xlabel '{/$(SERIF) time {/$(SLANT) i}}';
set ylabel '{/$(SERIF) time {/$(SLANT) j}}';
set size square;
set xrange $(XYRANGE);
set yrange $(XYRANGE);
set cblabel '{/$(SERIF) inter-point distance {/$(SLANT) d_{ij}}}';

set tmargin at screen $(YMARGIN)+($(VSIZE)/$(SCALE))/$(YSIZE);
set bmargin at screen $(YMARGIN);
set lmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE);
set rmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2+($(VSIZE)/$(SCALE)))/$(XSIZE);

set multiplot;

set colorbox vertical default;
set cbrange [0:ub];
plot input.".ld" with dots palette notitle;

set colorbox horizontal user origin (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE),.725+$(YMARGIN) size $(VSIZE)/$(SCALE)/$(XSIZE),.035;
set cblabel offset 0,6;
set cbtics offset 0,3,0;
set cbrange [lb:ub]; set log cb;
plot input.".ud" with dots palette notitle;

unset multiplot;

endef
export VPLOT_SVG

v%.svg: v%.dat
	GNUTERM='svg enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$VPLOT_SVG"
#	perl -pe "s/<circle id='gpDot' (.*)\/>/<circle id='gpDot' \1 fill='currentColor'\/>/" -i '$@'
	perl -pe "s/<circle id='gpDot' r='[^']*' (.*)\/>/<rect id='gpDot' width='$(shell printf '%g\n' $$(echo 530/500 | bc -l))' height='$(shell printf '%g\n' $$(echo 530/500 | bc -l))' \1 fill='currentColor'\/>/" -i '$@'

#
define PPLOT_EPS
lb=system("sed -ne '/^#minimum:	\\(.*\\)$$/{;s/^#minimum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
ub=system("sed -ne '/^#maximum:	\\(.*\\)$$/{;s/^#maximum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
set palette model HSV functions gray*4/6,1,1;
set xlabel '{/$(SERIF) time {/$(SLANT) i}}';
set ylabel '{/$(SERIF) time {/$(SLANT) j}}';
set size square;
set xrange $(XYRANGE);
set yrange $(XYRANGE);
set cblabel '{/$(SERIF) inter-point distance {/$(SLANT) d_{ij}}}';
comment='set datafile missing "NaN"';

set tmargin at screen $(YMARGIN)+($(VSIZE)/$(SCALE))/$(YSIZE);
set bmargin at screen $(YMARGIN);
set lmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE);
set rmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2+($(VSIZE)/$(SCALE)))/$(XSIZE);

set multiplot;

set colorbox vertical default;
set cbrange [0:ub];
plot input using 1:2:(!($$1 < $$2) ? $$3 : ub*(log($$3)-log(lb))/(log(ub)-log(lb))) with image notitle;

set colorbox horizontal user origin (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE),.725+$(YMARGIN) size $(VSIZE)/$(SCALE)/$(XSIZE),.035;
set cblabel offset 0,7.5;
set cbtics offset 0,4,0;
set cbrange [lb:ub]; set log cb;
plot input using 1:2:(($$1 < $$2) ? $$3 : lb*exp($$3*(log(ub)-log(lb))/ub)) with image notitle;

unset multiplot;

endef
export PPLOT_EPS

p%.eps: p%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$PPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$PPLOT_EPS"
	perl -ne 'if (!$$sw) { $$sw = !0 if (/^%%%%EndImage$$/); print; } else { $$flag = !0 if (/^%%%%BeginImage$$/); print if (!$$flag); $$flag = 0 if (/^%%%%EndImage$$/); }' -i '$@'
	perl -pe "s/\/Pnt {.*} def/\/Pnt {stroke gsave 0 setlinecap 1 setlinewidth M 1 0 V stroke grestore} def/" -i '$@'

#
define PPLOT_PDF
lb=system("sed -ne '/^#minimum:	\\(.*\\)$$/{;s/^#minimum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
ub=system("sed -ne '/^#maximum:	\\(.*\\)$$/{;s/^#maximum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
set palette model HSV functions gray*4/6,1,1;
set xlabel '{/$(SERIF) time {/$(SLANT) i}}';
set ylabel '{/$(SERIF) time {/$(SLANT) j}}';
set size square;
set xrange $(XYRANGE);
set yrange $(XYRANGE);
set cblabel '{/$(SERIF) inter-point distance {/$(SLANT) d_{ij}}}';
comment='set datafile missing "NaN"';

set tmargin at screen $(YMARGIN)+($(VSIZE)/$(SCALE))/$(YSIZE);
set bmargin at screen $(YMARGIN);
set lmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE);
set rmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2+($(VSIZE)/$(SCALE)))/$(XSIZE);

set multiplot;

set colorbox vertical default;
set cbrange [0:ub];
plot input using 1:2:(!($$1 < $$2) ? $$3 : NaN) with image notitle;

set colorbox horizontal user origin (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE),.725+$(YMARGIN) size $(VSIZE)/$(SCALE)/$(XSIZE),.035;
set cblabel offset 0,7.5;
set cbtics offset 0,4,0;
set cbrange [lb:ub]; set log cb;
plot input using 1:2:(($$1 < $$2) ? $$3 : NaN) with image notitle;

unset multiplot;

endef
export PPLOT_PDF

p%.pdf: p%.dat
	GNUTERM='pdf enhanced fontscale .425 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$PPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$PPLOT_PDF"

#
define PPLOT_SVG
lb=system("sed -ne '/^#minimum:	\\(.*\\)$$/{;s/^#minimum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
ub=system("sed -ne '/^#maximum:	\\(.*\\)$$/{;s/^#maximum:	\\(.*\\)$$/\\1/p;q;}' ".input.".lub");
set palette model HSV functions gray*4/6,1,1;
set xlabel '{/$(SERIF) time {/$(SLANT) i}}';
set ylabel '{/$(SERIF) time {/$(SLANT) j}}';
set size square;
set xrange $(XYRANGE);
set yrange $(XYRANGE);
set cblabel '{/$(SERIF) inter-point distance {/$(SLANT) d_{ij}}}';
comment='set datafile missing "NaN"';

set tmargin at screen $(YMARGIN)+($(VSIZE)/$(SCALE))/$(YSIZE);
set bmargin at screen $(YMARGIN);
set lmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE);
set rmargin at screen (($(XSIZE)-($(VSIZE)/$(SCALE)))/2+($(VSIZE)/$(SCALE)))/$(XSIZE);

set multiplot;

set colorbox vertical default;
set cbrange [0:ub];
plot input using 1:2:(!($$1 < $$2) ? $$3 : NaN) with image notitle;

set colorbox horizontal user origin (($(XSIZE)-($(VSIZE)/$(SCALE)))/2)/$(XSIZE),.725+$(YMARGIN) size $(VSIZE)/$(SCALE)/$(XSIZE),.035;
set cblabel offset 0,6;
set cbtics offset 0,3,0;
set cbrange [lb:ub]; set log cb;
plot input using 1:2:(($$1 < $$2) ? $$3 : NaN) with image notitle;

unset multiplot;

endef
export PPLOT_SVG

p%.svg: p%.dat
	GNUTERM='svg enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; $$PPLOT_SVG"
#	perl -pe "s/<circle id='gpDot' (.*)\/>/<circle id='gpDot' \1 fill='currentColor'\/>/" -i '$@'
	perl -pe "s/<circle id='gpDot' r='[^']*' (.*)\/>/<rect id='gpDot' width='1' height='1' \1 fill='currentColor'\/>/" -i '$@'

#
clean:
	rm -f $(EXES) $(EXES:%=v%.dat) $(EXES:%=p%.dat) $(EXES:%=v%.dat.lub) $(EXES:%=p%.dat.lub) $(EXES:%=v%.dat.ld) $(EXES:%=p%.dat.ld) $(EXES:%=v%.dat.ud) $(EXES:%=p%.dat.ud) .DS_Store

distclean: clean
	rm -f $(ALL)
