# $Id: importNOS_JsonReaders.rb 6708 2018-01-06 23:59:51Z flaterco $
# Functions for importNOS.rb:
# JSON file readers

def readTidesJson
  t_array = JSON.parse(File.read("tides.json"))["stations"]
  t = Hash.new
  t_array.each {|s|
    id = s["id"]
    raise "Unhandled: duplicate id in tides.json" if t.include?(id)
    t[id] = s
  }
  t
end

# This isn't the same because the ids aren't unique.  (id + bin)
# Just return the array.
def readCurJson
  # Ignore that line saying "units": "feet"
  JSON.parse(File.read("currents.json"))["stations"]
end

# A particular geogroup appears multiple times in geogroups-tides.json with
# the same geoGroupId but different parents.  WTF.
# id:  int; name: char; level: int; parents: set of int
Geogroup = Struct.new(:id, :name, :level, :parents)
def readGeogroupsTidesJson
  gt_array = JSON.parse(File.read("geogroups-tides.json"))["geoGroupList"]
  gt = Hash.new
  gt_array.each {|g|
    id = g["geoGroupId"].to_i
    parent = g["parentGeoGroupId"].to_i
    name = g["geoGroupName"]
    level = g["level"].to_i
    if gt.include?(id)
      if gt[id].name != name || gt[id].level != level
	raise "Unhandled: geogroup clash"
      end
      gt[id][:parents].add(parent)
    else
      gt[id] = Geogroup.new(id, name, level, Set.new([parent]))
    end
  }
  gt
end

def readStationsByGroupTidesJson
  sbgt = File.read("stations-by-group-tides.json")

  # stations-by-group-tides.json is the concatenation of hundreds of separate
  # geogroups queries.  We don't need to keep the queries separated, but we
  # do need to make it parse.
  sbgt.gsub!("    }\n  ]\n}{\n  \"stationList\": [", "    },")

  sbgt_array = JSON.parse(sbgt)["stationList"]
  sbgt = Hash.new
  sbgt_array.each{|sta|
    sid = sta["stationId"]
    # The geogroups themselves are in there with null sids.
    unless sid.nil?
      if sbgt.include?(sid)
	exist = sbgt[sid]

	# Special case fixup, 2017
	# Station 8730667, Alabama Point, appears in both Florida and Alabama.
	# 30.2786° N,  87.5550° W
	# Old name:  Alabama Point, Perdido Pass, Alabama
	# 1762: Perdido Bay, Gulf Coast-cont., Florida
	# 1392: Alabama
	# It is, in fact, just barely in Alabama.  The old name is accurate.
	if sid == "8730667"
	  sbgt[sid] = sta if sta["parentGeoGroupId"] == "1392"
	  next
	end

	# Verify that *only* the level field may disagree in duplicates.
	sta.each {|k,v|
	  unless k == "level" or exist[k] == v
	    print sid, " ", k, " ", v, " != ", exist[k], "\n"
	    raise "Unhandled: dup conflict in sbgt"
	  end
	}
      else
	sbgt[sid] = sta
      end
    end
  }
  sbgt
end

def readStationDatums(sid)
  fname = "tiderefs/" + sid + "/datums.json"
  if File.exist?(fname)
    datums = JSON.parse(File.read(fname))
    raise "Surprise units in datum file" if datums["units"] != "feet"
    datums
  else
    nil
  end
end

def readStationConstants(sid)
  fname = "tiderefs/" + sid + "/harcon.json"
  if File.exist?(fname)
    harcon = JSON.parse(File.read(fname))
    raise "Surprise units in harcon file" if harcon["units"] != "feet"
    harcon["HarmonicConstituents"]   # Array of records
  else
    print "No harmonic constants for requested station ", sid, "\n"
    raise "File not found"
  end
end
