Class: GrADS::Command

Inherits:
Object
  • Object
show all
Defined in:
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

Defined Under Namespace

Classes: DataHandler, Expression, Variable

Constant Summary collapse

COLORBAR_DEFAULT =
{
  :line=>true,
  :edge=>"triangle",
}

Instance Attribute 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
# File 'lib/grads/command.rb', line 185

def method_missing (id, *argv)
  return id, *argv
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

Instance Method Details

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



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
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
# File 'lib/grads/command.rb', line 567

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



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
# File 'lib/grads/lib/makecpt.rb', line 141

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

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



423
424
425
426
427
# File 'lib/grads/command.rb', line 423

def clear (*argv)
  @ccols = nil
  @clevs = nil
  put "clear " + argv.join(" ")
end

#close(dh) ⇒ Object



274
275
276
277
# File 'lib/grads/command.rb', line 274

def close (dh)
  file_id = dh.file_id
  put "close #{file_id}"
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



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

def debug_off
  @debug = false
end

#debug_onObject



241
242
243
# File 'lib/grads/command.rb', line 241

def debug_on
  @debug = true
end

#define(name, expr) ⇒ Object



366
367
368
369
# File 'lib/grads/command.rb', line 366

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

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



358
359
360
361
362
363
364
# File 'lib/grads/command.rb', line 358

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

#draw(*argv) ⇒ Object



389
390
391
# File 'lib/grads/command.rb', line 389

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

#enable(id, *argv) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/grads/command.rb', line 375

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



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

def eval (expr)
  put expr.to_s
end

#exec(name, *argv) ⇒ Object



329
330
331
# File 'lib/grads/command.rb', line 329

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

#exec_string(definition, *argv) ⇒ Object



333
334
335
336
337
338
339
340
341
# File 'lib/grads/command.rb', line 333

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



471
472
473
# File 'lib/grads/command.rb', line 471

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

#get_mouse_click(trans = nil) ⇒ Object



485
486
487
488
489
490
491
492
493
494
# File 'lib/grads/command.rb', line 485

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



452
453
454
455
456
457
458
459
# File 'lib/grads/command.rb', line 452

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



466
467
468
469
# File 'lib/grads/command.rb', line 466

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

#get_time_startObject



461
462
463
464
# File 'lib/grads/command.rb', line 461

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

#get_value(*argv) ⇒ Object



475
476
477
478
479
480
481
482
483
# File 'lib/grads/command.rb', line 475

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



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

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

#gr2xy(fi, fj) ⇒ Object



515
516
517
# File 'lib/grads/command.rb', line 515

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

#import(*args) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/grads/command.rb', line 537

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)


551
552
553
# File 'lib/grads/command.rb', line 551

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

#makecpt(*args) ⇒ Object



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
# File 'lib/grads/lib/makecpt.rb', line 76

def makecpt (*args)
  io = IO::popen("makecpt #{args.join(' ')}", "r")
  while line = io.gets
    if line =~ /COLOR_MODEL\s*=\s*\+?(HSV|RGB)/
      model = $1
      break
    end
  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{|v| v.to_f}
    when /\AF\s/
      frgb = line.split(/\s+/)[1..3].map{|v| v.to_f}
    else
      list << line.split(/\s+/)[0..3].map{|v| v.to_f}
      last = line.split(/\s+/)[4..7].map{|v| v.to_f}
    end
  end
  list << last
  io.close
  clevs = []
  ccols = []
  ic = 16
  if brgb
    if model == "HSV"
      set :rgb, ic, *_hsv2rgb(*brgb)
    else
      set :rgb, ic, *brgb
    end
    ccols << ic
  else
    ccols << 0
  end
  ic += 1
  list.each_with_index do |(v,r,g,b),i|
    if model == "HSV"
      set :rgb, i+ic, *_hsv2rgb(r, g, b)
    else
      set :rgb, i+ic, r, g, b
    end
    clevs << v
    if ! frgb and i == list.size - 1 
      ccols << 0
    else
      ccols << i+ic
    end
  end
  ic += list.size - 1
  if frgb
    if model == "HSV"
      set :rgb, ic, *_hsv2rgb(*frgb)
    else
      set :rgb, ic, *frgb
    end
  end
  set_clevs *clevs
  set_ccols *ccols
end

#open(*argv) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/grads/command.rb', line 258

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

#pauseObject



555
556
557
# File 'lib/grads/command.rb', line 555

def pause
  get_mouse_click
end

#put(*args) ⇒ Object



199
200
201
202
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
# File 'lib/grads/command.rb', line 199

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



189
190
191
192
193
194
195
# File 'lib/grads/command.rb', line 189

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

#redraw(*argv) ⇒ Object



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

def redraw (*argv)
  put "redraw " + argv.flatten.join(" ")
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



311
312
313
314
315
316
317
318
# File 'lib/grads/command.rb', line 311

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



343
344
345
346
347
348
349
350
351
352
# File 'lib/grads/command.rb', line 343

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



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/grads/command.rb', line 295

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



354
355
356
# File 'lib/grads/command.rb', line 354

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

#set_ccols(*argv) ⇒ Object



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

def set_ccols (*argv)
  @ccols = argv
  set :ccols, *argv
end

#set_clevs(*argv) ⇒ Object



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

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

#set_time(*argv) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/grads/command.rb', line 433

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/grads/lib/makecpt.rb', line 11

def setcpt (file)
  io = Kernel::open(file)
  while line = io.gets
    if line =~ /COLOR_MODEL\s*=\s*\+?(HSV|RGB)/
      model = $1
      break
    end
  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{|v| v.to_f}
    when /\AF\s/
      frgb = line.split(/\s+/)[1..3].map{|v| v.to_f}
    else
      list << line.lstrip.split(/\s+/)[0..3].map{|v| v.to_f}
      last = line.lstrip.split(/\s+/)[4..7].map{|v| v.to_f}
    end
  end
  list << last
  io.close
  clevs = []
  ccols = []
  ic = 16
  if brgb
    if model == "HSV"
      set :rgb, ic, *_hsv2rgb(*brgb)
    else
      set :rgb, ic, *brgb
    end
    ccols << ic
  else
    ccols << 0
  end
  ic += 1
  list.each_with_index do |(v,r,g,b),i|
    if model == "HSV"
      set :rgb, i+ic, *_hsv2rgb(r, g, b)
    else
      set :rgb, i+ic, r, g, b
    end
    clevs << v
    if ! frgb and i == list.size - 1 
      ccols << 0
    else
      ccols << i+ic
    end
  end
  ic += list.size - 1
  if frgb
    if model == "HSV"
      set :rgb, ic, *_hsv2rgb(*frgb)
    else
      set :rgb, ic, *frgb
    end
  end
  set_clevs *clevs
  set_ccols *ccols
end

#sublin(text, n) ⇒ Object



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

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

#subwrd(text, n) ⇒ Object



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

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

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



559
560
561
# File 'lib/grads/command.rb', line 559

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

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



563
564
565
# File 'lib/grads/command.rb', line 563

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

#transform(kind, v1, v2) ⇒ Object



496
497
498
499
500
501
# File 'lib/grads/command.rb', line 496

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

#w2gr(lat, lon) ⇒ Object



507
508
509
# File 'lib/grads/command.rb', line 507

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

#w2xy(lat, lon) ⇒ Object



503
504
505
# File 'lib/grads/command.rb', line 503

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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/grads/command.rb', line 279

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



320
321
322
323
324
325
326
327
# File 'lib/grads/command.rb', line 320

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



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

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

#xy2w(x, y) ⇒ Object



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

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