Jump to content

Module:Weather box/row

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dragons flight (talk | contribs) at 18:19, 9 March 2013. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
w = {};
math_mod = require( "Module:Math" );
wbc = require( "Module:WeatherBoxColors" );
  
function w.metric_to_inch( frame )
    local v1, v2, v3, v3_str, v4, v5
    v1 = frame.args[1] or '';
    if v1 ~= '' then
        return v1;
    end
    
    v2 = frame.args[2] or '';
    if v2 ~= '' then
        return v2;
    end

    v3 = frame.args[3] or '';
    if v3 ~= '' then
        v3, v3_str = math_mod._cleanNumber( frame, v3 );
        if v3 ~= nil then
            _, v4 = math_mod._cleanNumber( frame, frame.args[4] );
            if v4 == nil then            
                _, v5 = math_mod._cleanNumber( frame, frame.args['Jan'] );
            else
                v5 = nil;
            end
            
            if v4 == nil and v5 == nil then
                return math_mod._round( v3*2.54, math.max( 0, math_mod._precision( v3_str ) ) );
            else
                local p = nil;
                if v4 ~= nil then
                    p = math_mod._precision( v4 );
                elseif v5 ~= nil then
                    p = math_mod._precision( v5 );
                else
                    p = 0;
                end
                p = math.max( 0, p );
                    
                if p < 1 then
                    return math_mod._round( v3*2.54, 0 );
                else
                    if v4 ~= nil then
                        p = math.min( 1, p );
                    else
                        p = math.max( 0, math.min( math_mod._precision( v3_str ), 1 ) );
                    end                    
                    return math_mod._round( v3*25.4, p );
                end
            end
        end
    end

    return '';
end

function w.inch_to_metric( frame )
    local v1, v2, v3, v4, v5
    v1 = frame.args[1] or '';
    if v1 ~= '' then
        return v1;
    end
    
    v2 = frame.args[2] or '';
    if v2 ~= '' then
        v2 = math_mod._cleanNumber( frame, v2 );
        if v2 ~= nil then
            _, v4 = math_mod._cleanNumber( frame, frame.args[4] );
            if v4 == nil or v5 == '' then 
                v4 = v2;
            end
            
            return math_mod._round( v2/2.54, math.max( 0, math_mod._precision( v4 ) ) + 1 );
        end
    end

    v3 = frame.args[3] or '';
    if v3 ~= '' then
        v3 = math_mod._cleanNumber( frame, v3 );
        if v3 ~= nil then
            _, v5 = math_mod._cleanNumber( frame, frame.args[5] );
            if v5 == nil or v5 == '' then 
                v5 = v3;
            end
            
            return math_mod._round( v3/25.4, math.max( 0, math_mod._precision( v5 ) ) + 2 );
        end
    end

    return '';
end

function w.CtoF( frame )
    local v1, v2, v2_str, v3
    local delta;
    
    v1 = frame.args[1];
    v3 = '';
    if v1 ~= '' and v1 ~= nil then
        v3 = frame.args[3];        
        if v3 == nil or v3 == '' then
            v3 = v1;
        end
        _, v3 = math_mod._cleanNumber( frame, v3 );
    else
        v2, v2_str = math_mod._cleanNumber( frame, frame.args[2] );
        if v2 ~= nil then
            v3 = math_mod._cleanNumber( frame, frame.args[3] ) or v2;
            
            delta = math_mod._cleanNumber( frame, frame.args.delta ) or 0;
            v3 = math_mod._round( v3*1.8 + 32, math.max( 0, math_mod._precision( v2_str ) + delta ) );
        end
    end

    if v3 ~= nil then
        v3 = tostring( v3 );
        if v3:sub(1,1) == '-' then
            v3 = '−' .. v3:sub(2);
        end
        return v3;
    end
    return '';
end
        
function w.FtoC( frame )
    local v1, v2, v2_str, v3
    local delta
    
    v1 = frame.args[1];
    v3 = '';
    if v1 ~= '' and v1 ~= nil then
        v3 = frame.args[3];   
        if v3 == nil or v3 == '' then
            v3 = v1;
        end
        _, v3 = math_mod._cleanNumber( frame, v3 );
    else
        v2, v2_str = math_mod._cleanNumber( frame, frame.args[2] );
        if v2 ~= nil then
            v3 = math_mod._cleanNumber( frame, frame.args[3] ) or v2;

            delta = math_mod._cleanNumber( frame, frame.args.delta ) or 0;
            v3 = math_mod._round( (v3 - 32)/1.8, math.max( 0, math_mod._precision( v2_str ) + delta ) );
        end
    end

    if v3 ~= nil then
        v3 = tostring( v3 );
        if v3:sub(1,1) == '-' then
            v3 = '−' .. v3:sub(2);
        end
        return v3;
    end
    
    return '';
end        

function w.color_d( frame )
    local val = math_mod._cleanNumber( frame, frame.args[1] );
    return wbc._days_color( val );
end
function w.color_pastel( frame )
    local val = math_mod._cleanNumber( frame, frame.args[1] );
    return wbc._pastel_color( val );
end
function w.color_t( frame )
    local val = math_mod._cleanNumber( frame, frame.args[1] );
    return wbc._temperature_color( val );
end
function w.color_green( frame )
    local val = math_mod._cleanNumber( frame, frame.args[1] );
    return wbc._green_color( val );
end
function w.color_s( frame )
    local val = math_mod._cleanNumber( frame, frame.args[1] );
    return wbc._sunshine_color( val );
end
function w.color_h( frame )
    local val = math_mod._cleanNumber( frame, frame.args[1] );
    return wbc._humidity_color( val );
end
function w.color_p( frame )
    local val = math_mod._cleanNumber( frame, frame.args[1] );
    return wbc._precipitation_color( val );
end

function w.line( frame )
    local color_scheme = frame.args.color_scheme or 't';
    local scale_factor = math_mod._cleanNumber( frame, frame.args.scale_factor) or 1;
    local date_mode = frame.args.dates or false;
    if type( date_mode ) == 'string' then
        date_mode = date_mode:lower();
        if date_mode == '0' or data_mode == 'false' or
                date_mode == '' or date_mode == 'no' then
            date_mode = false;
        else
            date_mode = true;
        end
    end
    
    local pframe = frame:getParent();
    local show = pframe.args.show or '1'
    local alt = pframe.args.alt or '';
    local label = pframe.args.label or '';
    local result;
    
    color_scheme = wbc.interpret_color_code( color_scheme );
    
    result = {'|- \n! height="16" | ', label,  "\n"}
    months = { 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 
        'aug', 'sep', 'oct', 'nov', 'dec', 'yr' };
    month_adj = { 31/30, 28/30, 31/30, 1, 31/30, 1, 
        31/30, 31/30, 1, 31/30, 1, 31/30, 12.175 };
    
    local m1, m_show, m_alt, color_value;
    local m1_str, m_show_str, m_alt_string;
    for i, k in ipairs( months ) do
        m1_str = pframe.args[ k .. '1' ];
        m1 = math_mod._cleanNumber( frame, m1_str );
        if show == '1' then
            m_show = m1;
            m_show_str = m1_str;
        else
            m_show_str = pframe.args[ k .. show ];
            m_show = math_mod._cleanNumber( frame, m_show_str );
        end
        if alt == '1' then
            m_alt_str = m1_str;
            m_alt = m1;
        elseif alt ~= '' then
            m_alt_str = pframe.args[ k .. alt ];
            m_alt = math_mod._cleanNumber( frame, m_alt_str );
        else
            m_alt_str = nil;
            m_alt = nil;
        end        
        
        if m1 ~= nil then
            if date_mode then
                color_value = color_scheme( m1*scale_factor/month_adj[i] );
            else
                color_value = color_scheme( m1*scale_factor );
            end
        else
            color_value = color_scheme( nil );
        end
        
        if k == 'yr' then
            table.insert( result, table.concat( {'|style="', color_value, ' border-left-width:medium" | '} ) );
        else
            table.insert( result, table.concat( {'|style="', color_value, '" | '} ) );
        end
                
                
        if m_show == nil then
            table.insert( result, m_show_str .. "\n" );
        else
            if m_alt == nil then
                m_alt = m_alt_str;
            end
            
            m_show = tostring( m_show );
            if m_show:sub(1,1) == '-' then
                m_show = '−' .. m_show:sub(2);
            end
            
            if m_alt ~= nil then
                m_alt = tostring( m_alt );
                if m_alt:sub(1,1) == '-' then
                    m_alt = '−' .. m_alt:sub(2);
                end
                table.insert( result, table.concat( { m_show, " <br /> (", m_alt, ")\n" } ) ); 
            else
                table.insert( result, m_show .. "\n" ); 
            end    
        end        
    end
    
    return table.concat( result );
end

function checkFlag( flag )
    if flag == nil then
        return nil;
    elseif type( flag ) == 'boolean' then
        return flag;        
    elseif type( flag ) == 'string' then
        flag = flag:lower();
        if flag == '0' or flag == 'false' or
                flag == '' or flag == 'no' or
                flag == 'n' then
            return false;
        else
            return true;
        end
    else
        return error( 'Flag type not valid' );
    end    
end

function w.buildRow( frame )
    local mode = (frame.args.mode or 'basic'):lower();
    local group_name = frame.args.group_name;
    local first_value_string, second_value_string;
    local first_value_number, second_value_number, color_values;
    local color_scheme = frame.args.color_scheme or 't';
    local scale_factor = math_mod._cleanNumber( frame, frame.args.scale_factor) or 1;
    local date_mode = checkFlag( frame.args.date_mode or false );
    local label = frame.args.label or '';
    local annual_mode = (frame.args.annual_mode or 'avg'):lower();
    local include_space = checkFlag( frame.args.include_space or true );
    local result;

    local pframe = frame:getParent();
    local imperial_first = checkFlag( frame.args['imperial first'] );
    if imperial_first == nil then imperial_first = checkFlag( pframe.args['imperial first'] ); end
        
    local metric_first = checkFlag( frame.args['metric first'] );
    if metric_first == nil then metric_first = checkFlag( pframe.args['metric first'] ); end

    local single_line = checkFlag( frame.args['single line'] );
    if single_line == nil then single_line = checkFlag( pframe.args['single line'] ); end

    if imperial_first == nil and metric_first ~= nil then
        imperial_first = not metric_first;
    else
        imperial_first = true;
    end    

    if mode == 'basic' then
        first_value_string, first_value_number = getInputs( pframe, group_name, 
            nil, include_space );
        second_value_string = nil;
        second_value_number = nil;
    elseif mode == 'temperature' then
        first_value_string, first_value_number = getInputs( pframe, group_name, 
            {'C'}, include_space );
        second_value_string, second_value_number = getInputs( pframe, group_name, 
            {'F'}, include_space );
        first_value_string, first_value_number, second_value_string, second_value_number =
            reconcileTemperature( first_value_string, first_value_number, 
                second_value_string, second_value_number )
    elseif mode == "precipitation" then
        first_value_string, first_value_number, variant = getInputs( pframe, group_name, 
            {'cm', 'mm'}, include_space );
        second_value_string, second_value_number = getInputs( pframe, group_name, 
            {'inch'}, include_space );        
        first_value_string, first_value_number, second_value_string, second_value_number =
            reconcilePrecipitation( first_value_string, first_value_number, 
                second_value_string, second_value_number, variant )
    else
        error( 'Requested mode not recognized' );
    end  
    
    local good = false;
    for i = 1,13 do
        if first_value_string[i] ~= nil and first_value_string[i] ~= '' then
            good = true;
            break;
        end
    end        
    if not good then
        return '';
    end

    if first_value_string[13] == nil or first_value_string[13] == '' then
        first_value_string[13], first_value_number[13] = getAnnualValue( first_value_number, annual_mode );
    end
    if second_value_string ~= nil then
        if second_value_string[13] == nil or second_value_string[13] == '' then
            second_value_string[13], second_value_number[13] = getAnnualValue( second_value_number, annual_mode );
        end
        if mode == 'precipitation' then
            for i = 1,12 do
                if variant[i] ~= 0 then
                    variant[13] = variant[i];
                    break;
                end
            end
        end                        
    end   

    color_scheme = wbc.interpret_color_code( color_scheme );

    color_values = {};
    month_adj = { 31/30, 28/30, 31/30, 1, 31/30, 1, 
        31/30, 31/30, 1, 31/30, 1, 31/30, 12.175 };
    local adj;
    for i = 1,13 do        
        if first_value_number[i] ~= nil then
            adj = scale_factor;
            if date_mode then 
                adj = adj / month_adj[i];
            end
            if mode == "precipitation" then
                if variant[i] == 1 then
                    adj = adj * 10;
                end
            end
            table.insert( color_values, color_scheme( first_value_number[i] * adj ) );               
        else
            table.insert( color_values, color_scheme( nil ) );
        end
    end
    
    local lang = mw.getContentLanguage();
    for i = 1,13 do
        if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then
            if math.abs(first_value_number[i]) >= 1000 then
                first_value_string[i] = lang:formatNum( math.abs(first_value_number[i]) );
                if first_value_number[i] < 0 then
                    first_value_string[i] = '−' .. first_value_string[i];
                end
            elseif first_value_number[i] < 0 then
                first_value_string[i] = '−' .. first_value_string[i]:sub(2);
            end
        end
        if second_value_number ~= nil then
            if second_value_number[i] ~= nil and second_value_number[i] ~= -9999 then
                if math.abs(first_value_number[i]) >= 1000 then
                    second_value_string[i] = lang:formatNum( math.abs(second_value_number[i]) );
                    if second_value_number[i] < 0 then
                        second_value_string[i] = '−' .. second_value_string[i];
                    end
                elseif second_value_number[i] < 0 then
                    second_value_string[i] = '−' .. second_value_string[i]:sub(2);
                end
            end
        end
    end              

    if imperial_first and second_value_string ~= nil then
        local t = first_value_string;
        first_value_string = second_value_string;
        second_value_string = t;
    end    

    if not single_line then
        second_value_string = nil;
    end

    return makeLine( label, first_value_string, second_value_string, color_values, color_scheme );
end    

function makeLine( label, first_value_string, second_value_string, color_values, color_scheme )
    local result, color_str, value_str;
    
    result = {'|- \n! height="16" | ', label,  "\n"}
    for i = 1,13 do
        color_str = color_values[i];

        if i == 13 then
            table.insert( result, table.concat( {'|style="', color_str, ' border-left-width:medium" | '} ) );
        else
            table.insert( result, table.concat( {'|style="', color_str, '" | '} ) );
        end                

        value_str = first_value_string[i];
        if value_str ~= '' and value_str ~= nil then 
            table.insert( result, value_str );
        end
    
        if second_value_string ~= nil then
            value_str = second_value_string[i];
            if value_str ~= '' and value_str ~= nil then 
                table.insert( result, "<br /> (" .. value_str .. ")" );
            end    
        end
        table.insert( result, "\n" ); 
    end
    
    return table.concat( result );
end  

function getInputs( frame, group_name, suffix, include_space )
    local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' };
    local str, str2, val;
    
    local output_string = {};
    local output_value = {};
    local variant = {};
    if suffix == nil then
        for _, mon in ipairs( month_names ) do
            if include_space then
                str = ( frame.args[ mon .. ' ' .. group_name ] or '' );
            else
                str = ( frame.args[ mon .. group_name ] or '' );
            end                
            val, str2 = math_mod._cleanNumber( frame, str );
            if val ~= nil then 
                table.insert( output_string, str2 );
                table.insert( output_value, val );
            else
                table.insert( output_string, str );
                table.insert( output_value, -9999 );
            end                
        end
    else
        local updated = false;
        for _, mon in ipairs( month_names ) do
            updated = false;
            for i, suf in ipairs( suffix ) do
                if include_space then
                    str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ];
                else
                    str = frame.args[ mon .. group_name .. ' ' .. suf ];
                end                    
                if str ~= nil and str ~= '' then 
                    val, str2 = math_mod._cleanNumber( frame, str );
                    if val ~= nil then 
                        table.insert( output_string, str2 );
                        table.insert( output_value, val );
                    else
                        table.insert( output_string, str );
                        table.insert( output_value, -9999 );
                    end                
                    table.insert( variant, i );
                    updated = true;
                    break;
                end                
            end
            if not updated then
                table.insert( output_string, '' );
                table.insert( output_value, -9999 );
                table.insert( variant, 0 );
            end            
        end
    end
        
    return output_string, output_value, variant;
end

function getAnnualValue( values, mode )
    local total = 0;
    local val = 0;
    
    if mode == 'avg' or mode == 'sum' then
        local p1, p2;
        p1 = 0;
        for i = 1, 12 do
            val = values[i];
            if val == -9999 then
                return nil;
            end            
            
            p2 = math_mod._precision( val );
            if p2 > p1 then
                p1 = p2;
            end
            
            total = total + val;
        end
        if mode == 'avg' then
            total = math_mod._round( total / 12, p1 + 1 );
        end
        return tostring( total ), total;
    elseif mode == 'min' then
        local min_val = nil;
        for i = 1, 12 do
            val = values[i];
            if val ~= -9999 then
                if min_val == nil or val < min_val then
                    min_val = val;
                end                
            end            
        end
        return tostring( min_val ), min_val;
    elseif mode == 'max' then
        local max_val = nil;
        for i = 1, 12 do
            val = values[i];
            if val ~= -9999 then
                if max_val == nil or val > max_val then
                    max_val = val;
                end                
            end            
        end
        return tostring(max_val), max_val;
    else
        error( 'Unrecognized Annual Mode' );
    end
end        

function reconcileTemperature( C_degree_strings, C_degree_values, F_degree_strings, F_degree_values )
    local p;
    for i = 1,13 do
        if C_degree_strings[i] == '' then
            if F_degree_values[i] ~= -9999 then
                p = math.max( 0, math_mod._precision( F_degree_strings[i] ) );
                C_degree_values[i] = math_mod._round( (F_degree_values[i] - 32)*5/9, p );
                C_degree_strings[i] = tostring( C_degree_values[i] );
            end            
        elseif F_degree_strings[i] == '' then
            if C_degree_values[i] ~= -9999 then
                p = math.max( 0, math_mod._precision( C_degree_strings[i] ) );
                F_degree_values[i] = math_mod._round( C_degree_values[i]*9/5 + 32, p );
                F_degree_strings[i] = tostring( F_degree_values[i] );
            end                        
        end        
    end
    return C_degree_strings, C_degree_values, F_degree_strings, F_degree_values;
end    

function reconcilePrecipitation( M_degree_strings, M_degree_values, 
        I_degree_strings, I_degree_values, variant )
    local p;
    for i = 1,13 do
        if M_degree_strings[i] == '' then
            if I_degree_values[i] ~= -9999 then
                p = math.max( 0, math_mod._precision( I_degree_strings[i] ) );
                M_degree_values[i] = math_mod._round( I_degree_values[i]*2.54, p );
                M_degree_strings[i] = tostring( M_degree_values[i] );
            end            
        elseif I_degree_strings[i] == '' then
            if M_degree_values[i] ~= -9999 then
                if variant[i] == 1 then
                    p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) + 1;
                    I_degree_values[i] = M_degree_values[i]/2.54;
                else
                    p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) + 2;
                    I_degree_values[i] = M_degree_values[i]/25.4;
                end                    
                I_degree_values[i] = math_mod._round( I_degree_values[i], p );
                I_degree_strings[i] = tostring( I_degree_values[i] );
            end                        
        end  
    end
    return M_degree_strings, M_degree_values, I_degree_strings, I_degree_values;
end    

return w;