#
# 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)	\
$(EXES:%=bv%.dat) $(EXES:%=bv%.dat.ld) $(EXES:%=bv%.dat.ud) $(EXES:%=bv%.eps) $(EXES:%=bv%.pdf) $(EXES:%=bv%.svg)	\
$(EXES:%=bp%.dat) $(EXES:%=bp%.dat.ld) $(EXES:%=bp%.dat.ud) $(EXES:%=bp%.eps) $(EXES:%=bp%.pdf) $(EXES:%=bp%.svg)	\

all: $(ALL)

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

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

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

bp%.dat: % makefile
	./$< -t 2000 -i 2000 -C -P -B 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

bvrecurrence-lorenz_attractor.dat: recurrence-lorenz_attractor makefile
	./$< -t 500 -i 500 -d .064 -C -B 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

bprecurrence-lorenz_attractor.dat: recurrence-lorenz_attractor makefile
	./$< -t 2000 -i 2000 -d .016 -C -P -B 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

bvrecurrence-rossler_attractor.dat: recurrence-rossler_attractor makefile
	./$< -t 500 -i 500 -d .2 -C -B 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

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

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

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

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

bp%.dat.ld: % makefile
	./$< -t 2000 -i 2000 -L -C -P -B | 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

bvrecurrence-lorenz_attractor.dat.ld: recurrence-lorenz_attractor makefile
	./$< -t 500 -i 500 -d .064 -L -C -B | 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

bprecurrence-lorenz_attractor.dat.ld: recurrence-lorenz_attractor makefile
	./$< -t 2000 -i 2000 -d .016 -L -C -P -B | 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

bvrecurrence-rossler_attractor.dat.ld: recurrence-rossler_attractor makefile
	./$< -t 500 -i 500 -d .2 -L -C -B | 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

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

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

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

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

bp%.dat.ud: % makefile
	./$< -t 2000 -i 2000 -U -C -P -B | 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

bvrecurrence-lorenz_attractor.dat.ud: recurrence-lorenz_attractor makefile
	./$< -t 500 -i 500 -d .064 -U -C -B | 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

bprecurrence-lorenz_attractor.dat.ud: recurrence-lorenz_attractor makefile
	./$< -t 2000 -i 2000 -d .016 -U -C -P -B | 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

bvrecurrence-rossler_attractor.dat.ud: recurrence-rossler_attractor makefile
	./$< -t 500 -i 500 -d .2 -U -C -B | 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

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

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

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

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

YMARGIN=.1125

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

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

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

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

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

v%.svg p%.svg bv%.svg bp%.svg: SCALE=1.2
v%.svg p%.svg bv%.svg bp%.svg: XSIZE=600
v%.svg p%.svg bv%.svg bp%.svg: YSIZE=480
v%.svg p%.svg bv%.svg bp%.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];
if (!bin_flag) {
  plot input.".ld" with dots palette notitle;
}
else {
  plot input binary format="%int%int%double" using 1:2:(!($$1 < $$2) ? $$3 : ub*(log($$3)-log(lb))/(log(ub)-log(lb))) 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;
if (!bin_flag) {
  plot input.".ud" with dots palette notitle;
}
else {
  plot input binary format="%int%int%double" using 1:2:(($$1 < $$2) ? $$3 : lb*exp($$3*(log(ub)-log(lb))/ub)) 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='$<'; bin_flag=0; $$VPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$VPLOT_EPS"
	perl -pe "s/\/Pnt {.*} def/\/Pnt {stroke gsave 0 setlinecap 10 setlinewidth M 10 0 V stroke grestore} def/" -i '$@'

bv%.eps: bv%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_EPS"
	perl -ne 'if (!$$sw) { $$sw = !0 if (/^% End plot #1$$/); print; } else { $$flag = !0 if (/^% Begin plot #1$$/); print if (!$$flag); $$flag = 0 if (/^% End plot #1$$/); }' -i '$@'
	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];
if (!bin_flag) {
  plot input.".ld" with dots palette notitle;
}
else {
  plot input binary format="%int%int%double" using 1:2:(!($$1 < $$2) ? $$3 : ub*(log($$3)-log(lb))/(log(ub)-log(lb))) 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;
if (!bin_flag) {
  plot input.".ud" with dots palette notitle;
}
else {
  plot input binary format="%int%int%double" using 1:2:(($$1 < $$2) ? $$3 : lb*exp($$3*(log(ub)-log(lb))/ub)) 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='$<'; bin_flag=0; $$VPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$VPLOT_PDF"

bv%.pdf: bv%.dat
	GNUTERM='pdf enhanced fontscale .425 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$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];
if (!bin_flag) {
  plot input.".ld" with dots palette notitle;
}
else {
  plot input binary format="%int%int%double" using 1:2:(!($$1 < $$2) ? $$3 : ub*(log($$3)-log(lb))/(log(ub)-log(lb))) 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;
if (!bin_flag) {
  plot input.".ud" with dots palette notitle;
}
else {
  plot input.".ud" binary format="%int%int%double" using 1:2:(($$1 < $$2) ? $$3 : lb*exp($$3*(log(ub)-log(lb))/ub)) 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='$<'; bin_flag=0; $$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 '$@'

bv%.svg: bv%.dat
	GNUTERM='svg enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$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];
if (!bin_flag) {
  plot input using 1:2:(!($$1 < $$2) ? $$3 : ub*(log($$3)-log(lb))/(log(ub)-log(lb))) with image notitle;
}
else {
  plot input binary format="%int%int%double" 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;
if (!bin_flag) {
  plot input using 1:2:(($$1 < $$2) ? $$3 : lb*exp($$3*(log(ub)-log(lb))/ub)) with image notitle;
}
else {
  plot input binary format="%int%int%double" 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='$<'; bin_flag=0; $$PPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$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 '$@'

bp%.eps: bp%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$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];
if (!bin_flag) {
  plot input using 1:2:(!($$1 < $$2) ? $$3 : NaN) with image notitle;
}
else {
  plot input binary format="%int%int%double" 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;
if (!bin_flag) {
  plot input using 1:2:(($$1 < $$2) ? $$3 : NaN) with image notitle;
}
else {
  plot input binary format="%int%int%double" 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='$<'; bin_flag=0; $$PPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$PPLOT_PDF"

bp%.pdf: bp%.dat
	GNUTERM='pdf enhanced fontscale .425 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$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];
if (!bin_flag) {
  plot input using 1:2:(!($$1 < $$2) ? $$3 : NaN) with image notitle;
}
else {
  plot input binary format="%int%int%double" 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;
if (!bin_flag) {
  plot input using 1:2:(($$1 < $$2) ? $$3 : NaN) with image notitle;
}
else {
  plot input binary format="%int%int%double" 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='$<'; bin_flag=0; $$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 '$@'

bp%.svg: bp%.dat
	GNUTERM='svg enhanced fontscale 1 size $(XSIZE)*$(SCALE),$(YSIZE)*$(SCALE)' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$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
	rm -f $(EXES:%=bv%.dat) $(EXES:%=bv%.dat.lub) $(EXES:%=bv%.dat.ld) $(EXES:%=bv%.dat.ud) $(EXES:%=bv%.eps) $(EXES:%=bv%.pdf) $(EXES:%=bv%.svg)
	rm -f $(EXES:%=bp%.dat) $(EXES:%=bp%.dat.lub) $(EXES:%=bp%.dat.ld) $(EXES:%=bp%.dat.ud) $(EXES:%=bp%.eps) $(EXES:%=bp%.pdf) $(EXES:%=bp%.svg)

distclean: clean
	rm -f $(ALL)
