Vai al contenuto

Modulo:ClimaAnnuale

Da Wikipedia, l'enciclopedia libera.

Modulo in Lua per gestire le funzioni di {{ClimaAnnuale}} e {{ClimaAnnualeAustrale}}


local p = {}

--carico le tabelle dei colori
local tempo_config = mw.loadData( 'Modulo:ClimaAnnuale/Configurazione' );

local function dump(t, ...)
    local args = {...}
    for _, s in ipairs(args) do
        table.insert(t, s)
    end
end

-- Nota il lua non ha una funzione di arrotondamento
-- funzione copiata da  http://lua-users.org/wiki/SimpleRound
function round(num, idp)
    local mult = 10^(idp or 0)
    if num >= 0 then return math.floor(num * mult + 0.5) / mult
    else return math.ceil(num * mult - 0.5) / mult end
end

local function TempToColour(temp)
    temp = round(temp)
    if temp < -90 then return tempo_config.temperatura[-90] end
    return tempo_config.temperatura[temp] or 'FFFFFF'
end

local function WarmToColour(temp)
    temp = round(temp)
    if temp < 0 then return tempo_config.warm[0] end
    return tempo_config.warm[temp] or 'FF0060'
end

local function CloudToColour(temp)
    temp = round(temp, 1)
    if temp < 0.0 then return tempo_config.cloud[0.0] end
    return tempo_config.cloud[temp] or 'FFF0F0'
end


local function convert_to_FF(days, base, mult)
    local result = round(base - (days or 0) * mult)
    return math.max(math.min(result, 255), 0)
end

local function IceToColour(days)
    return string.format('%2X%2XE5', convert_to_FF(days, 229, 8), convert_to_FF(days, 230, 1))
end

local function Media( ... )
    local args = { ... }
    local sum = 0
    for _, s in ipairs(args) do
        sum = sum + s
    end
    return round(sum / #args, 1)
end

local function Totale( ... )
    local args = { ... }
    local sum = 0
    for _, s in ipairs(args) do
        sum = sum + s
    end
    return round(sum)
end


-- Configurazione dei dati da leggere:
---- name: nome base della variabile che contiene il valore
---- color: funzione che assegna il colore alla cella
---- aggregate: funzione per il calcolo dei valori aggregati (stagionali e annuali)
---- rowlabel: etichetta della riga del dato
---- aggregatelabel: attributo css "title" delle celle dei dati aggregati
local data_type = {
    { name = 'tempmax', color = TempToColour, aggregate = Media, yearname = nil, label = '[[Temperatura|T. max. media]] (°[[Grado Celsius|C]])', aggregatelabel = 'media' },
    { name = 'tempmedia', color = TempToColour, aggregate = Media, yearname = nil, label = '[[Temperatura|T. media]] (°[[Grado Celsius|C]])', aggregatelabel= 'media' },
    { name = 'tempmin', color = TempToColour, aggregate = Media, yearname = nil, label = '[[Temperatura|T. min. media]] (°[[Grado Celsius|C]])', aggregatelabel= 'media' },
    { name = 'tempassmax', color = TempToColour, aggregate = math.max, yearname = 'annotempassmax', label = '[[Temperatura|T. max. assoluta]] (°[[Grado Celsius|C]])', aggregatelabel= 'massimo' },
    { name = 'tempassmin', color = TempToColour, aggregate = math.min, yearname = 'annotempassmin', label='[[Temperatura|T. min. assoluta]] (°[[Grado Celsius|C]])', aggregatelabel= 'minimo' },
    { name = 'warm', color = WarmToColour, aggregate = Totale, yearname = nil, label = '[[Onda di calore|Giorni di calura]] ([[temperatura|T<sub>max</sub>]] ≥ 30 [[Grado Celsius|°C]])', aggregatelabel = 'totale'},
    { name = 'giornigelo', color =IceToColour, aggregate = Totale, yearname = nil, label = '[[Giorno di gelo|Giorni di gelo]] ([[temperatura|T<sub>min</sub>]] ≤ 0 [[Grado Celsius|°C]])', aggregatelabel = 'totale'},
    { name = 'giornighiaccio', color=IceToColour, aggregate = Totale, yearname=nil, label ='[[Giorno di ghiaccio|Giorni di ghiaccio]] ([[temperatura|T<sub>max</sub>]] ≤ 0 [[Grado Celsius|°C]])', aggregatelabel='totale'},
    { name = 'nubi', color=CloudToColour, aggregate=Media, yearname=nil, label='[[Nuvola#Nuvolosità|Nuvolosità]] ([[okta]] al [[giorno]])', aggregatelabel='media' },

}

local index_month =  {"01", "02", "03", "04", "05", "06", "07", "08", "09",  "10", "11", "12"}

local aggregate_index = { 12, 1, 2, 3, 4, 5, 6, 7, 8, 9,  10, 11}

local seasons = {
    boreale = { "[[Inverno|Inv]]", "[[Primavera|Pri]]", "[[Estate|Est]]", " [[Autunno|Aut]] " },
    australe = { "[[Estate|Est]]", "[[Autunno|Aut]]", "[[Inverno|Inv]]", "[[Primavera|Pri]]"  }
}


function p.ClimaAnnuale(frame)
    -- Se chiamata mediante  #invoke, usa gli argomenti passati al template invocante.
    -- Altrimenti a scopo di test assume che gli argomenti siano passati direttamente

    local emisphere = frame['emisfero']
    local season = seasons[emisphere] or seasons['boreale']
    local origArgs = frame:getParent().args
    local language = mw.language.getContentLanguage()

    output = {}
    dump(output, '<table class="wikitable" cellpadding="3" cellspacing="0" style="clear: center; float: center; margin: auto; margin-top:.5em; margin-bottom:.5em; border:1px solid #CCC; border-collapse:collapse; text-align: center;" >',
                 '<tr><th rowspan="2" style="border-right-width: 2px;">', origArgs['nome'] or 'Mese', '</th>',
                 '<th colspan="12"> [[Mese|Mesi]] </th>',
                 '<th colspan="4" style="border-left-width: 2px;" > [[Stagione|Stagioni]] </th>',
                 '<th rowspan="2" style="border-left-width: 2px; "> [[Anno]] </th>',
                 '</tr>',
                 '<tr>',
                 '<th> [[Gennaio|Gen]] </th>',
                 '<th> [[Febbraio|Feb]] </th>',
                 '<th> [[Marzo|Mar]] </th>',
                 '<th> [[Aprile|Apr]] </th>',
                 '<th> [[Maggio|Mag]] </th>',
                 '<th> [[Giugno|Giu]] </th>',
                 '<th> [[Luglio|Lug]] </th>',
                 '<th> [[Agosto|Ago]] </th>',
                 '<th> [[Settembre|Set]] </th>',
                 '<th> [[Ottobre|Ott]] </th>',
                 '<th> [[Novembre|Nov]] </th>',
                 '<th> [[Dicembre|Dic]] </th>')
    dump(output, '<th style="border-left-width: 2px;" >', season[1],  '</th>')
    for j = 2, 4 do
        dump(output, '<th>', season[j], '</th>')
    end
    dump(output, '</tr>')
    for _,row in ipairs(data_type) do
        local first_data = origArgs[row.name .. '01']
        if first_data and first_data ~= '' then
            local values = {}
            dump(output, '<tr><th style="border-right-width: 2px">', row.label, '</th>')
            for i, index_string in ipairs(index_month) do
                values[i] = tonumber(origArgs[row.name .. index_string]) or 0
                dump(output, '<td style="background:#', row.color(values[i]), ';">', language:formatNum( values[i]) )
                if row.yearname and origArgs[row.yearname .. index_string] then
                    dump(output, '<br /><small>([[', origArgs[row.yearname .. index_string], ']])</small>')
                end
                dump(output,'</td>')
            end
            for j = 1, 10, 3 do
                local aggregate_season = row.aggregate(values[aggregate_index[j]], values[aggregate_index[j+1]], values[aggregate_index[j+2]])
                dump(output, '<td title="', row.aggregatelabel, '" style="border-left-width: 2px;  background:#',
                    row.color(aggregate_season), ';">', language:formatNum(aggregate_season), '</td>' )
            end
            local aggregate_year = row.aggregate(unpack(values))
            dump(output, '<td title="', row.aggregatelabel, '" style="border-left-width: 2px; background:#',
                row.color(aggregate_year), ';">\'\'\'', language:formatNum(aggregate_year), "'''</td>")
            dump(output, '</tr>')
        end
    end
    dump(output, '</table>')
    return table.concat(output)
end

return p