-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add python script for generating a TIPMIP CO2 emission file. #14
Open
TomasTorsvik
wants to merge
4
commits into
NorESMhub:main
Choose a base branch
from
TomasTorsvik:add_CO2_emission_script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5c86c95
Add python script for generating a TIPMIP CO2 emission file.
TomasTorsvik aedbe7e
Update python script for generating a TIPMIP CO2 emission file.
TomasTorsvik 6dda4f2
Update TIPMIP esmission script
TomasTorsvik eec49f2
Also update the README.md file
TomasTorsvik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
PreProc/inputdata/atm/cam/ggas/create_TIPMIP_emission_file.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import numpy as np | ||
import sys | ||
import xarray as xr | ||
from scipy.io import netcdf | ||
import pandas as pd | ||
|
||
|
||
# load grid area from fx | ||
dataset = xr.open_dataset('/mnt/bgcdata-ns2980k/ffr043/TipESM/data/NorESM2-LM/1pctCO2/fx/areacella_fx_NorESM2-LM_1pctCO2_r1i1p1f1_gn.nc') | ||
areavar = dataset['areacella'] | ||
totarea = areavar.sum().values | ||
|
||
# load example emission file | ||
inpath = '/mnt/bgcdata-ns2980k/ffr043/TipESM/data' | ||
dataset = xr.open_mfdataset(inpath+'/emissions-cmip6_CO2_anthro_surface_175001-201512_fv_1.9x2.5_c20181011.nc', decode_times=False, format="NETCDF3_64BIT") | ||
co2var = dataset['CO2_flux'] | ||
|
||
# distribute emissions over time and space | ||
# translated to python from matlab based on script by J. Schwinger | ||
nyears = 250 | ||
nmonth = nyears * 12 | ||
nlon = 144 | ||
nlat = 96 | ||
start_year = 1 | ||
time_bnds = np.zeros((2, nmonth)) | ||
time = np.zeros(nmonth) | ||
date = np.zeros(nmonth, dtype=np.int32) | ||
co2flx = np.zeros((nmonth, nlat, nlon)) | ||
dayim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | ||
days = np.cumsum([0] + dayim[:-1]) + 1 | ||
daye = np.cumsum(dayim) | ||
|
||
# calculate emissions based on TCRE (Arora et al., 2020) | ||
TCRE = 1.32 #°C EgC-1 | ||
E = 1000 * 0.02 / TCRE #GtC yr-1 | ||
Edata = np.ones((nmonth, 1)) * E | ||
|
||
# (assumed) midpoint of each month in the format MMDD | ||
date_mint = [116, 215, 316, 416, 516, 616, 716, 816, 916, 1016, 1116, 1216] | ||
|
||
# Set emissions constant over each month or not | ||
constant_em = True | ||
|
||
for iy in range(1, nyears + 1): | ||
for im in range(1, 13): | ||
idx = (iy - 1) * 12 + im - 1 | ||
time_bnds[0, idx] = 365 * (iy - 1) + days[im - 1] - 1 | ||
time_bnds[1, idx] = 365 * (iy - 1) + daye[im - 1] | ||
time[idx] = np.sum(time_bnds[:, idx]) / 2.0 | ||
date[idx] = (1850 + (iy - 1)) * 10000 + date_mint[im - 1] | ||
|
||
# Spatially, emissions are distributed evenly over the sphere and months | ||
if start_year <= iy < start_year + nyears: | ||
if constant_em == True: | ||
# unit correction - Gt C yr-1 to kg CO2 s-1 | ||
co2flx[idx, :, :] = Edata[idx] * 1e12 * 3.664 / totarea / 86400.0 / 365.0 | ||
else: | ||
# unit correction sec month-1 | ||
dt = (time_bnds[1, idx] - time_bnds[0, idx]) * 86400.0 | ||
# unit correction - Gt C month-1 to kg CO2 s-1 | ||
co2flx[idx, :, :] = Edata[idx] * 1e12 * 3.664 / dt / totarea / 12 | ||
else: | ||
co2flx[idx, :, :] = 0.0 | ||
|
||
# write emissions to example dataset | ||
dataset = dataset.isel(time=slice(0, nmonth)) | ||
dataset['CO2_flux'].values = co2flx | ||
|
||
# assign attributes | ||
dataset = dataset.assign_attrs({'data_title':'Annual Anthropogenic Emissions of CO2 based on TCRE prepared for TIPMIP'}) | ||
dataset = dataset.assign_attrs({'data_creator':'F. Froeb ([email protected])'}) | ||
dataset = dataset.assign_attrs({'creation_date':'2024-08-03'}) | ||
|
||
#set encoding for netcdf file | ||
encoding = { | ||
'time':{'_FillValue': None}, | ||
'time_bnds':{'_FillValue': None}, | ||
'lon':{'zlib': True, 'shuffle': False, 'complevel': 1, 'fletcher32': False, 'contiguous': False, | ||
'dtype': 'float64', '_FillValue':None}, | ||
'lat':{'zlib': True, 'shuffle': False, 'complevel': 1, 'fletcher32': False, 'contiguous': False, | ||
'dtype': 'float64', '_FillValue':None}, | ||
'CO2_flux':{'zlib': True, 'shuffle': True, 'complevel': 9, 'fletcher32': False, 'contiguous': False, | ||
'dtype': 'float32', 'missing_value': 1e+20, '_FillValue': 1e+20} | ||
} | ||
|
||
#write netcdf | ||
dataset.to_netcdf('/mnt/bgcdata-ns2980k/ffr043/TipESM/data/emissions-ESM-tipmip_CO2_anthro_surface_185001-209912_fv_1.9x2.5_c20240823_cE.nc', mode="w", format="NETCDF3_64BIT", encoding=encoding, unlimited_dims='time') | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you leave this block here? I think the protocol specifies a constant emission rate, doesn't it? Then case
constant_em == False
is not needed and it is confusing that it is there as an option.(I don't think that there is a discernible difference between the two cases)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, the block can be removed for the TIPMIP protocol. I thought it could be of interest for other applications as a reference. If that is not useful and too confusing, I will remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have 30 years simulation with the
constant_em == False
option, I would like to see if this makes a difference at all for the model output (hopefully it doesn't). The script will in any case have to be changed to some extent if someone wants to use it to make a different emission input file.