These past few months I’ve been working with the Global Change Assessment Model (GCAM) which is an integrated assessment model (IAM) that combines models of the global climate and economic systems. I wrote an earlier post on compiling GCAM on a Unix cluster. This post discusses some visualization tools I’ve developed for GCAM output.
GCAM models energy and agriculture systems at a regional level, where the world is composed of 32 regions. We’re interested in tracking statistics (like the policy cost of stabilization) over time and across regions. This required three things:
- The ability to draw a global map.
- The ability to shade individual political units on that map.
- The ability to animate this map.
Dr. Jon Herman has already posted a good example of how to do (1) in python using matplotlib’s Basemap. We’ll appropriate some of his example for this example. The Basemap has the option to draw coastlines and boundaries, but these boundaries are not tied to shapes, meaning that you can’t assign different colors to individual countries (task (2) above). To do that, we need a shapefile containing information about political boundaries. You can find these for free from a number of sources online, but I like Natural Earth. They provide data on many different scales. For this application I downloaded their coarsest data set. To give each country a shade which is tied to data, we use matplotlib’s color map. The basic plan is to generate a colored map for each time-step in our data, and then to animate the maps using the convert linux command.
Now that we’ve described roughly how we’ll proceed, a word about the data we’re dealing with and how I’ve handled it. GCAM has 32 geo-political regions, some of which are individual countries (like the USA or China), while others are groups of countries (like Australia & New Zealand). I stored this information using a list of lists (i.e. a 32-element list, where each element is a list of countries in that region). I’ve creatively named this variable list_list in this example (see code below). For each of the regions GCAM produces a time series of policy costs as a fraction of GDP every 5 years from 2020-2100. I’ve creatively named this variable data. We want to tie the color of a country in each time to its policy cost relative to costs across countries and times. To do this, I wrote the following (clumsy!) Python function, which I explain below.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import matplotlib.cm as cm
import matplotlib as mpl
import numpy as np
norm = mpl.colors.Normalize(vmin=MN, vmax=MX)
cmap = cm.coolwarm
a = np.zeros([32,4])
a = colors.to_rgba(data)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
m = Basemap(projection='robin', lon_0=0,resolution='c')
m.drawparallels(np.arange(-90.,91.,30.), labels=[1,0,0,1], dashes=[1,1], linewidth=0.25, color='0.5',fontsize=14)
m.drawmeridians(np.arange(0., 360., 60.), labels=[1,0,0,1], dashes=[1,1], linewidth=0.25, color='0.5',fontsize=14)
year = [1990,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050,2055,2060,2065,2070,2075,2080,2085,2090,2095,2100]
GCAM_32 = ['PRI','USA','VIR']
GCAM_1 = ['BDI','COM','DJI','ERI','ETH','KEN','MDG','MUS','REU','RWA','SDS','SDN','SOM','UGA','SOL']
GCAM_2 = ['DZA','EGY','ESH','LBY','MAR','TUN','SAH']
GCAM_3 = ['AGO','BWA','LSO','MOZ','MWI','NAM','SWZ','TZA','ZMB','ZWE']
GCAM_4 = ['BEN','BFA','CAF','CIV','CMR','COD','COG','CPV','GAB','GHA','GIN','GMB','GNB','GNQ','LBR','MLI','MRT','NER','NGA','SEN','SLE','STP','TCD','TGO']
GCAM_6 = ['AUS','NZL']
GCAM_7 = ['BRA']
GCAM_8 = ['CAN']
GCAM_9 = ['ABW','AIA','ANT','ATG','BHS','BLZ','BMU','BRB','CRI','CUB','CYM','DMA','DOM','GLP','GRD','GTM','HND','HTI','JAM','KNA','LCA','MSR','MTQ','NIC','PAN','SLV','TTO','VCT']
GCAM_10 = ['ARM','AZE','GEO','KAZ','KGZ','MNG','TJK','TKM','UZB']
GCAM_11 = ['CHN','HKG','MAC']
GCAM_13 = ['BGR','CYP','CZE','EST','HUN','LTU','LVA','MLT','POL','ROM','SVK','SVN']
GCAM_14 = ['AND','AUT','BEL','CHI','DEU','DNK','ESP','FIN','FLK','FRA','FRO','GBR','GIB','GRC','GRL','IMN','IRL','ITA','LUX','MCO','NLD','PRT','SHN','SMR','SPM','SWE','TCA','VAT','VGB','WLF']
GCAM_15 = ['BLR','MDA','UKR']
GCAM_16 = ['ALB','BIH','HRV','MKD','MNE','SCG','SRB','TUR','YUG']
GCAM_17 = ['CHE','ISL','LIE','NOR','SJM']
GCAM_18 = ['IND']
GCAM_19 = ['IDN']
GCAM_20 = ['JPN']
GCAM_21 = ['MEX']
GCAM_22 = ['ARE','BHR','IRN','IRQ','ISR','JOR','KWT','LBN','OMN','PSE','QAT','SAU','SYR','YEM']
GCAM_23 = ['PAK']
GCAM_24 = ['RUS']
GCAM_25 = ['ZAF']
GCAM_26 = ['GUF','GUY','SUR','VEN']
GCAM_27 = ['BOL','CHL','ECU','PER','PRY','URY']
GCAM_28 = ['AFG','ASM','BGD','BTN','LAO','LKA','MDV','NPL']
GCAM_29 = ['KOR']
GCAM_30 = ['BRN','CCK','COK','CXR','FJI','FSM','GUM','KHM','KIR','MHL','MMR','MNP','MYS','MYT','NCL','NFK','NIU','NRU','PCI','PCN','PHL','PLW','PNG','PRK','PYF','SGP','SLB','SYC','THA','TKL','TLS','TON','TUV','VNM','VUT','WSM']
GCAM_31 = ['TWN']
GCAM_5 = ['ARG']
GCAM_12 = ['COL']
list_list = [GCAM_1,GCAM_2,GCAM_3,GCAM_4,GCAM_5,GCAM_6,GCAM_7,GCAM_8,GCAM_9,GCAM_10,GCAM_11,GCAM_12,GCAM_13,GCAM_14,GCAM_15,GCAM_16,GCAM_17,GCAM_18,GCAM_19,GCAM_20,GCAM_21,GCAM_22,GCAM_23,GCAM_24,GCAM_25,GCAM_26,GCAM_27,GCAM_28,GCAM_29,GCAM_30,GCAM_31,GCAM_32]
num = len(list_list)
for info, shape in zip(m.comarques_info,m.comarques):
for i in range(num):
if info['adm0_a3'] in list_list[i]:
patches1 = 
patches1.append( Polygon(np.array(shape), True) )
ax.set_title('Policy Cost',fontsize=25,y=1.01)#GDP Adjusted Policy Cost#Policy Cost#Policy Cost Reduction from Technology
cb = m.colorbar(colors,'right')
filename = &quot;out/map_%s.png&quot; %(str(idx).rjust(3,&quot;0&quot;))
The function’s name is world_plot and it’s inputs are:
- The raw data for a specific time step.
- The index of the time step for the map we are working with (e.g. idx=0 for 2020).
- The minimum and maximum of the data across countries and time.
(1) is plotted, (2) is used to name the resulting png figure (line 73), and (3) is used to scale the color colormap (line 11). On lines 2-8 we import the necessary Python packages, which in this case are pretty standard Matplotlib packages and numpy. On lines 11-16 we generate a numpy array which contains the rgba color code for each of the data points in data. In lines 18-19 we create the pyplot figure object.
On lines 21-24 we create and format the Basemap object. Note that on line 21 I’ve selected the Robinson projection, but that the Basemap provides many options.
Lines 26-60 are specific for this application, and certainly could have been handled more compactly if I wanted to invest the time. year is a list of time steps for our GCAM experiment, and lines 27-58 contain lists of three letter ID codes for each GCAM region, which are assembled into a list of lists (creatively called list_list) on line 60.
On line 61 we read the data from the shapefile database which was downloaded from Natural Earth. From lines 63-68 we loop through the info and shape attributes of the shapefile database, and determine which of the GCAM geo-political units each of the administrative units in the database is associated with. Once this is determined, the polygon associated with that administrative unit is given the correct color (lines 66-68).
Lines 69-72 are doing some final formatting and labeling, and in lines 73-75 we are giving the file a unique name (tied to the time step plotted) and saving the images to some output directory.
When we put this function into a loop over time, we generate a sequence of figures looking something like this:
To convert this sequence of PNGs to a gif file, we use the convert command in linux (or in my case Cygwin). So, we go to the command line and cd into the directory where we’ve saved our figures and type:
convert -delay 45 -loop 0 *.png globe_Cost_Reduction_faster.gif
Here the delay flag controls the framerate of the gif (in milliseconds), the loop flag controls whether the gif repeats, next I’m using a wildcat to include all of the pngs in the output directory, and the final input is the resulting name of the gif. The final product: