#!/usr/bin/ruby

require './ebm_fig'
require './ebm'
require './ebm_param'


if ARGV.size == 0 then
  iws = 1
  FigFN = "dcl"

  print "Select heat transfer type: \n"
  print "  1: Budyko-type\n"
  print "  2: Sellers-type\n"
  print "> "
  sIDHeatTransport = gets.to_i
  print "Select a number 1 or 2:\n"
  print "  1: Specify solar constant and find ice line latitude\n"
  print "  2: Specify ice line latitude and find solar constant\n"
  print "> "
  modenum = gets.to_i

  sIDHeatTransport = 1
  sSolarConst      = 1370.0
  sTempInit        = 400.0
  sAlbIceFree      = AlbedoIceFree
  sAlbIce          = AlbedoIce
  sTransferCoefC   = TransferCoefC
  sDiffCoefD       = DiffCoefD
  sLatIce          = -1
  plotnum          = 1
else
  iws = 2
  FigFN            = ARGV[0]

  modenum          = ARGV[1].to_i

  sIDHeatTransport = ARGV[2].to_i
  sSolarConst      = ARGV[3].to_f
  sTempInit        = ARGV[4].to_f
  sAlbIceFree      = ARGV[5].to_f
  sAlbIce          = ARGV[6].to_f
  sTransferCoefC   = ARGV[7].to_f
  sDiffCoefD       = ARGV[8].to_f
  sLatIce          = ARGV[9].to_f
  plotnum          = ARGV[10].to_i
end


sFlagDraw = true
#sFlagDraw = false

jmax = 128
ebm = EBM.new( jmax, sTransferCoefC, sDiffCoefD, sAlbIce, sAlbIceFree )


case modenum
when 1 then
  if iws == 1 then
    print "Input solar constant (W m-2): "
    sSolarRadTOA = gets.to_f / 4.0
    print "Input initial temperature (K): "
    sTempInit = gets.to_f
  end
  sSolarRadTOA = sSolarConst / 4.0
  ebm.ebm( sIDHeatTransport, sSolarRadTOA, sTempInit )
  y_Temp1 = ebm.get_temp
  y_Temp2 = -1
  y_DTDtSW1, y_DTDtLW1, y_DTDtTr1 = ebm.get_tend
when 2 then
  if iws == 1 then
    print "Input ice line latitude (degrees): "
    sLatIce = gets.to_f
  end
  if ( ( sLatIce < 0.0 ) || ( sLatIce > 90.0 ) ) then
    print "Latitude has to be greater than 0 and less than 90.\n"
    exit
  end
  y_Temp1 = NArray.sfloat(jmax)
  y_Temp2 = NArray.sfloat(jmax)
  sSolarRadTOA = ebm.ebm_fixlat( sIDHeatTransport, sLatIce, 1 )
  y_Temp1[true] = ebm.get_temp[true]
  y_DTDtSW1, y_DTDtLW1, y_DTDtTr1 = ebm.get_tend
  sSolarRadTOA = ebm.ebm_fixlat( sIDHeatTransport, sLatIce, 2 )
  y_Temp2[true] = ebm.get_temp[true]
  y_DTDtSW2, y_DTDtLW2, y_DTDtTr2 = ebm.get_tend
else
#    print "Unexpected mode\n"
#    exit
end

print "SolarRad: ", sSolarRadTOA, ", Ice line latitude : ", ebm.ice_line, "\n"

if sFlagDraw then
  draw_preparation( iws, FigFN )
  case sIDHeatTransport
  when 1 then
    title1 = sprintf( "Budyko-type, C = %#.2f (W m-2 K-1)", sTransferCoefC )
  when 2 then
    title1 = sprintf( "Sellers-type, D = %#.2f (W m-2 K-1)", sDiffCoefD )
  end
  title2 = sprintf( "As = %#.2f, Ai = %#.2f", sAlbIceFree, sAlbIce )
#  case modenum
#  when 1 then
    title3 = sprintf( "Fs/4 = %#.2f (W m-2)", sSolarRadTOA )
#  when 2 then
#    title3 = sprintf( "Specified ice lat. = %#.2f (degree)", ebm.ice_line )
#  end
  case plotnum
  when 1 then
    draw_temp( title1, title2, title3, ebm.get_tempice, ebm.get_lat, y_Temp1, y_Temp2 )
  when 2 then
    draw_tend( title1, title2, title3, -400.0, 400.0, ebm.get_lat, y_DTDtSW1, y_DTDtLW1, y_DTDtTr1, y_DTDtSW1+y_DTDtLW1+y_DTDtTr1 )
  end
  draw_finish
end
