Spring til indhold

Modul:Vejrboks

Fra Wikipedia, den frie encyklopædi
w = {};
math_mod = require( "Modul:Math" );
formatnum = require( "Modul:Formatnum" );
wbc = require( "Modul:WeatherBoxColors" );  

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;
    local first_value_number, color_values;
    local color_scheme = frame.args.color_scheme or 't';
    local scale_factor = math_mod._cleanNumber( frame.args.scale_factor) or 2;
    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();

    if mode == 'basic' then
        first_value_string, first_value_number = getInputs( pframe, group_name, 
            nil, include_space );
    elseif mode == 'temperature' then
        first_value_string, first_value_number = getInputs( pframe, group_name, 
            {'C'}, include_space );
    elseif mode == "precipitation" then
        first_value_string, first_value_number, variant = getInputs( pframe, group_name, 
            {'cm', 'mm'}, include_space );
    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

    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 and first_value_number[i] ~= -9999 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
    
    for i = 1,13 do
        if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then
			x=first_value_string[i];
			local decimal = x:find('%.')
			local p = 0;
			if decimal ~= nil then
				p = string.len(x) - decimal
			end
			if p == 0 then
				first_value_number[i] = math.floor( math_mod._round( first_value_number[i], 0 ) );
 			else
 				if first_value_number[i] >= 0 then
 					first_value_number[i] = first_value_number[i] + 0.001
 				else
 					first_value_number[i] = first_value_number[i] - 0.001
 				end
			end
    		first_value_string[i] = formatnum.formatNum(first_value_number[i],'da',p);
        end
    end              

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

function makeLine( label, first_value_string, color_values, color_scheme )
    local result, color_str, value_str;
    
    result = {'|- \n! height="16" style="text-align:left" | ', 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 );
        else
            table.insert( result, '—' );
        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( 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( 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 '', -9999;
            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


return w;