ruby-grads 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ #
2
+ # colorbar :title=>"colorbar"
3
+ # :color=>[0,10,2]
4
+ # :clevs=>[]
5
+ # :ccols=>[],
6
+ # :kind=>"",
7
+ # :tics=>[0,2],
8
+ # :pos=>[xmin,xmax,ymin,ymax],
9
+ # :font=>[fw,fh,ft]
10
+ # :edge=>"triangle"
11
+ # :dir=>"horizontal"
12
+ # :line=>true
13
+
14
+ require "stringio"
15
+
16
+ class GrADS::Command
17
+
18
+ COLORBAR_DEFAULT = {
19
+ :line=>true,
20
+ :edge=>"triangle",
21
+ }
22
+
23
+ def colorbar (opt={})
24
+ import :color, :xcbar
25
+ opt = COLORBAR_DEFAULT.clone.update(opt)
26
+ if opt[:color]
27
+ if opt[:color] == :clevs
28
+ arg = "-levs " + opt[:clevs].join(" ")
29
+ else
30
+ arg = [opt[:color]].flatten.join(" ")
31
+ end
32
+ if opt[:kind]
33
+ arg += " -kind " + opt[:kind]
34
+ end
35
+ out = color arg
36
+ clevs = ccols = nil
37
+ out.split(/\n/).each do |line|
38
+ case line
39
+ when /clevs/
40
+ clevs = line.split(/ +/)[1..-1]
41
+ when /ccols/
42
+ ccols = line.split(/ +/)[1..-1]
43
+ end
44
+ end
45
+ elsif opt[:clevs] and opt[:ccols]
46
+ clevs = opt[:clevs]
47
+ ccols = opt[:ccols]
48
+ else
49
+ if @clevs and @ccols
50
+ clevs = @clevs.clone
51
+ ccols = @ccols.clone
52
+ else
53
+ out = q :shades
54
+ io = StringIO.new(out)
55
+ io.gets
56
+ clevs = []
57
+ ccols = []
58
+ while line = io.gets
59
+ col, lev = *line.split(/\s+/)[0..1]
60
+ ccols << col.to_i
61
+ if lev != "<"
62
+ clevs << lev.to_f
63
+ end
64
+ end
65
+ end
66
+ end
67
+ set_clevs *clevs
68
+ set_ccols *ccols
69
+
70
+ arg = ""
71
+ if opt[:pos]
72
+ arg << opt[:pos].join(" ") + " "
73
+ xmin, xmax, ymin, ymax = *opt[:pos]
74
+ if not opt[:dir]
75
+ if ( xmax - xmin ) > ( ymax - ymin )
76
+ opt[:dir] = "horizontal"
77
+ else
78
+ opt[:dir] = "virtical"
79
+ end
80
+ end
81
+ else
82
+ cnum = ccols.size
83
+ xmin, xmax, ymin, ymax, opt[:dir] = *_guess_colorbar_position(cnum)
84
+ end
85
+
86
+ levcol = []
87
+ i = 0
88
+ clevs.each_index do
89
+ levcol << ccols[i]
90
+ levcol << clevs[i]
91
+ i += 1
92
+ end
93
+ levcol << ccols[i]
94
+ arg << "-levcol #{levcol.join(' ')} "
95
+
96
+ if opt[:tics]
97
+ if opt[:tics][0]
98
+ arg << "-fs #{opt[:tics][0]} "
99
+ end
100
+ if opt[:tics][1]
101
+ arg << "-fo #{opt[:tics][1]} "
102
+ end
103
+ end
104
+
105
+ if opt[:font]
106
+ if opt[:font][0]
107
+ arg << "-fw #{opt[:font][0]} "
108
+ end
109
+ if opt[:font][1]
110
+ arg << "-fh #{opt[:font][1]} "
111
+ end
112
+ if opt[:font][2]
113
+ arg << "-ft #{opt[:font][2]} "
114
+ end
115
+ end
116
+
117
+ if opt[:edge]
118
+ arg << "-edge #{opt[:edge]} "
119
+ end
120
+
121
+ if opt[:dir]
122
+ arg << "-dir #{opt[:dir]} "
123
+ end
124
+
125
+ if opt[:line]
126
+ arg << "-line on"
127
+ else
128
+ arg << "-line off"
129
+ end
130
+ xcbar arg
131
+
132
+ if opt[:title]
133
+ case opt[:title]
134
+ when String
135
+ text = opt[:title]
136
+ ts = 0.2
137
+ else
138
+ text = opt[:title][0]
139
+ ts = opt[:title][1] || 0.2
140
+ end
141
+ if opt[:dir] == "horizontal" or opt[:dir] == "h"
142
+ set :strsiz, ts
143
+ set :string, 1, "bc", 2
144
+ draw :string, (xmin+xmax)/2.0, ymax+(ymax-ymin)*0.5, text
145
+ else
146
+ set :strsiz, ts
147
+ set :string, 1, "bc", 2, 90
148
+ draw :string, xmin-(xmax-xmin)*0.5, (ymin+ymax)/2.0, text
149
+ end
150
+ end
151
+
152
+ end
153
+
154
+ def _guess_colorbar_position (cnum)
155
+
156
+ out = q :gxinfo
157
+
158
+ io = StringIO.new(out)
159
+ io.gets
160
+ xsize, ysize = *io.gets.split(/\s+/).values_at(3,5).map{|v| v.to_i}
161
+ xlmin, xlmax = *io.gets.split(/\s+/).values_at(3,5).map{|v| v.to_i}
162
+ ylmin, ylmax = *io.gets.split(/\s+/).values_at(3,5).map{|v| v.to_i}
163
+ xlwid = xlmax - xlmin
164
+ ylwid = ylmax - ylmin
165
+
166
+ if ylmin < 0.6 or xsize-xlmax > 1.5
167
+ direction = 'vertical'
168
+ xmin = xlmax + ( xsize - xlmax ) / 2 - 0.4
169
+ xmax = xmin + 0.2
170
+ y1wid = 0.5
171
+ if y1wid * cnum > ysize * 0.8
172
+ y1wid = ysize * 0.8 / cnum
173
+ end
174
+ ymin = ysize / 2 - y1wid * cnum / 2
175
+ ymax = ysize / 2 + y1wid * cnum / 2
176
+ else
177
+ direction = 'horizontal'
178
+ ymin = ylmin / 2
179
+ ymax = ymin + 0.2
180
+ x1wid = 0.8
181
+ if x1wid * cnum > xsize * 0.8
182
+ x1wid = xsize * 0.8 / cnum
183
+ end
184
+ xmin = xsize / 2 - x1wid * cnum / 2
185
+ xmax = xsize / 2 + x1wid * cnum / 2
186
+ end
187
+
188
+ return xmin, xmax, ymin, ymax, direction
189
+ end
190
+
191
+ end
192
+
@@ -0,0 +1,180 @@
1
+ class GrADS::Command
2
+
3
+ # --- makecpt("GMT's makecpt arguments as String")
4
+ #
5
+ # Sets clevs and ccols from the output of piped makecpt command.
6
+ #
7
+ # The continuous color pallet generated with makecpt option "-Z"
8
+ # is not supported.
9
+ #
10
+
11
+ def setcpt (file)
12
+ io = Kernel::open(file)
13
+ while line = io.gets
14
+ if line =~ /COLOR_MODEL\s*=\s*\+?(HSV|RGB)/
15
+ model = $1
16
+ break
17
+ end
18
+ end
19
+ bgrb = nil
20
+ frgb = nil
21
+ list = []
22
+ while line = io.gets
23
+ case line
24
+ when /\A[\#N]/
25
+ next
26
+ when /\AB\s/
27
+ brgb = line.split(/\s+/)[1..3].map{|v| v.to_f}
28
+ when /\AF\s/
29
+ frgb = line.split(/\s+/)[1..3].map{|v| v.to_f}
30
+ else
31
+ list << line.lstrip.split(/\s+/)[0..3].map{|v| v.to_f}
32
+ last = line.lstrip.split(/\s+/)[4..7].map{|v| v.to_f}
33
+ end
34
+ end
35
+ list << last
36
+ io.close
37
+ clevs = []
38
+ ccols = []
39
+ ic = 16
40
+ if brgb
41
+ if model == "HSV"
42
+ set :rgb, ic, *_hsv2rgb(*brgb)
43
+ else
44
+ set :rgb, ic, *brgb
45
+ end
46
+ ccols << ic
47
+ else
48
+ ccols << 0
49
+ end
50
+ ic += 1
51
+ list.each_with_index do |(v,r,g,b),i|
52
+ if model == "HSV"
53
+ set :rgb, i+ic, *_hsv2rgb(r, g, b)
54
+ else
55
+ set :rgb, i+ic, r, g, b
56
+ end
57
+ clevs << v
58
+ if ! frgb and i == list.size - 1
59
+ ccols << 0
60
+ else
61
+ ccols << i+ic
62
+ end
63
+ end
64
+ ic += list.size - 1
65
+ if frgb
66
+ if model == "HSV"
67
+ set :rgb, ic, *_hsv2rgb(*frgb)
68
+ else
69
+ set :rgb, ic, *frgb
70
+ end
71
+ end
72
+ set_clevs *clevs
73
+ set_ccols *ccols
74
+ end
75
+
76
+ def makecpt (*args)
77
+ io = IO::popen("makecpt #{args.join(' ')}", "r")
78
+ while line = io.gets
79
+ if line =~ /COLOR_MODEL\s*=\s*\+?(HSV|RGB)/
80
+ model = $1
81
+ break
82
+ end
83
+ end
84
+ bgrb = nil
85
+ frgb = nil
86
+ list = []
87
+ while line = io.gets
88
+ case line
89
+ when /\A[\#N]/
90
+ next
91
+ when /\AB\s/
92
+ brgb = line.split(/\s+/)[1..3].map{|v| v.to_f}
93
+ when /\AF\s/
94
+ frgb = line.split(/\s+/)[1..3].map{|v| v.to_f}
95
+ else
96
+ list << line.split(/\s+/)[0..3].map{|v| v.to_f}
97
+ last = line.split(/\s+/)[4..7].map{|v| v.to_f}
98
+ end
99
+ end
100
+ list << last
101
+ io.close
102
+ clevs = []
103
+ ccols = []
104
+ ic = 16
105
+ if brgb
106
+ if model == "HSV"
107
+ set :rgb, ic, *_hsv2rgb(*brgb)
108
+ else
109
+ set :rgb, ic, *brgb
110
+ end
111
+ ccols << ic
112
+ else
113
+ ccols << 0
114
+ end
115
+ ic += 1
116
+ list.each_with_index do |(v,r,g,b),i|
117
+ if model == "HSV"
118
+ set :rgb, i+ic, *_hsv2rgb(r, g, b)
119
+ else
120
+ set :rgb, i+ic, r, g, b
121
+ end
122
+ clevs << v
123
+ if ! frgb and i == list.size - 1
124
+ ccols << 0
125
+ else
126
+ ccols << i+ic
127
+ end
128
+ end
129
+ ic += list.size - 1
130
+ if frgb
131
+ if model == "HSV"
132
+ set :rgb, ic, *_hsv2rgb(*frgb)
133
+ else
134
+ set :rgb, ic, *frgb
135
+ end
136
+ end
137
+ set_clevs *clevs
138
+ set_ccols *ccols
139
+ end
140
+
141
+ def _hsv2rgb(h, s, v)
142
+ h = h.to_f
143
+ s = s.to_f
144
+ v = v.to_f
145
+
146
+ if s == 0 and h == 0
147
+ v = (255*v).floor
148
+ return v, v, v
149
+ end
150
+
151
+ if h == 360
152
+ h = 0
153
+ end
154
+
155
+ h /= 60.0
156
+ i = h.floor.to_i
157
+ f = h - i
158
+
159
+ p = (255*v*(1 - s)).floor
160
+ q = (255*v*(1 - s*f)).floor
161
+ t = (255*v*(1 - s*(1 - f))).floor
162
+ v = (255*v).floor
163
+
164
+ case i
165
+ when 0
166
+ return v, t, p
167
+ when 1
168
+ return q, v, p
169
+ when 2
170
+ return p, v, t
171
+ when 3
172
+ return p, q, v
173
+ when 4
174
+ return t, p, v
175
+ when 5
176
+ return v, p, q
177
+ end
178
+ end
179
+
180
+ end
@@ -0,0 +1,26 @@
1
+ require "tempfile"
2
+
3
+ class GrADS::Command
4
+
5
+ def save_image (filename, size=640, dpi=300)
6
+ io = Tempfile.open("CA_GrADS_", ".")
7
+ basename = io.path
8
+ enable(:print, basename) {
9
+ print
10
+ }
11
+ case filename
12
+ when /\.eps/
13
+ system %{
14
+ gxeps -R -i #{basename} -o #{filename}
15
+ }
16
+ else
17
+ system %{
18
+ gxeps -R -i #{basename} -o #{basename}.eps
19
+ convert -density #{dpi} -resize #{size} +antialias #{basename}.eps #{filename}
20
+ rm -f #{basename}.eps
21
+ }
22
+ end
23
+ io.close
24
+ end
25
+
26
+ end
@@ -0,0 +1,32 @@
1
+
2
+ class GrADS::Command
3
+
4
+ def xcbar_with_ccols (clevs, ccols, *xcbaropts)
5
+ import :xcbar
6
+ nlevs = clevs.length
7
+ levcol = []
8
+ nlevs.times do |i|
9
+ levcol << ccols[i]
10
+ levcol << clevs[i]
11
+ end
12
+ levcol << ccols.last
13
+ set :clevs, *clevs
14
+ set :ccols, *ccols
15
+ xcbar *[xcbaropts,"-levcol #{levcol.join(' ')}"].flatten
16
+ end
17
+
18
+ def xcbar_with_color (colorspec, *xcbaropts)
19
+ import :xcbar, :color
20
+ nlevs = clevs.length
21
+ levcol = []
22
+ nlevs.times do |i|
23
+ levcol << ccols[i]
24
+ levcol << clevs[i]
25
+ end
26
+ levcol << ccols.last
27
+ set :clevs, *clevs
28
+ set :ccols, *ccols
29
+ xcbar *[xcbaropts,"-levcol #{levcol.join(' ')}"].flatten
30
+ end
31
+
32
+ end
data/lib/grads.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "carray"
2
+
3
+ module GrADS
4
+ end
5
+
6
+ require "grads/gridded"
7
+ require "grads/command"
8
+
9
+ require "grads/lib/makecpt"
10
+ require "grads/lib/colorbar"
11
+ require "grads/lib/save_image"
@@ -0,0 +1,23 @@
1
+
2
+ Gem::Specification::new do |s|
3
+ version = "1.0.0"
4
+
5
+ files = Dir.glob("**/*") - [
6
+ Dir.glob("carray*.gem"),
7
+ ].flatten
8
+
9
+ s.platform = Gem::Platform::RUBY
10
+ s.name = "ruby-grads"
11
+ s.summary = "Library for driving GrADS from Ruby"
12
+ s.description = <<-HERE
13
+ Library for driving GrADS from Ruby
14
+ HERE
15
+ s.version = version
16
+ s.author = "Hiroki Motoyoshi"
17
+ s.email = ""
18
+ s.homepage = 'https://github.com/himotoyoshi/ruby-grads'
19
+ s.files = files
20
+ # s.extensions = [ "extconf.rb" ]
21
+ s.has_rdoc = false
22
+ s.required_ruby_version = ">= 1.8.1"
23
+ end
data/test/gradsdraw.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "grads"
2
+
3
+ GrADS.start {
4
+ draw line, 1, 2, 4, 5
5
+ draw rec, 1, 2, 4, 5
6
+ draw recf, 4, 5, 7, 8
7
+ gets
8
+ clear
9
+ draw polyf, 1, 1, 2, 1, 3, 2, 2, 2, 1, 3
10
+ gets
11
+ }
12
+
data/test/test.rb ADDED
@@ -0,0 +1,41 @@
1
+ require "carray"
2
+ require "grads"
3
+ require "grads/lib/makecpt"
4
+ require "grads/lib/colorbar"
5
+ require "grads/lib/save_image"
6
+
7
+ x = CArray.float(301).span(-2.5..2.5)[nil,:*]
8
+ y = CArray.float(301).span(-2.5..2.5)[:*,nil]
9
+
10
+ xx = x.bind_with(y)
11
+ yy = y.bind_with(x)
12
+
13
+ z = (-(xx**2+yy**2)).exp*2500-300
14
+ th = CAMath.atan2(yy,xx)
15
+ z += (6*th).cos*300
16
+
17
+ GrADS.start {
18
+
19
+ import :cbarn
20
+
21
+ set :mproj, :off
22
+ set :parea, 3, 8, 2, 7
23
+ set :grid, :off
24
+
25
+ zz = template2d(nil, z) {
26
+ xdef x
27
+ ydef y
28
+ }
29
+
30
+ makecpt "-Ccopper -T0/2500/500"
31
+
32
+ set :gxout, :shaded
33
+
34
+ d zz
35
+
36
+ colorbar
37
+
38
+ save_image("test.png")
39
+
40
+ STDIN.gets
41
+ }
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-grads
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroki Motoyoshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " Library for driving GrADS from Ruby\n"
14
+ email: ''
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - extconf.rb
21
+ - lib/grads.rb
22
+ - lib/grads/command.rb
23
+ - lib/grads/gridded.rb
24
+ - lib/grads/lib/colorbar.rb
25
+ - lib/grads/lib/makecpt.rb
26
+ - lib/grads/lib/save_image.rb
27
+ - lib/grads/lib/xcbar_with_ccols.rb
28
+ - ruby-grads.gemspec
29
+ - test/gradsdraw.rb
30
+ - test/test.rb
31
+ homepage: https://github.com/himotoyoshi/ruby-grads
32
+ licenses: []
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.8.1
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.7.7
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Library for driving GrADS from Ruby
54
+ test_files: []