ClimateMARGO / ClimateMARGO.jl

Julia implementation of MARGO, an idealized climate-economic modelling framework for Optimizing trade-offs between emissions Mitigation, Adaptation, carbon dioxide Removal, and solar Geoengineering.
https://margo.plutojl.org/
MIT License
67 stars 13 forks source link

"How do I extract ClimateMARGO.jl data as a csv file?" #70

Closed hdrake closed 3 years ago

hdrake commented 3 years ago

There are two ways of extracting data from the ClimateModel type. If the data you want is already an argument within the type (or a subtype), then you can just extract it like this: m.economics.baseline_emissions If the data does not live within the ClimateModel type (e.g. diagnostics like temperature or CO2 concentrations), then you need to use a diagnostic function that computes those timeseries based on the information in the ClimateModel type. Many of these are included in the Diagnostics submodule. For example, to extract the temperature resulting from mitigated emissions: temperature = T(m, M=true)

Here is a minimal example of using extracting data to make a csv file (see also the attached csv file that it creates). 

# First, we create an example instance of ClimateMARGO.jl, from which we want to extract some outputs
using ClimateMARGO
using ClimateMARGO.Models
params = deepcopy(ClimateMARGO.IO.included_configurations["default"]);
m = ClimateModel(params);

# Next, we use DataFrames.jl to store time-series of ClimateMARGO.jl diagnostics
# (which the typeof function tells us are of type Array{Float64,1}) as columns.
# We include the "year" as the first column, for reference.
using DataFrames
df = DataFrame(
    year = t(m),
    baseline_emissions = m.economics.baseline_emissions, # or emissions(m, M=false)
    controlled_emissions = emissions(m, M=true)
    temperature = T(m, M=true)
)

# Finally, we use CSV.jl to output this dataframe as a CSV
using CSV
CSV.write("ClimateMARGO_temperature_example.csv", df)

You can see the source code for all of the different diagnostics here: https://github.com/ClimateMARGO/ClimateMARGO.jl/tree/master/src/DiagnosticsIf there is a diagnostic that you are interested in, but that is not yet computed, then you can use the existing diagnostics as a template.

hdrake commented 3 years ago

If you find yourself frequently doing the above to extract data from ClimateMARGO types and saving them as CSV files, I would recommend writing a function to do so. See examples of how we've done this with the JSON file structure here: https://github.com/ClimateMARGO/ClimateMARGO.jl/blob/master/src/IO/json_io.jl

txlauren commented 2 years ago

Thank you for your detailed response. I've tried adapting this solution to extract a time-series of the control deployment percentages but each control has the same values throughout the time-series in the resulting csv.

df = DataFrame(
      year =t(m),
      M = m.controls.mitigate,
      R = m.controls.remove,
      G = m.controls.geoeng,
      A = m.controls.adapt
)

Would you be able to advise me on where I might be going wrong?

Kind regards, Tilly

hdrake commented 2 years ago

Hi @txlauren, thanks for reaching out on here! I think the issue is that the example here has not yet been run through the optimization. So the controls are likely set to their defaults of zero at all times.

Try running the following commands to import the optimization functions and run the model through the control-optimizer.

using ClimateMARGO.Optimization
@time optimize_controls!(m);

You can see a more complete example in the documentation. Hope that helps!

txlauren commented 2 years ago

Thank you so much @hdrake, that did the trick!