#!/usr/bin/ruby

include Math
require "narray"
require "numru/dcl"
include NumRu


def draw_preparation( iws = 1, figfn = 'dcl' )
  if iws == 2 then
    DCL.swcset( 'FNAME', figfn )
    DCL.sgiset( 'IFONT', 2 )
#    DCL.uziset( 'INDEXT0', 10 )
    DCL.uziset( 'INDEXT1', 19 )
    DCL.uziset( 'INDEXT2', 19 )
  end
  # draw a figure
  #   "display number" : 1 for monitor
  #                    : 2 for pdf file
  DCL.gropn( iws )                                   # open a device
end


def draw_finish
  DCL.grcls
end


def draw_temp( title1, title2, title3, sTempIce, y_Lat, y_Temp, y_Temp2 = -1 )

  # --- simple version from here ---
  #DCL.grfrm                                          # set a frame
  #DCL.usgrph( y_Lat, y_Temp )
  # --- simple version until here ---

  # --- detailed version from here ---
  DCL.grfrm                             # set a frame
  DCL.ussttl( 'Latitude', 'degrees', 'Temperature', 'K' )  # name and unit of axes
  DCL.grswnd(0.0, 90.0, 150.0, 350.0) # set range of axes
  DCL.grsvpt(0.2,0.9,0.2,0.8)           # set viewport
  itr = 1
  DCL.grstrn(itr)                       # select kind of axis, 
                                        #   1 : lin.-lin. plot
                                        #   2 : lin.-log. plot
                                        #   3 : log.-lin. plot
                                        #   4 : log.-log. plot
  DCL.grstrf                            # determine axes, 
  DCL.usdaxs                            # draw axes
  DCL.uuslnt(1)                         # set line type
  DCL.uuslni(19)                         # set line index
  DCL.uulin( y_Lat, y_Temp )            # draw a line
  # --- detailed version until here ---

  if y_Temp2.is_a? NArray then
    DCL.uuslnt(2)                         # set line type
    DCL.uuslni(19)                         # set line index
    DCL.uulin( y_Lat, y_Temp2 )          # draw a 2nd line
  end

  # draw an ice line
  y_LatIce  = NArray.sfloat(2)
  y_TempIce = NArray.sfloat(2)
  y_LatIce[0] =  0.0
  y_LatIce[1] = 90.0
  y_TempIce[0] = sTempIce
  y_TempIce[1] = sTempIce
  DCL.uuslnt(1 )                        # set line type
  DCL.uuslni(49)                        # set line index
  DCL.uulin( y_LatIce, y_TempIce )      # draw a 2nd line

  DCL.sgstxs(0.025)
  DCL.sgtxv(0.5,0.95, title1)
  DCL.sgtxv(0.5,0.90, title2)
  DCL.sgtxv(0.5,0.85, title3)
end

def draw_ice_lat( title1, title2, xmin, xmax, x_X1, y_Y1, x_X2, y_Y2, x_X3, y_Y3, x_X4, y_Y4 )

  # --- detailed version from here ---
  DCL.grfrm                             # set a frame
  DCL.ussttl( 'Solar constant', 'W m-2', 'Ice line latitude', 'degrees' )  # name and unit of axes
  DCL.grswnd(xmin, xmax, 0.0, 90.0) # set range of axes
  DCL.grsvpt(0.2,0.9,0.2,0.8)           # set viewport
  itr = 1
  DCL.grstrn(itr)                       # select kind of axis, 
                                        #   1 : lin.-lin. plot
                                        #   2 : lin.-log. plot
                                        #   3 : log.-lin. plot
                                        #   4 : log.-log. plot
  DCL.grstrf                            # determine axes, 
  DCL.usdaxs                            # draw axes
#  DCL.uuslnt(1)                         # set line type
#  DCL.uuslni(1)                         # set line index

  DCL.sgspms(0.05)
  DCL.sgspmi(19)
  DCL.sgpmu( x_X1, y_Y1 )            # draw a line
  # --- detailed version until here ---

  if y_Y2.is_a? NArray then
    DCL.sgspmi(19)
    DCL.sgpmu( x_X2, y_Y2 )            # draw a line
  end

  if y_Y3.is_a? NArray then
    DCL.sgspmi(49)
    DCL.sgpmu( x_X3, y_Y3 )            # draw a line
  end

  if y_Y4.is_a? NArray then
    DCL.sgspmi(29)
    DCL.sgpmu( x_X4, y_Y4 )            # draw a line
  end

  DCL.sgstxs(0.025)
  DCL.sgtxv(0.5,0.95, title1)
  DCL.sgtxv(0.5,0.90, title2)

end

def draw_gmtemp( xmin, xmax, ymin, ymax, x_X1, y_Y1, x_X2, y_Y2, x_X3, y_Y3, x_X4, y_Y4 )

  # --- detailed version from here ---
  DCL.grfrm                             # set a frame
  DCL.ussttl( 'Solar constant', 'W m-2', 'Global mean temperature', 'K' )  # name and unit of axes
  DCL.grswnd(xmin, xmax, ymin, ymax) # set range of axes
  DCL.grsvpt(0.2,0.9,0.2,0.8)           # set viewport
  itr = 1
  DCL.grstrn(itr)                       # select kind of axis, 
                                        #   1 : lin.-lin. plot
                                        #   2 : lin.-log. plot
                                        #   3 : log.-lin. plot
                                        #   4 : log.-log. plot
  DCL.grstrf                            # determine axes, 
  DCL.usdaxs                            # draw axes
#  DCL.uuslnt(1)                         # set line type
#  DCL.uuslni(1)                         # set line index
  DCL.sgspmt(9)
  DCL.sgspmi(10)
  DCL.sgspms(0.01)

  DCL.sgpmu( x_X1, y_Y1 )            # draw a line
  # --- detailed version until here ---

  if y_Y2.is_a? NArray then
    DCL.sgspmi(10)
    DCL.sgpmu( x_X2, y_Y2 )            # draw a line
  end

  if y_Y3.is_a? NArray then
    DCL.sgspmi(40)
    DCL.sgpmu( x_X3, y_Y3 )            # draw a line
  end

  if y_Y4.is_a? NArray then
    DCL.sgspmi(20)
    DCL.sgpmu( x_X4, y_Y4 )            # draw a line
  end

end

def draw_tend( title1, title2, title3, ymin, ymax, x_X, y_Y1, y_Y2 = 0, y_Y3 = 0, y_Y4 = 0 )

  # --- detailed version from here ---
  DCL.grfrm                             # set a frame
  DCL.ussttl( 'Latitude', 'degrees', 'Tendency', 'K s-1' )  # name and unit of axes
  DCL.grswnd(0.0, 90.0, ymin, ymax) # set range of axes
  DCL.grsvpt(0.2,0.9,0.2,0.8)           # set viewport
  itr = 1
  DCL.grstrn(itr)                       # select kind of axis, 
                                        #   1 : lin.-lin. plot
                                        #   2 : lin.-log. plot
                                        #   3 : log.-lin. plot
                                        #   4 : log.-log. plot
  DCL.grstrf                            # determine axes, 
  DCL.usdaxs                            # draw axes
  DCL.uuslnt(1)                         # set line type
  #  DCL.uuslni(1)                         # set line index
  DCL.uuslnt(1)                     # set line type

  DCL.uuslni(29)                    # set line index
  DCL.uulin( x_X, y_Y1 )            # draw a line
  # --- detailed version until here ---

  if y_Y2.is_a? NArray then
    DCL.uuslnt(1)                     # set line type
    DCL.uuslni(49)                    # set line index
    DCL.uulin( x_X, y_Y2 )            # draw a line
  end

  if y_Y3.is_a? NArray then
    DCL.uuslnt(1)                     # set line type
    DCL.uuslni(39)                    # set line index
    DCL.uulin( x_X, y_Y3 )            # draw a line
  end

  if y_Y4.is_a? NArray then
    DCL.uuslnt(2)                     # set line type
    DCL.uuslni(19)                    # set line index
    DCL.uulin( x_X, y_Y4 )            # draw a line
  end

  DCL.sgstxs(0.025)
  DCL.sgtxv(0.5,0.95, title1)
  DCL.sgtxv(0.5,0.90, title2)
  DCL.sgtxv(0.5,0.85, title3)

end
