Skip to content

Commit 27cc4e3

Browse files
committed
Added CO2 concentration fdata retrieval get_co2_concentration
1 parent 7cbbf93 commit 27cc4e3

File tree

4 files changed

+114
-49
lines changed

4 files changed

+114
-49
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
1717
FTPClient = "01fcc997-4f28-56b8-8a06-30002c134abb"
1818
GeoDataFrames = "62cb38b5-d8d2-4862-a48e-6a340996859f"
1919
Geomorphometry = "714e3e49-7933-471a-9334-4a6a65a92f36"
20+
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
2021
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
2122
Pipe = "b98c9c47-44ae-5843-9183-064241ee97a0"
2223
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
@@ -35,6 +36,7 @@ CondaPkg = "0.2.26"
3536
Dates = "1.11"
3637
DocStringExtensions = "0.9.3"
3738
GeoDataFrames = "0.3.11"
39+
HTTP = "1.10.16"
3840
Logging = "1.11"
3941
Pipe = "1.3.0"
4042
PythonCall = "0.9.24"

repository/default/scenarios/default/res/settings_res_fr.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# A vector with the information of what data to force download
2-
# possible values are: (empty, default), "dtm", "soil", "adm_borders", "clc", "forinv"
2+
# possible values are: (empty, default), "dtm", "soil", "adm_borders", "clc", "forinv", "clim_h", "clim_f", "co2_conc_h", "co2_conc_f"
33
force_download: []
44
# A vector with the information of which parts of ML workflow to force retrain
55
# possible values are: (empty, default), "tree_volumes", "ae_climh"
@@ -54,6 +54,7 @@ data_sources:
5454
activity_id: "ScenarioMIP"
5555
table_id: "Amon"
5656
fixed_climate: true
57+
# Standard ssp scenarios: 126, 245, 370 and 585. See https://www.dkrz.de/en/communication/climate-simulations/cmip6-en/the-ssp-scenarios
5758
experiment_id: "ssp126"
5859
experiment_id_extreme: "ssp585"
5960
institution_id: "MPI-M"
@@ -70,6 +71,14 @@ data_sources:
7071
ae_max_ntrains: 100
7172
ae_hidden_layer_size: 600
7273
ae_encoded_size: 6
74+
75+
# See also:
76+
# - https://greenhousegases.science.unimelb.edu.au/#!/view
77+
# - https://gmd.copernicus.org/articles/13/3571/2020/
78+
# - https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_annmean_mlo.txt
79+
co2_conc_url: "https://raw.githubusercontent.com/SantanderMetGroup/wrf-ghg-files/refs/heads/master/CAMtr_volume_mixing_ratio.${SSP_SCENARIO}"
80+
81+
7382

7483
px_fixed_data:
7584
ae_nsample: 100000

src/Regions/fr/src/Res/Get_data.jl

Lines changed: 101 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function get_data!(settings,mask)
1919
@info "- getting climate (historic or future depending on scenario)..."
2020
input_rasters["clim"] = get_climate_data!(settings,mask)
2121
settings["res"]["fr"]["input_rasters"] = input_rasters
22+
@info "- getting CO2 atmospheric concentration (historic or future depending on scenario)..."
23+
co2_conc_h, co2_conc_f = get_co2_concentration(settings,mask)
24+
settings["res"]["fr"]["co2_conc_h"] = co2_conc_h; settings["res"]["fr"]["co2_conc_f"] = co2_conc_f
25+
2226
@info "- getting inventory data..."
2327
inv_data = get_inventory_data(settings,mask)
2428
settings["res"]["fr"]["inv_data"] = inv_data
@@ -265,53 +269,6 @@ function get_clc(settings,mask)
265269
return clc_vars
266270
end
267271

268-
"""
269-
get_inventory_data(settings,mask)
270-
271-
Download the forest inventory data and change the (X,Y) coordinated of the points to the CRS used in the model.
272-
Returns a dicionary with the file paths (no further elaborated, still in csv format)
273-
"""
274-
function get_inventory_data(settings,mask)
275-
forinv_url = settings["res"]["fr"]["data_sources"]["forest_inventory_url"]
276-
force = "forinv" in settings["res"]["fr"]["force_download"]
277-
forest_inventory_crs = settings["res"]["fr"]["data_sources"]["forest_inventory_cres_epsg_id"]
278-
forinv_dirpath = joinpath(settings["res"]["fr"]["cache_path"],"forinv")
279-
forinv_dldirpath = joinpath(settings["res"]["fr"]["temp_path"],"forinv")
280-
forinv_dlpath = joinpath(forinv_dldirpath,basename(forinv_url))
281-
forinv_unzippeddir = joinpath(forinv_dldirpath,"data")
282-
forinv_data = Dict(
283-
"points" => joinpath(forinv_dirpath,"PLACETTE.csv"),
284-
"trees" => joinpath(forinv_dirpath,"ARBRE.csv"),
285-
"death_trees" => joinpath(forinv_dirpath,"BOIS_MORT.csv"),
286-
"tree_cover" => joinpath(forinv_dirpath,"COUVERT.csv"),
287-
"points_toposoil" => joinpath(forinv_dirpath,"ECOLOGIE.csv"),
288-
"species" => joinpath(forinv_dirpath,"FLORE.csv"),
289-
"habitat" => joinpath(forinv_dirpath,"HABITAT.csv"),
290-
)
291-
forinv_meta = Dict(
292-
"species_latin_name" => joinpath(forinv_dirpath,"espar-cdref13.csv"),
293-
"vars_availability_by_year" => joinpath(forinv_dirpath,"summary_data.csv"), # recapitulatif_donnees.csv is the French version
294-
"metadata" => joinpath(forinv_dirpath,"metadata.csv"), # metadonnees.csv is the French version
295-
)
296-
forinv_vars = Dict("data" => forinv_data, "meta"=>forinv_meta)
297-
(isdir(forinv_dirpath) && (!force) ) && return forinv_vars
298-
isdir(forinv_dirpath) || mkpath(forinv_dirpath)
299-
isdir(forinv_dldirpath) || mkpath(forinv_dldirpath)
300-
Downloads.download(forinv_url,forinv_dlpath)
301-
unzip(forinv_dlpath,forinv_unzippeddir)
302-
mv(forinv_unzippeddir,forinv_dirpath,force=true)
303-
points = CSV.read(forinv_data["points"],DataFrames.DataFrame)
304-
trans = Proj.Transformation("EPSG:$(forest_inventory_crs)", "EPSG:$(settings["simulation_region"]["cres_epsg_id"])", always_xy=true)
305-
for p in eachrow(points)
306-
(X,Y) = trans(p.XL,p.YL)
307-
p.XL = X
308-
p.YL = Y
309-
end
310-
CSV.write(forinv_data["points"],points)
311-
rm(forinv_dldirpath,recursive=true)
312-
return forinv_vars
313-
end
314-
315272
"""
316273
get_climate_data!(settings,mask)
317274
@@ -340,7 +297,7 @@ function get_climate_data!(settings,mask)
340297
hyears = clim_settings["hist_years"]
341298
fyears = parse(Int64,clim_settings["fefps"][1:4]):parse(Int64,clim_settings["fefpe"][1:4])
342299
fyears = maximum(hyears)+1:fyears[end]
343-
clim_settings["fut_years"] = fyears
300+
settings["res"]["fr"]["data_sources"]["clim"]["fut_years"] = fyears # updating the settings with the future years
344301
mmonths = [m<10 ? "0$m" : "$m" for m in 1:12]
345302
tr_fs_h = clim_settings["transformations_h"]
346303
tr_fs_f = clim_settings["transformations_f"]
@@ -511,4 +468,100 @@ chelsa_cmip6(
511468

512469
end
513470

471+
"""
472+
get_co2_concentration(settings,mask)
473+
474+
Download and retrieve the yearly average CO2 concentration in the atmosphere, historical and scenario-dependant future one.
475+
"""
476+
function get_co2_concentration(settings,mask)
477+
478+
verbosity = settings["verbosity"]
479+
verbose = verbosity in [GenFSM.HIGH, GenFSM.FULL]
480+
481+
# Remember that scenario is already embedded in the temp path, but the cache path shares several scenarios
482+
co2_h_destfolder = joinpath(settings["res"]["fr"]["cache_path"],"co2_historical")
483+
co2_f_destfolder = joinpath(settings["res"]["fr"]["cache_path"],"co2_future",settings["scenario"])
484+
isdir(co2_h_destfolder) || mkpath(co2_h_destfolder)
485+
isdir(co2_f_destfolder) || mkpath(co2_f_destfolder)
486+
co2_h_destpath = joinpath(co2_h_destfolder,"co2_conc.csv")
487+
co2_f_destpath = joinpath(co2_f_destfolder,"co2_conc.csv")
488+
clim_settings = settings["res"]["fr"]["data_sources"]["clim"]
489+
force_h = "co2_conc_h" in settings["res"]["fr"]["force_download"]
490+
force_f = "co2_conc_f" in settings["res"]["fr"]["force_download"]
491+
freeze = clim_settings["fixed_climate"] # boolean
492+
hyears = clim_settings["hist_years"]
493+
fyears = clim_settings["fut_years"] # added in get_climate_data!
494+
495+
if !force_h && !force_f && ispath(co2_h_destpath) && ispath(co2_f_destpath)
496+
return (co2_h_destpath, co2_f_destpath)
497+
end
498+
499+
# ok, we need to do somehting, let's download the file and get the data
500+
co2_conc_url = settings["res"]["fr"]["data_sources"]["co2_conc_url"]
501+
co2_conc_url = replace(co2_conc_url,"\${SSP_SCENARIO}" => uppercase(clim_settings["experiment_id"])) # eg. "ssp585" or "ssp126
502+
503+
data = @pipe HTTP.get(co2_conc_url).body |>
504+
CSV.File(_,delim=' ',header=false, ignorerepeated=true, skipto=3) |> DataFrames.DataFrame
505+
co2_h = data[in.(data[:,1],Ref(hyears)),2]
506+
if freeze
507+
co2_f = fill(co2_h[end], length(fyears))
508+
else
509+
co2_f = data[in.(data[:,1],Ref(fyears)),2]
510+
end
511+
512+
if force_h || ! ispath(co2_h_destpath)
513+
CSV.write(co2_h_destpath, DataFrames.DataFrame(years=hyears,co2_conc=co2_h))
514+
end
515+
if force_f || ! ispath(co2_f_destpath)
516+
CSV.write(co2_f_destpath, DataFrames.DataFrame(years=fyears,co2_conc=co2_f))
517+
end
518+
return (co2_h_destpath, co2_f_destpath)
519+
end
520+
514521

522+
"""
523+
get_inventory_data(settings,mask)
524+
525+
Download the forest inventory data and change the (X,Y) coordinated of the points to the CRS used in the model.
526+
Returns a dicionary with the file paths (no further elaborated, still in csv format)
527+
"""
528+
function get_inventory_data(settings,mask)
529+
forinv_url = settings["res"]["fr"]["data_sources"]["forest_inventory_url"]
530+
force = "forinv" in settings["res"]["fr"]["force_download"]
531+
forest_inventory_crs = settings["res"]["fr"]["data_sources"]["forest_inventory_cres_epsg_id"]
532+
forinv_dirpath = joinpath(settings["res"]["fr"]["cache_path"],"forinv")
533+
forinv_dldirpath = joinpath(settings["res"]["fr"]["temp_path"],"forinv")
534+
forinv_dlpath = joinpath(forinv_dldirpath,basename(forinv_url))
535+
forinv_unzippeddir = joinpath(forinv_dldirpath,"data")
536+
forinv_data = Dict(
537+
"points" => joinpath(forinv_dirpath,"PLACETTE.csv"),
538+
"trees" => joinpath(forinv_dirpath,"ARBRE.csv"),
539+
"death_trees" => joinpath(forinv_dirpath,"BOIS_MORT.csv"),
540+
"tree_cover" => joinpath(forinv_dirpath,"COUVERT.csv"),
541+
"points_toposoil" => joinpath(forinv_dirpath,"ECOLOGIE.csv"),
542+
"species" => joinpath(forinv_dirpath,"FLORE.csv"),
543+
"habitat" => joinpath(forinv_dirpath,"HABITAT.csv"),
544+
)
545+
forinv_meta = Dict(
546+
"species_latin_name" => joinpath(forinv_dirpath,"espar-cdref13.csv"),
547+
"vars_availability_by_year" => joinpath(forinv_dirpath,"summary_data.csv"), # recapitulatif_donnees.csv is the French version
548+
"metadata" => joinpath(forinv_dirpath,"metadata.csv"), # metadonnees.csv is the French version
549+
)
550+
forinv_vars = Dict("data" => forinv_data, "meta"=>forinv_meta)
551+
(isdir(forinv_dirpath) && (!force) ) && return forinv_vars
552+
isdir(forinv_dirpath) || mkpath(forinv_dirpath)
553+
isdir(forinv_dldirpath) || mkpath(forinv_dldirpath)
554+
Downloads.download(forinv_url,forinv_dlpath)
555+
unzip(forinv_dlpath,forinv_unzippeddir)
556+
mv(forinv_unzippeddir,forinv_dirpath,force=true)
557+
points = CSV.read(forinv_data["points"],DataFrames.DataFrame)
558+
trans = Proj.Transformation("EPSG:$(forest_inventory_crs)", "EPSG:$(settings["simulation_region"]["cres_epsg_id"])", always_xy=true)
559+
for p in eachrow(points)
560+
(X,Y) = trans(p.XL,p.YL)
561+
p.XL = X
562+
p.YL = Y
563+
end
564+
CSV.write(forinv_data["points"],points)
565+
rm(forinv_dldirpath,recursive=true)
566+
return forinv_vars
567+
end

src/Regions/fr/src/fr.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using DocStringExtensions, Dates
99
import Downloads
1010
import StatsBase
1111
import Pipe: @pipe
12+
import HTTP
1213
import ArchGDAL, Rasters, ZipFile, DataStructures # , FTPClient
1314
import Geomorphometry # for slope and aspect
1415
import Shapefile

0 commit comments

Comments
 (0)