Vai al contenuto

Modulo:ClimaAnnuale

Da Wikipedia, l'enciclopedia libera.
Versione del 19 dic 2013 alle 09:16 di Moroboshi (discussione | contributi) (fix mesi e colori)

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 presa 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
    if temp > 60 then return tempo_config.temperatura[60] end
    return tempo_config.temperatura[temp]
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


-- 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, labeld = '[[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' },

}

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

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_series) 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[j], values[j+1], values[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