-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContourExport.wl
147 lines (109 loc) · 4.11 KB
/
ContourExport.wl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(* ::Package:: *)
BeginPackage["ContourExport`"]
ContourExport::usage="Export background and contour lines of the passed ContourPlot to a PNG and a CSV file, respectively."
SampleStep::usage="SampleStep is an option for ContourExport which specifies the number of steps between contour line samples."
WriteTikz::usage="WriteTikz is an option for ContourExport that controls whether to write a TikZ picture to a file."
PrintColorbar::usage="PrintColorbar is an option for ContourExport that specifies whether to print the PgfPlots colorbar definitions."
Standalone::usage="Standalone is a setting for the WriteTikz option. It specifies that a standalone TikZ picture should be created."
Begin["`Private`"]
getSurface[Graphics[GraphicsComplex[v_,{d_,__}],___],size_,ar_]:=
Rasterize[
Graphics[GraphicsComplex[v,d],PlotRangePadding->None,AspectRatio->ar],
"Image",
ImageSize->size
]
getLines[Graphics[GraphicsComplex[v_,{_,d_}],___]]:=Block[{l},
l=d//.{{}->Nothing,Directive[__]->Nothing,Tooltip[e_,_]->e};
Flatten[l]/.Line[c_]:>v[[c]]
]
getHeights[Graphics[GraphicsComplex[_,{_,d_}],___]]:=Reverse[d/.{Tooltip[_,v_]:>v,{}->Nothing}]
getColors[Graphics[GraphicsComplex[_,{d_,_}],___]]:=d[[All,2]]
exportLines[f_String,l:{{{_?NumericQ,_?NumericQ}..}..}]:=
(
WriteString[f,StringRiffle[ExportString[#,"CSV"]&/@l,"\n\n"]];
Close[f]
)
safeRange[max_,step_]:=DeleteDuplicates[Append[Range[1,max,step],max]]
decimate[step_][lis_]:=
Table[
Through[(Interpolation/@Transpose[lis])[x]],
{x,safeRange[Length[lis],step]}
]
formatColor[RGBColor[c__]]:=StringRiffle[Round[{c}*255],{"rgb255=(",",",")"}]
colorBarString[g:Graphics[GraphicsComplex[___],___]]:=Block[{h=getHeights[g]},
StringTemplate["colormap={}{``},\ncolorbar style={\n ytick={1,...,``},\n yticklabels={``}\n},"]
[StringRiffle[formatColor/@getColors[g],","],
Length[h],
StringRiffle[h,","]]
]
labelString[labels:{{_,_},{_,_}}]:=Block[{l=labels/.None->Nothing},
StringRiffle[{
If[Length[l[[2]]]==1,"xlabel = {$x$},",Nothing],
If[Length[l[[1]]]==1,"ylabel = {$y$},",Nothing]},
"\n"
]
]
tikzString[fprefix_String,g:Graphics[GraphicsComplex[___],o_]]:=
Block[{r=PlotRange/.o,l=FrameLabel/.o,ls=labelString[l]},
StringTemplate[
"\\begin{tikzpicture}
\\begin{axis}[
domain = <* #r[[1,1]] *>:<* #r[[1,2]] *>,
y domain = <* #r[[2,1]] *>:<* #r[[2,2]] *>,`labels`
minor tick num = 4,
colorbar as palette,
colorbar style={
grid=major,
grid style={black},
ytick style={draw=none},
},
enlargelimits = false,
axis on top,
% Output from PrintColorbar
`colorbar`
]
% Colored background
\\addplot graphics[xmin = <* #r[[1,1]] *>, xmax = <* #r[[1,2]] *>, ymin = <* #r[[2,1]] *>, ymax = <* #r[[2,2]] *>] {`fprefix`.png};
% Contour lines
\\addplot[mark=none] table[col sep=comma] {`fprefix`.csv};
\\end{axis}
\\end{tikzpicture}\n"][
<|"fprefix"->fprefix,
"colorbar"->StringReplace[colorBarString[g],"\n"->"\n "],
"labels"->If[ls=="",ls,StringReplace["\n"<>ls,"\n"->"\n "]],
"r"->r|>
]
]
writeTikz[fprefix_String,g:Graphics[GraphicsComplex[___],_]]:=
Block[{f=fprefix<>".tex"},
(
WriteString[f,tikzString[fprefix,g]];
Close[f]
)
]
writeTikzStandalone[fprefix_String,g:Graphics[GraphicsComplex[___],_]]:=
Block[{f=fprefix<>".tex"},
(
WriteString[f,
"\\documentclass{standalone}
\\usepackage{pgfplots}
\\begin{document}\n"
<>tikzString[fprefix,g]<>
"\\end{document}\n"];
Close[f]
)
]
printColorbar[g:Graphics[GraphicsComplex[___],___]]:=
Print[colorBarString[g]]
ContourExport[fprefix_String,g:Graphics[GraphicsComplex[___],___],OptionsPattern[]]:=
(
If[OptionValue[PrintColorbar],printColorbar[g]];
{
exportLines[fprefix<>".csv",decimate[OptionValue[SampleStep]]/@getLines[g]],
Export[fprefix<>".png",getSurface[g,OptionValue[ImageSize],OptionValue[AspectRatio]],"CompressionLevel"->1],
Switch[OptionValue[WriteTikz],True,writeTikz[fprefix,g],Standalone,writeTikzStandalone[fprefix,g],_,Nothing]
}
)
Options[ContourExport]={SampleStep->5,ImageSize->Automatic,AspectRatio->Automatic,WriteTikz->True,PrintColorbar->False}
End[]
EndPackage[]