Module:Sandbox/Anon423/ClimateCommons

Module:Sandbox/Anon423/ClimateCommons

local p = {}

function p.getMonthHighestTemp(month, files)
  local max

  month = string.format("%02d", month)

  for file,data in pairs(files)
  do
    field_index = p.getColumnIndex("extremeMax", data)
    for index,values in pairs(data.data)
    do
      if string.find(string.sub(values[1], 1, 3), month, 1, true)
      then
        if max == nil or values[field_index] > max
        then
          max = values[field_index]
        end
      end
    end
  end

  return max
end
function p.getMonthLowestTemp(month, files)
  local min

  month = string.format("%02d", month)

  for file,data in pairs(files)
  do
    field_index = p.getColumnIndex("extremeMin", data)
    for index,values in pairs(data.data)
    do
      if string.find(string.sub(values[1], 1, 3), month, 1, true)
      then
        if min == nil or values[field_index] < min
        then
          min = values[field_index]
        end
      end
    end
  end

  return min
end

function p.getMonthHighestRainfall(month, files)
  local max

  month = string.format("%02d", month)

  for file,data in pairs(files)
  do
    field_index = p.getColumnIndex("extremeRainfall", data)
    for index,values in pairs(data.data)
    do
      if string.find(string.sub(values[1], 1, 3), month, 1, true)
      then
        if max == nil or values[field_index] > max
        then
          max = values[field_index]
        end
      end
    end
  end

  return max
end

function p.getMonthHighestSnowfall(month, files)
  local max

  month = string.format("%02d", month)

  for file,data in pairs(files)
  do
    field_index = p.getColumnIndex("extremeSnowfall", data)
    for index,values in pairs(data.data)
    do
      if string.find(string.sub(values[1], 1, 3), month, 1, true)
      then
        if max == nil or values[field_index] > max
        then
          max = values[field_index]
        end
      end
    end
  end

  return max
end

function p.getColumnIndex(name, file)
  for index,value in pairs(file["schema"]["fields"])
  do
    if value["name"] == name
    then
      return index
    end
  end

  return nil
end

function p.main(frame)
  debug = ""
  local args = frame:getParent().args
  local template_args = {}
  local donnees = args["données"]
  local files = {}
  local months = {"jan", "fev", "mar", "avr", "mai", "jui", "jul", "aou", "sep", "oct", "nov", "dec"}

  -- Duplicate in args
  for key,value in pairs(args) do
    template_args[key] = args[key]
  end

  -- Open data files
  for file in string.gmatch(donnees, "[^%s]+")
  do
    files[file] = mw.ext.data.get(file)
  end

  -- Temperature extreme high
  for num,month in pairs(months)
  do
    if template_args["tmax-record-" .. month] == nil or template_args["tmax-record-" .. month] == ""
    then
      template_args["tmax-record-" .. month] = p.getMonthHighestTemp(num, files)
      template_args["tmax-record-date-" .. month] = nil
    elseif template_args["tmax-record-" .. month] == "-"
    then
      template_args["tmax-record-" .. month] = nil
    end
  end

  -- Temperature extreme low
  for num,month in pairs(months)
  do
    if template_args["tmin-record-" .. month] == nil or template_args["tmin-record-" .. month] == ""
    then
      template_args["tmin-record-" .. month] = p.getMonthLowestTemp(num, files)
      template_args["tmin-record-date-" .. month] = nil
    elseif template_args["tmin-record-" .. month] == "-"
    then
      template_args["tmin-record-" .. month] = nil
    end
  end

  -- Rainfall max
  for num,month in pairs(months)
  do
    if template_args["pluie-record-" .. month] == nil or template_args["pluie-record-" .. month] == ""
    then
      template_args["pluie-record-" .. month] = p.getMonthHighestRainfall(num, files)
      template_args["pluie-record-date-" .. month] = nil
    elseif template_args["pluie-record-" .. month] == "-"
    then
      template_args["pluie-record-" .. month] = nil
    end
  end

  -- Snowfall max
  for num,month in pairs(months)
  do
    if template_args["neige-record-" .. month] == nil or template_args["neige-record-" .. month] == ""
    then
      template_args["neige-record-" .. month] = p.getMonthHighestSnowfall(num, files)
      template_args["neige-record-date-" .. month] = nil
    elseif template_args["neige-record-" .. month] == "-"
    then
      template_args["neige-record-" .. month] = nil
    end
  end

  -- Force debug, if any
  if debug ~= ""
  then
    return debug
  else
    return frame:expandTemplate{title = 'Weather box', args = template_args}
  end
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.