Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hdrake opened this issue Apr 21, 2021 · 4 comments
Closed

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

hdrake opened this issue Apr 21, 2021 · 4 comments

Comments

@hdrake
Copy link
Collaborator

hdrake commented Apr 21, 2021

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 hdrake closed this as completed Apr 21, 2021
@hdrake
Copy link
Collaborator Author

hdrake commented Apr 21, 2021

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
Copy link

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
Copy link
Collaborator Author

hdrake commented Jan 31, 2022

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
Copy link

txlauren commented Feb 2, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants