Skip to content

Commit

Permalink
remove Python dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ctroupin committed Dec 6, 2024
1 parent a6bb069 commit f7f5c92
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 47 deletions.
30 changes: 24 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
name = "DIVAndFairEase"
uuid = "f8965cc9-43af-47ab-b800-13a38f4ffdea"
authors = ["ctroupin <[email protected]>"]
version = "0.1.0"

[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
Contour = "d38c429a-6771-53c6-b99e-75d170b6e991"
DIVAnd = "efc8151c-67de-5a8f-9a35-d8f54746ae9d"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
GeoMakie = "db073c08-6b98-4ee5-b6a4-5efafb3259c6"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
NaNStatistics = "b946abbf-3ea7-4610-9019-9858bfdeaf2d"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"

[compat]
CairoMakie = "0.12.16"
Colors = "0.12.11"
Contour = "0.6.3"
Dates = "1.11.0"
GeoJSON = "0.8.1"
GeoMakie = "0.7.9"
HTTP = "1.10.12"
JSON = "0.21.4"
JSON3 = "1.14.1"
Makie = "0.21.16"
NCDatasets = "0.14.6"
NaNStatistics = "0.6.43"
OrderedCollections = "1.7.0"
80 changes: 39 additions & 41 deletions src/DIVAndFairEase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ using Dates
using NCDatasets
using OrderedCollections
using NaNStatistics
using PyPlot
const plt = PyPlot
using PyCall
mpl = pyimport("matplotlib")


"""
varbyunits(footprintfile, units)
Expand Down Expand Up @@ -52,7 +49,7 @@ function varbyunits(footprintfile::String, units::Vector{String})
end

"""
prepare_query_new(parameter1, parameter2, datestart, dateend, mindepth, maxdepth, minlon, maxlon, minlat, maxlat)
prepare_query(parameter1, parameter2, datestart, dateend, mindepth, maxdepth, minlon, maxlon, minlat, maxlat)
Prepare the JSON query that will be passed to the API, based on the data, depth and coordinate ranges.
"""
Expand All @@ -61,7 +58,7 @@ function prepare_query_new(datasource::AbstractString, parameter1::String, param
minlat::Float64, maxlat::Float64; dateref::Date=Dates.Date(1950, 1, 1)
)

# The reference data can change according to the datasource!
# The reference date can change according to the datasource!
if datasource == "World Ocean Database"
dateref = Dates.Date(1770, 1, 1)
elseif datasource == "EMODnet Chemistry"
Expand Down Expand Up @@ -96,8 +93,8 @@ function prepare_query_new(datasource::AbstractString, parameter1::String, param

elseif occursin("CORA", datasource)
queryparams = [
OrderedDict("column_name" => parameter1, "alias" => parameter1),
OrderedDict("column_name" => "JULD", "alias" => "TIME"),
OrderedDict("column_name" => parameter1, "alias" => parameter1, "skip_fill_values" => true),
OrderedDict("column_name" => "cf_datetime", "alias" => "TIME"),
OrderedDict("column_name" => "DEPH", "alias" => "DEPTH"),
OrderedDict("column_name" => "LONGITUDE", "alias" => "LONGITUDE"),
OrderedDict("column_name" => "LATITUDE", "alias" => "LATITUDE")
Expand Down Expand Up @@ -130,7 +127,7 @@ function prepare_query_new(datasource::AbstractString, parameter1::String, param
]
end

# Filters for the coordinates
# Filters for the coordinates and variables
if datasource == "SeaDataNet CDI TS"
filters = [
OrderedDict("for_query_parameter" => "TIME", "min" => Dates.format(datestart, "yyyymmddT00:00:00"), "max" => Dates.format(dateend, "yyyymmddT00:00:00")),
Expand All @@ -141,7 +138,7 @@ function prepare_query_new(datasource::AbstractString, parameter1::String, param
elseif occursin("CORA", datasource)
@info("Working with CORA dataset")
filters = [
OrderedDict("for_query_parameter" => "TIME", "min" => mintemporal, "max" => maxtemporal),
OrderedDict("for_query_parameter" => "TIME", "min" => Dates.format(datestart, "yyyy-mm-ddT00:00:00"), "max" => Dates.format(dateend, "yyyy-mm-ddT00:00:00"), "cast" => "timestamp"),
OrderedDict("for_query_parameter" => "DEPTH", "min" => mindepth, "max" => maxdepth),
OrderedDict("for_query_parameter" => "LONGITUDE", "min" => minlon, "max" => maxlon),
OrderedDict("for_query_parameter" => "LATITUDE", "min" => minlat, "max" => maxlat),
Expand All @@ -165,6 +162,7 @@ function prepare_query_new(datasource::AbstractString, parameter1::String, param
return body
end


"""
prepare_query(parameter, unit, datestart, dateend, mindepth, maxdept, minlon, maxlon, minlat, maxlat)
Expand Down Expand Up @@ -344,41 +342,41 @@ end
Prepare a geoJSON file containing the contour of `field2D` and a file containing the
color function in Javascript.
"""
function write_field_json(lon, lat, field2D::Matrix{AbstractFloat}, Δvar::Float64; cmap=plt.cm.RdYlBu_r,
resfile::AbstractString="./field.js", funfile::AbstractString="./colorfunction.js")

vmin = nanminimum(field2D)
vmax = nanmaximum(field2D)
@info("vmin = $vmin, vmax = $vmax")
values = collect(floor(vmin / Δvar ) * Δvar : Δvar : floor(vmax / Δvar ) * Δvar)
norm = mpl.colors.Normalize(vmin=values[1], vmax=vmax[end])

fieldjson = field2json(lon, lat, field2D, values)

# Write the contours in the geoJSON file
open(resfile, "w") do df
write(df, "var field = ")
write(df, fieldjson)
end
# function write_field_json(lon, lat, field2D::Matrix{AbstractFloat}, Δvar::Float64; cmap=plt.cm.RdYlBu_r,
# resfile::AbstractString="./field.js", funfile::AbstractString="./colorfunction.js")

# vmin = nanminimum(field2D)
# vmax = nanmaximum(field2D)
# @info("vmin = $vmin, vmax = $vmax")
# values = collect(floor(vmin / Δvar ) * Δvar : Δvar : floor(vmax / Δvar ) * Δvar)
# norm = mpl.colors.Normalize(vmin=values[1], vmax=vmax[end])

# fieldjson = field2json(lon, lat, field2D, values)

# # Write the contours in the geoJSON file
# open(resfile, "w") do df
# write(df, "var field = ")
# write(df, fieldjson)
# end

# Prepare the color function
colorlistrgb = cmap.(norm.(values))
colorlisthex = [hex(RGB(thecolor[1], thecolor[2], thecolor[3])) for thecolor in colorlistrgb];
# # Prepare the color function
# colorlistrgb = cmap.(norm.(values))
# colorlisthex = [hex(RGB(thecolor[1], thecolor[2], thecolor[3])) for thecolor in colorlistrgb];

# Write the color function (Javascript)
open(funfile, "w") do df
write(df, "function getMoreColor(d) {")
write(df, "return ")
for (vv, cc) in zip(values[1:end-1], colorlisthex[1:end-1])
write(df, "d < $(vv) ? '#$(cc)' :")
end
write(df, "'#$(colorlisthex[end])'; ")
write(df, "}")
end
# # Write the color function (Javascript)
# open(funfile, "w") do df
# write(df, "function getMoreColor(d) {")
# write(df, "return ")
# for (vv, cc) in zip(values[1:end-1], colorlisthex[1:end-1])
# write(df, "d < $(vv) ? '#$(cc)' :")
# end
# write(df, "'#$(colorlisthex[end])'; ")
# write(df, "}")
# end

return nothing
# return nothing

end
# end


"""
Expand Down

0 comments on commit f7f5c92

Please sign in to comment.