:name: streamline
:description: streamline
:ndims: 2
:nvars: 2
:vizshot_method: streamline
:attributes: []
:script: |
  newfrm = opt.delete(:newfrm)
  if @set[:fig] && (itr=@set[:fig]['itr']) && itr>=10
    lon = gphys.coord(0)
    lat = gphys.coord(1)
    if lon.min >= 120 && lon.max <= 150 && lat.min >= 20 && lat.max <=50
      GGraph::set_map({'coast_japan'=>true})
    else
      GGraph::set_map({'coast_world'=>true})
    end
    if itr==31
      DCL.sgpset('lclip',true) 
      GGraph.set_map('vpt_boundary'=>3)
    end
  end
  GGraph::streamline(gphys, gphys1, newfrm, opt)

:ggraph: |
  gropn_1_if_not_yet
  unless defined?(@@streamline_options)
    @@streamline_options = Misc::KeywordOptAutoHelp.new(
      ['title', nil, 'Title of the figure'],
      ['annotate', true, 'if false, do not put texts on the right margin even when newframe==true'],
      ['index', 1, 'line index'],
      ['type', 1, 'line type']
    )
  end
  opts = @@streamline_options.interpret(options)
  gphys = gphys.first2D.copy
  gphys1 = gphys1.first2D.copy
  len = gphys.length
  unless len == gphys1.length
    raise ArgumentError, "length of gphys and gphys1 do not agree with each other"
  end
  u = gphys.data
  v = gphys1.data
  x = gphys.axis(0).pos
  y = gphys.axis(1).pos
  xx = NArray.sfloat(*(u.shape)).fill!(0).add!(x.val.newdim(-1))
  yy = NArray.sfloat(*(u.shape)).fill!(0).add!(y.val.newdim(0))

  dxmax = 0.1

  dlx = (x.val[1..-1] - x.val[0..-2])*0.5
  raise "x axis is not uniform" if (dlx.max - dlx.min)/(dlx.mean.abs) > dxmax
  puts "warning: x axis is not uniform" if dlx.max != dlx.min
  dlx = dlx.mean

  dly = (y.val[1..-1] - y.val[0..-2])*0.5
  raise "y axis is not uniform" if (dly.max - dly.min)/(dly.mean.abs) > dxmax
  puts "warning: y axis is not uniform" if dly.max != dly.min
  dly = dly.mean

  dl = (dlx.abs + dly.abs) * 0.5
  raise "dx != dy" if (dlx.abs - dly.abs).abs / dl > dxmax
  puts "warning: dx != dy" if dlx.abs != dly.abs

  if dlx < 0
    u = u[-1..0,true]
    v = v[-1..0,true]
    xx = xx[-1..0,true]
    yy = yy[-1..0,true]
  end
  if dly < 0
    u = u[true,-1..0]
    v = v[true,-1..0]
    xx = xx[true,-1..0]
    yy = yy[true,-1..0]
  end

  idensity = 40
  type = opts['type']
  index = opts['index']

  if newframe
    fig(x,y)
    if map_trn?
      map
    else
      axes(x, y)
    end
    title( opts['title'] )
    annotate(gphys.lost_axes) if opts['annotate']
  end

  a = 6370000.0
  dlon_dt = (u.val * 180 / Math::PI / a).div!((NMath.cos( y.val / 180 * Math::PI)).newdim(0))
  dlat_dt = v.val * 180 / Math::PI / a 

  require "numru/dclext_streamline"
  #DCLExt::streamline(u.val, v.val, xx, yy, dl, idensity, type, index)
  DCLExt::streamline(dlon_dt, dlat_dt, xx, yy, dl, idensity, type, index)

:setting_html: |
  <%
  @order_type = "streamline"
  @order_names = %w(X Y)
  @num_for_each = 2
  @group_name = "diagram"
  @order = @analysis.draw_variables_order
  %>
  <div>
    X-Y component
    <%= render(:partial => "variables_order") %>
  </div>
