# $B7nJ?6Q$7$?J*M}NL$N0^EY7PEYJ,I[$rIA2h$9$k%9%/%j%W%H(B
# dcpam $B$H(B NCEP $B$r(B 2 $BKgJB$Y$FIA2h$9$kMQ(B
#
# * 2011/12/04 $B%U%!%$%kL>(B, $B%?%$%H%k$rJQ99(B
# * 2011/12/01 OLRA $B$K$bBP1~(B
# * 2011/11/29 viewport $B$J$I$N:Y$+$$@_Dj$r=$@5(B
# * 2011/11/28 drawxy_clim_mon.rb $B$r$b$H$K:n@.(B

require "numru/ggraph"
include NumRu

######################################

def clim_mean( iyrs, iyre, imon, gphys, data )

  if data == "dcpam" then

     dataNumOfDay = 4
     daysOfMonth  = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

     daysofyear = 0
     daysOfMonth.each do |i|
       daysofyear += i
     end

     time = gphys.coord('time').val

     for iyr in iyrs..iyre
       t = daysofyear*(iyr-1)
       for i in 1..imon-1
         t = t + daysOfMonth[i-1]
       end

       ts = t
       te = t + daysOfMonth[imon-1]

       ts = ts * dataNumOfDay
       te = te * dataNumOfDay - 1

       if iyr == iyrs
         gphystmpD = gphys.cut('time'=>time[ts]..time[te]).mean('time')
       else
         gphystmpD = gphystmpD + gphys.cut('time'=>time[ts]..time[te]).mean('time')
       end
         gphystmpD = gphystmpD / ( iyre - iyrs + 1 )
     end
 
     return gphystmpD

  elsif data == "NCEP" then

    hoursOfDay = 24
    daysofyear = 0

    time = gphys.coord('time').val

    for iyr in iyrs..iyre
 
     t  = (iyr - 1948) * 12
     t  = t + (imon - 1)
     ts = t
 
#      p iyrs, imon, ts

      if iyr == iyrs
        gphystmpN = gphys.cut('time'=>time[ts])
      else
        gphystmpN = gphystmpN + gphys.cut('time'=>time[ts])
      end
    end

     gphystmpN = gphystmpN / ( iyre - iyrs + 1 )

     return gphystmpN

  end

end

######################################

iyrsD = ARGV[4].to_i
iyreD = ARGV[5].to_i
iyrsN = ARGV[6].to_i
iyreN = ARGV[7].to_i

imon = ARGV[8].to_i

title = ARGV[9]

dirD    = ARGV[0]
dirN  = ARGV[1] 


# $BJ*M}JQ?t$N@_Dj$H(B gphys $B$N=`Hw(B

## dcpam $B7W;;7k2LMQ(B

vnameD = ARGV[2]
gphysD = GPhys::NetCDF_IO.open(dirD+'/'+vnameD+".nc", vnameD) 

tmD = gphysD.shape[2] # number of elements for time      dimension
timeD = gphysD.coord('time').val

## NCEP $BMQ(B

if vnameD == "OLRA" || vnameD == "OSRA"  # $BJ|<M%U%i%C%/%9$N>l9g(B

   vnameN1 = ARGV[3]
   vnameN1_longname = vnameN1 + "top"

   vnameN2 = 'uswrf'
   vnameN2_longname = vnameN2 + "top"

   gphysN1 = GPhys::NetCDF_IO.open(dirN+'/'+vnameN1_longname + ".mon.mean"+".nc", vnameN1)
   gphysN2 = GPhys::NetCDF_IO.open(dirN+'/'+vnameN2_longname + ".mon.mean"+".nc", vnameN2)

   if vnameD ==  "OSRA"

      gphysD = - gphysD
      gphysN = gphysN1 - gphysN2
   else
      gphysD = gphysD
      gphysN = gphysN1
   end

else                                     # $BJ|<M%U%i%C%/%90J30$N>l9g(B

   vnameN = ARGV[3]
   gphysN = GPhys::NetCDF_IO.open(dirN+'/'+vnameN+ ".mon.mean"+".nc", vnameN) 

end

tmN = gphysN.shape[0] # number of elements for time      dimension
timeN = gphysN.coord('time').val



#< DCL$B$N%*!<%W%s$H@_Dj(B >
DCL.gropn(2)
DCL.sgpset('lcntl', false)   # $B@)8fJ8;z$r2r<a$7$J$$(B
DCL.sgpset('lfull',true)     # $BA42hLLI=<((B
DCL.sgpset('lcorner', false)   # $B%3!<%J!<%^!<%/$r=q$+$J$$(B
DCL.uzfact(0.5)             # $B:BI8<4$NJ8;zNs%5%$%:$r(B 0.5 $BG\(B
DCL.sgpset('lfprop',true)    # $B%W%m%]!<%7%g%J%k%U%)%s%H$r;H$&(B
DCL.udpset('lmsg',false)     # $B%3%s%?!<4V3VHsI=<((B

#< GGraph $B$K$h$k(B $BIA2h(B >
vpt = NArray[0.2, 0.7, 0.01, 0.21]
vpt1 = (vpt + ([0.050]*2 + [0.32]*2) ).to_a
vpt2 = (vpt + ([0.050]*2 + [0.10]*2) ).to_a

GGraph.set_axes('xlabelint'=>90) # x $B<4$KCM$rBG$D4V3V(B
GGraph.set_axes('ylabelint'=>30) # y $B<4$KCM$rBG$D4V3V(B

GGraph.set_fig 'itr'=>10, 'window'=>[0,360,-90,90]
GGraph.set_map 'coast_world'=>true, 'grid'=>false

data = "dcpam"
gphystmpD = clim_mean( iyrsD, iyreD, imon, gphysD, data )

data = "NCEP"
gphystmpN = clim_mean( iyrsN, iyreN, imon, gphysN, data )


if vnameD == 'SurfTemp'

  GGraph.tone( gphystmp, true, 
             'lev'=>[210,220,230,240,250,260,270,280,290,300],
             # $B%l%Y%k!u%Q%?!<%s$rM[$K;XDj(B
             'pat'=>[20999,30999,40999,45999,50999,55999,60999,70999,80999,85999,90999], 
             # $B%Q%?%s$NJ}$,(B1$B$DB?"*!^!g$^$G(B
             'map_axes'=>true )
  #GGraph.contour( gphystmp, false, 'lev'=>[200,210,220,230,240,250,260,270,280,290,300], 'index'=>3 )      # $B;29M$^$G(B

elsif vnameD == 'Rain'

  # 1 $BKgL\$NIA2h$N@_Dj(B
  GGraph.set_fig('viewport'=>vpt1)
  GGraph.set_axes('xunits'=>'', 'yunits'=>'', 'xtitle'=>'')
  DCL.uzpset('labelxb',false)

  # $BIA2h(B
  gphystmpD = gphystmpD / 2.5e6
  GGraph.tone( gphystmpD, true, 
             'lev'=>[0,1e-5,2e-5,4e-5,8e-5,12e-5,16e-5,20e-5,24e-5,28e-5,32e-5],
             # $B%l%Y%k!u%Q%?!<%s$rM[$K;XDj(B
             'pat'=>[1,20999,30999,35999,40999,50999,60999,65999,70999,75999,80999,85999], 
             # $B%Q%?%s$NJ}$,(B1$B$DB?"*!^!g$^$G(B
             'map_axes'=>true,
             'annot'=>false, 'titl'=>'' )

  # $B?^$N:8>e$N%?%$%H%k(B
  DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','dcpam5',-1) ; DCL.uzpset('pad1',0.7)


  # 2 $BKgL\$NIA2h$N@_Dj(B
  GGraph.set_fig('viewport'=>vpt2,'new_frame'=>false)
  GGraph.set_axes('xunits'=>nil, 'yunits'=>'', 'xtitle'=>'longitude','ytitle'=>'latitude')
  DCL.uzpset('labelxb',true)

  # $BIA2h(B
  GGraph.tone( gphystmpN, true, 
             'lev'=>[0,1e-5,2e-5,4e-5,8e-5,12e-5,16e-5,20e-5,24e-5,28e-5,32e-5],
             # $B%l%Y%k!u%Q%?!<%s$rM[$K;XDj(B
             'pat'=>[1,20999,30999,35999,40999,50999,60999,65999,70999,75999,80999,85999], 
             # $B%Q%?%s$NJ}$,(B1$B$DB?"*!^!g$^$G(B
             'map_axes'=>true,
             'annot'=>false, 'titl'=>'' )

  # $B?^$N:8>e$N%?%$%H%k(B
  DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','NCEP',-1) ; DCL.uzpset('pad1',0.7)

  #GGraph.contour( gphystmp, false, 'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], 'index'=>3 )      # $B;29M$^$G(B


  # $B%+%i!<%P!<$N@_Dj(B
  GGraph.color_bar( 'voff'=>0.05, 'vcent'=>0.3,'units'=>'(kg/m/s)' )  

  # $B?^$N%?%$%H%k$N@_Dj(B
  DCL::sgtxzv(0.5,vpt1[3]+0.06, 'Monthly mean precipitation '+'('+title+')',
              1.15*DCL.uzpget('rsizec2'),0,0,3)



elsif vnameD == 'SoilMoist'

  GGraph.tone( gphystmp, true, 
             'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150],
             # $B%l%Y%k!u%Q%?!<%s$rM[$K;XDj(B
             'pat'=>[15999,20999,25999,30999,35999,40999,45999,50999,55999,60999,65999,70999,75999,80999,85999,90999,95999],
             # $B%Q%?%s$NJ}$,(B1$B$DB?"*!^!g$^$G(B
             'map_axes'=>true )
  #GGraph.contour( gphystmp, false, 'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], 'index'=>3 )      # $B;29M$^$G(B

elsif vnameD == 'SurfSnow'

  GGraph.tone( gphystmp, true, 
             'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150],
             # $B%l%Y%k!u%Q%?!<%s$rM[$K;XDj(B
             'pat'=>[15999,20999,25999,30999,35999,40999,45999,50999,55999,60999,65999,70999,75999,80999,85999,90999,95999], 
             # $B%Q%?%s$NJ}$,(B1$B$DB?"*!^!g$^$G(B
             'map_axes'=>true )
  #GGraph.contour( gphystmp, false, 'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], 'index'=>3 )      # $B;29M$^$G(B

elsif vnameD == 'OLRA'

  # 1 $BKgL\$NIA2h$N@_Dj(B
  GGraph.set_fig('viewport'=>vpt1)
  GGraph.set_axes('xunits'=>'', 'yunits'=>'', 'xtitle'=>'')
  DCL.uzpset('labelxb',false)

  GGraph.tone( gphystmpD, true, 
             'lev'=>[100,120,140,160,180,200,220,240,260,280,300],
             # $B%l%Y%k!u%Q%?!<%s$rM[$K;XDj(B
             'pat'=>[10999,15999,20999,25999,30999,35999,40999,50999,60999,70999,80999,90999],
             # $B%Q%?%s$NJ}$,(B1$B$DB?"*!^!g$^$G(B
             'map_axes'=>true,
             'annot'=>false, 'titl'=>'')
 
  # $B?^$N:8>e$N%?%$%H%k(B
  DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','dcpam5',-1) ; DCL.uzpset('pad1',0.7)


  # 2 $BKgL\$NIA2h$N@_Dj(B
  GGraph.set_fig('viewport'=>vpt2,'new_frame'=>false)
  GGraph.set_axes('xunits'=>nil, 'yunits'=>'', 'xtitle'=>'longitude','ytitle'=>'latitude')
  DCL.uzpset('labelxb',true)

  # $BIA2h(B
  GGraph.tone( gphystmpN, true, 
             'lev'=>[100,120,140,160,180,200,220,240,260,280,300],
             # $B%l%Y%k!u%Q%?!<%s$rM[$K;XDj(B
             'pat'=>[10999,15999,20999,25999,30999,35999,40999,50999,60999,70999,80999,90999],
             # $B%Q%?%s$NJ}$,(B1$B$DB?"*!^!g$^$G(B
             'map_axes'=>true,
             'annot'=>false, 'titl'=>'')

  # $B?^$N:8>e$N%?%$%H%k(B
  DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','NCEP',-1) ; DCL.uzpset('pad1',0.7)

  #GGraph.contour( gphystmp, false, 'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], 'index'=>3 )      # $B;29M$^$G(B

  # $B%+%i!<%P!<$N@_Dj(B
  GGraph.color_bar( 'voff'=>0.05, 'vcent'=>0.3,'units'=>'(W/m^2)' )  

  # $B?^$N%?%$%H%k$N@_Dj(B
  DCL::sgtxzv(0.5,vpt1[3]+0.06, 'Monthly OLR  '+'('+title+')',
              1.15*DCL.uzpget('rsizec2'),0,0,3)

end

DCL.grcls

