Class: GrADS::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/grads/lib/axis_xtime.rb,
lib/grads/command.rb,
lib/grads/lib/makecpt.rb,
lib/grads/lib/colorbar.rb,
lib/grads/lib/save_image.rb,
lib/grads/lib/xcbar_with_ccols.rb

Overview

draw_xlab_time(dummy, offset: nil, skip: nil)

Defined Under Namespace

Classes: Area, DataHandler, Expression, LINE, RGB, TILE, Variable

Constant Summary collapse

COLOR_TABLE =
{
  "background" => 0,
  "foregraound" => 1,
  "red" => 2,
  "green" => 3,
  "dark blue" => 4,
  "light blue" => 5,
  "magenta" => 6,
  "yellow" => 7,
  "orange" => 8,
  "purple" => 9,
  "yellow green" => 10,
  "medium blue" => 11,
  "dark yellow" => 12,
  "aqua" => 13,
  "dark purple" => 14,
  "gray" => 15,
}
RGB_TABLE =
{
  "red"   => RGB(255,0,0),
  "green" => RGB(0,255,0),
  "blue"  => RGB(0,0,255),
}
COLORBAR_DEFAULT =
{
  :line=>true,
  :edge=>"triangle",
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*argv, &block) ⇒ Command

Returns a new instance of Command.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/grads/command.rb', line 115

def initialize (*argv, &block)
  if argv.last.is_a?(Hash)
    options = argv.pop
  else
    options = {}
  end
  if argv.first
    grads_command = argv.first
  else
    grads_command = "grads"
  end
  if options[:batch] 
    grads_command = grads_command + " -b"
  end
  if options[:portrait]
    grads_command += " -p "
  elsif options[:landscape]
    grads_command += " -l "
  else
    grads_command += " -l "
  end
  begin
    @io, @stdout, @stderr = Open3.popen3(grads_command)
  rescue NotImplementedError
    raise NotImplementedError, "system dosen't support Open3.popen3"
  end
  @debug = false
  @log = ""
  @imported = []
  @echoback = false
  @listen = Thread.start {
    Thread.abort_on_exception = true
    begin
      while line = @stderr.gets ### read(1024)
        STDERR << line
        STDERR.flush
      end
    rescue IOError ### @stdout may externally closed
    end
  }
  @io.puts "ECHOBACK_TEST_FOR_RUBY_GRADS"
  while @stdout.gets !~ /Unknown command: ECHOBACK_TEST_FOR_RUBY_GRADS/
  end
  @io.puts "ECHOBACK_TEST_FOR_RUBY_GRADS"
  if @stdout.gets =~ /ga-> ECHOBACK_TEST_FOR_RUBY_GRADS/
    @echoback = true
    @stdout.gets
  else
    @echoback = false
  end
  put "set grads off"
  put "set timelab off"
  if block_given?
    begin
      case block.arity
      when 1
        yield(self)
      when -1, 0
        instance_eval(&block)
      else
        raise "invalid # of block parameters"
      end
    ensure
      self.quit
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *argv) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/grads/command.rb', line 185

def method_missing (id, *argv)
  if argv.empty?
    return id
  else
    super
  end
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



183
184
185
# File 'lib/grads/command.rb', line 183

def log
  @log
end

Class Method Details

.RGB(*argv) ⇒ Object



1100
1101
1102
1103
1104
1105
1106
1107
# File 'lib/grads/command.rb', line 1100

def self.RGB (*argv)
  case argv.size
  when 1
    return GrADS::Command::RGB_TABLE[argv[0]]
  else
    return GrADS::Command::RGB.new(*argv)
  end
end

Instance Method Details

#__template__(d, sd, *args, &block) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/grads/command.rb', line 589

def __template__ (d, sd, *args, &block)
  if args.last.is_a?(Hash)
    opts = args.pop
  else
    opts = {}
  end
  args = args.map{|v| CA_FLOAT(v) }
  dataio   = Tempfile.open("CA_GrADS_", ".")
  dataname = dataio.path
  datactl  = dataname + ".ctl"
  if sd
    result   = q :file, sd.file_id
    ctlfile  = subwrd(sublin(result, 2), 2)
    ctl = GrADS::Gridded.new(ctlfile)
  else
    ref = args.first
    ctl = GrADS::Gridded::Writer.new
    case d
    when 2
      if ref.rank == 2
        ctl.define {
          tdef "1 linear 0z01jan2000 1dy"
          zdef "1 linear 1 1"
          ydef ref.dim0, "linear 1 1"
          xdef ref.dim1, "linear 1 1"
        }
      else
        ctl.define {
          tdef ref.dim0, "linear 0z01jan2000 1dy"
          zdef "1 linear 1 1"
          ydef ref.dim1, "linear 1 1"
          xdef ref.dim2, "linear 1 1"
        }
      end
    when 3
      if ref.rank == 3
        ctl.define {
          tdef "1 linear 0z01jan2000 1dy"
          zdef ref.dim0, "linear 1 1"
          ydef ref.dim1, "linear 1 1"
          xdef ref.dim2, "linear 1 1"
        }
      else
        ctl.define {
          tdef ref.dim0, "linear 0z01jan2000 1dy"
          zdef ref.dim1, "linear 1 1"
          ydef ref.dim2, "linear 1 1"
          xdef ref.dim3, "linear 1 1"
        }
      end
    end
  end
  ctl.define {
    if opts[:undef]
      undef! opts[:undef]
    else
      undef! 1e30
    end
  }
  if block
    ctl.define(&block)
  end
  begin
    names = []
    ctl.template(datactl) do
      dset dataname
      args.each_with_index do |arg, i|
        names << ( name = "tmpvar#{i}" )
        case d
        when 2
          var2d(name, arg)
        when 3
          var3d(name, arg)
        end
      end
    end
    ss = open(datactl)
    vars = []
    names.each do |name|
      vars << define(name, ss.send(name))
    end
    return vars
  ensure
    dataio.close
    File.unlink(datactl)
  end
end

#_guess_colorbar_position(cnum) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/grads/lib/colorbar.rb', line 154

def _guess_colorbar_position (cnum)

  out = q :gxinfo

  io = StringIO.new(out)
  io.gets
  xsize, ysize = *io.gets.split(/\s+/).values_at(3,5).map{|v| v.to_i}
  xlmin, xlmax = *io.gets.split(/\s+/).values_at(3,5).map{|v| v.to_i}
  ylmin, ylmax = *io.gets.split(/\s+/).values_at(3,5).map{|v| v.to_i}
  xlwid = xlmax - xlmin
  ylwid = ylmax - ylmin

  if ylmin < 0.6 or xsize-xlmax > 1.5
    direction = 'vertical'
    xmin = xlmax + ( xsize - xlmax ) / 2 - 0.4
    xmax = xmin + 0.2
    y1wid = 0.5
    if y1wid * cnum > ysize * 0.8 
      y1wid = ysize * 0.8 / cnum
    end
    ymin = ysize / 2 - y1wid * cnum / 2
    ymax = ysize / 2 + y1wid * cnum / 2
  else
    direction = 'horizontal'
    ymin = ylmin / 2
    ymax = ymin + 0.2
    x1wid = 0.8
    if x1wid * cnum > xsize * 0.8
      x1wid = xsize * 0.8 / cnum
    end
    xmin = xsize / 2 - x1wid * cnum / 2
    xmax = xsize / 2 + x1wid * cnum / 2
  end

  return xmin, xmax, ymin, ymax, direction
end

#_hsv2rgb(h, s, v) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/grads/lib/makecpt.rb', line 111

def _hsv2rgb(h, s, v)
  h = h.to_f
  s = s.to_f
  v = v.to_f

  if s == 0 and h == 0
    v = (255*v).floor
    return v, v, v
  end

  if h == 360
    h = 0
  end

  h /= 60.0
  i = h.floor.to_i
  f = h - i

  p = (255*v*(1 - s)).floor
  q = (255*v*(1 - s*f)).floor
  t = (255*v*(1 - s*(1 - f))).floor
  v = (255*v).floor

  case i
  when 0
    return v, t, p
  when 1
    return q, v, p
  when 2
    return p, v, t
  when 3
    return p, q, v
  when 4
    return t, p, v
  when 5
    return v, p, q
  end
end

#axis(dummy, &block) ⇒ Object



869
870
871
872
873
874
875
876
# File 'lib/grads/command.rb', line 869

def axis (dummy, &block)
  begin
    axis_on
    display_nothing(dummy, &block)
  ensure
    axis_off
  end
end

#axis_offObject



833
834
835
836
837
838
839
840
# File 'lib/grads/command.rb', line 833

def axis_off
  set :frame, :off
  set :xlab, :off
  set :ylab, :off
  set :grid, :off
  set :grads, :off
  set :mpdraw, :off
end

#axis_onObject



842
843
844
845
846
847
848
849
# File 'lib/grads/command.rb', line 842

def axis_on
  set :frame, :on
  set :xlab, :on
  set :ylab, :on
  set :grid, :on
  set :grads, :on
  set :mpdraw, :on
end

#axis_xtime(dummy, offset: nil, skip: nil, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/grads/lib/axis_xtime.rb', line 65

def axis_xtime (dummy, offset: nil, skip: nil, &block)
  puts ans = query("dims")
  t1 = subwrd(sublin(ans, 5), 11).to_i
  t2 = subwrd(sublin(ans, 5), 13).to_i
  d1 = Time.parse(subwrd(sublin(ans, 5), 6).sub(/Z/," "))
  d2 = Time.parse(subwrd(sublin(ans, 5), 8).sub(/Z/," "))
  nt = t2 - t1 + 1
  nd = (d2 - d1)/86400.0
  dt = ((d2-d1)/(t2-t1)).round
  time = CArray.object(nt).seq(d1,dt)

  type = nil
  skip = nil

  if offset
    time = time + offset
  end

  if nd <= 1
    type = "day"
  elsif nd <= 3
    type = "3days"
  elsif nd <= 7
    type = "week"
  elsif nd <= 14
    type = "2weeks"
  elsif nd <= 31
    type = "month"
  elsif nd <= 120
    type = "season"
  elsif nd <= 360
    type = "year"
  else
    type = "years"
  end
  
  unless skip
    case type
    when "day"
      skip = dt.to_i/3600
    when "3days"
      skip = 3*dt.to_i/3600
    when "week"
      skip = 6*dt.to_i/3600
    when "2weeks"
      skip = 12*dt.to_i/3600
    when "month"
      skip = 24*dt.to_i/3600
    when "season"
      skip = 3*24*dt.to_i/3600
    when "year", "years"
      skip = 10*24*dt.to_i/3600
    end
  end

  xlabs = []
  xlabs2 = []
  flag = false

  case type
  when "day", "3days", "week"
    hour0 = 0
    time[[nil,skip]].each do |t|
      if not flag 
        xlabs << t.strftime("%d%b%Y").upcase            
        flag = true
      elsif hour0 > t.hour
        xlabs << t.strftime("%d%b").upcase       
      else
        xlabs << t.strftime("")
      end
      hour0 = t.hour
    end
    time[[nil,skip]].each do |t|
      if nt > 73
        if t.hour % 3 == 0
          xlabs2 << t.strftime("%H")      
        end
      else
        xlabs2 << t.strftime("%H")            
      end
    end
    units = ["hour", "day"]
  when "2weeks"
    hour0 = 0
    time[[nil,skip]].each do |t|
      if not flag 
        xlabs << t.strftime("%d%b%Y").upcase            
        flag = true
      elsif hour0 > t.hour
        xlabs << t.strftime("%d").upcase       
      else
        xlabs << t.strftime("")
      end
      hour0 = t.hour
    end
    time[[nil,skip]].each do |t|
      if nt > 73
        if t.hour % 3 == 0
          xlabs2 << t.strftime("%H")      
        end
      else
        xlabs2 << t.strftime("%H")            
      end
    end
    units = ["hour", "day"]
  when "month","season", "year"
    day0 = 1
    time[[nil,skip]].each do |t|
      if not flag
        xlabs << t.strftime("%d%b%Y").upcase            
        flag = true
      elsif day0 > t.day
        xlabs << t.strftime("%b").upcase       
      else
        xlabs << t.strftime("")
      end
      day0 = t.day
    end
    time[[nil,skip]].each do |t|
      if nt > 73
        if t.hour % 3 == 0
          xlabs2 << t.strftime("%d")      
        end
      else
        xlabs2 << t.strftime("%d")            
      end
    end
    units = ["day", "month"]
  end

  begin
    axis_off
    set :xlab, :on
    set :xlabs, xlabs2.join(" | ")
    set :xlpos, 0, :b

    display_nothing(dummy)
  
    axis_on
    set :xlab, :on
    set :xlabs, xlabs.join(" | ")
    set :xlpos, -0.3, :b

    display_nothing(dummy, &block) 
  ensure
    set :xlpos, 0, b
  end
  
  return units
end

#clear(*argv) ⇒ Object Also known as: c



440
441
442
# File 'lib/grads/command.rb', line 440

def clear (*argv)
  put "clear " + argv.join(" ")
end

#close(dh) ⇒ Object



278
279
280
281
# File 'lib/grads/command.rb', line 278

def close (dh)
  file_id = dh.file_id
  put "close #{file_id}"
end

#COLOR(name) ⇒ Object



921
922
923
924
925
926
927
# File 'lib/grads/command.rb', line 921

def COLOR (name)
  if COLOR_TABLE.has_key?(name)
    COLOR_TABLE[name]
  else
    -1
  end
end

#colorbar(opt = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/grads/lib/colorbar.rb', line 23

def colorbar (opt={})
  import :color, :xcbar
  opt = COLORBAR_DEFAULT.clone.update(opt)
  if opt[:color]
    if opt[:color] == :clevs
      arg = "-levs " + opt[:clevs].join(" ")
    else
      arg = [opt[:color]].flatten.join(" ")
    end
    if opt[:kind]
      arg += " -kind " + opt[:kind]
    end
    out = color arg
    clevs = ccols = nil
    out.split(/\n/).each do |line|
      case line 
      when /clevs/
        clevs = line.split(/ +/)[1..-1]
      when /ccols/
        ccols = line.split(/ +/)[1..-1]      
      end
    end
  elsif opt[:clevs] and opt[:ccols]
    clevs = opt[:clevs]
    ccols = opt[:ccols]
  else
    if @clevs and @ccols
      clevs = @clevs.clone
      ccols = @ccols.clone
    else
      out = q :shades
      io = StringIO.new(out)
      io.gets
      clevs = []
      ccols = []
      while line = io.gets
        col, lev = *line.split(/\s+/)[0..1]
        ccols << col.to_i
        if lev != "<"
          clevs << lev.to_f
        end
      end
    end
  end
  set_clevs *clevs
  set_ccols *ccols

  arg = ""
  if opt[:pos]
    arg << opt[:pos].join(" ") + " "
    xmin, xmax, ymin, ymax = *opt[:pos]
    if not opt[:dir]
      if ( xmax - xmin ) > ( ymax - ymin )
        opt[:dir] = "horizontal"
      else
        opt[:dir] = "virtical"
      end
    end
  else
    cnum = ccols.size
    xmin, xmax, ymin, ymax, opt[:dir] = *_guess_colorbar_position(cnum)
  end

  levcol = []
  i = 0
  clevs.each_index do 
    levcol << ccols[i]
    levcol << clevs[i]
    i += 1
  end
  levcol << ccols[i]
  arg << "-levcol #{levcol.join(' ')} "

  if opt[:tics]
    if opt[:tics][0]
      arg << "-fs #{opt[:tics][0]} "
    end
    if opt[:tics][1]
      arg << "-fo #{opt[:tics][1]} "
    end
  end

  if opt[:font]
    if opt[:font][0]
      arg << "-fw #{opt[:font][0]} "
    end
    if opt[:font][1]
      arg << "-fh #{opt[:font][1]} "
    end
    if opt[:font][2]
      arg << "-ft #{opt[:font][2]} "
    end
  end

  if opt[:edge]
    arg << "-edge #{opt[:edge]} "
  end

  if opt[:dir]
    arg << "-dir #{opt[:dir]} "
  end

  if opt[:line]
    arg << "-line on"
  else
    arg << "-line off"
  end
  xcbar arg

  if opt[:title]
    case opt[:title]
    when String
      text = opt[:title]
      ts = 0.2
    else
      text = opt[:title][0]
      ts   = opt[:title][1] || 0.2
    end
    if opt[:dir] == "horizontal" or opt[:dir] == "h"
      set :strsiz, ts
      set :string, 1, "bc", 2
      draw :string, (xmin+xmax)/2.0, ymax+(ymax-ymin)*0.5, text
    else
      set :strsiz, ts
      set :string, 1, "bc", 2, 90
      draw :string, xmin-(xmax-xmin)*0.5, (ymin+ymax)/2.0, text
    end
  end

end

#debug_offObject



249
250
251
# File 'lib/grads/command.rb', line 249

def debug_off
  @debug = false
end

#debug_onObject



245
246
247
# File 'lib/grads/command.rb', line 245

def debug_on
  @debug = true
end

#define(name, expr) ⇒ Object



370
371
372
373
# File 'lib/grads/command.rb', line 370

def define (name, expr)
  put "define #{name} = " + expr
  return Variable.new(nil, name)
end

#display(*argv) ⇒ Object Also known as: d



362
363
364
365
366
367
368
# File 'lib/grads/command.rb', line 362

def display (*argv)
  if argv.first.is_a?(Array)
    put "display " + argv.first.map{|v| "("+v.to_s+")" }.join(";")
  else
    put "display " + argv.join(" ")
  end
end

#display_nothing(dummy) ⇒ Object



807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/grads/command.rb', line 807

def display_nothing (dummy)
  ans = query("dims")
  case ans.scan("varying").size
  when 2
    set :gxout, :contour
    set :clevs, -9.99e99
    set :ccolor, -1
    set :clab, :off
    set :cmark, 0
  when 1
    set :gxout, :line
    set :line, -1
    set :ylab, :off
    set :vrange, -9.99e99, -9.99e98
  end
  if block_given?
    yield
  end
  case dummy
  when String
    put "display #{dummy}"
  else
    display dummy
  end
end

#draw(*argv) ⇒ Object



393
394
395
# File 'lib/grads/command.rb', line 393

def draw (*argv)
  put "draw " + argv.flatten.join(" ")
end

#draw_xlabel(text, area:, color: 1, just: "bc", thickness: 3, rot: 0, offset: "M", side: "b") ⇒ Object



890
891
892
893
894
895
896
897
898
899
900
# File 'lib/grads/command.rb', line 890

def draw_xlabel (text, area:, color: 1, just: "bc", thickness: 3, rot: 0, offset: "M", side: "b")
  set :string, color, just, thickness, rot
  case side
  when "b"
    draw :string, area.center, area.y1-len(offset), text
  when "t"
    draw :string, area.center, area.y2+len(offset), text
  else
    raise "invalid side sepecification"
  end
end

#draw_ylabel(text, area:, color: 1, just: "bc", thickness: 3, rot: 90, offset: "M", side: "l") ⇒ Object



878
879
880
881
882
883
884
885
886
887
888
# File 'lib/grads/command.rb', line 878

def draw_ylabel (text, area:, color: 1, just: "bc", thickness: 3, rot: 90, offset: "M", side: "l")
  set :string, color, just, thickness, rot
  case side
  when "l"
    draw :string, area.x1-len(offset), area.mid, text
  when "r"
    draw :string, area.x2+len(offset), area.mid, text
  else
    raise "invalid side sepecification"
  end
end

#enable(id, *argv) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/grads/command.rb', line 379

def enable (id, *argv)
  case id.to_s
  when "print"
    begin
      put "enable print " + argv.join(" ")
      yield
    ensure
      put "disable print"
    end
  else
    put "enable #{id} " + argv.join(" ")      
  end
end

#eval(expr) ⇒ Object



375
376
377
# File 'lib/grads/command.rb', line 375

def eval (expr)
  put expr.to_s
end

#exec(name, *argv) ⇒ Object



333
334
335
# File 'lib/grads/command.rb', line 333

def exec (name, *argv)
  put "exec #{name} " + argv.flatten.join(" ")
end

#exec_string(definition, *argv) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/grads/command.rb', line 337

def exec_string (definition, *argv)
  io = Tempfile.open("CA_GrADS_", ".")
  io.write definition 
  io.puts
  io.flush
  return exec(io.path, *argv)
ensure
  io.close
end

#get_levelObject



486
487
488
# File 'lib/grads/command.rb', line 486

def get_level
  return subwrd(sublin(query("dims"),4),6).to_f    
end

#get_mouse_click(trans = nil) ⇒ Object



500
501
502
503
504
505
506
507
508
509
# File 'lib/grads/command.rb', line 500

def get_mouse_click (trans = nil)
  list = query("pos").split(/\s+/)
  button = list[4].to_i
  x, y = list[2].to_f, list[3].to_f
  if trans
    list = query(trans, x, y).split(/\s+/)
    x, y = list[2].to_f, list[5].to_f
  end
  return x, y, button
end

#get_timeObject



467
468
469
470
471
472
473
474
# File 'lib/grads/command.rb', line 467

def get_time
  list = query("time").split(/\s+/)
  if list[2] == list[4]
    return DateTime.parse(list[2].sub(/Z/," "))
  else
    return DateTime.parse(list[2].sub(/Z/," "))..DateTime.parse(list[4].sub(/Z/," "))
  end
end

#get_time_endObject



481
482
483
484
# File 'lib/grads/command.rb', line 481

def get_time_end
  list = query("time").split(/\s+/)
  return DateTime.parse(list[4].sub(/Z/," "))
end

#get_time_startObject



476
477
478
479
# File 'lib/grads/command.rb', line 476

def get_time_start
  list = query("time").split(/\s+/)
  return DateTime.parse(list[2].sub(/Z/," "))
end

#get_value(*argv) ⇒ Object



490
491
492
493
494
495
496
497
498
# File 'lib/grads/command.rb', line 490

def get_value (*argv)
  var = argv.shift
  if argv.empty?
    list = query(:defval, var, 0, 0).split(/\s+/)
  else
    list = query(:defval, var, *argv).split(/\s+/)
  end
  return list[2].to_f
end

#gr2w(fi, fj) ⇒ Object



526
527
528
# File 'lib/grads/command.rb', line 526

def gr2w (fi, fj)
  return transform(:gr2w, fi, fj)
end

#gr2xy(fi, fj) ⇒ Object



530
531
532
# File 'lib/grads/command.rb', line 530

def gr2xy (fi, fj)
  return transform(:gr2xy, fi, fj)
end

#import(*args) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/grads/command.rb', line 559

def import (*args)
  args.each do |name|
    if @imported.include?(name)
      next
    end
    self.class.module_eval %{
      def #{name} (*argv)
        put "#{name} " + argv.join(" ")
      end
    }
    @imported.push name
  end
end

#imported?(arg) ⇒ Boolean

Returns:

  • (Boolean)


573
574
575
# File 'lib/grads/command.rb', line 573

def imported? (arg)
  return @imported.include?(arg)
end

#len(str) ⇒ Object



791
792
793
794
# File 'lib/grads/command.rb', line 791

def len (str)
  ans = query("string #{str}")
  return subwrd(ans, 4).to_f
end

#LINE(*argv) ⇒ Object



1083
1084
1085
# File 'lib/grads/command.rb', line 1083

def LINE (*argv)
  return LINE.new(*argv)
end

#makecpt(*args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/grads/lib/makecpt.rb', line 59

def makecpt (*args)
  begin
    io = IO::popen("makecpt -Fr #{args.join(' ')}", "r")
  rescue Errno::ENOENT
    io = IO::popen("gmt makecpt -Fr #{args.join(' ')}", "r")
  end
  bgrb = nil
  frgb = nil
  list = []
  while line = io.gets
    case line
    when /\A[\#N]/
      next
    when /\AB\s/
      brgb = line.split(/[\s\/]/)[1..3].map(&:to_f)
    when /\AF\s/
      frgb = line.split(/[\s\/]/)[1..3].map(&:to_f)
    else
      list << line.split(/[\s\/]/)[0..3].map(&:to_f)
      last = line.split(/[\s\/]/)[4..7].map(&:to_f)
    end
  end
  list << last
  io.close
  clevs = []
  ccols = []
  ic = 16
  if brgb
    set :rgb, ic, *brgb.map(&:round)
    ccols << ic
  else
    ccols << -1
  end
  ic += 1
  list.each_with_index do |(v,r,g,b),i|
    set :rgb, i+ic, r.round, g.round, b.round
    clevs << v
    if ! frgb and i == list.size - 1 
      ccols << -1
    else
      ccols << i+ic
    end
  end
  ic += list.size - 1
  if frgb
    set :rgb, ic, *frgb.map(&:round)
  end
  set_clevs *clevs
  set_ccols *ccols
  return clevs, ccols
end

#open(*argv) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/grads/command.rb', line 262

def open (*argv)
  text = put "open " + argv.join(" ")
  file_id = nil
  text.each_line do |line|
    if line =~ /is open as file/
      file_id = subwrd(line, 8).to_i
      break
    end
  end
  if file_id 
    return DataHandler.new(file_id)
  else
    raise "failed to open file #{argv.first}"
  end
end

#parea(x1, x2, y1, y2) {|Area.new(x1, x2, y1, y2)| ... } ⇒ Object

Yields:

  • (Area.new(x1, x2, y1, y2))


796
797
798
799
800
# File 'lib/grads/command.rb', line 796

def parea (x1, x2, y1, y2)
  set :parea, x1, x2, y1, y2
  set :clip, x1, x2, y1, y2
  yield Area.new(x1, x2, y1, y2)
end

#pauseObject



577
578
579
# File 'lib/grads/command.rb', line 577

def pause
  get_mouse_click
end

#plot(var, *args) ⇒ Object



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/grads/command.rb', line 851

def plot (var, *args)
  begin
    axis_off
    if block_given?
      yield
    end
    set :gxout, *args
    case var
    when String
      put "display #{var}"
    else
      display var
    end
  ensure
      axis_on
  end
end

#put(*args) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/grads/command.rb', line 203

def put (*args)
  command = args.join("\n")
  @log << command << "\n"
  if @debug
    STDERR.puts "ga-> " + command 
  end
  thread = Thread.start {
    begin
      size = command.size
      s = 0
      while s < size
        s += @io.write(command[s, 1024])
      end
      @io.puts
      @io.puts "SIGNAL_FOR_RUBY_GRADS"
      @io.flush
    rescue Errno::EPIPE
    end
  }
  output = ""
  while line = @stdout.gets
    if @echoback and line =~ /^ga->\s+/
      next
    else
      line.sub!(/^ga->\s+/,'')
    end
    case line
    when /SIGNAL_FOR_RUBY_GRADS/
      STDERR.print(output) if @debug and not output.empty?
      return output.chomp
    when /^(\w+) error: (.*?) /
      output << line
      STDERR.print output
      raise "GRADS Error"
    else
      output << line
    end
  end
ensure
  thread.join
end

#quitObject Also known as: exit



193
194
195
196
197
198
199
# File 'lib/grads/command.rb', line 193

def quit
  put("quit")
  @io.close
  @listen.join
  @stdout.close
  @stderr.close
end

#redraw(*argv) ⇒ Object



397
398
399
# File 'lib/grads/command.rb', line 397

def redraw (*argv)
  put "redraw " + argv.flatten.join(" ")
end

#RGB(*argv) ⇒ Object



1091
1092
1093
1094
1095
1096
1097
1098
# File 'lib/grads/command.rb', line 1091

def RGB (*argv)
  case argv.size
  when 1
    return GrADS::Command::RGB_TABLE[argv[0]]
  else
    return GrADS::Command::RGB.new(*argv)
  end
end

#save_image(filename, size = 640, dpi = 300) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/grads/lib/save_image.rb', line 5

def save_image (filename, size=640, dpi=300)
  io = Tempfile.open("CA_GrADS_", ".")
  basename = io.path
  enable(:print, basename) {
    print
  }
  case filename
  when /\.eps/
    system %{
      gxeps -R -i #{basename} -o #{filename}
    }
  else
    system %{
      gxeps -R -i #{basename} -o #{basename}.eps
      convert -density #{dpi} -resize #{size} +antialias #{basename}.eps #{filename}
      rm -f #{basename}.eps
    }
  end
  io.close
end

#scan(ctltext) ⇒ Object



315
316
317
318
319
320
321
322
# File 'lib/grads/command.rb', line 315

def scan (ctltext)
  io = Tempfile.open("CA_GrADS_", ".")
  io.write ctltext
  io.flush
  return open(io.path)
ensure
  io.close
end

#script(name, definition) ⇒ Object



347
348
349
350
351
352
353
354
355
356
# File 'lib/grads/command.rb', line 347

def script (name, definition)
  io = Tempfile.new("CA_GrADS_", ".")
  io.write(definition)
  io.flush
  instance_eval %{
    def #{name} (*args)
      run "#{io.path}", *args
    end
  }
end

#sdfopen(*argv) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/grads/command.rb', line 299

def sdfopen (*argv)
  text = put "sdfopen " + argv.join(" ")
  file_id = nil
  text.each_line do |line|
    if line =~ /is open as file/
      file_id = subwrd(line, 8).to_i
      break
    end
  end
  if file_id 
    return DataHandler.new(file_id)
  else
    raise "failed to open file #{argv.first}"
  end
end

#set(*argv) ⇒ Object



358
359
360
# File 'lib/grads/command.rb', line 358

def set (*argv)
  put "set " + argv.flatten.join(" ")
end

#set_ccols(*argv) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/grads/command.rb', line 546

def set_ccols (*argv)
  list = []
  argv.each do |arg|
    case arg
    when RGB
      list << set_rgb(arg)
    else
      list << arg
    end
  end
  set :ccols, *list
end

#set_clevs(*argv) ⇒ Object



542
543
544
# File 'lib/grads/command.rb', line 542

def set_clevs (*argv)
  set :clevs, *argv
end

#set_line(obj) ⇒ Object



1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
# File 'lib/grads/command.rb', line 1118

def set_line (obj)
  args = obj.to_a
  if obj.color.is_a?(RGB)
    args[0] = set_rgb(obj.color)
  end    
  set :line, *args
  set :ccolor, args[0]
  set :cstyle, args[1]
  set :cthick, args[2]
end

#set_rgb(*argv) ⇒ Object



1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/grads/command.rb', line 1129

def set_rgb (*argv)
  list = []
  argv.each do |obj|
    num = RGB.next_number
    case obj
    when TILE
      nt = TILE.next_number
      args = obj.to_a
      if obj.color.is_a?(RGB)
        c = set_rgb(obj.color)
        args[4] = c
      end
      set :tile, nt, *args.to_a
      set :rgb, num, :tile, nt
    when RGB
      set :rgb, num, *obj.to_a        
    end
    list << num
  end
  return list
end

#set_time(*argv) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/grads/command.rb', line 448

def set_time (*argv)
  list = []
  argv.each do |time|
    timestr = nil
    case time
    when DateTime, Time
      timestr = time.strftime("%H:%Mz%d%b%Y")    
    when Date
      timestr = time.strftime("%d%b%Y")
    when String
      timestr = time
    else
      raise "unknown time variable #{time.inspect}"
    end
    list << timestr
  end
  set :time, *list
end

#setcpt(file) ⇒ Object

— makecpt(“GMT’s makecpt arguments as String”)

Sets clevs and ccols from the output of piped makecpt command.

The continuous color pallet generated with makecpt option “-Z” is not supported.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/grads/lib/makecpt.rb', line 11

def setcpt (file)
  io = Kernel::open(file)
  bgrb = nil
  frgb = nil
  list = []
  while line = io.gets
    case line
    when /\A[\#N]/
      next
    when /\AB\s/
      brgb = line.split(/[\s\/]+/)[1..3].map(&:to_f)
    when /\AF\s/
      frgb = line.split(/[\s\/]+/)[1..3].map(&:to_f)
    else
      list << line.split(/[\s\/]+/)[0..3].map(&:to_f)
      last = line.split(/[\s\/]+/)[4..7].map(&:to_f)
    end
  end
  list << last
  io.close
  clevs = []
  ccols = []
  ic = 16
  if brgb
    set :rgb, ic, *brgb.map(&:round)
    ccols << ic
  else
    ccols << -1
  end
  ic += 1
  list.each_with_index do |(v,r,g,b),i|
    set :rgb, i+ic, r.round, g.round, b.round
    clevs << v
    if ! frgb and i == list.size - 1 
      ccols << -1
    else
      ccols << i+ic
    end
  end
  ic += list.size - 1
  if frgb
    set :rgb, ic, *frgb.map(&:round)
  end
  set_clevs *clevs
  set_ccols *ccols
  return clevs, ccols
end

#sublin(text, n) ⇒ Object



253
254
255
# File 'lib/grads/command.rb', line 253

def sublin (text, n)
  return text.split(/\n/)[n-1]
end

#subwrd(text, n) ⇒ Object



257
258
259
260
# File 'lib/grads/command.rb', line 257

def subwrd (text, n)
  wrd = text.strip.split(/\s*\n\s*|\s+/)[n-1]
  return wrd ? wrd.strip : ""
end

#template2d(sd, *args, &block) ⇒ Object



581
582
583
# File 'lib/grads/command.rb', line 581

def template2d (sd, *args, &block)
  return __template__(2, sd, *args, &block)
end

#template3d(sd, *args, &block) ⇒ Object



585
586
587
# File 'lib/grads/command.rb', line 585

def template3d (sd, *args, &block)
  return __template__(3, sd, *args, &block)
end

#TILE(*argv) ⇒ Object



1087
1088
1089
# File 'lib/grads/command.rb', line 1087

def TILE (*argv)
  return TILE.new(*argv)
end

#time_skipObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/grads/lib/axis_xtime.rb', line 11

def time_skip 
  ans = query("dims")
  t1 = subwrd(sublin(ans, 5), 11).to_i
  t2 = subwrd(sublin(ans, 5), 13).to_i
  d1 = Time.parse(subwrd(sublin(ans, 5), 6).sub(/Z/," "))
  d2 = Time.parse(subwrd(sublin(ans, 5), 8).sub(/Z/," "))
  nt = t2 - t1 + 1
  nd = (d2 - d1)/86400.0
  dt = ((d2-d1)/(t2-t1)).round

  type = nil
  skip = nil

  if nd <= 1
    type = "day"
  elsif nd <= 3
    type = "3days"
  elsif nd <= 7
    type = "week"
  elsif nd <= 14
    type = "2weeks"
  elsif nd <= 31
    type = "month"
  elsif nd <= 120
    type = "season"
  elsif nd <= 360
    type = "year"
  else
    type = "years"
  end

  unless skip
    case type
    when "day"
      skip = dt.to_i/3600
    when "3days"
      skip = 3*dt.to_i/3600
    when "week"
      skip = 6*dt.to_i/3600
    when "2weeks"
      skip = 12*dt.to_i/3600
    when "month"
      skip = 24*dt.to_i/3600
    when "season"
      skip = 3*24*dt.to_i/3600
    when "year", "years"
      skip = 10*24*dt.to_i/3600
    end
  end
  
  return skip
end

#transform(kind, v1, v2) ⇒ Object



511
512
513
514
515
516
# File 'lib/grads/command.rb', line 511

def transform (kind, v1, v2)
  result = query(kind, v1, v2)
  r1 = subwrd(result, 3)
  r2 = subwrd(result, 6)
  return r1, r2
end

#vpage(x1, x2, y1, y2) {|Area.new(x1, x2, y1, y2)| ... } ⇒ Object

Yields:

  • (Area.new(x1, x2, y1, y2))


802
803
804
805
# File 'lib/grads/command.rb', line 802

def vpage (x1, x2, y1, y2)
  set :vpage, x1, x2, y1, y2
  yield Area.new(x1, x2, y1, y2)
end

#w2gr(lat, lon) ⇒ Object



522
523
524
# File 'lib/grads/command.rb', line 522

def w2gr (lat, lon)
  return transform(:w2gr, lat, lon)
end

#w2xy(lat, lon) ⇒ Object



518
519
520
# File 'lib/grads/command.rb', line 518

def w2xy (lat, lon)
  return transform(:w2xy, lat, lon)
end

#xcbar_with_ccols(clevs, ccols, *xcbaropts) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/grads/lib/xcbar_with_ccols.rb', line 4

def xcbar_with_ccols (clevs, ccols, *xcbaropts)
  import :xcbar
  nlevs = clevs.length
  levcol = []
  nlevs.times do |i|
    levcol << ccols[i]
    levcol << clevs[i]
  end
  levcol << ccols.last
  set :clevs, *clevs
  set :ccols, *ccols
  xcbar *[xcbaropts,"-levcol #{levcol.join(' ')}"].flatten
end

#xcbar_with_color(colorspec, *xcbaropts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/grads/lib/xcbar_with_ccols.rb', line 18

def xcbar_with_color (colorspec, *xcbaropts)
  import :xcbar, :color
  nlevs = clevs.length
  levcol = []
  nlevs.times do |i|
    levcol << ccols[i]
    levcol << clevs[i]
  end
  levcol << ccols.last
  set :clevs, *clevs
  set :ccols, *ccols
  xcbar *[xcbaropts,"-levcol #{levcol.join(' ')}"].flatten
end

#xdfopen(*argv) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/grads/command.rb', line 283

def xdfopen (*argv)
  text = put "xdfopen " + argv.join(" ")
  file_id = nil
  text.each_line do |line|
    if line =~ /is open as file/
      file_id = subwrd(line, 8).to_i
      break
    end
  end
  if file_id 
    return DataHandler.new(file_id)
  else
    raise "failed to open file #{argv.first}"
  end
end

#xdfscan(ctltext) ⇒ Object



324
325
326
327
328
329
330
331
# File 'lib/grads/command.rb', line 324

def xdfscan (ctltext)
  io = Tempfile.open("CA_GrADS_", ".")
  io.write ctltext
  io.flush
  return xdfopen(io.path)
ensure
  io.close
end

#xy2gr(x, y) ⇒ Object



538
539
540
# File 'lib/grads/command.rb', line 538

def xy2gr (x, y)
  return transform(:xy2gr, x, y)
end

#xy2w(x, y) ⇒ Object



534
535
536
# File 'lib/grads/command.rb', line 534

def xy2w (x, y)
  return transform(:xy2w, x, y)
end