Skip to content

Instantly share code, notes, and snippets.

@nocollier
Last active January 14, 2020 15:00
Show Gist options
  • Save nocollier/6f848e5b643d6f83537ac5a9bab0f9e0 to your computer and use it in GitHub Desktop.
Save nocollier/6f848e5b643d6f83537ac5a9bab0f9e0 to your computer and use it in GitHub Desktop.
Converts GCP carbon emission data to netCDF4 for use in ILAMB
import argparse
import glob
from netCDF4 import Dataset
import numpy as np
import xlrd,os,time
remote_source = "doi:10.18160/GCP-2019"
gist_source = "https://gist.github.com/nocollier/6f848e5b643d6f83537ac5a9bab0f9e0"
local_source = "Global_Carbon_Budget_2019v1.0.xlsx"
stamp = time.strftime('%Y-%m-%d', time.localtime(os.path.getmtime(local_source)))
# Parse information from the XLS workbook
land_sink_uncertainty = 0.9 # Pg
land_use_change_uncertainty = 0.7 # Pg
uncertainty = np.linalg.norm([land_sink_uncertainty,land_use_change_uncertainty])
year = []; luc = []; ls = []
with xlrd.open_workbook(local_source) as wb:
ws = wb.sheet_by_index(1)
for i in range(20,79):
year.append(float(ws.cell_value(i,1)))
luc .append(float(ws.cell_value(i,3)))
ls .append(float(ws.cell_value(i,6)))
year = np.asarray(year)
luc = np.asarray(luc )
ls = np.asarray(ls )
# Convert information into arrays ready to write
tb = (np.asarray([year,year+1]).T-1850)*365
t = tb.mean(axis=1)
nbp = ls-luc
nbp_bnds = np.asarray([nbp-uncertainty,nbp+uncertainty]).T
with Dataset("nbp_1960-2018.nc", mode="w") as dset:
# dimensions
dset.createDimension("time", size=t.size)
dset.createDimension("nb", size=2)
# time
T = dset.createVariable("time", t.dtype, ("time"))
T[...] = t
T.units = "days since 1850-01-01 00:00:00"
T.calendar = "noleap"
T.bounds = "time_bounds"
# time bounds
TB = dset.createVariable("time_bounds", t.dtype, ("time", "nb"))
TB[...] = tb
# data
D = dset.createVariable("nbp", nbp.dtype, ("time", ), fill_value = -99.99)
D[...] = nbp
D.units = "Pg yr-1"
D.standard_name = "surface_net_downward_mass_flux_of_carbon_dioxide_expressed_as_carbon_due_to_all_land_processes"
D.actual_range = np.asarray([nbp.min(),nbp.max()])
D.bounds = "nbp_bnds"
D.comment = "Computed by subtracting land use change emissions from the land sink"
# data bounds
DB = dset.createVariable("nbp_bnds", nbp.dtype, ("time", "nb"), fill_value = -99.99)
DB[...] = nbp_bnds
DB.units = "Pg yr-1"
DB.standard_name = "uncertainty bounds for global net downward land carbon flux"
# global attributes
dset.title = "Land anthropogenic carbon flux estimates"
dset.institution = "Global Carbon Project https://www.icos-cp.eu/GCP/2019"
dset.history = """
%s: downloaded source from %s
%s: converted to netCDF with %s""" % (stamp, remote_source, stamp, gist_source)
dset.references = """
@Article{essd-11-1783-2019,
author = {Friedlingstein, P. and Jones, M. W. and O'Sullivan, M. and Andrew, R. M. and Hauck, J. and Peters, G. P. and Peters, W. and Pongratz, J. and Sitch, S. and Le Qu\'er\'e, C. and Bakker, D. C. E. and Canadell, J. G. and Ciais, P. and Jackson, R. B. and Anthoni, P. and Barbero, L. and Bastos, A. and Bastrikov, V. and Becker, M. and Bopp, L. and Buitenhuis, E. and Chandra, N. and Chevallier, F. and Chini, L. P. and Currie, K. I. and Feely, R. A. and Gehlen, M. and Gilfillan, D. and Gkritzalis, T. and Goll, D. S. and Gruber, N. and Gutekunst, S. and Harris, I. and Haverd, V. and Houghton, R. A. and Hurtt, G. and Ilyina, T. and Jain, A. K. and Joetzjer, E. and Kaplan, J. O. and Kato, E. and Klein Goldewijk, K. and Korsbakken, J. I. and Landsch\"utzer, P. and Lauvset, S. K. and Lef\`evre, N. and Lenton, A. and Lienert, S. and Lombardozzi, D. and Marland, G. and McGuire, P. C. and Melton, J. R. and Metzl, N. and Munro, D. R. and Nabel, J. E. M. S. and Nakaoka, S.-I. and Neill, C. and Omar, A. M. and Ono, T. and Peregon, A. and Pierrot, D. and Poulter, B. and Rehder, G. and Resplandy, L. and Robertson, E. and R\"odenbeck, C. and S\'ef\'erian, R. and Schwinger, J. and Smith, N. and Tans, P. P. and Tian, H. and Tilbrook, B. and Tubiello, F. N. and van der Werf, G. R. and Wiltshire, A. J. and Zaehle, S.},
title = {Global Carbon Budget 2019},
journal = {Earth System Science Data},
volume = {11},
year = {2019},
number = {4},
pages = {1783--1838},
url = {https://www.earth-syst-sci-data.net/11/1783/2019/},
doi = {doi:10.5194/essd-11-1783-2019}
}"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment