#!/usr/local/bin/ruby
#
#
#  ./weather.rb OAK.wdat [C|F]

require "ClibPDF"

CITYNAME = "OAKLAND"

tempUnit = ["degrees F", "degrees C"]

def plot_temp(pdf, useC)
  vtmax= -999.0; vtmin = 9999.0
  recmin = 50.0; recmax = 100.0

  for i in $temp
	vtmax = i if i > vtmax
	vtmin = i if i < vtmin
  end
  recmin, recmax = ClibPDF.suggestMinMaxForLinearDomain(vtmin, vtmax)

  axStart = $xtime[0]
  axEnd = $xtime[$xtime.size]

  recStart, recEnd = ClibPDF.suggestTimeDomainParams(axStart, axEnd)
  tempDomain = ClibPDF::PlotDomain.createTimePlotDomain(1.7 * ClibPDF::INCH, 5.5 * ClibPDF::INCH, 6.0 * ClibPDF::INCH, 2.5 * ClibPDF::INCH,
														   recStart, recEnd, recmin, recmax, ClibPDF::TIME, ClibPDF::LINEAR, 0)
  oldDomain = pdf.setPlotDomain(tempDomain)
  if useC
	tempDomain.setLinearMeshParams(ClibPDF::Y_MESH, recmin, 5.0, recmin, 1.0)
  else
	tempDomain.setLinearMeshParams(ClibPDF::Y_MESH, recmin, 10.0, recmin, 5.0)
  end
  tempDomain.fillDomainWithRGBcolor(1.0, 0.9, 0.9)
  tempDomain.drawMeshForDomain

  pdf.setgray(0.0)
  tAxis = ClibPDF::PlotDomain.createTimeAxis(0.0, 6.0 * ClibPDF::INCH, ClibPDF::TIME, recStart, recEnd)
  ClibPDF.attachAxisToDomain(tAxis, tempDomain, 0.0, -0.2 * ClibPDF::INCH)
  ClibPDF.setTimeAxisNumberFormat(tAxis, ClibPDF::MONTH_NAME, ClibPDF::YEAR_2DIGIT, "Helvetica", 12.0)
  ClibPDF.setAxisLabel(tAxis, "Date", "Times-Roman", "MacRomanEncoding", 16.0)
  ClibPDF.drawAxis(tAxis)

  yAxis = ClibPDF.createAxis(90.0, 2.5 * ClibPDF.INCH, ClibPDF::LINEAR, recmin, recmax)
  ClibPDF.attachAxisToDomain(yAxis, tempDomain, -0.2 * ClibPDF::INCH, 0.0)
  ClibPDF.setAxisNumberFormat(yAxis, "%.f", "Helvetica", 12.0)
  if useC
	ClibPDF.setAxisLabel(yAxis, "Temperature [degrees C]", "Times-Roman", "MacRomanEncoding", 16.0)
  else
	ClibPDF.setAxisLabel(yAxis, "Temperature [degrees F]", "Times-Roman", "MacRomanEncoding", 16.0)
  end

  if useC
	ClibPDF.setLinearAxisParams(yAxis, recmin, 5.0, recmin, 1.0)
  else	
	ClibPDF.setLinearAxisParams(yAxis, recmin, 10.0, recmin, 5.0)
  end
  ClibPDF.drawAxis(yAxis)
  ClibPDF.freeAxis(yAxis)

  pdf.comments("\n%% plot the temperature curve.\n")
  pdf.setlinewidth(1.5)
  pdf.setrgbcolorStroke(1.0, 0.0, 0.0)

  for i in $xtime
	xt = tm_to_NumDays(recStart, i);
	if i == 0
	  pdf.moveto(xt, i)
	else
	  pdf.lineto(xt, i)
	end
  end

  pdf.setlinejoin(1)
  pdf.stroke
  pdf.setlinejoin(0)

  pdf.setPlotDomain(oldDomain)
  tempDomain.freePlotDomain
end

def plot_rHumidity(pdf)
end

def read_datafile(gafile, useC)
  ndata = 0

  fp = open(gafile, "r")
  while linebuf = fp.gets
	if linebuf =~ /^#/ || linebuf =~ /^%/ || linebuf.length < 3
	  next
	end
	if linebuf =~ /^(19|20)/
	  if /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/
		year = $1; month = $2; day = $3; hour = $4; minute = $5; second = $6
		$xtime.push(Time.mktime(year, month, day, hour, minute, second))
	  end
	else
	  if /^(\S+).+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+/
		sky = $2
		vt = $3.to_i
		vdew = $4.to_i
		vrh = $5.to_i
		if useC == 1
		  $temp.push(0.5555555 * (vt - 32.0))
		else
		  $temp.push(vt)
		end
		$humidity.push(vrh)
	  end
	end
  end
  fp.close
  STDERR.printf "%d records read.\n", ndata
end

def do_TextStuff(useC)
end

$temp = []
$humidity = []
$xtime = []

useCentigrade = 0

if ARGV.size < 2
  printf "Usage: %s datafile [C|F]\n", ARGV[0]
  printf "        Arg2 for temp unit: C=centigrade, F=Fahrenheit\n"
  exit 1
end

STDERR.print "ARGV.size: ", ARGV.size, "\n"
STDERR.print "ARGV[0]  : ", ARGV[0], "\n"

if ARGV.size == 2
  if ARGV[2] == 'C' || ARGV[2] == 'c'
	useCentigrade = 1
  end
end

gfile = ARGV[0]
read_datafile(gfile, useCentigrade)

pdf = ClibPDF::PDF.open(0)
pdf.enableCompression(1)
pdf.init
pdf.pageInit(1, ClibPDF::PORTRAIT, ClibPDF::LETTER, ClibPDF::LETTER)

pdf.setCreator("weather.rb")
pdf.setTitle("Weather of OAK California")
pdf.setSubject("Weather of Oakland International Airport, California")
pdf.setKeywords("Weather")

plot_temp(pdf, useCentigrade)
plot_rHumidity(pdf)
do_TextStuff(useCentigrade)

pdf.finalizeAll
pdf.savePDFmemoryStreamToFile("weather.pdf")

#pdf.launchPreview
pdf.close
