// iplot_co2.js
// @version 0.6.2
// @copyright 2021,2024; Friedrich Wichmann
// Javascript-Code für die Diagramme und Tabellen mit CO2-Emissionen
const ppi_sek = [0, 'ind', 'ene', 'ver', 'geb', 'lan', 'abf', 'sen'];
var ppi_winW = [0, 0, 0, 0];
var ppi_redb = null; /* Sektor-Reduktions-Array für Plan B */
var ppi_inp_tredb = 1; /* -Felder für Reduktion schreibbar? */
var ppi_spez = 0; /* Spezialmodus */
var ppi_drai = 0; /* mit Extra-CO2-Senken */
var ppi_tred = 0; /* mit Anzeige der Reduktion */
var ppi_rgap = 0; /* mit Anzeige der Reduktionslücke */
var ppi_itot = 0; /* mit Flächensummen */
var ppi_iran = 0; /* mit Langzeit-Bereich */
var ppi_stumm = 0; /* stumm */
var ppi_temco = []; /* Farben für Temperatur */
var ppi_taby = 0; /* mit Jahr in Tabelle */
var ppi_lan_neg = 2035; /* Landwirtschaft negativ x-Wert */
/* Initialisierung für Diagramm dnr */
function initTestpage(dnr) {
clog("iplot_co2_js init " + dnr);
var canvas = document.querySelector("#ppi-diag-" + dnr);
checkSizeDrawCanvas(canvas, dnr, -1);
window.addEventListener('resize', function () {
var canvas = document.querySelector("#ppi-diag-" + dnr);
checkSizeDrawCanvas(canvas, dnr);
});
/* nur für iplot_co2_temp dnr={5,6} */
var labsol = document.querySelector("#ppi-lab-sol");
if (labsol && window.location.host.indexOf("localhost") < 0) {
labsol.classList.add("ppi-lab-sol-0");
}
var form5 = document.querySelector("#ppi-form-5");
var elems5 = (form5) ? form5.elements : undefined;
for (var i=0; elems5 && i= 0 || name5.indexOf("ppi-inp-5") >= 0) {
elems5[i].addEventListener('change',
function () { checkbox5Change(5); });
continue;
}
}
/* nur für iplot_co2_de dnr={4,3} */
var form = document.querySelector("#ppi-plan-" + dnr);
var elems = (form) ? form.elements : undefined;
if (!elems) return;
for (var i=0; i2-Senken" ],
txt5 : [ "Red", "Reduktion CO2-Ausstoß" ],
txt6 : [ "Lücke", "Lücke bei Einsparung von CO2" ],
txt7 : [ "stumm", "stumm" ],
txt8 : [ "Tabelle", "in Tabelle" ],
varr : "u",
varri : "v0",
vgap : "u",
vgapi : "v1",
lev : undefined,
bis : 2020,
tot : {
hist : { val : 0 },
pl19 : { val : 0 },
plta : { val : 0 },
/*pl21 : { val : 0 },
mypl : { val : 0 },*/
pl90 : 12700,
},
span1 : document.querySelector("#ppi-span1-" + dnr),
span2 : document.querySelector("#ppi-span2-" + dnr),
span3 : document.querySelector("#ppi-span3-" + dnr),
spanS : document.querySelector("#ppi-span-" + dnr + "sp"),
spanD : document.querySelector("#ppi-span-" + dnr + "dr"),
spanR : document.querySelector("#ppi-span-" + dnr + "tr"),
spanG : document.querySelector("#ppi-span-" + dnr + "ga"),
spanM : document.querySelector("#ppi-span-" + dnr + "st"),
spanL : document.querySelector("#ppi-span-" + dnr + "ta"),
trsen : document.querySelector("#ppi-tr-sen-3"),
};
var lim15 = ref([dnr, data.col.lim15, data.row.lim]);
var lim17 = ref([dnr, data.col.lim17, data.row.lim]);
var limwg = ref([dnr, data.col.limwg, data.row.lim]);
var limki = ref([dnr, data.col.limki, data.row.lim]);
var tol = 100;
data.lev = [ lim15+tol, lim17+tol, limwg+tol, limki+tol ];
if (dnr == 4) {
data.bis = getYearBis(dnr);
data.row.yrbis = data.row.yr1990 + data.bis - 1990;
data.row.yrab = data.row.yrbis + 1;
data.lnrn2045 = "l4"; data.lnrn2050 = "l5";
data.lnrzalt = "l6"; data.lnrzneu = "l8";
data.lnrmplan = "l9";
}
if (!ppi_redb) {
ppi_redb = data.red0;
}
getTotRedArrow(dnr, data, pl);
getRedGapArrow(dnr, data, pl);
getChangeList(dnr, data);
fillTableWithPlanData(dnr, data, row);
if (data.span2) data.span2.innerText = "";
if (data.span3) data.span3.innerText = "";
if (data.spanS) data.spanS.innerText = data.txt3[(ppi_spez) ? 1 : 0];
if (data.spanD) data.spanD.innerHTML = data.txt4[(ppi_drai) ? 1 : 0];
if (data.spanR) data.spanR.innerHTML = data.txt5[(ppi_tred) ? 1 : 0];
if (data.spanG) data.spanG.innerHTML = data.txt6[(ppi_rgap) ? 1 : 0];
if (data.spanM) data.spanM.innerHTML = data.txt7[(ppi_stumm) ? 1 : 0];
if (data.spanL) data.spanL.innerHTML = data.txt8[(ppi_taby) ? 1 : 0];
if (!pl) {
planChangeWeiterSo(dnr, data);
}
else if (pl=='A') {
planChangeBReg2019(dnr, data);
}
else if (pl=='N') {
planChangeBReg2021(dnr, data);
}
else if (pl=='B') {
planCheckboxTo(dnr, pl);
planChangeMeinPlan(dnr, row, data);
}
}
/* Tabelle mit CO2-Daten von 2019 und BReg Plänen füllen */
function fillTableWithPlanData(dnr, d, row)
{
if (dnr < 3 || dnr > 4) {
return;
}
if (d.trsen) {
d.trsen.className = (ppi_drai) ? "ppi-tr-sen-1" : "ppi-tr-sen-0";
}
var inptr = document.querySelector("#ppi-inp-"+dnr+"tr-l input[name='ppi-inp-"+dnr+"tr']");
var taby = d.bis;
if (inptr && parseInt(inptr.value) && parseInt(inptr.value) < d.bis) {
taby = parseInt(inptr.value);
}
var spnta = document.querySelector("#ppi-tstta-th-3");
if (spnta) {
spnta.innerHTML = taby + "
[Mt]
CO2-Äq.";
}
var sum1990 = 0.0, sum2019 = 0.0, sum20ta = 0.0, wav2019 = 0, wav2021 = 0;
var isl = (ppi_drai) ? 0 : 2;
for (var i=0; i[Mt]";
}
var td3 = document.querySelector("#ppi-tredn-"+d.sek[i]+"-3");
if (td3) {
td3.innerHTML = (br2021) ? br2021 : "0 [Mt]";
}
if (row < 0) {
var inp = document.querySelector("input[name='ppi-tredb-"+d.sek[i]+"-3']");
if (inp) {
var old = inp.value;
inp.value = "-"+ppi_redb[i+1]+((i==d.idrai) ? "" : " %");
ppi_redb[i+1] = (typeof(old) == "string" && i!=d.idrai) ? -Number(old.slice(0, -2)) : -old;
}
}
}
td0 = document.querySelector("#ppi-tst90-sum-3");
if (td0 && sum1990) {
td0.innerText = sum1990.toFixed(1);
}
d.tot.hist.val = sum1990;
td1 = document.querySelector("#ppi-tst19-sum-3");
if (td1) {
td1.innerText = sum2019.toFixed(1);
}
d.tot.pl19.val = sum2019;
tdta = document.querySelector("#ppi-tstta-sum-3");
if (tdta) {
tdta.innerText = sum20ta.toFixed(1);
}
d.tot.plta.val = sum20ta;
td2 = document.querySelector("#ppi-treda-sum-3");
if (td2) {
td2.innerText = (sum2019 > 0) ? (wav2019 / sum2019).toFixed(0)+" %" : "";
}
td3 = document.querySelector("#ppi-tredn-sum-3");
if (td3 && wav2021) {
td3.innerText = (sum2019 > 0) ? (wav2021 / sum2019).toFixed(0) + " %" : "";
}
td1 = document.querySelector("#ppi-tst90-19-tot-3");
var to90bis = (dnr == 4) ? ref([dnr, d.col.tot, d.row.to90bis]) : undefined;
if (td1 && to90bis) {
td1.innerText = (to90bis*1000).toFixed(0);
}
td2 = document.querySelector("#ppi-treda-tot-3");
var tobrpla = (dnr == 4) ? ref([dnr, d.col.tot, d.row.brpla]) : undefined;
if (td2 && tobrpla) {
td2.innerText = (tobrpla*1000).toFixed(0);
}
td3 = document.querySelector("#ppi-tredn-tot-3");
/*if (td3) {
td3.className = "ppi-improved2";
}*/
var tobrpln = (dnr == 4) ? ref([dnr, d.col.tot, d.row.brpln]) : undefined;
if (td3 && tobrpln) {
td3.innerText = (tobrpln*1000).toFixed(0);
}
var spn1 = document.querySelector("#ppi-span-4in span");
var spn2 = document.querySelector("#ppi-span-4in span ~ span");
if (spn1 && spn2) {
spn1.innerText = "1990 - "+d.bis;
spn2.innerText = (d.bis + 1) + " - 2050 / bis 0";
}
var spn3 = document.querySelector("#ppi-tst90-19-tot-3 ~ span");
var spn4 = document.querySelector("#ppi-tst19-td-3 span ~ span");
var spn5 = document.querySelector("#ppi-tst90-19-ta-td-3 span ~ span");
var td4 = document.querySelector("#ppi-tredb-tot-3");
if (spn3 && spn4 && spn5 && td4) {
spn3.innerText = " (1990-"+d.bis+")";
spn4.innerText = " (1990-"+d.bis+")";
spn5.innerText = " (1990-"+d.bis+")";
td4.innerText = "("+(d.bis + 1)+"-2050)";
}
}
/* Hilfsfunktion */
function setClassInnerText(el, txt, cls) {
if (el) {
el.className = cls;
el.innerText = txt;
}
}
/* Gibt Daten für den Gesamt-Reduktions-Pfeil in d.varr,d.varri zurück */
function getTotRedArrow(dnr, d, pl) {
var blist = document.querySelectorAll("#ppi-inp-"+dnr+"tr-l button");
var inptr = document.querySelector("#ppi-inp-"+dnr+"tr-l input[name='ppi-inp-"+dnr+"tr']");
if (!ppi_tred || dnr < 3 || dnr > 4) {
d.varr = "u";
d.varri = d.varr202x;
for (var i=0; i 2050) year = 2050;
}
var red = 0;
var stbis = null;
if (year <= d.bis)
{
stbis = ref([dnr, d.col.tot, d.row.yr1990 + year - 1990]);
red = (st1990) ? Math.round(stbis/st1990*100)-100 : 0;
} else {
var plcol = d.col.trend;
if (pl == 'A') plcol = d.col.planA;
else if (pl == 'N') plcol = d.col.planN;
else if (pl == 'B') plcol = d.col.myplfw;
stbis = ref([dnr, plcol, d.row.yr1990 + year - 1990]);
red = (st1990) ? Math.round(stbis/st1990*100)-100 : 0;
}
var txt = red.toString()+" %";
var tyd = (year < 2025) ? 5 : -125;
d.varri = "v0";
/* [x,y2,c1,ty,w,yd,c2,txt] */
d.varr = [year, st1990, stbis, "grey", "1a", 1, tyd, "#cc33ff", txt];
clog("getTotRedArrow year="+year+" "+pl+" varri="+d.varri+" varr="+d.varr);
}
/* Gibt Daten für den Reduktionslücken-Pfeil in d.vgap,d.vgapi zurück */
function getRedGapArrow(dnr, d, plan) {
var blist = document.querySelectorAll("#ppi-inp-"+dnr+"ga-l button");
var inpga = document.querySelector("#ppi-inp-"+dnr+"ga-l input[name='ppi-inp-"+dnr+"ga']");
if (!ppi_rgap || dnr < 3 || dnr > 4 || (plan != 'N' && plan != 'A' && plan != 'B')) {
d.vgap = "u";
d.vgapi = d.vgap202x;
for (var i=0; i d.bis) year = d.bis;
}
var st1990 = ref([dnr, d.col.tot, d.row.yr1990]);
var stbis = ref([dnr, d.col.tot, d.row.yr1990 + year - 1990]);
var colpl = (plan == 'N') ? d.col.planN : (plan == 'A') ? d.col.planA : d.col.myplfw;
var stpl = ref([dnr, colpl, d.row.yr1990 + year - 1990]);
var gsum = 0;
for (var y = 2020; y <= year; y++) {
var g = ref([dnr, d.col.tot, d.row.yr1990 + y - 1990]) - ref([dnr, colpl, d.row.yr1990 + y - 1990]);
gsum += Math.round(g);
}
/* [x,y1,y2,c1,ty,w,yd,c2,txt] */
var gap = (st1990) ? Math.round((stbis-stpl)/st1990*100) : 0;
var txt = "Δ "+gap.toString()+" %\n∑ "+gsum.toString()+" Mt";
var c1 = (stbis <= stpl) ? "green" : "red";
var ya = (stbis <= stpl) ? stpl : stbis;
var ye = (stbis <= stpl) ? stbis : stpl;
d.vgapi = "v1";
d.vgap = [year, ya, ye, c1, "1b", 1, 30, c1, txt];
clog("getRedGapArrow year="+year+" vgapi="+d.vgapi+" vgap="+d.vgap);
}
/* Setzt die Änderungsliste in d.chglist für ppi_itot, ppi_stumm, varri, vgapi, f202x */
function getChangeList(dnr, d) {
d.chglist = [ d.itotswit, (ppi_itot) ? 1 : 0,
d.varri, d.varr, d.vgapi, d.vgap,
d.yr202x, d.f202x,
d.axisynr, (ppi_stumm) ? "s" : 1, d.axisanr, (ppi_stumm) ? "s" : 1,
d.explmod, (ppi_stumm) ? 0 : 1];
}
/* Berechne die CO2-Äq. für 2030 aus dem Plan pl für Spalte col. */
function calcRed2030(dnr, pl, col, row) {
var tdnr = (dnr == 2) ? 3 : dnr;
if (tdnr < 3 || tdnr > 4) return;
var v2019 = ref([tdnr, col, row.st2019]);
var red = 0;
if (typeof(pl) == "number") {
red = pl;
}
else if (pl == 'A') {
var br2019 = ref([tdnr, col, row.br2019]);
if (typeof(br2019) == "string") br2019 = br2019.slice(0, -2);
red = -br2019*0.01;
}
else if (pl == 'N') {
var br2021 = ref([tdnr, col, row.br2021]);
if (br2021 && typeof(br2021) == "string") br2021 = br2021.slice(0, -2);
if (br2021) red = -br2021*0.01;
else red = 0.0;
}
var v2030 = v2019 * (1.0 - red);
return parseFloat(v2030.toFixed(1));
}
/* Reduktion berechnen und eintragen */
function planChangeRed(dnr, d, redpl, tot) {
var sum = 0.0;
var isl = (ppi_drai) ? 0 : 2;
for (var i=0; i 0) {
chglist = chglist.concat(d.yr2030, [mod]);
}
dispChange(dnr, chglist);
var tot = (dnr == 4) ? ref([dnr, d.col.tot, d.row.brpln]) * 1000 : undefined;
planChangeRed(dnr, d, 'N', tot);
}
/* Plan einstellen (None = "weiter so", 'A', 'N' oder 'B') */
function planCheckboxTo(dnr, pl) {
var form = document.querySelector("#ppi-plan-" + dnr);
var elems = form.elements;
for (var i=0; i upv) {
val = upv;
}
return Math.round(val);
}
/* Gibt Liste mit Punkten der Linie für Mein Plan zurück */
function calcLinePlanB(ybis, stbis, pl30, low1) {
var m = (pl30 - stbis) / (2030 - ybis);
var pl50 = stbis + m * (2050 - ybis);
if (m >= 0) {
return [[ ybis, stbis, 2030, pl30.toFixed(1), 2050, pl50.toFixed(1) ], null];
}
/* Formel: low1 = m * (x1 - ybis) + stbis */
var x1 = (low1 - stbis) / m + ybis;
var xi = (3*ybis + x1) * 0.25;
if (xi > 2045) xi = 2045;
if (pl50 < low1) {
pl50 = low1.toFixed(1);
return [[ ybis, stbis, x1.toFixed(0), pl50, 2050, pl50 ], xi.toFixed(0)];
}
return [[ ybis, stbis, 2030, pl30.toFixed(1), 2050, pl50.toFixed(1) ], xi.toFixed(0)];
}
/* Gesamt CO2eq für Mein Plan für ybis+1=202x ... 2050 berechnen */
function calcTotal2xTo50And0(t20bis, ybis, stbis, pl30, low1) {
var tot1 = 0.0;
var tot2 = 0.0;
var m = (pl30 - stbis) / (2030 - ybis);
var mgr0 = (m > 0) ? 1 : 0;
var nlow = 0;
var sum30 = 0, tot30 = 0;
for (var yr = ybis+1; yr <= 2100; yr++) {
var sum = stbis + (yr - ybis) * m;
if (yr == 30) {
sum30 = sum;
}
if (yr >= 30) {
tot30 += sum;
}
if (sum > low1 && yr > 2050) {
tot2 += sum;
}
else if (sum > low1) {
tot1 += sum;
}
else if (yr <= 2050) {
nlow++;
tot1 += (nlow <= 10) ? low1 : 0.0;
}
}
clog("calcTotal2xTo50: t20bis="+t20bis+" stbis="+stbis
+" pl30="+pl30.toFixed(2)+" sum30="+sum30.toFixed(2)
+" low1="+low1.toFixed(2)+" nlow="+nlow
+" tot1="+tot1.toFixed(2)+" t30="+tot30.toFixed(2)
+" tot2="+tot2.toFixed(2));
return [tot1, tot2, mgr0];
}
/* Mein Plan B eingestellt */
function planChangeMeinPlan(dnr, row, d) {
if (dnr < 3 || dnr > 4) return;
var mod = [], sum = 0.0, wav = 0, sum2019 = 0.0;
var lowr30 = 0;
setClassInnerText(d.span1, d.txt1.meinpl, "ppi-choose");
var isl = (ppi_drai) ? 0 : 2;
for (var i=0; id.lev[l]; l++) {};
d.span1.className = d.cls1[l];
setClassInnerText(d.span2, "Ergebnis:", "");
setClassInnerText(d.span3, d.txt2[l], d.cls2[l]);
td3 = document.querySelector("#ppi-tredb-sum-3");
if (sum2019 > 0) {
setClassInnerText(td3, (wav / sum2019).toFixed(0) + " %", "ppi-tmy " + d.cls2[l]);
}
for (var i=0; i 0) {
chglist = chglist.concat(d.yr2030, (v) ? [mod] : 0);
}
if (pli.length > 0) {
chglist = chglist.concat(d.plin2035, (v) ? [pli] : 0);
}
dispChange(dnr, chglist);
}
/* Gesamt CO2eq ermitteln */
function planTotal(dnr, data, pl) {
return 0;
}
/* Button Event: Mein Plan Prozentzahl ändern */
function planClick(dnr, pm, row) {
var tdnr = (dnr == 4) ? 3 : 0; // Wert nicht doppelt ändern
var inp = document.querySelector('input[name="ppi-tredb-'+ppi_sek[row]+'-'+tdnr+'"]');
if (inp) {
var val = parseInt(inp.value);
if (!val) {
val = 0;
}
if (val < 0) {
val = -val;
}
val = ((val - 5*pm) / 5).toFixed(0) * 5;
if (val < 0) {
val = 0;
}
var sek = ppi_sek[row];
val = inputValCheck(val.toString(), sek);
inp.value = (0-val) + ((sek == 'sen') ? " [Mt]" : " %");
}
clog("planClick dnr=" + dnr + " pm=" + pm + " row=" + row +
" inp=" + ((inp) ? inp.value : "null"));
planChange(dnr, 'B', row);
}
/* Auf Defaults zurück setzen */
function defaultCheckbox5States(elems, name, vals) {
var def = [0, 1, 1, 0, 0, 0];
setCheckboxStatesByList(elems, name, def);
for (var h=0; h= 0 && tselv <= 100) {
ppi_temco[tselv] = teminp.value;
temvals = ""
for (var h=0; h 0) temvals = temvals + "-";
temvals = temvals + (h).toString() + ":" + co;
}
}
clog("checkbox5Change: dnr="+dnr+" temsel="+tselv+" teminp="+teminp.value+" temvals="+temvals);
}
else if (teminp && temsel && temsel.value == "uni") {
temvals = teminp.value;
ppi_temco = [teminp.value];
clog("checkbox5Change: dnr="+dnr+" temsel="+temsel.value+" teminp="+teminp.value+" temvals="+temvals);
}
else {
temvals = undefined;
ppi_temco = [];
}
if (chks) {
ppi_stumm = getCheckboxState(chks);
}
const d = {
lnrmno : "l1",
lnrldo : "l2",
lnrtemp : "l3",
lnrsol : "l4",
lnrsafe : "l5",
axisynr : "a1",
axisanr : "a2",
fillmod : "f5",
tempmod : "c",
explmod : "e",
}
const fiscol = (fisinp && fisinp.value != "#000000") ? fisinp.value : undefined;
const fillsafe = [1850, 350, 2030, 450, 'lightyellow', fiscol];
var chglist5 = [d.lnrmno, vals[1], d.lnrldo, vals[2],
d.lnrtemp, (!vals[3]) ? 0 : (temvals) ? d.tempmod + temvals : 'u',
d.axisynr, (ppi_stumm) ? 's' : 1,
d.axisanr, (ppi_stumm) ? 0 : vals[3],
d.lnrsol, vals[4], d.lnrsafe, vals[5],
d.fillmod, (vals[5] && fiscol) ? fillsafe : 'u',
d.explmod, (ppi_stumm) ? 0 : 1];
var chglist6 = [];
if (valsi) {
var old_iran = ppi_iran;
ppi_iran = (valsi[1]) ? 1 : (valsi[2]) ? 2 : 0;
if (old_iran == 1 && ppi_iran == 1 && valsi[2]) {
ppi_iran = 2;
valsi[1] = 0;
setCheckboxStatesByList(form.elements, namei, valsi);
}
else if (old_iran == 2 && ppi_iran == 1 && valsi[2]) {
valsi[2] = 0;
setCheckboxStatesByList(form.elements, namei, valsi);
}
/* [xmin, xmax, xstep, daxis, dgrid] */
var range = (ppi_iran) ? (ppi_iran == 2) ? [ 1930, 2030, 1, 10, 5 ] : [ 1000, 2050, 1, 100, 50 ] : "u";
var rang2 = (ppi_iran) ? (ppi_iran == 2) ? [ 1930, 2030, 1 ] : [ 1000, 2050, 1 ] : "u";
var rang4 = (ppi_iran) ? (ppi_iran == 2) ? [ 1930, 2030, 1 ] : [ 1700, 2050, 1 ] : "u";
clog("checkbox5Change: dnr="+dnr+" valsi="+valsi+" range="+range+" rang2="+rang2);
chglist5 = chglist5.concat("r0", [range], "r2", [rang2], "r3", [rang2], "r4", [rang4], "r5", [rang2]);
chglist6 = chglist6.concat("r0", [range]);
}
dispChange(5, chglist5);
dispChange(6, chglist6);
}
/* Pfeil Gesamtreduktion (ki='tr') oder Reduktionslücke (ki='ga') ändern */
function redArrowClick(dnr, pm, ki) {
const tdnr = (dnr == 4) ? 3 : 0; // Wert nicht doppelt ändern
const inpki = document.querySelector("#ppi-inp-"+dnr+ki+"-l input[name='ppi-inp-"+dnr+ki+"']");
if (inpki) {
var ival = parseInt(inpki.value);
var ybis = getYearBis(dnr);
ival = (ival) ? ival + pm : ybis;
if (ival < 1990) ival = 1990;
if (ki == 'ga' && ival < 2020) ival = 2020;
if (ki == 'ga' && ival > ybis) ival = ybis;
if (ki == 'tr' && ival > 2050) ival = 2050;
clog("redArrowClick: dnr="+dnr+" ki="+ki+" tred="+ppi_tred+" rgap="+ppi_rgap+" taby="+ppi_taby+" ival="+ival);
inpki.value = ival;
if (ki == 'tr' && ppi_taby) {
var curW = window.innerWidth;
checkTable3Size(dnr, curW);
}
var form2 = document.querySelector("#ppi-plan-" + dnr);
var name2 = "ppi-pchk-" + dnr;
var plan = getRadioState(form2.elements, name2);
planChange(dnr, plan);
}
}
const urlNOAAccgg = "https://gml.noaa.gov/webdata/ccgg/trends/rss.xml";
const nAuNOAAccgg = 3;
/* Aktuelle CO2-Werte lesen */
function initCurrCO2Table(dnr, result) {
if (dnr != 5) return;
clog("initCurrCO2Table result.selau="+((result) ? result.selau : "undef")
+" a.len="+((result && result.a) ? result.a.length : 0));
/* Select-Autor prüfen */
var form1 = document.querySelector("#ppi-currco2-5");
var selauin = document.querySelector("#ppi-currco2-5 input[name='ppi-selau-in']");
var jsonarr = (result) ? result.a : undefined;
var selau = (result) ? result.selau : nAuNOAAccgg;
if (jsonarr == undefined) {
clog("initCurrCO2Table jsonarr undef");
return;
}
/* Initial ist result.selau=3 gesetzt und .value=0; das jsonarr ist [].
* Dann wird der Wert auf .value=3 gesetzt und das Formular abgeschickt.
*
* - Initialzustand: Die ursprüngliche HTML-Seite wird mit 'GET' geholt und hat ein Script mit
* 'currCO2Result.selau=3', das die Autor-Id der Syndication konfiguriert.
* - Ein Code-Snippet liefert das Array 'currCO2Result.a' und 'currCO2Result.inpselau' mit dem
* Eingabewert des Inputelements (name='ppi-selau-in') oder einen 'error'-Eintrag mit Fehlermeldung.
* - Aktuelle Implementierung (Web):
* Nach Formular-Submit mit _POST['ppi-selau-in']= (und _POST['ppi-do']='Q') wird das
* Array 'a' zurück mit den Datensätzen und 'inpselau' mit dem Eingabewert gefüllt.
* Das Formular wird nicht erneut abgeschickt, wenn der erste 'author' im Array 'result.a'
* gleich dem Wert 'result.selau' (bzw. dem Default 3) ist oder 'result.selau' null ist
* oder das Array leer ist (und vorher das 'selauin' Input-Element mit name='ppi-selau-in'
* 'wahr' ist oder der Zahl '0' entspricht).
* - Aktuelle Implementierung (lokal):
* Das Formular wird nicht erneut abgeschickt, wenn...
* - Neue Regel:
* Das Formular wird nicht erneut abgeschickt, wenn 'inpselau' oder 'error' vorhanden ist.
*/
if (selauin || selauin == 0) {
var selaudb = (jsonarr.length) ? jsonarr[0].author : undefined;
var oldau = selauin.getAttribute("value");
selauin.setAttribute("value", selau);
clog("initCurrCO2Table Attribut 'value' von "+selauin.name+" von "+oldau+" auf "+selau+" gesetzt"
+" (selaudb "+selaudb+")");
if (selaudb != selau && selau && !result.error) {
clog("initCurrCO2Table doing form1.submit");
form1.submit();
return;
}
updateCurrCO2Table(dnr, jsonarr);
}
else {
clog("querySelector für ppi-selau-in ohne Ergebnis");
return;
}
}
function updateCurrCO2Table(dnr, jsonarr) {
var str = (jsonarr.length && jsonarr[0].cont) ? jsonarr[0].cont : undefined;
clog("updateCurrCO2: "+jsonarr.length+" cont: "+((str) ? str.length : "undef"));
if (!str) return;
var p1 = str.indexOf("Week starting on ");
var p2 = str.indexOf(":", p1);
var p3 = str.indexOf("ppm", p2);
clog("updateCurrCO2: p1="+p1+" p2="+p2+" p3="+p3);
if (p1 <= 0 || p2 <= p1) {
clog("updateCurrCO2: str=<"+str+">");
return;
}
var date1 = Date.parse(str.substring(p1 + 17, p2));
var dtfOpts1 = { 'year': undefined, 'month': 'numeric', 'day': 'numeric', 'timeStyle': undefined };
var strDate1 = new Intl.DateTimeFormat('de-DE', dtfOpts1).format(date1).replaceAll(".", ". ");
var date2 = date1 + 6*1000*60*60*24;
var dtfOpts2 = { 'year': 'numeric', 'month': 'numeric', 'day': 'numeric', 'timeStyle': undefined };
var strDate2 = new Intl.DateTimeFormat('de-DE', dtfOpts2).format(date2).replaceAll(".", ". ");
if (p2 <= 0 || p3 <= p2) return;
var currppm = str.substring(p2 + 1, p3 + 3).trim();
var p4 = str.indexOf("value from 1 year ago:", p3);
var p5 = str.indexOf("ppm", p4);
var p6 = str.indexOf("value from 10 years ago:", p5);
var p7 = str.indexOf("ppm", p6);
clog("updateCurrCO2: p4="+p4+" p5="+p5+" p6="+p6+" p7="+p7);
if (p4 <= 0 || p5 <= p4) return;
var oneyrppm = str.substring(p4 + 22, p5 + 3).trim();
if (p6 <= 0 || p7 <= p6) return;
var tenyrppm = str.substring(p6 + 24, p7 + 3).trim();
clog("updateCurrCO2: found current: "+currppm+", 1 year ago: "+oneyrppm+", 10 years ago: "+tenyrppm+" for "+strDate1);
var currco2 = document.querySelector("#ppi-currco2 table");
if (!currco2) return;
var sp1 = document.querySelector("#ppi-currco2-tr1 span");
if (sp1) sp1.innerHTML = strDate1;
var sp2 = document.querySelector("#ppi-currco2-tr1 span~span");
if (sp2) sp2.innerHTML = strDate2;
var td1 = document.querySelector("#ppi-currco2-tr1 .ppi-currco2-c");
if (td1) td1.innerHTML = currppm;
var td2 = document.querySelector("#ppi-currco2-tr2 .ppi-currco2-c");
if (td2) td2.innerHTML = oneyrppm;
var td3 = document.querySelector("#ppi-currco2-tr3 .ppi-currco2-c");
if (td3) td3.innerHTML = tenyrppm;
}
/* Ende */