@@ -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
266270end
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
512469end
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
0 commit comments