Module:Weather box/row: Difference between revisions

Content deleted Content added
No edit summary
update from sandbox: tweak to reduce duplication
 
(35 intermediate revisions by 9 users not shown)
Line 1:
local math_mod = require('Module:Math')
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
 
local traceText
v3 = frame.args[3] or '';
local Value
if v3 ~= '' then
Value = {
v3, v3_str = math_mod._cleanNumber( frame, v3 );
lang = mw.getContentLanguage(),
if v3 ~= nil then
getDisplay = function (self, second)
_, v4 = math_mod._cleanNumber( frame, frame.args[4] );
if not self:isValid() then
if v4 == nil then
return nil
_, v5 = math_mod._cleanNumber( frame, frame.args['Jan'] );
end
else
local display = self.string
v5 = nil;
if display == 'trace' then
end
if second then
-- If a cell displays "cm (inch)", show "trace" not "trace (trace)".
if v4 == nil and v5 == nil then
return nil
return math_mod._round( v3*2.54, math.max( 0, math_mod._precision( v3_str ) ) );
end
else
return traceText or 'trace'
local p = nil;
end
if v4 ~= nil then
if math.abs(self.number) >= 1000 then
p = math_mod._precision( v4 );
display = self.lang:formatNum(math.abs(self.number))
elseif v5 ~= nil then
if self.number < 0 then
p = math_mod._precision( v5 );
display = '−' .. display
else
end
p = 0;
elseif self.number < 0 then
end
display = '−' .. display:sub(2)
p = math.max( 0, p );
end
return display
if p < 1 then
end,
return math_mod._round( v3*2.54, 0 );
getPrecision = function (self)
else
local result = rawget(self, 'precision')
if v4 ~= nil then
if not result then
p = math.min( 1, p );
if self:isValid() then
else
p result = math.max( 0, math.min( math_mod._precision( v3_str self.string), 1 ) );
else
end
result = 0
return math_mod._round( v3*25.4, p );
end
end
rawset(self, 'precision', result)
end
end
return result
end
end,
isValid = function (self)
return self.number ~= nil and self.number ~= -9999
end,
new = function (v)
local val, str, precision
if type(v) == 'string' then
if v == 'trace' then
val, str, precision = 0, 'trace', 0
else
val, str = math_mod._cleanNumber(v)
end
elseif type(v) == 'number' then
val, str = v, tostring(v)
end
if not val then
val, str = -9999, ''
end
return setmetatable({
number = val,
string = str,
precision = precision,
}, Value)
end,
converts = {
in2cm = { factor = 2.54 },
in2mm = { factor = 25.4 },
cm2in = { factor = 1/2.54, p2max = 1 },
mm2in = { factor = 1/25.4, p2max = 0 },
},
setConvert = function (self, invalue, units)
-- Use method modified from [[Module:Convert]] to determine precision.
if invalue.string == 'trace' then
self.number, self.string, self.precision = 0, 'trace', 0
return
end
local convert = self.converts[units] or error('Unknown units')
local outnum = invalue.number * convert.factor
local precision = invalue:getPrecision()
if outnum > 0 then
local adjust = math.log10(1/convert.factor) + math.log10(2)
local p1 = math.floor(precision + adjust)
local p2 = 1 - math.floor(math.log10(outnum))
if convert.p2max then
p2 = math.min(p2, convert.p2max)
end
precision = math.max(p1, p2)
end
self:setNumberRounded(outnum, precision)
end,
setNumberRounded = function (self, number, precision)
if precision > 2 then
precision = 2
end
self.number = math_mod._round(number, precision)
if precision < 0 then
self.string = tostring(self.number)
else
local fmt = '%.' .. string.format('%d', precision) .. 'f'
self.string = string.format(fmt, self.number)
end
end,
}
Value.__index = Value
 
local function checkFlag(flag, default)
return '';
if flag == nil then
return default
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
 
local function makeLine(label, first_values, second_values, color_values)
function w.inch_to_metric( frame )
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
local v1, v2, v3, v4, v5
for v1i = frame.args[1], or13 '';do
table.insert(result,
if v1 ~= '' then
'|style="' .. color_values[i] ..
return v1;
(i == 13 and ' border-left-width:medium"' or '"') ..
end
' class="notheme"| '
)
v2 = frame.args[2] or '';
local display = first_values[i]:getDisplay()
if v2 ~= '' then
if display then
v2 = math_mod._cleanNumber( frame, v2 );
table.insert(result, display)
if v2 ~= nil then
if second_values ~= nil then
_, v4 = math_mod._cleanNumber( frame, frame.args[4] );
display = second_values[i]:getDisplay(true)
if v4 == nil or v5 == '' then
if display then
v4 = v2;
table.insert(result, "<br />(" .. display .. ")")
end
end
end
return math_mod._round( v2/2.54, math.max( 0, math_mod._precision( v4 ) ) + 1 );
else
end
table.insert(result, '—')
end
end
table.insert(result, "\n")
end
return table.concat(result)
end
 
local function getInputs(args, group_name, suffix, include_space)
v3 = frame.args[3] or '';
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
if v3 ~= '' then
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
v3 = math_mod._cleanNumber( frame, v3 );
local str
if v3 ~= nil then
local values = {}
_, v5 = math_mod._cleanNumber( frame, frame.args[5] );
if v5suffix == nil or v5 == '' then
for i, mon in ipairs(month_names) do
v5 = v3;
if include_space then
end
str = args[ mon .. ' ' .. group_name ] or ''
else
return math_mod._round( v3/25.4, math.max( 0, math_mod._precision( v5 ) ) + 2 );
str = args[ mon .. group_name ] or ''
end
end
values[i] = Value.new(str)
 
end
return '';
else
for i, mon in ipairs(month_names) do
local value, updated
for var, suf in ipairs(suffix) do
if include_space then
str = args[ mon .. ' ' .. group_name .. ' ' .. suf ]
else
str = args[ mon .. group_name .. ' ' .. suf ]
end
if str ~= nil and str ~= '' then
value = Value.new(str)
value.variant = var
updated = true
break
end
end
if not updated then
value = Value.new()
value.variant = 0
end
values[i] = value
end
end
return values
end
 
local function w.CtoFgetAnnualValue(values, frame mode)
if mode == 'avg' or mode == 'sum' then
local v1, v2, v2_str, v3
local total = 0
local delta;
local p1, p2, variant
p1 = 0
v1 = frame.args[1];
for i = 1, v3 =12 '';do
if not values[i]:isValid() then
if v1 ~= '' and v1 ~= nil then
return Value.new()
v3 = frame.args[3];
end
if v3 == nil or v3 == '' then
if not variant then
v3 = v1;
local var = values[i].variant
end
if var and var ~= 0 then
_, v3 = math_mod._cleanNumber( frame, v3 );
variant = var
else
end
v2, v2_str = math_mod._cleanNumber( frame, frame.args[2] );
end
if v2 ~= nil then
p2 = values[i]:getPrecision()
v3 = math_mod._cleanNumber( frame, frame.args[3] ) or v2;
if p2 > p1 then
p1 = p2
delta = math_mod._cleanNumber( frame, frame.args.delta ) or 0;
end
v3 = math_mod._round( v3*1.8 + 32, math.max( 0, math_mod._precision( v2_str ) + delta ) );
total = total + values[i].number
end
end
local value = Value.new(total)
 
if v3mode ~== nil'avg' then
value:setNumberRounded(total / 12, p1)
v3 = tostring( v3 );
end
if v3:sub(1,1) == '-' then
value.variant = variant
v3 = '−' .. v3:sub(2);
return value
end
elseif mode == 'min' then
return v3;
local target
end
for i = 1, return12 '';do
if values[i]:isValid() then
if target == nil or values[i].number < target.number then
target = values[i]
end
end
end
return target or Value.new()
elseif mode == 'max' then
local target
for i = 1, 12 do
if values[i]:isValid() then
if target == nil or values[i].number > target.number then
target = values[i]
end
end
end
return target or Value.new()
else
error('Unrecognized Annual Mode')
end
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;
 
local function reconcileTemperature(C_values, F_values)
delta = math_mod._cleanNumber( frame, frame.args.delta ) or 0;
for i = 1, 13 do
v3 = math_mod._round( (v3 - 32)/1.8, math.max( 0, math_mod._precision( v2_str ) + delta ) );
local p
end
if C_values[i].string == '' then
end
if F_values[i]:isValid() then
 
p = F_values[i]:getPrecision()
if v3 ~= nil then
C_values[i]:setNumberRounded((F_values[i].number - 32)*5/9, p)
v3 = tostring( v3 );
end
if v3:sub(1,1) == '-' then
elseif F_values[i].string == '' then
v3 = '−' .. v3:sub(2);
if C_values[i]:isValid() then
end
p = C_values[i]:getPrecision()
return v3;
F_values[i]:setNumberRounded(C_values[i].number*9/5 + 32, p)
end
end
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
 
local function reconcilePrecipitation(M_values, I_values, prefer_cm)
function w.line( frame )
local v_class = 0
local color_scheme = frame.args.color_scheme or 't';
for i = 1, 13 do
local scale_factor = math_mod._cleanNumber( frame, frame.args.scale_factor) or 1;
if M_values[i].variant == 1 then
local date_mode = frame.args.dates or false;
v_class = 1
if type( date_mode ) == 'string' then
elseif M_values[i].variant == 2 then
date_mode = date_mode:lower();
v_class = 2
if date_mode == '0' or data_mode == 'false' or
end
date_mode == '' or date_mode == 'no' then
end
date_mode = false;
if v_class == 0 then
else
if prefer_cm then
date_mode = true;
v_class = 1
end
else
end
v_class = 2
end
local pframe = frame:getParent();
end
local show = pframe.args.show or '1'
for i = 1, 13 do
local alt = pframe.args.alt or '';
local units
local label = pframe.args.label or '';
if M_values[i].string == '' then
local result;
if I_values[i]:isValid() then
if v_class == 1 then
color_scheme = wbc.interpret_color_code( color_scheme );
units = 'in2cm'
else
result = {'|- \n! height="16" | ', label, "\n"}
units = 'in2mm'
months = { 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
end
'aug', 'sep', 'oct', 'nov', 'dec', 'yr' };
M_values[i]:setConvert(I_values[i], units)
month_adj = { 31/30, 28/30, 31/30, 1, 31/30, 1,
M_values[i].variant = v_class
31/30, 31/30, 1, 31/30, 1, 31/30, 12.175 };
end
elseif I_values[i].string == '' then
local m1, m_show, m_alt, color_value;
if M_values[i]:isValid() then
local m1_str, m_show_str, m_alt_string;
if M_values[i].variant == 1 then
for i, k in ipairs( months ) do
units = 'cm2in'
m1_str = pframe.args[ k .. '1' ];
else
m1 = math_mod._cleanNumber( frame, m1_str );
units = 'mm2in'
if show == '1' then
end
m_show = m1;
I_values[i]:setConvert(M_values[i], units)
m_show_str = m1_str;
end
else
end
m_show_str = pframe.args[ k .. show ];
end
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
 
local function checkFlag_buildRow(definition, flagargs, options)
options = options or {}
if flag == nil then
local wbc = require('Module:Weather box/colors' .. (options.sandbox or ''))
return nil;
local mode = (definition.mode or 'basic'):lower()
elseif type( flag ) == 'boolean' then
local group_name = definition.group_name
return flag;
local first_values, second_values
elseif type( flag ) == 'string' then
local color_values
flag = flag:lower();
local color_scheme = definition.color_scheme or 't'
if flag == '0' or flag == 'false' or
local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1
flag == '' or flag == 'no' or
local date_mode = checkFlag(definition.date_mode, false)
flag == 'n' then
local label = definition.label or ''
return false;
local annual_mode = (definition.annual_mode or 'avg'):lower()
else
local include_space = checkFlag(definition.include_space, true)
return true;
local second_line = checkFlag(definition.second_line, false)
end
local prefer_cm = checkFlag(definition.prefer_cm, false)
else
local imperial_first = checkFlag(args['imperial first'])
return error( 'Flag type not valid' );
local metric_first = checkFlag(args['metric first'])
end
local wantSingleLine = options.wantSingleLine or checkFlag(args['single line'])
local trace = args.trace
if trace and trace ~= '' then
traceText = trace
end
if imperial_first == nil then
imperial_first = metric_first == nil and true or not metric_first
end
if mode == 'basic' then
first_values = getInputs(args, group_name, nil, include_space)
second_values = nil
elseif mode == 'temperature' then
first_values = getInputs(args, group_name, {'C'}, include_space)
second_values = getInputs(args, group_name, {'F'}, include_space)
reconcileTemperature(first_values, second_values)
elseif mode == "precipitation" then
first_values = getInputs(args, group_name, {'cm', 'mm'}, include_space)
second_values = getInputs(args, group_name, {'inch'}, include_space)
reconcilePrecipitation(first_values, second_values, prefer_cm)
else
error('Requested mode not recognized')
end
local good = false
for i = 1, 13 do
if first_values[i].string ~= '' then
good = true
break
end
end
if not good then
return ''
end
if first_values[13].string == '' then
first_values[13] = getAnnualValue(first_values, annual_mode)
end
if second_values ~= nil then
if second_values[13].string == '' then
second_values[13] = getAnnualValue(second_values, annual_mode)
end
end
color_scheme = wbc.interpret_color_code(color_scheme)
color_values = {}
local month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1,
31/30, 31/30, 1, 31/30, 1, 31/30, 365.25/30 }
for i = 1, 13 do
if first_values[i]:isValid() then
local adj = scale_factor
if date_mode then
adj = adj / month_adj[i]
end
if mode == "precipitation" then
if first_values[i].variant == 1 then
adj = adj * 10
end
end
table.insert(color_values, color_scheme(first_values[i].number * adj))
else
table.insert(color_values, color_scheme(nil))
end
end
if imperial_first and second_values ~= nil then
first_values, second_values = second_values, first_values
end
if not wantSingleLine then
if second_line and second_values ~= nil then
first_values = second_values
end
second_values = nil
end
return makeLine(label, first_values, second_values, color_values)
end
 
local function w.buildRow( frame )
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters
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 imperial_first = checkFlag( frame.args.imperial_first );
local metric_first = checkFlag( frame.args.metric_first );
local result;
 
if imperial_first == nil and metric_first ~= nil then
imperial_first = not metric_first;
else
imperial_first = true;
end
 
local pframe = frame:getParent();
 
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
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
 
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
 
return {
function getAnnualValue( values, mode )
buildRow = buildRow,
local total = 0;
_buildRow = _buildRow,
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;