Documentation for this module may be created at Module:Sandbox/Denelson83/doc

local function grid_extents ( frame )
    local series = tonumber(frame.args[1])
    local area = (frame.args[2])
    local sheet = tonumber(frame.args[3])
    
    local belt
    local belt_area_south = {["M"]=12, ["N"]=12, ["O"]=12, ["P"]=12,
    	                     ["L"]=8,  ["K"]=8,  ["J"]=8,  ["I"]=8,
    	                     ["E"]=4,  ["F"]=4,  ["G"]=4,  ["H"]=4,
    	                     ["D"]=0,  ["C"]=0,  ["B"]=0,  ["A"]=0
    }
    local belt_area_north = {["G"]=12, ["H"]=12,
    	                     ["F"]=8,  ["E"]=8,
    	                     ["C"]=4,  ["D"]=4,
    	                     ["B"]=0,  ["A"]=0
    }
    local belt_sheet = {[13]=3, [14]=3, [15]=3, [16]=3,
    	                [12]=2, [11]=2, [10]=2, [9] =2,
    	                [5] =1, [6] =1, [7] =1, [8] =1,
    	                [4] =0, [3] =0, [2] =0, [1] =0
    }

    if series >= 120 then    
    	belt = 160 + series % 10 * 16 + belt_area_north[area] + belt_sheet[sheet]
    elseif series < 120 and series % 10 * 16 >= 112 then
    	belt = series % 10 * 16 + belt_area_north[area] + belt_sheet[sheet]
    else
    	belt = series % 10 * 16 + belt_area_south[area] + belt_sheet[sheet]
    end

    local strip
    
    local strip_series_high_arctic = {[12] = 16,
    	                              [34] = 48,
    	                              [56] = 80,
    	                              [78] = 112,
    	                              [91] = 144
    	                             }

    local strip_area_southern = {["M"]=12, ["N"]=8, ["O"]=4, ["P"]=0,
    	                         ["L"]=12, ["K"]=8, ["J"]=4, ["I"]=0,
    	                         ["E"]=12, ["F"]=8, ["G"]=4, ["H"]=0,
       	                         ["D"]=12, ["C"]=8, ["B"]=4, ["A"]=0
    }
    local strip_area_arctic = {["G"]=8, ["H"]=0,
    	                       ["F"]=8, ["E"]=0,
    	                       ["C"]=8, ["D"]=0,
                               ["B"]=8, ["A"]=0
    }
    local strip_area_high_arctic = {["G"]=16, ["H"]=0,
    	                            ["F"]=16, ["E"]=0,
    	                            ["C"]=16, ["D"]=0,
                                    ["B"]=16, ["A"]=0
    }

    local strip_sheet_southern = {[13]=3, [14]=2, [15]=1, [16]=0,
    	                          [12]=3, [11]=2, [10]=1, [ 9]=0,
    	                          [ 5]=3, [ 6]=2, [ 7]=1, [ 8]=0,
       	                          [ 4]=3, [ 3]=2, [ 2]=1, [ 1]=0
    }
    local strip_sheet_arctic = {[13]=6, [14]=4, [15]=2, [16]=0,
    	                        [12]=6, [11]=4, [10]=2, [ 9]=0,
    	                        [ 5]=6, [ 6]=4, [ 7]=2, [ 8]=0,
       	                        [ 4]=6, [ 3]=4, [ 2]=2, [ 1]=0
    }
    local strip_sheet_high_arctic = {[13]=12, [14]=8, [15]=4, [16]=0,
    	                             [12]=12, [11]=8, [10]=4, [ 9]=0,
    	                             [ 5]=12, [ 6]=8, [ 7]=4, [ 8]=0,
       	                             [ 4]=12, [ 3]=8, [ 2]=4, [ 1]=0
    }

    local east_limit, west_limit;           -- For 1:50,000 scale map sheet
    local area_east_limit, area_west_limit; -- For 1:250,000 scale map area

    if series >= 120 then    
		strip = strip_series_high_arctic[math.floor(series / 10)]
		      + strip_area_high_arctic[area]
		      + strip_sheet_high_arctic[sheet];

    	east_limit = strip * 0.5 + 48
    	west_limit = (strip + 4) * 0.5 + 48
    	area_east_limit = math.floor(strip / 16) * 8 + 48
    	area_west_limit = math.floor((strip + 16) / 16) * 8 + 48

    elseif series < 120 and math.floor(series % 10) >= 7 then
		strip = math.floor(series / 10) * 16
		      + strip_area_arctic[area]
		      + strip_sheet_arctic[sheet];

    	east_limit = strip * 0.5 + 48
    	west_limit = (strip + 2) * 0.5 + 48
    	area_east_limit = math.floor(strip / 8) * 4 + 48
    	area_west_limit = math.floor((strip + 8) / 8) * 4 + 48

    else
		strip = math.floor(series / 10) * 16
		      + strip_area_southern[area]
		      + strip_sheet_southern[sheet];

    	east_limit = strip * 0.5 + 48
    	west_limit = (strip + 1) * 0.5 + 48
    	area_east_limit = math.floor(strip / 4) * 2 + 48
    	area_west_limit = math.floor((strip + 4) / 4) * 2 + 48

    end
    

    local grid_limits = {
    	["south"] = belt * 0.25 + 40,
    	["north"] = belt * 0.25 + 40.25,
    	["area_south"] = math.floor(belt / 4) + 40,
    	["area_north"] = math.floor(belt / 4) + 41,
    	["east"] = east_limit,
    	["west"] = west_limit,
    	["area_east"] = area_east_limit,
    	["area_west"] = area_west_limit
    }
    
    return "1:50,000 limits: " .. grid_limits["south"], "°N, " .. grid_limits["north"] .. "°N, " .. grid_limits["east"] .. "°W, " .. grid_limits["west"] .. "°W, " .. " - 1:250,000 limits: " .. grid_limits["area_south"] .. "°N, " .. grid_limits["area_north"] .. "°N, " .. grid_limits["area_east"], "°W, " .. grid_limits["area_west"], "°W"
end

return {
    grid_extents = grid_extents
}