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

Add python script for generating a TIPMIP CO2 emission file. #14

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions PreProc/inputdata/atm/cam/ggas/create_TIPMIP_emission_file.py
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

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)

Copy link

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.

Copy link
Author

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.

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')



1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Miscellaneous pre- and post-processing scripts for NorESM
- mg_budget_plot.py: Script for plotting the the tendency terms that contribute to the total tendencies of the cloud microphysics scheme by Morrison and Gettelman in NorESM2/CAM6
- PostProc: Scripts for post processing model output
- rcat_noresm.sh: Concatenate and compress NorESM model output
- PreProc: Scripts for preparing model input datasets
- Utilities: Miscellaneous scripts and programs
- buildCPRNC_NIRD.sh: File to build the cprnc tool needed by rcat_noresm.sh
- test_rcat_noresm.sh: Unit tests for rcat_noresm.sh