Skip to content

Page2

CMIP6/Germany

Klimaprojektionen

{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A recreation of a New York Times chart showing U.S. budget forecasts versus reality.", "height": 400, "padding": 5, "background": null, "title": {"text": "Klimatrends, Deutschland, CMIP6: ssp126 vs ssp585","anchor":"start","fontSize":20}, "config": { "axisBand": { "bandPosition": 0, "labelPadding": 0, "tickExtra": false } }, "signals": [ { "name": "dragging", "value": false, "on": [ {"events": "@handle:mousedown", "update": "true"}, {"events": "window:mouseup", "update": "false"} ] }, { "name": "handleYear", "value": 2020, "on": [{ "events": "[@handle:mousedown, window:mouseup] > window:mousemove!", "update": "invert('x', clamp(x(), 0, width))" }] }, { "name": "currentYear", "update": "clamp(handleYear, 1960, 2100)" }, { "name": "tipYear", "on": [{ "events": "mousemove", "update": "dragging ? tipYear : invert('x', x())" }] }, { "name": "tipValue", "on": [{ "events": "mousemove", "update": "dragging ? tipValue : invert('y', y())" }] }, { "name": "cursor", "value": "default", "on": [{ "events": {"signal": "dragging"}, "update": "dragging ? 'pointer' : 'default'" }] } ], "data": [ { "name": "budgets", "url": "includes/cmip6_tas.json", "transform": [ { "type": "formula", "as": "abs", "expr": "abs(datum.value)" }, { "type": "formula", "as": "type", "expr": "datum.value < 0 ? 'deficit' : 'surplus'" } ] }, { "name": "budgets-current", "source": "budgets", "transform": [ { "type": "filter", "expr": "datum.budgetYear <= currentYear" } ] }, { "name": "budgets-actual", "source": "budgets", "transform": [ { "type": "filter", "expr": "datum.budgetYear <= currentYear && datum.forecastYear == datum.budgetYear - 1" } ] }, { "name": "tooltip", "source": "budgets", "transform": [ { "type": "filter", "expr": "datum.budgetYear <= currentYear && datum.forecastYear == tipYear && abs(datum.value - tipValue) <= 0.1" }, { "type": "aggregate", "fields": ["value", "value"], "ops": ["min", "argmin"], "as": ["min", "argmin"] }, { "type": "formula", "as": "tooltipYear", "expr": "datum.argmin.budgetYear" } ] }, { "name": "tooltip-forecast", "source": "budgets", "transform": [ { "type": "lookup", "from": "tooltip", "key": "tooltipYear", "fields": ["budgetYear"], "as": ["tooltip"] }, { "type": "filter", "expr": "datum.tooltip" } ] } ], "scales": [ { "name": "x", "type": "band", "domain": {"data": "budgets", "field": "forecastYear"}, "range": "width" }, { "name": "y", "type": "linear", "zero": true, "domain": {"data": "budgets", "field": "value"}, "range": "height" } ], "axes": [ { "orient": "bottom", "scale": "x", "grid": true, "domain": false, "values": [1960,1970,1980,1990,2000,2010,2020,2030,2040,2050,2060,2070,2080,2090,2100], "tickSize": 0, "encode": { "grid": { "enter": { "stroke": {"value": "gray"}, "strokeOpacity": {"value": 0.5} } }, "labels": { "update": { "x": {"scale": "x", "field": "value"} } } } }, { "orient": "right", "scale": "y", "grid": true, "domain": false, "values": [-1,0,1,2,3,4,5,6], "tickSize": 0, "encode": { "grid": { "enter": { "stroke": {"value": "gray"}, "strokeOpacity": {"value": 0.5} } }, "labels": { "enter": { "text": {"signal": "format(datum.value, '$.1f') + ' trillion'"} } } } } ], "marks": [ { "type": "group", "from": { "facet": { "name": "facet", "data": "budgets-current", "groupby": "budgetYear" } }, "marks": [ { "type": "line", "from": {"data": "facet"}, "encode": { "update": { "x": {"scale": "x", "field": "forecastYear"}, "y": {"scale": "y", "field": "value"}, "stroke": {"value": "orangered"}, "strokeWidth": {"value": 2.0}, "strokeOpacity": {"value": 0.75} } } } ] }, { "type": "line", "from": {"data": "budgets-actual"}, "encode": { "update": { "x": {"scale": "x", "field": "forecastYear"}, "y": {"scale": "y", "field": "value"}, "stroke": {"value": "steelblue"}, "strokeWidth": {"value": 3} } } }, { "type": "line", "from": {"data": "tooltip-forecast"}, "encode": { "update": { "x": {"scale": "x", "field": "forecastYear"}, "y": {"scale": "y", "field": "value"}, "stroke": {"value": "black"}, "strokeWidth": {"value": 1} } } }, { "type": "symbol", "from": {"data": "tooltip"}, "encode": { "update": { "x": {"scale": "x", "field": "argmin.forecastYear"}, "y": {"scale": "y", "field": "argmin.value"}, "size": {"value": 50}, "fill": {"value": "black"} } } }, { "type": "rule", "encode": { "enter": { "y": {"scale": "y", "value": -1}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 1} }, "update": { "x": {"value": 0}, "x2": {"scale": "x", "signal": "currentYear"} } } }, { "name": "handle", "type": "symbol", "encode": { "enter": { "y": {"scale": "y", "value": -1, "offset": 0}, "shape": {"value": "triangle-down"}, "size": {"value": 400}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 0.5} }, "update": { "x": {"scale": "x", "signal": "currentYear"}, "fill": {"signal": "dragging ? 'lemonchiffon' : '#fff'"} }, "hover": { "fill": {"value": "orangered"}, "cursor": {"value": "pointer"} } } }, { "type": "text", "encode": { "enter": { "x": {"value": 0}, "y": {"value": 0}, "fontSize": {"value": 32}, "fontWeight": {"value": "bold"}, "fill": {"value": "orangered"} }, "update": { "text": {"signal": "currentYear"} } } } ] }

{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A recreation of a New York Times chart showing U.S. budget forecasts versus reality.", "width": 1000, "height": 400, "padding": 5, "background": null, "title": {"text": "Klimatrends, Deutschland, CMIP6: ssp126 vs ssp585","anchor":"start","fontSize":20}, "config": { "axisBand": { "bandPosition": 0, "labelPadding": 0, "tickExtra": false } }, "signals": [ { "name": "dragging", "value": false, "on": [ {"events": "@handle:mousedown", "update": "true"}, {"events": "window:mouseup", "update": "false"} ] }, { "name": "handleYear", "value": 2020, "on": [{ "events": "[@handle:mousedown, window:mouseup] > window:mousemove!", "update": "invert('x', clamp(x(), 0, width))" }] }, { "name": "currentYear", "update": "clamp(handleYear, 1960, 2100)" }, { "name": "tipYear", "on": [{ "events": "mousemove", "update": "dragging ? tipYear : invert('x', x())" }] }, { "name": "tipValue", "on": [{ "events": "mousemove", "update": "dragging ? tipValue : invert('y', y())" }] }, { "name": "cursor", "value": "default", "on": [{ "events": {"signal": "dragging"}, "update": "dragging ? 'pointer' : 'default'" }] } ], "data": [ { "name": "budgets", "url": "includes/cmip6_pr.json", "transform": [ { "type": "formula", "as": "abs", "expr": "abs(datum.value)" }, { "type": "formula", "as": "type", "expr": "datum.value < 0 ? 'deficit' : 'surplus'" } ] }, { "name": "budgets-current", "source": "budgets", "transform": [ { "type": "filter", "expr": "datum.budgetYear <= currentYear" } ] }, { "name": "budgets-actual", "source": "budgets", "transform": [ { "type": "filter", "expr": "datum.budgetYear <= currentYear && datum.forecastYear == datum.budgetYear - 1" } ] }, { "name": "tooltip", "source": "budgets", "transform": [ { "type": "filter", "expr": "datum.budgetYear <= currentYear && datum.forecastYear == tipYear && abs(datum.value - tipValue) <= 0.1" }, { "type": "aggregate", "fields": ["value", "value"], "ops": ["min", "argmin"], "as": ["min", "argmin"] }, { "type": "formula", "as": "tooltipYear", "expr": "datum.argmin.budgetYear" } ] }, { "name": "tooltip-forecast", "source": "budgets", "transform": [ { "type": "lookup", "from": "tooltip", "key": "tooltipYear", "fields": ["budgetYear"], "as": ["tooltip"] }, { "type": "filter", "expr": "datum.tooltip" } ] } ], "scales": [ { "name": "x", "type": "band", "domain": {"data": "budgets", "field": "forecastYear"}, "range": "width" }, { "name": "y", "type": "linear", "zero": true, "domain": {"data": "budgets", "field": "value"}, "range": "height" } ], "axes": [ { "orient": "bottom", "scale": "x", "grid": true, "domain": false, "values": [1960,1970,1980,1990,2000,2010,2020,2030,2040,2050,2060,2070,2080,2090,2100], "tickSize": 0, "encode": { "grid": { "enter": { "stroke": {"value": "gray"}, "strokeOpacity": {"value": 0.5} } }, "labels": { "update": { "x": {"scale": "x", "field": "value"} } } } }, { "orient": "right", "scale": "y", "grid": true, "domain": false, "values": [-0.05,0,0.05,0.1,0.15,0.2,0.25], "tickSize": 0, "encode": { "grid": { "enter": { "stroke": {"value": "gray"}, "strokeOpacity": {"value": 0.5} } }, "labels": { "enter": { "text": {"signal": "format(datum.value, '$.1f') + ' trillion'"} } } } } ], "marks": [ { "type": "group", "from": { "facet": { "name": "facet", "data": "budgets-current", "groupby": "budgetYear" } }, "marks": [ { "type": "line", "from": {"data": "facet"}, "encode": { "update": { "x": {"scale": "x", "field": "forecastYear"}, "y": {"scale": "y", "field": "value"}, "stroke": {"value": "dodgerblue"}, "strokeWidth": {"value": 2.0}, "strokeOpacity": {"value": 0.75} } } } ] }, { "type": "line", "from": {"data": "budgets-actual"}, "encode": { "update": { "x": {"scale": "x", "field": "forecastYear"}, "y": {"scale": "y", "field": "value"}, "stroke": {"value": "steelblue"}, "strokeWidth": {"value": 3} } } }, { "type": "line", "from": {"data": "tooltip-forecast"}, "encode": { "update": { "x": {"scale": "x", "field": "forecastYear"}, "y": {"scale": "y", "field": "value"}, "stroke": {"value": "black"}, "strokeWidth": {"value": 1} } } }, { "type": "symbol", "from": {"data": "tooltip"}, "encode": { "update": { "x": {"scale": "x", "field": "argmin.forecastYear"}, "y": {"scale": "y", "field": "argmin.value"}, "size": {"value": 50}, "fill": {"value": "black"} } } }, { "type": "rule", "encode": { "enter": { "y": {"scale": "y", "value": -0.10}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 1} }, "update": { "x": {"value": 0}, "x2": {"scale": "x", "signal": "currentYear"} } } }, { "name": "handle", "type": "symbol", "encode": { "enter": { "y": {"scale": "y", "value": -0.1, "offset": 0}, "shape": {"value": "triangle-down"}, "size": {"value": 400}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 0.5} }, "update": { "x": {"scale": "x", "signal": "currentYear"}, "fill": {"signal": "dragging ? 'lemonchiffon' : '#fff'"} }, "hover": { "fill": {"value": "dodgerblue"}, "cursor": {"value": "pointer"} } } }, { "type": "text", "encode": { "enter": { "x": {"value": 0}, "y": {"value": 0}, "fontSize": {"value": 30}, "fontWeight": {"value": "bold"}, "fill": {"value": "dodgerblue"} }, "update": { "text": {"signal": "currentYear"} } } } ] }

{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "data": {"url": "includes/cmip6.csv"}, "height": 500, "title": {"text": "Klimatrends, Deutschland, CMIP6: ssp126 vs ssp585","anchor":"start","fontSize":20}, "params": [{ "name": "_select_", "value": "tas585", "bind": {"input": "select","options": ["tas126","tas585","pr126","pr585"]} }], "layer": [{ "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[1961,1990]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"line","color":"#DFFF00","strokeWidth":3,"point": {"filled": false,"fill": "white","size":100,"color":"#DFFF00"}}, "encoding": { "x": {"type": "temporal", "field": "date", "timeUnit": "month","title":null}, "y": {"type": "quantitative","aggregate": "mean","field": "y","scale": {"zero": false}}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"} } }, { "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[1961,1990]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"rule"}, "encoding": { "y": {"aggregate": "mean","field": "y","type": "quantitative"}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"}, "color": {"value": "#DFFF00"}, "size": {"value": 3} } }, { "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[1991,2020]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"line","color":"#FFBF00","strokeWidth":3,"point": {"filled": false,"fill": "white","size":100,"color":"#FFBF00"}}, "encoding": { "x": {"type": "temporal", "field": "date", "timeUnit": "month","title":null}, "y": {"type": "quantitative","aggregate": "mean","field": "y","scale": {"zero": false}}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"} } }, { "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[1991,2020]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"rule"}, "encoding": { "y": {"aggregate": "mean","field": "y","type": "quantitative"}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"}, "color": {"value": "#FFBF00"}, "size": {"value": 3} } }, { "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[2021,2050]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"line","color":"#FF7F50","strokeWidth":3,"point": {"filled": false,"fill": "white","size":100,"color":"#FF7F50"}}, "encoding": { "x": {"type": "temporal", "field": "date", "timeUnit": "month","title":null}, "y": {"type": "quantitative","aggregate": "mean","field": "y","scale": {"zero": false}}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"} } }, { "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[2021,2050]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"rule"}, "encoding": { "y": {"aggregate": "mean","field": "y","type": "quantitative"}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"}, "color": {"value": "#FF7F50"}, "size": {"value": 3} } }, { "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[2051,2080]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"line","color":"#DE3163","strokeWidth":3,"point": {"filled": false,"fill": "white","size":100,"color":"#DE3163"}}, "encoding": { "x": {"type": "temporal", "field": "date", "timeUnit": "month","title":null}, "y": {"type": "quantitative","aggregate": "mean","field": "y","scale": {"zero": false}}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"} } }, { "transform": [{ "filter": {"timeUnit": "year", "field":"date","range":[2051,2080]} }, { "calculate": "datum[_select_]", "as": "y" }], "mark": {"type":"rule"}, "encoding": { "y": {"aggregate": "mean","field": "y","type": "quantitative"}, "tooltip": {"field": "y","aggregate": "mean","type": "quantitative","format": ".1f"}, "color": {"value": "#DE3163"}, "size": {"value": 3} } } ] }

{ "data": { "url": "includes/cmip6_parallel.json" }, "transform": [ {"window": [{"op": "count", "as": "index"}]}, {"fold": ["00","01","02","03","04","05","06","07","08","09","10","11","12"]} ], "mark": "line", "encoding": { "color": {"type": "nominal", "field": "species","scale": "ord"}, "detail": {"type": "nominal", "field": "index"}, "opacity": {"value": 0.5}, "strokeWidth":{"value":0.5}, "x": {"type": "nominal", "field": "key","scale": "ord","title":null}, "y": {"type": "quantitative", "field": "value","title":null} }, "width": 1000, "height": 500 }

Code

Writing JSON

import numpy as N

for p in ['tas','pr']:

    file = '../data/icmip6_'+p+'_mon_onemean_ssp126_Germany_5lan_su_100p.dat'
    ssp126 = N.genfromtxt(file,comments='#',dtype=float)

    ssp126 = ssp126[:,1:]

    file = '../data/icmip6_'+p+'_mon_onemean_ssp585_Germany_5lan_su_100p.dat'
    ssp585 = N.genfromtxt(file,comments='#',dtype=float)

    jo = N.array(ssp585[:,0],int)
    ssp585 = ssp585[:,1:]

    nj = len(jo)

    f = open('includes/cmip6_'+p+'.json','w')
    f.write('[\n')

    for j in range(nj):

        if((jo[j]>=1961)&(jo[j]<=2099)):

            tmp = N.mean(ssp126[j,:])-N.mean(ssp126[0:30,:])

            f.write('\t{\n')

            f.write('\t\t"budgetYear": %i,\n'%jo[j])
            f.write('\t\t"forecastYear": %i,\n'%(jo[j]))
            f.write('\t\t"value": %.3f\n'%tmp)
            f.write('\t},\n')

            tmp = N.mean(ssp585[j,:])-N.mean(ssp585[0:30,:])

            f.write('\t{\n')

            f.write('\t\t"budgetYear": %i,\n'%jo[j])
            f.write('\t\t"forecastYear": %i,\n'%(jo[j]))
            f.write('\t\t"value": %.3f\n'%tmp)

            if(jo[j]==2099):

                f.write('\t}\n')

            else:

                f.write('\t},\n')

    f.write(']\n')
    f.close()

Writing CSV

import numpy as N

file = '../data/icmip6_tas_mon_onemean_ssp126_Germany_5lan_su_100p.dat'
tas126 = N.genfromtxt(file,comments='#',dtype=float)

jo = N.array(tas126[:,0],int)
tas126 = tas126[:,1:]

file = '../data/icmip6_tas_mon_onemean_ssp585_Germany_5lan_su_100p.dat'
tas585 = N.genfromtxt(file,comments='#',dtype=float)
tas585 = tas585[:,1:]

file = '../data/icmip6_pr_mon_onemean_ssp126_Germany_5lan_su_100p.dat'
pr126 = N.genfromtxt(file,comments='#',dtype=float)
pr126 = pr126[:,1:]

file = '../data/icmip6_pr_mon_onemean_ssp585_Germany_5lan_su_100p.dat'
pr585 = N.genfromtxt(file,comments='#',dtype=float)
pr585 = pr585[:,1:]

nj = len(jo)
mo = [1,2,3,4,5,6,7,8,9,10,11,12]
nm = len(mo)

print (jo)

f = open('./includes/cmip6.csv','w')
f.write('date,tas126,tas585,pr126,pr585\n')

for j in range(nj):
    for m in range(nm):

        if((jo[j]>=1961)&(jo[j]<=2099)):

           f.write('%04i-%02i-01,%.1f,%.1f,%.1f,%.1f\n'%(jo[j],mo[m],tas126[j,m],tas585[j,m],pr126[j,m],pr585[j,m]))

           if((jo[j]==2099)&(mo[m]==12)):

               f.write('\t}\n')

           else:

               f.write('\t},\n') 

f.close()

Writing Parallel

tas126 = N.cumsum(tas126,1)
tas585 = N.cumsum(tas585,1)
pr126 = N.cumsum(pr126,1)
pr585 = N.cumsum(pr585,1)

f = open('./includes/cmip6_parallel.json','w')

f.write('[\n')

for p in ['tas126','tas585','pr126','pr585']:
    for j in range(nj):

        if((jo[j]>=1961)&(jo[j]<=2099)):

            f.write('\t{')

            f.write('"species": "%s",'%p)
            f.write('"Year": "%i",'%jo[j])
            f.write('"00": %.3f,'%0)

            for m in range(nm): 

                if(p=="tas126"): f.write('"%02i": %.3f'%(mo[m],tas126[j,m]))
                if(p=="tas585"): f.write('"%02i": %.3f'%(mo[m],tas585[j,m]))
                if(p=="pr126"): f.write('"%02i": %.3f'%(mo[m],pr126[j,m]))
                if(p=="pr585"): f.write('"%02i": %.3f'%(mo[m],pr585[j,m]))

                if(mo[m]!=12):

                   f.write(',')

            if((jo[j]==2099)&(p=='pr585')):

                 f.write('}\n')

            else:

                 f.write('},\n')

f.write(']\n')

f.close()