-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInteroperability.qmd
More file actions
183 lines (136 loc) · 5.41 KB
/
Interoperability.qmd
File metadata and controls
183 lines (136 loc) · 5.41 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
---
title: "Interoperability with Other Packages"
---
## Interoperability with (some) other packages
GMT.jl types are plain and not fancy types built around matrices that implement the AbstractArray
and Tables interfaces. So, if one wants to make a *x,y* or *x,y,z* plot out of data stored in a type
with some other internal organization, the simplest is to extract the data in a Mxn matrix and
pass it directly to the {{< gmtref plot >}} or {{< gmtref plot3 >}} or, often even simpler, the `viz`
function.
GMT.jl does not have a recipes system like Plots or Makie but it can still find its way to recognize
types that it has no clue what they are. The next three sections show how we can make *x,y[,z]* plots
as well as maps from DataFrames, ODE (SciML) and Rasters types (other commonly used types may be added
in future). And that, repeating, without having no clue of what packages implement those types.
### DataFrames
As an example, we show an alternative to the Plots solution presented in this [SO question]
(https://stackoverflow.com/questions/69795215/plot-dataframes-in-julia-using-plots)
- Create and plot a DataFrame
```{julia}
using GMT, DataFrames
df = DataFrame(t = 1:10, series1 = sin.(1:10), series2=rand(10));
plot(df, show=true)
```
But one problem with the above solution is that although the `df` has three columns it only plotted a single curve.
This happens because in GMT 3rd and on columns may be used to control color, symbol sizes etc and cannot therefore
be assumed to *plotting data* by default. If we want that all columns are interpreted as data, we use the `multicol`
option, like:
```{julia}
using GMT, DataFrames
df = DataFrame(t = 1:10, series1 = sin.(1:10), series2=rand(10));
plot(df, legend=:colnames, multicol=true, show=true)
```
in the example above we took the legend entry from the column names, but if we want to use other labels we can do:
```julia
plot(df, legend=("A","B"), multicol=true, show=true)
```
### DifferentialEquations
The examples in this section were taken from DifferentialEquations.jl [examples](https://docs.sciml.ai/DiffEqDocs/stable/basics/plot/#Example)
```julia
using GMT, DifferentialEquations
function lorenz(du, u, p, t)
du[1] = p[1] * (u[2] - u[1])
du[2] = u[1] * (p[2] - u[3]) - u[2]
du[3] = u[1] * u[2] - p[3] * u[3]
end
u0 = [1.0, 5.0, 10.0];
tspan = (0.0, 100.0);
p = (10.0, 28.0, 8 / 3);
prob = ODEProblem(lorenz, u0, tspan, p);
sol = solve(prob);
plot(sol, multi=true, legend=:colnames, show=true)
```
{width=80% .center}
If we make a 3D plot of the _sol_ result, we get the following because first dimension
in the converted type is the time.
```julia
using GMT, DifferentialEquations
function lorenz(du, u, p, t)
du[1] = p[1] * (u[2] - u[1])
du[2] = u[1] * (p[2] - u[3]) - u[2]
du[3] = u[1] * u[2] - p[3] * u[3]
end
u0 = [1.0, 5.0, 10.0];
tspan = (0.0, 100.0);
p = (10.0, 28.0, 8 / 3);
prob = ODEProblem(lorenz, u0, tspan, p);
sol = solve(prob);
plot3(sol, show=true)
```
{width=80% .center}
To plot the parametric curve with the three _u1, u2, u3_ components, we do:
```julia
using GMT, DifferentialEquations
function lorenz(du, u, p, t)
du[1] = p[1] * (u[2] - u[1])
du[2] = u[1] * (p[2] - u[3]) - u[2]
du[3] = u[1] * u[2] - p[3] * u[3]
end
u0 = [1.0, 5.0, 10.0];
tspan = (0.0, 100.0);
p = (10.0, 28.0, 8 / 3);
prob = ODEProblem(lorenz, u0, tspan, p);
sol = solve(prob);
plot3(sol, x=:u1, y=:u2, z=:u3, show=true)
```
{width=80% .center}
To interpolate the solution (must be done manually) at 5 times more points that original, do:
```julia
using GMT, DifferentialEquations
function lorenz(du, u, p, t)
du[1] = p[1] * (u[2] - u[1])
du[2] = u[1] * (p[2] - u[3]) - u[2]
du[3] = u[1] * u[2] - p[3] * u[3]
end
u0 = [1.0, 5.0, 10.0];
tspan = (0.0, 100.0);
p = (10.0, 28.0, 8 / 3);
prob = ODEProblem(lorenz, u0, tspan, p);
sol = solve(prob);
plot3(sol, x=:u1, y=:u2, z=:u3, interp=5, show=true)
```
{width=80% .center}
### Rasters
Though many of the use cases shown could be reproduced by GMT.jl directly (it also reads netCDF and multi-layred files)
the Rasters.jl package is able to keep some metadata (namely the time axis) and lazy reading of large files that GMT
is not yet able to match.
Here we will reproduce some of the examples from this Rasters.jl [docs](https://rafaqz.github.io/Rasters.jl/dev/#Examples-and-Plotting).
```julia
using GMT
using Rasters, RasterDataSources, ArchGDAL
ENV["RASTERDATASOURCES_PATH"] = tempdir();
A = Raster(WorldClim{BioClim}, 5);
viz(A, colorbar=true)
```
{width=80% .center}
```{julia}
using GMT, Rasters
import NCDatasets
url = "https://archive.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc";
filename = download(url, "tos_O1_2001-2002.nc");
A = Raster(filename);
viz(A[Ti=6], proj=:guess, coast=true, colorbar=true)
```
```{julia}
url = "https://archive.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc";
filename = download(url, "tos_O1_2001-2002.nc");
A = Raster(filename);
viz(A[Ti=1:6], proj=:Robinson, coast=true, colorbar=true)
```
But _I Don't like Kelvins_. Fine, want centigrade? Just offset the _z_ values.
```{julia}
url = "https://archive.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc";
filename = download(url, "tos_O1_2001-2002.nc");
A = Raster(filename);
G = mat2grid(A[Ti=1:6], offset=-273.15);
viz(G, proj=:Robinson, coast=true, colorbar=true)
```