carray 1.3.6 → 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (315) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +32 -0
  3. data/LICENSE +22 -0
  4. data/NEWS.md +65 -0
  5. data/README.md +23 -26
  6. data/Rakefile +31 -0
  7. data/TODO.md +17 -0
  8. data/carray.gemspec +24 -31
  9. data/{ca_iter_block.c → ext/ca_iter_block.c} +16 -18
  10. data/{ca_iter_dimension.c → ext/ca_iter_dimension.c} +20 -21
  11. data/{ca_iter_window.c → ext/ca_iter_window.c} +12 -14
  12. data/{ca_obj_array.c → ext/ca_obj_array.c} +451 -176
  13. data/{ca_obj_bitarray.c → ext/ca_obj_bitarray.c} +18 -23
  14. data/{ca_obj_bitfield.c → ext/ca_obj_bitfield.c} +12 -16
  15. data/{ca_obj_block.c → ext/ca_obj_block.c} +47 -54
  16. data/{ca_obj_fake.c → ext/ca_obj_fake.c} +10 -12
  17. data/{ca_obj_farray.c → ext/ca_obj_farray.c} +21 -23
  18. data/{ca_obj_field.c → ext/ca_obj_field.c} +30 -32
  19. data/{ca_obj_grid.c → ext/ca_obj_grid.c} +32 -33
  20. data/{ca_obj_mapping.c → ext/ca_obj_mapping.c} +11 -13
  21. data/{ca_obj_object.c → ext/ca_obj_object.c} +40 -42
  22. data/{ca_obj_reduce.c → ext/ca_obj_reduce.c} +3 -5
  23. data/{ca_obj_refer.c → ext/ca_obj_refer.c} +38 -40
  24. data/{ca_obj_repeat.c → ext/ca_obj_repeat.c} +45 -47
  25. data/{ca_obj_select.c → ext/ca_obj_select.c} +4 -6
  26. data/{ca_obj_shift.c → ext/ca_obj_shift.c} +26 -28
  27. data/{ca_obj_transpose.c → ext/ca_obj_transpose.c} +26 -28
  28. data/{ca_obj_unbound_repeat.c → ext/ca_obj_unbound_repeat.c} +106 -160
  29. data/{ca_obj_window.c → ext/ca_obj_window.c} +33 -35
  30. data/{carray.h → ext/carray.h} +90 -59
  31. data/{carray_access.c → ext/carray_access.c} +244 -109
  32. data/{carray_attribute.c → ext/carray_attribute.c} +161 -207
  33. data/{carray_call_cfunc.c → ext/carray_call_cfunc.c} +1 -3
  34. data/{carray_cast.c → ext/carray_cast.c} +350 -149
  35. data/{carray_cast_func.rb → ext/carray_cast_func.rb} +1 -2
  36. data/{carray_class.c → ext/carray_class.c} +28 -36
  37. data/{carray_conversion.c → ext/carray_conversion.c} +58 -70
  38. data/{carray_copy.c → ext/carray_copy.c} +34 -50
  39. data/{carray_core.c → ext/carray_core.c} +75 -62
  40. data/ext/carray_data_type.c +66 -0
  41. data/{carray_element.c → ext/carray_element.c} +34 -53
  42. data/{carray_generate.c → ext/carray_generate.c} +137 -50
  43. data/{carray_iterator.c → ext/carray_iterator.c} +53 -53
  44. data/{carray_loop.c → ext/carray_loop.c} +77 -106
  45. data/{carray_mask.c → ext/carray_mask.c} +95 -114
  46. data/{carray_math.rb → ext/carray_math.rb} +47 -31
  47. data/ext/{mathfunc/carray_mathfunc.c → carray_mathfunc.c} +1 -3
  48. data/{carray_numeric.c → ext/carray_numeric.c} +43 -46
  49. data/{carray_operator.c → ext/carray_operator.c} +72 -36
  50. data/{carray_order.c → ext/carray_order.c} +73 -217
  51. data/{carray_sort_addr.c → ext/carray_sort_addr.c} +14 -21
  52. data/{carray_stat.c → ext/carray_stat.c} +6 -8
  53. data/{carray_stat_proc.rb → ext/carray_stat_proc.rb} +25 -27
  54. data/{carray_test.c → ext/carray_test.c} +59 -51
  55. data/{carray_undef.c → ext/carray_undef.c} +1 -11
  56. data/{carray_utils.c → ext/carray_utils.c} +12 -4
  57. data/{extconf.rb → ext/extconf.rb} +1 -6
  58. data/{mkmath.rb → ext/mkmath.rb} +13 -3
  59. data/{ruby_carray.c → ext/ruby_carray.c} +20 -8
  60. data/{ruby_ccomplex.c → ext/ruby_ccomplex.c} +1 -3
  61. data/{ruby_float_func.c → ext/ruby_float_func.c} +1 -3
  62. data/ext/version.h +16 -0
  63. data/{version.rb → ext/version.rb} +0 -0
  64. data/lib/carray.rb +52 -10
  65. data/lib/carray/{base/autoload.rb → autoload.rb} +48 -8
  66. data/lib/carray/autoload/autoload_base.rb +1 -5
  67. data/lib/carray/autoload/autoload_gem_cairo.rb +9 -0
  68. data/lib/carray/autoload/autoload_gem_ffi.rb +9 -0
  69. data/lib/carray/autoload/autoload_gem_gnuplot.rb +2 -0
  70. data/lib/carray/autoload/autoload_gem_io_csv.rb +14 -0
  71. data/lib/carray/autoload/autoload_gem_io_pg.rb +6 -0
  72. data/lib/carray/autoload/autoload_gem_io_sqlite3.rb +12 -0
  73. data/lib/carray/autoload/autoload_gem_narray.rb +10 -0
  74. data/lib/carray/autoload/autoload_gem_numo_narray.rb +15 -0
  75. data/lib/carray/autoload/autoload_gem_opencv.rb +16 -0
  76. data/lib/carray/autoload/autoload_gem_random.rb +8 -0
  77. data/lib/carray/autoload/autoload_gem_rmagick.rb +23 -0
  78. data/lib/carray/autoload/{autoload_graphics_zimg.rb → autoload_gem_zimg.rb} +0 -0
  79. data/lib/carray/basic.rb +191 -0
  80. data/lib/carray/broadcast.rb +82 -0
  81. data/lib/carray/compose.rb +315 -0
  82. data/lib/carray/construct.rb +460 -0
  83. data/lib/carray/convert.rb +115 -0
  84. data/lib/carray/info.rb +1 -3
  85. data/lib/carray/{base/inspect.rb → inspect.rb} +9 -11
  86. data/lib/carray/io/imagemagick.rb +2 -4
  87. data/lib/carray/{base/iterator.rb → iterator.rb} +6 -6
  88. data/lib/carray/mask.rb +102 -0
  89. data/lib/carray/{base/math.rb → math.rb} +21 -53
  90. data/lib/carray/math/histogram.rb +8 -10
  91. data/lib/carray/math/recurrence.rb +1 -3
  92. data/lib/carray/mkmf.rb +9 -3
  93. data/lib/carray/object/ca_obj_iterator.rb +1 -3
  94. data/lib/carray/object/ca_obj_link.rb +1 -3
  95. data/lib/carray/object/ca_obj_pack.rb +9 -11
  96. data/lib/carray/obsolete.rb +270 -0
  97. data/lib/carray/ordering.rb +181 -0
  98. data/lib/carray/{base/serialize.rb → serialize.rb} +50 -66
  99. data/lib/carray/string.rb +188 -0
  100. data/lib/carray/{base/struct.rb → struct.rb} +19 -21
  101. data/lib/carray/{io/table.rb → table.rb} +1 -9
  102. data/lib/carray/testing.rb +51 -0
  103. data/lib/carray/time.rb +76 -0
  104. data/lib/carray/transform.rb +98 -0
  105. data/misc/Methods.ja.md +182 -0
  106. data/{NOTE → misc/NOTE} +0 -0
  107. data/spec/Classes/CABitfield_spec.rb +58 -0
  108. data/spec/Classes/CABlockIterator_spec.rb +114 -0
  109. data/spec/Classes/CABlock_spec.rb +205 -0
  110. data/spec/Classes/CAField_spec.rb +39 -0
  111. data/spec/Classes/CAGrid_spec.rb +75 -0
  112. data/spec/Classes/CAMap_spec.rb +0 -0
  113. data/{test/test_CAMapping.rb → spec/Classes/CAMapping_spec.rb} +35 -36
  114. data/spec/Classes/CAObject_attribute_spec.rb +33 -0
  115. data/spec/Classes/CAObject_spec.rb +33 -0
  116. data/spec/Classes/CARefer_spec.rb +93 -0
  117. data/spec/Classes/CARepeat_spec.rb +65 -0
  118. data/spec/Classes/CASelect_spec.rb +22 -0
  119. data/spec/Classes/CAShift_spec.rb +16 -0
  120. data/spec/Classes/CAStruct_spec.rb +71 -0
  121. data/{test/test_CATranspose.rb → spec/Classes/CATranspose_spec.rb} +20 -21
  122. data/spec/Classes/CAUnboudRepeat_spec.rb +102 -0
  123. data/spec/Classes/CAWindow_spec.rb +54 -0
  124. data/spec/Classes/CAWrap_spec.rb +8 -0
  125. data/{test/test_CArray.rb → spec/Classes/CArray_spec.rb} +48 -92
  126. data/spec/Classes/CScalar_spec.rb +55 -0
  127. data/spec/Features/feature_130_spec.rb +19 -0
  128. data/spec/Features/feature_attributes_spec.rb +280 -0
  129. data/spec/Features/feature_boolean_spec.rb +98 -0
  130. data/spec/Features/feature_broadcast.rb +116 -0
  131. data/spec/Features/feature_cast_function.rb +19 -0
  132. data/spec/Features/feature_cast_spec.rb +33 -0
  133. data/spec/Features/feature_class_spec.rb +84 -0
  134. data/spec/Features/feature_complex_spec.rb +42 -0
  135. data/{test/test_composite.rb → spec/Features/feature_composite_spec.rb} +17 -18
  136. data/spec/Features/feature_convert_spec.rb +46 -0
  137. data/spec/Features/feature_copy_spec.rb +123 -0
  138. data/spec/Features/feature_creation_spec.rb +84 -0
  139. data/spec/Features/feature_element_spec.rb +144 -0
  140. data/spec/Features/feature_extream_spec.rb +54 -0
  141. data/spec/Features/feature_generate_spec.rb +74 -0
  142. data/spec/Features/feature_index_spec.rb +69 -0
  143. data/spec/Features/feature_mask_spec.rb +580 -0
  144. data/spec/Features/feature_math_spec.rb +97 -0
  145. data/spec/Features/feature_order_spec.rb +146 -0
  146. data/spec/Features/feature_ref_store_spec.rb +209 -0
  147. data/spec/Features/feature_serialization_spec.rb +125 -0
  148. data/spec/Features/feature_stat_spec.rb +397 -0
  149. data/spec/Features/feature_virtual_spec.rb +48 -0
  150. data/spec/Features/method_eq_spec.rb +81 -0
  151. data/spec/Features/method_is_nan_spec.rb +12 -0
  152. data/spec/Features/method_map_spec.rb +54 -0
  153. data/spec/Features/method_max_with.rb +20 -0
  154. data/spec/Features/method_min_with.rb +19 -0
  155. data/spec/Features/method_ne_spec.rb +18 -0
  156. data/spec/Features/method_project_spec.rb +188 -0
  157. data/spec/Features/method_ref_spec.rb +27 -0
  158. data/spec/Features/method_round_spec.rb +11 -0
  159. data/spec/Features/method_s_linspace_spec.rb +48 -0
  160. data/spec/Features/method_s_span_spec.rb +14 -0
  161. data/spec/Features/method_seq_spec.rb +47 -0
  162. data/spec/Features/method_sort_with.rb +43 -0
  163. data/spec/Features/method_sorted_with.rb +29 -0
  164. data/spec/Features/method_span_spec.rb +42 -0
  165. data/spec/Features/method_wrap_readonly_spec.rb +43 -0
  166. data/{test → spec/UnitTest}/test_CAVirtual.rb +0 -0
  167. data/spec/spec_all.rb +0 -1
  168. data/utils/convert_test.rb +73 -0
  169. data/utils/{extract_rdoc.rb → extract_yard.rb} +7 -12
  170. data/{devel → utils}/guess_shape.rb +0 -0
  171. data/utils/{diff_method.rb → monkey_patch_methods.rb} +17 -7
  172. metadata +173 -316
  173. data/COPYING +0 -56
  174. data/GPL +0 -340
  175. data/Gemfile +0 -8
  176. data/Gemfile.lock +0 -33
  177. data/LEGAL +0 -50
  178. data/Makefile +0 -451
  179. data/TODO +0 -5
  180. data/carray_cast_func.c +0 -8670
  181. data/carray_config.h +0 -26
  182. data/carray_math.c +0 -24805
  183. data/carray_random.c +0 -531
  184. data/carray_stat_proc.c +0 -12170
  185. data/ext/calculus/carray_calculus.c +0 -931
  186. data/ext/calculus/carray_interp.c +0 -358
  187. data/ext/calculus/extconf.rb +0 -12
  188. data/ext/calculus/lib/autoload/autoload_math_calculus.rb +0 -2
  189. data/ext/calculus/lib/math/calculus.rb +0 -119
  190. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +0 -31
  191. data/ext/fortio/extconf.rb +0 -3
  192. data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +0 -5
  193. data/ext/fortio/lib/carray/io/fortran_format.rb +0 -43
  194. data/ext/fortio/lib/fortio.rb +0 -3
  195. data/ext/fortio/lib/fortio/fortran_format.rb +0 -605
  196. data/ext/fortio/lib/fortio/fortran_format.tab.rb +0 -536
  197. data/ext/fortio/lib/fortio/fortran_format.y +0 -215
  198. data/ext/fortio/lib/fortio/fortran_namelist.rb +0 -151
  199. data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +0 -470
  200. data/ext/fortio/lib/fortio/fortran_namelist.y +0 -213
  201. data/ext/fortio/lib/fortio/fortran_sequential.rb +0 -345
  202. data/ext/fortio/ruby_fortio.c +0 -182
  203. data/ext/fortio/test/test_H.rb +0 -5
  204. data/ext/fortio/test/test_T.rb +0 -7
  205. data/ext/fortio/test/test_fortran_format.rb +0 -86
  206. data/ext/fortio/test/test_namelist.rb +0 -25
  207. data/ext/fortio/test/test_namelist_write.rb +0 -10
  208. data/ext/fortio/test/test_sequential.rb +0 -13
  209. data/ext/fortio/test/test_sequential2.rb +0 -13
  210. data/ext/fortio/work/test.rb +0 -10
  211. data/ext/fortio/work/test_e.rb +0 -19
  212. data/ext/fortio/work/test_ep.rb +0 -10
  213. data/ext/fortio/work/test_parse.rb +0 -12
  214. data/ext/imagemap/carray_imagemap.c +0 -495
  215. data/ext/imagemap/doc/call_graph.dot +0 -28
  216. data/ext/imagemap/draw.c +0 -567
  217. data/ext/imagemap/extconf.rb +0 -13
  218. data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +0 -1
  219. data/ext/imagemap/lib/graphics/imagemap.rb +0 -273
  220. data/ext/imagemap/lib/image_map.rb +0 -4
  221. data/ext/imagemap/test/swath_index.rb +0 -83
  222. data/ext/imagemap/test/swath_warp.rb +0 -99
  223. data/ext/imagemap/test/test.rb +0 -23
  224. data/ext/imagemap/test/test_image.rb +0 -42
  225. data/ext/imagemap/test/test_line.rb +0 -14
  226. data/ext/imagemap/test/test_rotate.rb +0 -17
  227. data/ext/imagemap/test/test_triangle.rb +0 -20
  228. data/ext/imagemap/test/test_warp.rb +0 -26
  229. data/ext/mathfunc/extconf.rb +0 -18
  230. data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +0 -1
  231. data/ext/mathfunc/lib/math/mathfunc.rb +0 -15
  232. data/ext/mathfunc/test/test_hypot.rb +0 -5
  233. data/ext/mathfunc/test/test_j0.rb +0 -22
  234. data/ext/mathfunc/test/test_jn.rb +0 -8
  235. data/ext/mathfunc/test/test_sph.rb +0 -9
  236. data/ext/narray/README +0 -22
  237. data/ext/narray/ca_wrap_narray.c +0 -500
  238. data/ext/narray/carray_narray.c +0 -21
  239. data/ext/narray/extconf.rb +0 -57
  240. data/ext/narray/lib/autoload/autoload_math_narray.rb +0 -1
  241. data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +0 -11
  242. data/ext/narray/lib/math/narray.rb +0 -17
  243. data/ext/narray/lib/math/narray_miss.rb +0 -45
  244. data/lib/carray/autoload/autoload_graphics_gnuplot.rb +0 -2
  245. data/lib/carray/autoload/autoload_io_csv.rb +0 -14
  246. data/lib/carray/autoload/autoload_io_pg.rb +0 -6
  247. data/lib/carray/autoload/autoload_io_sqlite3.rb +0 -12
  248. data/lib/carray/autoload/autoload_io_table.rb +0 -1
  249. data/lib/carray/autoload/autoload_math_interp.rb +0 -4
  250. data/lib/carray/base/basic.rb +0 -1090
  251. data/lib/carray/base/obsolete.rb +0 -131
  252. data/lib/carray/graphics/gnuplot.rb +0 -2131
  253. data/lib/carray/graphics/zimg.rb +0 -296
  254. data/lib/carray/io/csv.rb +0 -572
  255. data/lib/carray/io/pg.rb +0 -101
  256. data/lib/carray/io/sqlite3.rb +0 -215
  257. data/lib/carray/math/interp.rb +0 -57
  258. data/lib/carray/math/interp/adapter_gsl_spline.rb +0 -47
  259. data/mkmf.log +0 -375
  260. data/mt19937ar.c +0 -182
  261. data/mt19937ar.h +0 -86
  262. data/rdoc_ext.rb +0 -1115
  263. data/rdoc_main.rb +0 -27
  264. data/rdoc_math.rb +0 -5
  265. data/rdoc_stat.rb +0 -31
  266. data/spec/CABlockIterator/CABlockIterator_spec.rb +0 -113
  267. data/spec/CArray/bug/store_spec.rb +0 -27
  268. data/spec/CArray/index/repeat_spec.rb +0 -10
  269. data/spec/CArray/method/eq_spec.rb +0 -80
  270. data/spec/CArray/method/is_nan_spec.rb +0 -12
  271. data/spec/CArray/method/ne_spec.rb +0 -18
  272. data/spec/CArray/method/round_spec.rb +0 -11
  273. data/spec/CArray/object/_attribute_spec.rb +0 -32
  274. data/spec/CArray/object/s_new_spec.rb +0 -31
  275. data/spec/CArray/serialize/Serialization_spec.rb +0 -89
  276. data/test/test_130.rb +0 -23
  277. data/test/test_ALL.rb +0 -51
  278. data/test/test_CABitfield.rb +0 -59
  279. data/test/test_CABlock.rb +0 -208
  280. data/test/test_CAField.rb +0 -40
  281. data/test/test_CAGrid.rb +0 -76
  282. data/test/test_CAMmap.rb +0 -11
  283. data/test/test_CARefer.rb +0 -94
  284. data/test/test_CARepeat.rb +0 -66
  285. data/test/test_CASelect.rb +0 -23
  286. data/test/test_CAShift.rb +0 -17
  287. data/test/test_CAWindow.rb +0 -55
  288. data/test/test_CAWrap.rb +0 -9
  289. data/test/test_CComplex.rb +0 -83
  290. data/test/test_CScalar.rb +0 -91
  291. data/test/test_attribute.rb +0 -281
  292. data/test/test_block_iterator.rb +0 -17
  293. data/test/test_boolean.rb +0 -99
  294. data/test/test_cast.rb +0 -33
  295. data/test/test_class.rb +0 -85
  296. data/test/test_complex.rb +0 -43
  297. data/test/test_convert.rb +0 -79
  298. data/test/test_copy.rb +0 -141
  299. data/test/test_creation.rb +0 -85
  300. data/test/test_element.rb +0 -146
  301. data/test/test_extream.rb +0 -55
  302. data/test/test_generate.rb +0 -75
  303. data/test/test_index.rb +0 -71
  304. data/test/test_mask.rb +0 -578
  305. data/test/test_math.rb +0 -98
  306. data/test/test_narray.rb +0 -64
  307. data/test/test_order.rb +0 -147
  308. data/test/test_random.rb +0 -15
  309. data/test/test_ref_store.rb +0 -211
  310. data/test/test_stat.rb +0 -414
  311. data/test/test_struct.rb +0 -72
  312. data/test/test_virtual.rb +0 -49
  313. data/utils/create_rdoc.sh +0 -9
  314. data/utils/make_tgz.sh +0 -3
  315. data/version.h +0 -18
@@ -1,131 +0,0 @@
1
-
2
- class CArray
3
-
4
- def fa # :nodoc:
5
- warn "CArray#fa will be obsolete, use CArray#t"
6
- return self.t
7
- end
8
-
9
- def block_iterator (*argv) # :nodoc:
10
- warn "CArray#block_iterator will be obsolete, use CArray#blocks"
11
- return blocks(*argv)
12
- end
13
-
14
- def window_iterator (*argv) # :nodoc:
15
- warn "CArray#window_iterator will be obsolete, use CArray#windows"
16
- return windows(*argv)
17
- end
18
-
19
- def rotated (*argv) # :nodoc:
20
- warn "CArray#rotated will be obsolete, use CArray#rolled"
21
- argv.push({:roll => Array.new(rank){1} })
22
- return shifted(*argv)
23
- end
24
-
25
- def rotate! (*argv) # :nodoc:
26
- warn "CArray#rotate! will be obsolete, use CArray#roll!"
27
- self[] = self.rolled(*argv)
28
- return self
29
- end
30
-
31
- def rotate (*argv) # :nodoc:
32
- warn "CArray#rotate will be obsolete, use CArray#roll"
33
- return self.rolled(*argv).to_ca
34
- end
35
-
36
- def select (&block) # :nodoc:
37
- warn "CArray#select will be obsolete"
38
- case block.arity
39
- when 1
40
- return self[*yield(self)]
41
- when -1, 0
42
- return self[*instance_exec(&block)]
43
- else
44
- raise
45
- end
46
- end
47
-
48
- def transform (type, dim, opt = {}) # :nodoc:
49
- warn("CArray#transform will be obsolete")
50
- return refer(type, dim, opt).to_ca
51
- end
52
-
53
- def classify (klass, outlier = nil) # :nodoc:
54
- warn "CArray#classify will be obsolete"
55
- b = CArray.int32(*dim)
56
- f = CArray.boolean(*dim) { 1 }
57
- attach {
58
- (klass.elements-1).times do |i|
59
- r = f.and(self < klass[i+1])
60
- b[r] = i
61
- f[r] = 0
62
- end
63
- if outlier
64
- b[self < klass[0]] = outlier
65
- b[f] = outlier
66
- else
67
- b[self < klass[0]] = -1
68
- b[f] = klass.elements-1
69
- end
70
- }
71
- return b
72
- end
73
-
74
-
75
- =begin
76
- def histogram (klass)
77
- c = CArray.int32(klass.elements-1)
78
- f = CArray.boolean(*dim) { 1 }
79
- attach {
80
- k = 0
81
- r = f.and(self < klass[0])
82
- f[r] = 0
83
- (klass.elements-1).times do |i|
84
- r = f.and(self < klass[i+1])
85
- c[k] = r.count_true
86
- f[r] = 0
87
- k+=1
88
- end
89
- }
90
- return c
91
- end
92
- =end
93
-
94
- end
95
-
96
- class Numeric
97
-
98
- [
99
- :boolean,
100
- :int8,
101
- :uint8,
102
- :int16,
103
- :uint16,
104
- :int32,
105
- :uint32,
106
- :int64,
107
- :uint64,
108
- :float32,
109
- :float64,
110
- :float128,
111
- :cmplx64,
112
- :cmplx128,
113
- :cmplx256,
114
- :byte,
115
- :short,
116
- :int,
117
- :float,
118
- :double,
119
- :complex,
120
- :dcomplex,
121
- :object
122
- ].each do |name|
123
- class_eval %{
124
- def #{name} ()
125
- warn "Numeric##{name} will be obsolete"
126
- CScalar.new(#{name.inspect}) {self}
127
- end
128
- }
129
- end
130
-
131
- end
@@ -1,2131 +0,0 @@
1
- # ----------------------------------------------------------------------------
2
- #
3
- # carray/graphics/gnuplot.rb
4
- #
5
- # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
- #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
10
- #
11
- # ----------------------------------------------------------------------------
12
-
13
- require "date"
14
- require "time"
15
- require "open3"
16
- require "carray"
17
- require "ostruct"
18
-
19
- def CA.gnuplot (*argv, &block)
20
- CA::Gnuplot.new(*argv, &block)
21
- end
22
-
23
- class CA::Gnuplot # :nodoc:
24
-
25
- if File.directory?("/tmp")
26
- TMPDIR = "/tmp"
27
- else
28
- TMPDIR = "."
29
- end
30
-
31
- def initialize (command = "gnuplot", stdout: STDOUT, timezone: "", &block)
32
- begin
33
- @io, @stdout, @stderr = Open3.popen3(command + " -noraise")
34
- rescue NotImplementedError
35
- raise NotImplementedError, "system dosen't support Open3.popen3"
36
- end
37
-
38
- @defaults = {}
39
- @temp_file_count = 0
40
- @multiplot_mode = false
41
- @script_mode = false
42
- @pause_mode = :default
43
- @reset_mode = :default
44
- @init = nil
45
- @last = nil
46
- @listen = Thread.start {
47
- Thread.abort_on_exception = true
48
- begin
49
- stdout << @stdout.gets
50
- while line = @stdout.gets ### read(1024)
51
- # unless line =~ /\Agnuplot>/
52
- stdout << line
53
- stdout.flush
54
- # end
55
- end
56
- rescue IOError ### @stdout may externally closed
57
- end
58
- }
59
-
60
- @gnuplot_version = evaluate("GPVAL_VERSION").to_f
61
-
62
- epoch = evaluate %{strftime("%Y-%m-%d %H:%M:%S #{timezone}",0)}
63
- @EP_T = Time.parse(epoch)
64
- @EP_D = Date.parse(epoch)
65
- @EP_DT = DateTime.parse(epoch)
66
-
67
- @reset_script = reset_script() ## RESET_SCRIPT
68
-
69
- reset
70
- if block_given?
71
- begin
72
- case block.arity
73
- when 1
74
- yield(self)
75
- when -1, 0
76
- instance_exec(&block)
77
- else
78
- raise "invalid # of block parameters"
79
- end
80
- ensure
81
- self.close
82
- end
83
- end
84
- end
85
-
86
- attr_accessor :debug, :script_mode
87
- attr_reader :multiplot_mode, :multiplot_option, :last
88
-
89
- def close
90
- put("quit")
91
- @io.close
92
- @listen.join
93
- @stdout.close
94
- @stderr.close
95
- end
96
-
97
- def put (*args)
98
- lines = args.map {|s| s.respond_to?(:to_gnuplot) ? s.to_gnuplot : s.to_s }
99
- command = lines.join("\n")
100
- if @debug
101
- STDERR.puts command
102
- end
103
- thread = Thread.start {
104
- begin
105
- size = command.size
106
- s = 0
107
- while s < size
108
- s += @io.write(command[s, 2048])
109
- end
110
- @io.puts
111
- @io.puts %{ print "SIGNAL FOR CA::Gnuplot" }
112
- @io.flush
113
- rescue Errno::EPIPE
114
- end
115
- }
116
- output = ""
117
- while line = @stderr.gets
118
- case line.chomp
119
- when /SIGNAL FOR CA::Gnuplot$/
120
- STDERR.print(output) if @debug and not output.empty?
121
- return output.chomp
122
- when / line \d+: (.*?) /
123
- if $1 =~ /warning:/
124
- output << line
125
- STDERR.print output
126
- next
127
- else
128
- output << line
129
- STDERR.print output
130
- raise "Gnuplot Processor Error"
131
- end
132
- else
133
- output << line
134
- end
135
- end
136
- ensure
137
- thread.join
138
- end
139
-
140
- def reset_script
141
- # TO BE COMENNTED OUT
142
- # set size ratio 0 1,1
143
- # set origin 0,0
144
- # set lmargin -1
145
- # set bmargin -1
146
- # set rmargin -1
147
- # set tmargin -1
148
- # set locale "ja_JP.UTF-8"
149
- # set decimalsign
150
- # set encoding
151
- # set loadpath
152
- # set fontpath
153
- # set psdir
154
- # set fit brief errorvariables nocovariancevariables errorscaling prescale nowrap
155
- script = put(%{ save set "|cat 1>&2" }).split($/)
156
- script.delete_if {|s| s =~ /^set size ratio/ }
157
- script.delete_if {|s| s =~ /^set origin/ }
158
- script.delete_if {|s| s =~ /^set .margin/ }
159
- script.delete_if {|s| s =~ /^set locale/ }
160
- script.delete_if {|s| s =~ /^set decimalsign/ }
161
- script.delete_if {|s| s =~ /^set encoding/ }
162
- script.delete_if {|s| s =~ /^set loadpath/ }
163
- script.delete_if {|s| s =~ /^set fontpath/ }
164
- script.delete_if {|s| s =~ /^set psdir/ }
165
- script.delete_if {|s| s =~ /^set fit/ }
166
- return script.join($/)
167
- end
168
-
169
- def debug_on
170
- self.debug = true
171
- end
172
-
173
- def debug_off
174
- self.debug = false
175
- end
176
-
177
- def init (&block)
178
- @init = block
179
- @init.call
180
- return @init
181
- end
182
-
183
- def set (*argv)
184
- put "set " + argv.map{|s| s.gsub(/\n/,'') }.join(" ")
185
- end
186
-
187
- def unset (*argv)
188
- put "unset " + argv.map{|s| s.gsub(/\n/,'') }.join(" ")
189
- end
190
-
191
- def evaluate (expr)
192
- return put("print #{expr}")
193
- end
194
-
195
- def color_style (fgcolor, bgcolor = nil, framecolor = nil, shadowcolor = nil)
196
- if fgcolor
197
- @defaults[:fgcolor] = fgcolor
198
- put %{ fgcolor = "#{fgcolor}" }
199
- end
200
- if bgcolor
201
- @defaults[:bgcolor] = bgcolor
202
- put %{ bgcolor = "#{bgcolor}" }
203
- end
204
- if framecolor
205
- @defaults[:framecolor] = framecolor
206
- put %{ framecolor = "#{framecolor}" }
207
- end
208
- if shadowcolor
209
- @defaults[:shadowcolor] = shadowcolor
210
- put %{ shadowcolor = "#{shadowcolor}" }
211
- end
212
- end
213
-
214
- def framecolor (spec)
215
- @defaults[:framecolor] = spec
216
- put %{ framecolor = "#{spec}" }
217
- end
218
-
219
- alias frame_color framecolor
220
-
221
- def shadowcolor (spec)
222
- @defaults[:shadowcolor] = spec
223
- put %{ shadowcolor = "#{spec}" }
224
- end
225
-
226
- alias shadow_color shadowcolor
227
-
228
- def bgcolor (spec)
229
- @defaults[:bgcolor] = spec
230
- put %{ bgcolor = "#{spec}" }
231
- end
232
-
233
- alias bg_color bgcolor
234
-
235
- def fgcolor (spec)
236
- @defaults[:fgcolor] = spec
237
- put %{ fgcolor = "#{spec}" }
238
- end
239
-
240
- alias fg_color fgcolor
241
-
242
- def color_scheme (*names)
243
- if @gnuplot_version >= 5.0
244
- case names.first
245
- when :keynote
246
- put %{
247
- set linetype 1 lc rgb "#BB2C2F"
248
- set linetype 2 lc rgb "#5E9648"
249
- set linetype 3 lc rgb "#2E578B"
250
- set linetype 4 lc rgb "#6F3B77"
251
- set linetype 5 lc rgb "#002C63"
252
- set linetype 6 lc rgb "#E6A03D"
253
- set linetype 7 lc rgb "#7D807E"
254
- set linetype 8 lc rgb "#1B0C00"
255
- set linetype cycle 8
256
- }
257
- when :grads
258
- put %{
259
- set linetype 2 lc rgb "#fa3c3c"
260
- set linetype 3 lc rgb "#00dc00"
261
- set linetype 4 lc rgb "#1e3cff"
262
- set linetype 5 lc rgb "#00c8c8"
263
- set linetype 6 lc rgb "#f00082"
264
- set linetype 7 lc rgb "#e6dc32"
265
- set linetype 8 lc rgb "#f08228"
266
- set linetype 9 lc rgb "#a000c8"
267
- set linetype 10 lc rgb "#a0e632"
268
- set linetype 11 lc rgb "#00a0ff"
269
- set linetype 12 lc rgb "#e6af2d"
270
- set linetype 13 lc rgb "#00d28c"
271
- set linetype 14 lc rgb "#8200dc"
272
- set linetype 15 lc rgb "#aaaaaa"
273
- set linetype cycle 15
274
- }
275
- when :podo
276
- set %{ colorsequence podo }
277
- when :classic
278
- set %{ colorsequence classic }
279
- when :default
280
- set %{ colorsequence default }
281
- else
282
- style = []
283
- names.each_with_index do |col, i|
284
- style << %{ set linetype #{i+1} lc rgb "#{col.to_s}"}
285
- end
286
- put style.join("\n")
287
- end
288
- else
289
- case names.first
290
- when :keynote
291
- put %{
292
- set style increment user
293
- set style line 1 lc rgb "#BB2C2F"
294
- set style line 2 lc rgb "#5E9648"
295
- set style line 3 lc rgb "#2E578B"
296
- set style line 4 lc rgb "#6F3B77"
297
- set style line 5 lc rgb "#002C63"
298
- set style line 6 lc rgb "#E6A03D"
299
- set style line 7 lc rgb "#7D807E"
300
- set style line 8 lc rgb "#1B0C00"
301
- }
302
- when :grads
303
- put %{
304
- set style increment user
305
- set style line 2 lc rgb "#fa3c3c"
306
- set style line 3 lc rgb "#00dc00"
307
- set style line 4 lc rgb "#1e3cff"
308
- set style line 5 lc rgb "#00c8c8"
309
- set style line 6 lc rgb "#f00082"
310
- set style line 7 lc rgb "#e6dc32"
311
- set style line 8 lc rgb "#f08228"
312
- set style line 9 lc rgb "#a000c8"
313
- set style line 10 lc rgb "#a0e632"
314
- set style line 11 lc rgb "#00a0ff"
315
- set style line 12 lc rgb "#e6af2d"
316
- set style line 13 lc rgb "#00d28c"
317
- set style line 14 lc rgb "#8200dc"
318
- set style line 15 lc rgb "#aaaaaa"
319
- }
320
- else
321
- style = ["set style increment user"]
322
- names.each_with_index do |col, i|
323
- style << %{ set style line #{i+1} lc rgb "#{col.to_s}"}
324
- end
325
- put style.join("\n")
326
- end
327
- end
328
- end
329
-
330
-
331
- def pause_mode (arg = nil)
332
- # :none - no pausing
333
- # :default - return on terminal
334
- # :mouse - mouse click on window
335
- # :keypress - keypress on window
336
- orig = @pause_mode
337
- if arg
338
- @pause_mode = arg
339
- else
340
- @pause_mode = :default
341
- end
342
- if block_given?
343
- begin
344
- yield
345
- ensure
346
- @pause_mode = orig
347
- end
348
- end
349
- end
350
-
351
- def pause (arg = -1)
352
- if @multiplot_mode
353
- return
354
- else
355
- case @pause_mode
356
- when :none
357
- return
358
- when :mouse
359
- pause_mouse
360
- when :keypress
361
- pause_keypress
362
- else # :key
363
- term = evaluate("GPVAL_TERM")
364
- case term
365
- when /x11/, /aqua/, /wxt/
366
- STDIN.gets
367
- end
368
- end
369
- end
370
- end
371
-
372
- def pause_mouse
373
- put "pause mouse"
374
- mx = evaluate "MOUSE_X"
375
- my = evaluate "MOUSE_Y"
376
- mk = evaluate "MOUSE_KEY"
377
- return [mx, my, mk]
378
- end
379
-
380
- def pause_keypress
381
- put "pause mouse keypress"
382
- mx = evaluate "MOUSE_X"
383
- my = evaluate "MOUSE_Y"
384
- mk = evaluate "MOUSE_KEY"
385
- return [mx, my, mk]
386
- end
387
-
388
- def reset_mode (arg = nil)
389
- orig = @reset_mode
390
- if arg
391
- @reset_mode = arg
392
- else
393
- @reset_mode = :default
394
- end
395
- if block_given?
396
- begin
397
- yield
398
- ensure
399
- @reset_mode = orig
400
- end
401
- end
402
- end
403
-
404
- def reset ()
405
- if @script_mode or @reset_mode == :none
406
- return
407
- end
408
- if @multiplot_mode and
409
- ( @multiplot_option[:layout] or @multiplot_option[:noreset] )
410
- put @reset_script
411
- put("set xyplane relative 0",
412
- "set key Left reverse noautotitle")
413
- else
414
- put "reset"
415
- put("set xyplane relative 0",
416
- "set key Left reverse noautotitle")
417
- end
418
- end
419
-
420
- def terminal (text)
421
- text = text.split("\n").map{|l| l.strip }.join(" ").strip
422
- put("set term #{text}")
423
- end
424
-
425
- def output (text)
426
- @output = text
427
- text = text.split("\n").map{|l| l.strip }.join(" ").strip
428
- put("set output '#{text}'")
429
- end
430
-
431
- def time (data, timezone = nil)
432
-
433
- if timezone
434
- epoch = evaluate %{strftime("%Y-%m-%d %H:%M:%S #{timezone}",0)}
435
- ep_t = Time.parse(epoch)
436
- ep_d = Date.parse(epoch)
437
- ep_dt = DateTime.parse(epoch)
438
- else
439
- ep_t = @EP_T
440
- ep_d = @EP_D
441
- ep_dt = @EP_DT
442
- end
443
-
444
- case data
445
- when Numeric
446
- data
447
- when String
448
- Time.parse(data) - @EP_T
449
- when Time
450
- data - ep_t
451
- when Date
452
- (data - ep_d)*86400
453
- when DateTime
454
- (data - ep_dt)*86400
455
- when CArray
456
- return data.convert(CA_DOUBLE){|x|
457
- case x
458
- when Numeric
459
- x
460
- when Time
461
- x - ep_t
462
- when Date
463
- (x - ep_d)*86400
464
- when DateTime
465
- (x - ep_dt)*86400
466
- when String
467
- Time.parse(x) - ep_t
468
- end
469
- }
470
- when Range
471
- time(data.first, timezone)..time(data.last, timezone)
472
- end
473
- end
474
-
475
- def csv (*args)
476
- list = []
477
- args.each do |arg|
478
- case arg
479
- when Array
480
- list.push(arg.to_ca)
481
- else
482
- list.push(arg)
483
- end
484
- end
485
- return CArray.join(:object, list).to_csv
486
- end
487
-
488
- private
489
-
490
- def with_tempfile (nfiles=1)
491
- tempfile = Array.new(nfiles) {
492
- @temp_file_count += 1
493
- File.join(TMPDIR, "CA_Gnuplot_#{$$}_#{@temp_file_count}.dat")
494
- }
495
- yield(*tempfile)
496
- ensure
497
- tempfile.each do |file|
498
- if File.exist?(file)
499
- File.unlink(file)
500
- end
501
- end
502
- end
503
-
504
- def parse_args (argv, &block)
505
- opt = @defaults.clone
506
- if argv.size >= 1 and argv.last.is_a?(Hash)
507
- opt.update(argv.pop)
508
- elsif argv.size >= 1 and argv.last.is_a?(Option)
509
- opt = argv.pop
510
- end
511
- if block
512
- block.call(argv)
513
- end
514
- plots = argv.clone
515
- if opt.is_a? Option
516
- return plots, opt
517
- else
518
- return plots, Option.new(self, opt)
519
- end
520
- end
521
-
522
- def parse_data (list)
523
- list = list.clone
524
- conf = []
525
- while not list.empty? and
526
- ( list.last.is_a?(Integer) or
527
- list.last.is_a?(Symbol) or
528
- list.last.is_a?(String) or
529
- list.last.nil? )
530
- conf.unshift(list.pop)
531
- end
532
- if conf.last.is_a?(Integer)
533
- idx = conf.pop
534
- case idx
535
- when 1,2
536
- iy = idx
537
- axis = "x1y#{iy}"
538
- when 11,12,21,22
539
- ix = idx / 10
540
- iy = idx % 10
541
- axis = "x#{ix}y#{iy}"
542
- else
543
- raise "unknown axis specification"
544
- end
545
- end
546
- if conf.last.is_a?(Symbol)
547
- axis = conf.pop.to_s
548
- end
549
- using_ok = false
550
- using = nil
551
- if (conf.size == 2 or conf.size == 3 or conf.size == 4)
552
- if conf.first =~ /^every /
553
- dataspec = conf.shift
554
- elsif conf.first =~ /^using /
555
- using = conf.shift
556
- using_ok = true
557
- else
558
- dataspec = nil
559
- end
560
- else
561
- dataspec = nil
562
- end
563
- title, with = *conf
564
- if title =~ /\A\s*(col|column|columnhead|columnheader(\(.*?\)|))\s*\z/
565
- title = $1
566
- else
567
- title = %{"#{title}"}
568
- end
569
- return list, dataspec, using, title, with, axis
570
- end
571
-
572
- def histogram_tics (names)
573
- return names.to_a.map.with_index {|n,i| [i,n.to_s]}
574
- end
575
-
576
- public
577
-
578
- def plot_number
579
- return @multiplot_plot_number
580
- end
581
-
582
- def multiplot (option = {})
583
- @multiplot_mode = true
584
- @multiplot_option = option
585
- @multiplot_plot_number = 1
586
- @saved_init = @init
587
- options = ""
588
- if option[:layout]
589
- options << " layout " + option[:layout].join(",")
590
- if option[:columnsfirst]
591
- options << " columnsfirst "
592
- else
593
- options << " rowsfirst "
594
- end
595
- if option[:upwards]
596
- options << " upwards "
597
- else
598
- options << " downwards "
599
- end
600
- end
601
- if option[:scale]
602
- options << " scale " + option[:scale].join(",")
603
- end
604
- if option[:offset]
605
- options << " offset " + option[:offset].join(",")
606
- end
607
- if option[:title]
608
- options << " title '" + option[:title] + "'"
609
- end
610
- put( "set multiplot" + options )
611
- reset()
612
- yield
613
- ensure
614
- put( "unset multiplot" )
615
- @multiplot_option = nil
616
- @multiplot_mode = false
617
- @multiplot_plot_number = 0
618
- @init = @saved_init
619
- unless option[:nopause]
620
- pause
621
- end
622
- unless option[:noreset]
623
- reset
624
- end
625
- end
626
-
627
- def canvas (*argv, &block)
628
- plots, opt = parse_args(argv, &block)
629
- put %{
630
- unset xtics
631
- unset x2tics
632
- unset ytics
633
- unset y2tics
634
- unset xlabel
635
- unset ylabel
636
- unset x2label
637
- unset y2label
638
- unset key
639
- }
640
- opt.set(:margin, :title, :bgcolor,
641
- :xaxis, :yaxis, :x2axis, :y2axis,
642
- :border, :parametric,
643
- :options)
644
- put %{
645
- plot "-" with dots
646
- -1e30,-1e30
647
- e
648
- }
649
- if @multiplot_mode
650
- @multiplot_plot_number += 1
651
- end
652
- pause() unless opt[:nopause]
653
- reset() unless opt[:noreset]
654
- end
655
-
656
- def scanvas (*argv, &block)
657
- plots, opt = parse_args(argv, &block)
658
- put %{
659
- unset xtics
660
- unset x2tics
661
- unset ytics
662
- unset y2tics
663
- unset ztics
664
- unset xlabel
665
- unset ylabel
666
- unset zlabel
667
- unset x2label
668
- unset y2label
669
- unset key
670
- }
671
- opt.set(:margin, :title, :bgcolor,
672
- :xaxis, :yaxis, :zaxis,
673
- :border, :parametric,
674
- :options)
675
- put %{
676
- splot "-" with dots
677
- -1e30,-1e30,-1e30
678
- e
679
- }
680
- if @multiplot_mode
681
- @multiplot_plot_number += 1
682
- end
683
- pause() unless opt[:nopause]
684
- reset() unless opt[:noreset]
685
- end
686
-
687
- #
688
- # with csv file
689
- # ["foo.csv","1:2","title","with","y2"]
690
- #
691
- def blank (*argv, &block)
692
- plots, opt = parse_args(argv, &block)
693
- put %{
694
- unset xtics
695
- unset ytics
696
- unset x2tics
697
- unset y2tics
698
- unset xlabel
699
- unset ylabel
700
- unset x2label
701
- unset y2label
702
- unset border
703
- unset key
704
- unset grid
705
- }
706
- opt.set(:margin, :title, :bgcolor,
707
- :parametric,
708
- :options)
709
- put %{
710
- plot [0:1] [0:1] "-" with dots
711
- -1,-1
712
- e
713
- }
714
- if @multiplot_mode
715
- @multiplot_plot_number += 1
716
- end
717
- pause() unless opt[:nopause]
718
- reset() unless opt[:noreset]
719
- end
720
-
721
- #
722
- # with csv file
723
- # ["foo.csv","1:2","title","with","axes"]
724
- #
725
- # with csv string
726
- # ["1,2\n2,3\n4,5","1:2","title","with","axes"]
727
- #
728
- def plot (*argv, &block)
729
- @init.call if @init
730
- plots, opt = parse_args(argv, &block)
731
- @last = plots + [opt]
732
- with_tempfile(plots.size) { |*tempfile|
733
- plot_list = []
734
- plots.each_with_index do |arg, i|
735
- arg = arg.clone
736
- local_ranges = []
737
- loop do
738
- case arg.first
739
- when Range
740
- rng = arg.shift
741
- local_ranges << "[" + [rng.begin,rng.end].join(":") + "] "
742
- when Hash
743
- var,rng = arg.shift.first
744
- case var
745
- when :x
746
- local_ranges[0] = "[" + [rng.begin,rng.end].join(":") + "] "
747
- when :y
748
- local_ranges[1] = "[" + [rng.begin,rng.end].join(":") + "] "
749
- when :z
750
- local_ranges[2] = "[" + [rng.begin,rng.end].join(":") + "] "
751
- else
752
- local_ranges << "[#{var}=" + [rng.begin,rng.end].join(":") + "] "
753
- end
754
- else
755
- break
756
- end
757
- end
758
- if i == 0 and not local_ranges.empty?
759
- local_ranges.unshift "sample"
760
- end
761
- local_ranges = local_ranges.map{|v| v.nil? ? "[:]" : v }
762
- if arg.first.is_a?(Symbol) and arg.first == :newhistogram
763
- arg.shift
764
- arg, dataspec, using, title, with, axis = *parse_data(arg)
765
- plot_cmd = [
766
- "newhistogram",
767
- using ? using : "",
768
- title ? title : "",
769
- with ? with : "",
770
- ]
771
- elsif arg.first.is_a?(DataBlock)
772
- datablock = arg.shift
773
- if datablock.is_csv?
774
- put("set datafile separator ','")
775
- end
776
- arg, dataspec, using, title, with, axis = *parse_data(arg)
777
- plot_cmd = local_ranges + [
778
- datablock.name,
779
- dataspec ? dataspec : "",
780
- @gnuplot_version >= 4.4 ? "volatile" : "",
781
- opt[:using] ? "using " + opt[:using] :
782
- using ? using : "",
783
- axis ? "axes #{axis}" : "",
784
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
785
- title ? "title #{title}" : ""
786
- ]
787
- elsif arg.first.is_a?(String)
788
- file = arg.shift
789
- if file =~ /\.(png|jpg)\z/
790
- arg, dataspec, using, title, with, axis = *parse_data(arg)
791
- plot_cmd = local_ranges + [
792
- "'#{file}'",
793
- dataspec ? dataspec : "",
794
- "binary filetype=auto",
795
- @gnuplot_version >= 4.4 ? "volatile" : "",
796
- opt[:using] ? "using " + opt[:using] :
797
- using ? using : "",
798
- axis ? "axes #{axis}" : "",
799
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
800
- title ? "title #{title}" : ""
801
- ]
802
- else
803
- if file == ""
804
- elsif file =~ /,/ or file =~ /[\n\r]/
805
- if file =~ /,/
806
- put("set datafile separator ','")
807
- end
808
- file = file.gsub(/UNDEF/, "NaN")
809
- open(tempfile[i], "w") { |io| io.write file }
810
- file = tempfile[i]
811
- else
812
- unless File.exist?(file)
813
- raise "can't open file #{file} for plot2d"
814
- end
815
- end
816
- arg, dataspec, using, title, with, axis = *parse_data(arg)
817
- plot_cmd = local_ranges + [
818
- "'#{file}'",
819
- dataspec ? dataspec : "",
820
- @gnuplot_version >= 4.4 ? "volatile" : "",
821
- opt[:using] ? "using " + opt[:using] :
822
- using ? using : "",
823
- axis ? "axes #{axis}" : "",
824
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
825
- title ? "title #{title}" : ""
826
- ]
827
- end
828
- elsif arg.first.is_a?(Array)
829
- funcs, dataspec, using, title, with, axis = *parse_data(arg)
830
- plot_cmd = local_ranges + [
831
- funcs.join(","),
832
- dataspec ? dataspec : "",
833
- opt[:using] ? "using " + opt[:using] :
834
- using ? using : "",
835
- axis ? "axes #{axis}" : "",
836
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
837
- title ? "title #{title}" : ""
838
- ]
839
- else
840
- arg, dataspec, using, title, with, axis = *parse_data(arg)
841
- arg = arg.map{|x| CArray.wrap_readonly(x, CA_DOUBLE) }
842
- if with.to_s =~ /rgbimage/ or opt[:with].to_s =~ /rgbimage/
843
- if arg[2] and arg[2].rank >= 2 and
844
- ( arg[2].size != arg[0].size or arg[2].size != arg[1].size )
845
- xlen = arg[0].size
846
- ylen = arg[1].size
847
- arg[0] = arg[0][ylen,:%]
848
- arg[1] = arg[1][:%,xlen]
849
- if arg[2].rank == 3
850
- arg2 = arg[2]
851
- arg[2] = arg2[nil,nil,0]
852
- arg[3] = arg2[nil,nil,1]
853
- arg[4] = arg2[nil,nil,2]
854
- end
855
- end
856
- is_image = true
857
- is_rgb = true
858
- elsif with.to_s =~ /image/ or opt[:with].to_s =~ /image/
859
- if arg[2] and arg[2].rank == 2 and
860
- ( arg[2].size != arg[0].size or arg[2].size != arg[1].size )
861
- xlen = arg[0].size
862
- ylen = arg[1].size
863
- arg[0] = arg[0][ylen,:%]
864
- arg[1] = arg[1][:%,xlen]
865
- end
866
- is_image = true
867
- is_rgb = false
868
- else
869
- arg = arg.map{|x| x.rank > 1 ? x[nil] : x }
870
- is_image = false
871
- end
872
- out = CArray.merge(CA_DOUBLE, arg, -1)
873
- if is_image
874
- if is_rgb
875
- datalen = out.dim2
876
- if arg.size == 1
877
- datalen = 1
878
- array = "(" + [out.dim1,out.dim0].join(',') + ")"
879
- record = nil
880
- else
881
- datalen = 5
882
- array = nil
883
- record = "(" + [out.dim1,out.dim0].join(',') + ")"
884
- end
885
- else
886
- if arg.size == 1
887
- datalen = 1
888
- array = "(" + [out.dim1,out.dim0].join(',') + ")"
889
- record = nil
890
- else
891
- datalen = 3
892
- array = nil
893
- record = out.dim1*out.dim0
894
- end
895
- end
896
- else
897
- datalen = arg.size
898
- record = out.dim0
899
- array = nil
900
- end
901
- open(tempfile[i], "w") { |io|
902
- out.unmask_copy(0.0/0.0).dump_binary(io)
903
- }
904
- plot_cmd = local_ranges + [
905
- "'#{tempfile[i]}'",
906
- "binary",
907
- record ? "record=#{record}" : "",
908
- array ? "array=#{array}" : "",
909
- "format='#{'%double'*datalen}'",
910
- dataspec ? dataspec : "",
911
- @gnuplot_version >= 4.4 ? "volatile" : "",
912
- opt[:using] ? "using " + opt[:using] :
913
- using ? using :
914
- "using " + "#{(1..datalen).map{|x|'%i' % x}.join(':')}",
915
- axis ? "axes #{axis}" : "",
916
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
917
- title ? "title #{title}" : ""
918
- ]
919
- end
920
- plot_list.push(plot_cmd.join(" "))
921
- end
922
- opt.set(:margin, :title, :key, :bgcolor, :palette,
923
- :timefmt, :xaxis, :x2axis, :yaxis, :y2axis,
924
- :cbaxis, :grid, :border, :parametric,
925
- :options)
926
- put("plot " + plot_list.join(","))
927
- if @multiplot_mode
928
- @multiplot_plot_number += 1
929
- end
930
- pause() unless opt[:nopause]
931
- reset() unless opt[:noreset]
932
- }
933
- end
934
-
935
- alias scatter plot
936
- alias plot2d plot
937
-
938
- def splot (*argv, &block)
939
- @init.call if @init
940
- plots, opt = parse_args(argv, &block)
941
- @last = plots + [opt]
942
- with_tempfile(plots.size) { |*tempfile|
943
- plot_list = []
944
- plots.each_with_index do |arg, i|
945
- arg = arg.clone
946
- local_ranges = []
947
- loop do
948
- case arg.first
949
- when Range
950
- rng = arg.shift
951
- local_ranges << "[" + [rng.begin,rng.end].join(":") + "] "
952
- when Hash
953
- var,rng = arg.shift.first
954
- case var
955
- when :x
956
- local_ranges[0] = "[" + [rng.begin,rng.end].join(":") + "] "
957
- when :y
958
- local_ranges[1] = "[" + [rng.begin,rng.end].join(":") + "] "
959
- when :z
960
- local_ranges[2] = "[" + [rng.begin,rng.end].join(":") + "] "
961
- else
962
- local_ranges << "[#{var}=" + [rng.begin,rng.end].join(":") + "] "
963
- end
964
- else
965
- break
966
- end
967
- end
968
- if i == 0 and not local_ranges.empty?
969
- local_ranges.unshift "sample"
970
- end
971
- local_ranges = local_ranges.map{|v| v.nil? ? "[:]" : v }
972
- if arg.first.is_a?(DataBlock)
973
- datablock = arg.shift
974
- if datablock.is_csv?
975
- put("set datafile separator ','")
976
- end
977
- arg, dataspec, using, title, with, axis = *parse_data(arg)
978
- plot_cmd = local_ranges + [
979
- datablock.name,
980
- dataspec ? dataspec : "",
981
- @gnuplot_version >= 4.4 ? "volatile" : "",
982
- opt[:using] ? "using " + opt[:using] :
983
- using ? using : "",
984
- axis ? "axes #{axis}" : "",
985
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
986
- title ? "title #{title}" : ""
987
- ]
988
- elsif arg.first.is_a?(String)
989
- file = arg.shift
990
- if file =~ /\.(png|jpg)\z/
991
- arg, dataspec, using, title, with, axis = *parse_data(arg)
992
- plot_cmd = [
993
- "'#{file}'",
994
- dataspec ? dataspec : "",
995
- "binary filetype=auto",
996
- @gnuplot_version >= 4.4 ? "volatile" : "",
997
- opt[:using] ? "using " + opt[:using] :
998
- using ? using : "",
999
- axis ? "axes #{axis}" : "",
1000
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
1001
- title ? "title #{title}" : ""
1002
- ]
1003
- else
1004
- if file == ""
1005
- elsif file =~ /,/ or file =~ /[\n\r]/
1006
- if file =~ /,/
1007
- put("set datafile separator ','")
1008
- end
1009
- file = file.gsub(/UNDEF/, "NaN")
1010
- open(tempfile[i], "w") { |io| io.write file }
1011
- file = tempfile[i]
1012
- else
1013
- unless File.exist?(file)
1014
- raise "can't open file #{file} for splot"
1015
- end
1016
- end
1017
- arg, dataspec, using, title, with, axis = *parse_data(arg)
1018
- plot_cmd = [
1019
- "'#{file}'",
1020
- dataspec ? dataspec : "",
1021
- @gnuplot_version >= 4.4 ? "volatile" : "",
1022
- opt[:using] ? "using " + opt[:using] :
1023
- using ? using : "",
1024
- axis ? "axes #{axis}" : "",
1025
- with ? "with #{with}"
1026
- : opt[:with] ? "with #{opt[:with]}" : "",
1027
- title ? "title #{title}" : ""
1028
- ]
1029
- end
1030
- elsif arg.first.is_a?(Array)
1031
- funcs, dataspec, using, title, with, axis = *parse_data(arg)
1032
- plot_cmd = [
1033
- funcs.join(","),
1034
- dataspec ? dataspec : "",
1035
- opt[:using] ? "using " + opt[:using] :
1036
- using ? using : "",
1037
- axis ? "axes #{axis}" : "",
1038
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
1039
- title ? "title #{title}" : ""
1040
- ]
1041
- else
1042
- arg, dataspec, using, title, with, axis = *parse_data(arg)
1043
- arg = arg.map{|x| CArray.wrap_readonly(x, CA_DOUBLE) }
1044
- if arg[2] and arg[2].rank == 2 and
1045
- ( arg[2].size != arg[0].size or arg[2].size != arg[1].size )
1046
- xlen = arg[0].size
1047
- ylen = arg[1].size
1048
- arg[0] = arg[0][ylen,:%]
1049
- arg[1] = arg[1][:%,xlen]
1050
- is_image = true
1051
- elsif arg[0] and arg[0].rank == 2
1052
- is_image = true
1053
- elsif arg[0] and arg[0].rank == 3
1054
- is_image = true
1055
- else
1056
- is_image = false
1057
- end
1058
- out = CArray.merge(CA_DOUBLE, arg, -1)
1059
- if is_image
1060
- datalen = out.dim2
1061
- record = "(" + [out.dim1,out.dim0].join(",") + ")"
1062
- else
1063
- datalen = arg.size
1064
- record = out.dim0
1065
- end
1066
- open(tempfile[i], "w") { |io|
1067
- out.unmask_copy(0.0/0.0).dump_binary(io)
1068
- }
1069
- plot_cmd = local_ranges + [
1070
- "'#{tempfile[i]}'",
1071
- "binary",
1072
- record ? "record=#{record}" : "",
1073
- "format='#{'%double'*datalen}'",
1074
- dataspec ? dataspec : "",
1075
- @gnuplot_version >= 4.4 ? "volatile" : "",
1076
- opt[:using] ? "using " + opt[:using] :
1077
- using ? using :
1078
- "using " + "#{(1..datalen).map{|x|'%i' % x}.join(':')}",
1079
- axis ? "axes #{axis}" : "",
1080
- with ? "with #{with}" : opt[:with] ? "with #{opt[:with]}" : "",
1081
- title ? "title #{title}" : ""
1082
- ]
1083
- end
1084
- plot_list.push(plot_cmd.join(" "))
1085
- end
1086
- opt.set(:margin, :title, :key, :bgcolor, :view, :palette,
1087
- :timefmt, :xaxis, :yaxis, :zaxis,
1088
- :cbaxis, :border, :parametric, :options)
1089
- put("splot " + plot_list.join(","))
1090
- if @multiplot_mode
1091
- @multiplot_plot_number += 1
1092
- end
1093
- pause() unless opt[:nopause]
1094
- reset() unless opt[:noreset]
1095
- }
1096
- end
1097
-
1098
- alias scatter3d splot
1099
- alias mesh3d splot
1100
- alias grid3d splot
1101
-
1102
- def image (*argv, &block)
1103
- raise "image() will be obsolete, use plot(..., :with=>'(rgb)image')"
1104
- end
1105
-
1106
- def imagemap (*argv, &block)
1107
- raise "imagemap() will be obsolete, use plot(..., :with=>'(rgb)image')"
1108
- end
1109
-
1110
- #
1111
- # fit(function, params, data, options)
1112
- #
1113
- #
1114
- # with csv file
1115
- # ["foo.csv","1:2","title","with","axes"]
1116
- #
1117
- # with csv string
1118
- # ["1,2\n2,3\n4,5","1:2","title","with","axes"]
1119
- #
1120
- def fit (expr, params, data, opt = {})
1121
- with_tempfile(2) { |tempfile, logfile|
1122
- if data.first.is_a?(DataBlock)
1123
- datablock = data.shift
1124
- if datablock.is_csv?
1125
- put("set datafile separator ','")
1126
- end
1127
- using = data.shift
1128
- fitdata = [
1129
- datablock.name,
1130
- using ? "using " + using :
1131
- opt[:using] ? "using " + opt[:using] : "",
1132
- ]
1133
- elsif data.first.is_a?(String)
1134
- file = data.shift
1135
- if file =~ /,/ or file =~ /[\n\r]/
1136
- if file =~ /,/
1137
- put("set datafile separator ','")
1138
- end
1139
- open(tempfile, "w") { |io| io.write file }
1140
- file = tempfile
1141
- else
1142
- unless File.exist?(file)
1143
- raise "can't open file #{file} for plot2d"
1144
- end
1145
- end
1146
- using = data.shift
1147
- fitdata = [
1148
- "'#{file}'",
1149
- using ? "using " + using :
1150
- opt[:using] ? "using " + opt[:using] : "",
1151
- ]
1152
- else
1153
- arg = data.map{|x| CArray.wrap_readonly(x, CA_DOUBLE) }
1154
- if arg[2] and arg[2].rank == 2 and
1155
- ( arg[2].size != arg[0].size or arg[2].size != arg[1].size )
1156
- xlen = arg[0].size
1157
- ylen = arg[1].size
1158
- arg[0] = arg[0][ylen,:%]
1159
- arg[1] = arg[1][:%,xlen]
1160
- record = arg[0].dim1
1161
- elsif arg[0] and arg[0].rank == 2
1162
- record = arg[0].dim1
1163
- else
1164
- record = arg[0].dim0
1165
- end
1166
- datalen = arg.size
1167
- out = CArray.merge(CA_DOUBLE, arg, -1)
1168
- open(tempfile, "w") { |io|
1169
- out.unmask_copy(0.0/0.0).dump_binary(io)
1170
- }
1171
- fitdata = [
1172
- "'#{tempfile}'",
1173
- "binary",
1174
- record ? "record=#{record}" : "",
1175
- "format='#{'%double'*datalen}'",
1176
- "using " + ( opt[:using] ||
1177
- "#{(1..datalen).map{|x|'($%i)' % x}.join(':')}" ),
1178
- ]
1179
- end
1180
- if opt[:ranges]
1181
- ranges = opt[:ranges]
1182
- else
1183
- ranges = ""
1184
- end
1185
- set("fit logfile '#{logfile}' errorvariables covariancevariables")
1186
- put("fit #{ranges} #{expr} #{fitdata.join(" ")} via " + params.map{|x| x.to_s}.join(","))
1187
- res = OpenStruct.new
1188
- res.expression = expr
1189
- res.parameters = params
1190
- result_params = put("print " + params.map{|x| x.to_s}.join(",")).split.map{|_x| _x.to_f}
1191
- result_err = put("print " + params.map{|x| x.to_s + "_err"}.join(",")).split.map{|_x| _x.to_f}
1192
- result_cov = put("print " + params.map{|x| params.map{|y| "FIT_COV_" + x.to_s + "_" + y.to_s }}.join(",")).split.map{|_x| _x.to_f}
1193
- params.each_with_index do |name, i|
1194
- res.send(name.to_s+"=", result_params[i])
1195
- res.send(name.to_s+"_err=", result_err[i])
1196
- end
1197
- i=0
1198
- params.each do |x|
1199
- params.each do |y|
1200
- res.send("cov_" + x.to_s + "_" + y.to_s + "=", result_cov[i])
1201
- end
1202
- i+=1
1203
- end
1204
- res.log = File.read(logfile)
1205
- result = put("print FIT_NDF, FIT_WSSR, FIT_STDFIT, FIT_P")
1206
- res.ndf, res.wssr, res.stdfit, res.fit_p = result.split.map{|_x| _x.to_f}
1207
-
1208
- return result_params, res
1209
- }
1210
- end
1211
-
1212
- #
1213
- # my_palette = [[0, '#ffffff'],
1214
- # [1, '#909090'],
1215
- # [5, '#000090'],
1216
- # [10, '#000fff'],
1217
- # [25, '#0090ff'],
1218
- # [50, '#0fffee'],
1219
- # [75, '#90ff70'],
1220
- # [100,'#ffee00'],
1221
- # [200,'#ff7000'],
1222
- # [300,'#ee0000'],
1223
- # [400,'#7f0000']]
1224
- # index = gp.set_palette_discrete(depth, my_palette)
1225
- def set_palette_discrete (data, my_palette, option = {})
1226
- default = { :model=>"RGB",
1227
- :continuous=>false,
1228
- :lower=>nil,
1229
- :upper=>nil,
1230
- :lower_label=>false,
1231
- :upper_label=>false }
1232
- option = default.update(option)
1233
- model, continuous, lower, lower_label, upper, upper_label =
1234
- option.values_at(:model, :continuous, :lower, :lower_label, :upper, :upper_label)
1235
- levels = my_palette.size
1236
- scale = my_palette.map{|r| r[0] }
1237
- scalec = scale.clone
1238
- color = my_palette.map{|r| r[1] }
1239
- out = []
1240
- cbmin = 0
1241
- cbmax = levels - 1
1242
- maxcolors = levels-1
1243
- if continuous
1244
- if lower
1245
- out << [-1, %{'#{lower}'}]
1246
- cbmin = -1
1247
- maxcolors += 1
1248
- end
1249
- (levels).times do |i|
1250
- level = i
1251
- out << [level, %{'#{color[i]}'}]
1252
- end
1253
- if upper
1254
- out << [levels, %{'#{upper}'}]
1255
- cbmax = levels
1256
- maxcolors += 1
1257
- end
1258
- else
1259
- if lower
1260
- out << [-1, %{'#{lower}'}]
1261
- out << [0, %{'#{lower}'}]
1262
- cbmin = -1
1263
- maxcolors += 1
1264
- end
1265
- (levels-1).times do |i|
1266
- level = i
1267
- level1 = i+1
1268
- out << [level, %{'#{color[i]}'}]
1269
- out << [level1, %{'#{color[i]}'}]
1270
- end
1271
- if upper
1272
- out << [levels-1, %{'#{upper}'}]
1273
- out << [levels, %{'#{upper}'}]
1274
- cbmax = levels
1275
- maxcolors += 1
1276
- end
1277
- end
1278
- put ["set palette model #{model} maxcolors #{maxcolors} defined (\\",
1279
- out.map{|list| " " + list.join(" ")}.join(",\\\n") + "\\",
1280
- ")"].join("\n")
1281
- out = []
1282
- if lower and lower_label
1283
- out << [%{''{/Symbol <}#{scale.first}'}, cbmin]
1284
- end
1285
- scale.each_with_index do |s, i|
1286
- out << [%{'#{s}'}, i]
1287
- end
1288
- if upper and upper_label
1289
- out << [%{'{/Symbol \\263}#{scale.last}'}, cbmax]
1290
- end
1291
- put %{ set cbtics (#{out.map {|d| d.join(" ")}.join(",")}) }
1292
- put %{ set cbrange [#{cbmin}:#{cbmax}] }
1293
- return CA_DOUBLE(scale).section(data)
1294
- end
1295
-
1296
- #
1297
- def set_xtics_monthly (start, last, format: nil, interval: nil, linewidth: 1, style: "lc rgb 'black' back", grid: false)
1298
- unless format
1299
- format = "%b"
1300
- end
1301
- unless interval
1302
- interval = 3
1303
- end
1304
- case start
1305
- when String
1306
- start = DateTime.parse(start)
1307
- end
1308
- case last
1309
- when String
1310
- last = DateTime.parse(last)
1311
- end
1312
- if interval < 0
1313
- start = DateTime.parse(start.strftime("%Y-%m-01")) + 0.5
1314
- last = (DateTime.parse(last.strftime("%Y-%m-01"))) + 0.5 >> 1
1315
- else
1316
- start = DateTime.parse(start.strftime("%Y-%m-01"))
1317
- last = (DateTime.parse(last.strftime("%Y-%m-01"))) >> 1
1318
- end
1319
- set %{ xrange [#{time(start)}:#{time(last)}] }
1320
- if linewidth > 0
1321
- set %{ xtics ("" 0) scale 0,2 mirror }
1322
- set %{ x2tics ("" 0) scale 0,1 mirror }
1323
- else
1324
- set %{ xtics ("" 0) scale 0,0 mirror }
1325
- set %{ x2tics ("" 0) scale 0,0 mirror }
1326
- end
1327
- d = start
1328
- while d <= last
1329
- set %{ xtics add ('' #{time(d)} 1) }
1330
- set %{ xtics add ('#{d.strftime(format)}' #{time(d+15)} 0) }
1331
- case interval
1332
- when -2,2
1333
- set %{ x2tics add ('' #{time(d+15)} 1) }
1334
- when -3,3
1335
- set %{ x2tics add ('' #{time(d+10)} 1) }
1336
- set %{ x2tics add ('' #{time(d+20)} 1) }
1337
- when -6,6
1338
- set %{ x2tics add ('' #{time(d+5)} 1) }
1339
- set %{ x2tics add ('' #{time(d+10)} 1) }
1340
- set %{ x2tics add ('' #{time(d+15)} 1) }
1341
- set %{ x2tics add ('' #{time(d+20)} 1) }
1342
- set %{ x2tics add ('' #{time(d+25)} 1) }
1343
- else
1344
- end
1345
- d >>= 1
1346
- end
1347
- if grid and linewidth > 0
1348
- d = start >> 1
1349
- while d < last
1350
- set %{ arrow nohead from #{time(d)}, graph 0
1351
- rto graph 0, graph 1 #{style} }
1352
- d >>= 1
1353
- end
1354
- end
1355
- end
1356
-
1357
- class DataBlock
1358
-
1359
- @@datablock_count = 0
1360
-
1361
- def initialize (processor, *args)
1362
- put_ok = false
1363
- if args.size == 1 and args.first.is_a?(Symbol)
1364
- @name = "$" + args.first.to_s
1365
- @text = processor.put %{ print #{@name}}
1366
- elsif args.size == 1 and args.first.is_a?(String)
1367
- @@datablock_count += 1
1368
- @name = "$DBLK_#{@@datablock_count}"
1369
- @text = args.first.dup
1370
- put_ok = true
1371
- else
1372
- @@datablock_count += 1
1373
- @name = "$DBLK_#{@@datablock_count}"
1374
- @text = CArray.join(*args).to_csv
1375
- @text.gsub!(/UNDEF/,"NaN")
1376
- put_ok = true
1377
- end
1378
- @text.strip!
1379
- @text.chomp!
1380
- if put_ok
1381
- processor.put %{
1382
- #{@name} <<__EOD__
1383
- #{@text}
1384
- __EOD__
1385
- }
1386
- end
1387
- end
1388
-
1389
- def is_csv?
1390
- return (@text =~ /,/)
1391
- end
1392
-
1393
- def to_s
1394
- return @text.dup
1395
- end
1396
-
1397
- def to_ca
1398
- data = []
1399
- @text.each_line do |line|
1400
- case line
1401
- when /^#/
1402
- else
1403
- data << line.strip.split(/ +/)
1404
- end
1405
- end
1406
- return CA_OBJECT(data)
1407
- end
1408
-
1409
- attr_reader :name, :text
1410
-
1411
- end
1412
-
1413
- def datablock (*args)
1414
- return DataBlock.new(self, *args)
1415
- end
1416
-
1417
- class Option # :nodoc:
1418
-
1419
- def initialize (processor, option)
1420
- @processor = processor
1421
- @o = option
1422
- end
1423
-
1424
- def set (*names)
1425
- names.each do |name|
1426
- self.send("set_#{name}")
1427
- end
1428
- end
1429
-
1430
- def [] (name)
1431
- @o[name]
1432
- end
1433
-
1434
- def []= (name, value)
1435
- @o[name] = value
1436
- end
1437
-
1438
- def update (hash)
1439
- @o.update(hash)
1440
- end
1441
-
1442
- private
1443
-
1444
- def put (*lines)
1445
- return @processor.put(*lines)
1446
- end
1447
-
1448
- def evaluate (expr)
1449
- return @processor.evaluate(expr)
1450
- end
1451
-
1452
- def set_title
1453
- textcolor = @o[:fgcolor] ? "textcolor rgb \"#{@o[:fgcolor]}\"" : ""
1454
- if @o[:title]
1455
- put('set title "' + @o[:title] + '" ' + textcolor)
1456
- end
1457
- end
1458
-
1459
- def set_key
1460
- # ver = evaluate("GPVAL_VERSION")
1461
- # if ver.to_f <= 4.2
1462
- # tc = ""
1463
- # else
1464
- # tc = @o[:fgcolor] ? "tc rgb \"#{@o[:fgcolor]}\"" : ""
1465
- # end
1466
- # put("set key #{tc}")
1467
- end
1468
-
1469
- def set_bgcolor
1470
- if @o[:framecolor]
1471
- put "set object rect from screen 0, screen 0 " \
1472
- "to screen 1, screen 1 behind " \
1473
- "fc rgb '#{@o[:framecolor]}' " \
1474
- "fillstyle solid 1.0 noborder"
1475
- end
1476
- if @o[:shadowcolor]
1477
- put "set object rect from graph 0.01, graph -0.02 " \
1478
- "to graph 1.01, graph 0.98 behind " \
1479
- "fc rgb '#{@o[:shadowcolor]}' " \
1480
- "fillstyle solid 1.0 noborder"
1481
- end
1482
- if @o[:bgcolor]
1483
- put "set object rect from graph 0, graph 0 " \
1484
- "to graph 1, graph 1 behind " \
1485
- "fc rgb '#{@o[:bgcolor]}' " \
1486
- "fillstyle solid 1.0 noborder"
1487
- end
1488
- end
1489
-
1490
- def set_border
1491
- if @o[:noborder]
1492
- put("unset border")
1493
- end
1494
- if @o[:noaxis]
1495
- put("unset xtics")
1496
- put("unset ytics")
1497
- put("unset ztics")
1498
- put("unset xlabel")
1499
- put("unset ylabel")
1500
- put("unset zlabel")
1501
- return
1502
- end
1503
- if @o[:fgcolor]
1504
- put("set border linecolor rgb \"#{@o[:fgcolor]}\" ")
1505
- if @o[:zeroaxis]
1506
- put("set xzeroaxis linecolor rgb \"#{@o[:fgcolor]}\" ")
1507
- put("set yzeroaxis linecolor rgb \"#{@o[:fgcolor]}\" ")
1508
- end
1509
- else
1510
- if @o[:zeroaxis]
1511
- put("set xzeroaxis",
1512
- "set yzeroaxis")
1513
- end
1514
- end
1515
- end
1516
-
1517
- def set_axis(axis)
1518
- label, range, fmt, tics, mtics, ticsopts = *@o[axis]
1519
- logscale = @o["#{axis}log".intern]
1520
- timescale = @o["#{axis}time".intern]
1521
- reverse = @o["#{axis}reverse".intern]
1522
- textcolor = @o[:fgcolor] ? "textcolor rgb \"#{@o[:fgcolor]}\"" : ""
1523
- if label
1524
- put("set #{axis}label " + %{ "#{label} "} + textcolor)
1525
- else
1526
- put("unset #{axis}label")
1527
- end
1528
- if timescale
1529
- put("set #{axis}data time")
1530
- end
1531
- if range
1532
- case range
1533
- when Range
1534
- put("set #{axis}range [" + [range.begin,range.end].join(":") + "] " +
1535
- ( reverse ? "reverse " : "" ))
1536
- when Array
1537
- srange = range.join(":")
1538
- put("set #{axis}range [" + srange + "] " + ( reverse ? "reverse " : "" ))
1539
- else
1540
- raise "invalid range specification"
1541
- end
1542
- else
1543
- if reverse
1544
- put("set #{axis}range [*:*] reverse")
1545
- end
1546
- end
1547
- if tics
1548
- if tics.is_a?(Array)
1549
- ticslist = tics.collect do |v|
1550
- v = [v].flatten
1551
- case v.size
1552
- when 1
1553
- v.first.to_s
1554
- else
1555
- ['"'+v[1].to_s+'"', v[0], v[2]||0].join(" ")
1556
- end
1557
- end.join(",")
1558
- put("set #{axis}tics (#{ticslist}) #{textcolor} #{ticsopts}")
1559
- else
1560
- put("set #{axis}tics #{tics} #{textcolor} #{ticsopts}")
1561
- end
1562
- else
1563
- put("set #{axis}tics #{textcolor} #{ticsopts}")
1564
- end
1565
- if mtics
1566
- put("set m#{axis}tics #{mtics}")
1567
- end
1568
- if fmt
1569
- put(%{ set format #{axis} "#{fmt}"})
1570
- end
1571
- if logscale
1572
- put("set logscale #{axis}")
1573
- end
1574
- end
1575
-
1576
- def set_xaxis
1577
- set_axis(:x)
1578
- end
1579
-
1580
- def set_yaxis
1581
- set_axis(:y)
1582
- end
1583
-
1584
- def set_x2axis
1585
- if @o[:x2]
1586
- set_axis(:x2)
1587
- end
1588
- end
1589
-
1590
- def set_y2axis
1591
- if @o[:y2]
1592
- set_axis(:y2)
1593
- end
1594
- end
1595
-
1596
- def set_zaxis
1597
- set_axis(:z)
1598
- end
1599
-
1600
- def set_cbaxis
1601
- set_axis(:cb)
1602
- end
1603
-
1604
- def set_grid
1605
- if @o[:grid]
1606
- linecolor = @o[:fgcolor] ? "linecolor rgb \"#{@o[:fgcolor]}\"" : ""
1607
- case @o[:grid]
1608
- when Array
1609
- tics = @o[:grid].map{|a| "#{a}tics" }.join(" ")
1610
- put("set grid #{tics} back linetype 0 #{linecolor}")
1611
- else
1612
- put("set grid xtics ytics y2tics back linetype 0 #{linecolor}")
1613
- end
1614
- end
1615
- end
1616
-
1617
- def set_options
1618
- if @o[:ratio]
1619
- put("set size ratio #{@o[:ratio]}")
1620
- # put("set view equal xy")
1621
- end
1622
- end
1623
-
1624
- def set_timefmt
1625
- if @o[:timefmt]
1626
- put("set timefmt \"#{@o[:timefmt]}\"")
1627
- end
1628
- end
1629
-
1630
- def set_view
1631
- if @o[:view]
1632
- put("set view #{[@o[:view]].flatten.join(",")}")
1633
- end
1634
- end
1635
-
1636
- def set_parametric
1637
- if @o[:parametric]
1638
- put("set parametric")
1639
- end
1640
- end
1641
-
1642
- def set_margin
1643
- if @o[:floating] or
1644
- ( @processor.multiplot_mode and
1645
- ( @processor.multiplot_option[:layout] or
1646
- @processor.multiplot_option[:floating] ) )
1647
- if @o[:nomargin]
1648
- put("set lmargin 0",
1649
- "set rmargin 0",
1650
- "set tmargin 0",
1651
- "set bmargin 0")
1652
- end
1653
- else
1654
- put("set size 0.7, 0.7",
1655
- "set origin 0.15, 0.15",
1656
- "set lmargin 0",
1657
- "set rmargin 0",
1658
- "set tmargin 0",
1659
- "set bmargin 0")
1660
- end
1661
- end
1662
-
1663
- def set_palette (*argv)
1664
- case @o[:palette]
1665
- when Array
1666
- list = @o[:palette].clone
1667
- kind = list.shift
1668
- Palette.set(@processor, kind.intern, *list)
1669
- else
1670
- Palette.set(@processor, @o[:palette])
1671
- end
1672
- end
1673
-
1674
- end
1675
-
1676
- def palette (kind, *argv)
1677
- return Palette.set(self, kind, *argv)
1678
- end
1679
-
1680
- module Palette
1681
-
1682
- def self.quote (text)
1683
- case text
1684
- when Symbol
1685
- text.to_s
1686
- when /^\{\{(.*)\}\}$/
1687
- $1
1688
- else
1689
- text = text.clone
1690
- if text[0, 1] == "\\"
1691
- text[1..-1]
1692
- else
1693
- text.gsub!(/\n/, '\\n')
1694
- text.gsub!(/\t/, '\\t')
1695
- text.gsub!(/"/, '\\\\"')
1696
- '"' + text + '"'
1697
- end
1698
- end
1699
- end
1700
-
1701
- def self.set (gp, kind, *argv)
1702
- if argv.last.is_a?(Hash)
1703
- opt = argv.pop
1704
- else
1705
- opt = {}
1706
- end
1707
- if opt[:model]
1708
- gp.set %{ palette model #{opt[:model]} }
1709
- end
1710
- if opt[:maxcolors]
1711
- gp.set %{ palette maxcolors #{opt[:maxcolors]} }
1712
- end
1713
- case kind
1714
- when :defined
1715
- self.set_palette_array(gp, argv)
1716
- when :random
1717
- n = argv.first || 10
1718
- list = []
1719
- n.times do |i|
1720
- value = rand(256)*0x10000 + rand(256)*0x100 + rand(256)
1721
- list << [i, "#%6x" % value]
1722
- end
1723
- self.set_palette_array(gp, list)
1724
- when :rgbformulae
1725
- set_palette_formula(gp, *argv[0,3])
1726
- when :file
1727
- set_palette_file(gp, argv.first)
1728
- when :gmt
1729
- set_palette_gmt(gp, argv.first, opt)
1730
- when :cpt_city
1731
- set_palette_cpt_city(gp, argv.first, opt)
1732
- when Symbol
1733
- set_palette_predefined(gp, kind)
1734
- when String
1735
- set_palette_string(gp, kind)
1736
- when CArray
1737
- set_palette_carray(gp, kind)
1738
- when Array
1739
- self.set_palette_array(gp, kind)
1740
- when ColorPalette
1741
- set_palette_color_palette(gp, kind)
1742
- end
1743
- end
1744
-
1745
- def self.set_palette_formula (gp, *argv)
1746
- gp.set 'palette rgbformulae ' + argv.join(', ')
1747
- end
1748
-
1749
- def self.set_palette_file (gp, *argv)
1750
- gp.set 'palette file ' + quote(argv[0]) + " " + argv[1].to_s
1751
- end
1752
-
1753
- def self.set_palette_string (gp, string)
1754
- gp.set "palette " + string
1755
- end
1756
-
1757
- def self.set_palette_array (gp, array)
1758
- gp.set "palette " +
1759
- 'defined (' + array.map { |x|
1760
- case x[1]
1761
- when String
1762
- [x[0], quote(x[1])].join(' ')
1763
- else
1764
- x.join(' ')
1765
- end
1766
- }.join(', ') + ')'
1767
- end
1768
-
1769
- def self.set_palette_carray (gp, ca)
1770
- gp.instance_exec {
1771
- with_tempfile(1) { |tmpfile|
1772
- open(tmpfile, "w") {|io| ca.dump_binary(io)}
1773
- if ca.rank == 1 or ( ca.rank == 2 and ca.dim1 == 1)
1774
- set("palette file '#{tmpfile}' binary record=#{ca.dim0} using 1:1:1 model #{model}")
1775
- elsif ca.rank == 2 and ca.dim1 == 3
1776
- set("palette file '#{tmpfile}' binary record=#{ca.dim0} using 1:2:3 model #{model}")
1777
- else
1778
- raise "invalid palette specification"
1779
- end
1780
- }
1781
- }
1782
- end
1783
-
1784
- def self.set_palette_color_palette (gp, pal)
1785
- gp.put(pal.to_gnuplot)
1786
- end
1787
-
1788
- def self.set_palette_gmt (gp, name, opt)
1789
- cpt = ColorPalette::CPT(name, :continuous=>opt[:continuous])
1790
- if opt[:use_scale]
1791
- min, max = cpt.min, cpt.max
1792
- gp.set %{ cbrange [#{min}:#{max}] }
1793
- end
1794
- gp.put(cpt.to_gnuplot)
1795
- end
1796
-
1797
- def self.set_palette_cpt_city (gp, name, opt)
1798
- file = File.expand_path(File.join(ENV["CPTCITY"],name) + ".cpt")
1799
- cpt = ColorPalette::CPT(file, :continuous=>opt[:continuous])
1800
- if opt[:use_scale]
1801
- min, max = cpt.min, cpt.max
1802
- gp.set %{ cbrange [#{min}:#{max}] }
1803
- end
1804
- gp.put(cpt.to_gnuplot)
1805
- end
1806
-
1807
- def self.set_palette_predefined (gp, name)
1808
- case name.to_s
1809
- when "gray"
1810
- gp.set %{ palette defined (0 "#FFFFFF", \
1811
- 1 "#000000") }
1812
- when "gray_inv"
1813
- gp.set %{ palette defined (0 "#000000", \
1814
- 1 "#FFFFFF") }
1815
- when "rainbow"
1816
- gp.set %{ palette rgbformulae 22,13,-31 }
1817
- when "polar"
1818
- gp.set %{ palette defined (-1 "blue", \
1819
- 0 "white", \
1820
- 1 "red") }
1821
- when "jet"
1822
- gp.set %{ palette defined (0 "#00007F", \
1823
- 0.125 "#0000FF", \
1824
- 0.375 "#FFFFFF", \
1825
- 0.625 "#FFFF00", \
1826
- 0.875 "#FF0000", \
1827
- 1 "#7F0000") }
1828
- when "split"
1829
- gp.set %{ palette defined (-1.0 "#7F7FFF", \
1830
- -0.5 "#000080", \
1831
- 0.0 "#000000", \
1832
- 0.5 "#800000", \
1833
- 1.0 "#FF7F7F" ) }
1834
- when "green_metal"
1835
- gp.set %{ palette defined ( 0.0 "#000000", \
1836
- 0.5263 "#8D8D6C", \
1837
- 0.6842 "#92BDBE", \
1838
- 1.0 "#FFFFFF" )}
1839
- when "matlab"
1840
- gp.set %{ palette defined ( 0 '#000090', \
1841
- 1 '#000fff', \
1842
- 2 '#0090ff', \
1843
- 3 '#0fffee', \
1844
- 4 '#90ff70', \
1845
- 5 '#ffee00', \
1846
- 6 '#ff7000', \
1847
- 7 '#ee0000', \
1848
- 8 '#7f0000')}
1849
- when "matlabw"
1850
- gp.set %{ palette defined ( 0 'white',
1851
- 0.01 '#000090', \
1852
- 1 '#000fff', \
1853
- 2 '#0090ff', \
1854
- 3 '#0fffee', \
1855
- 4 '#90ff70', \
1856
- 5 '#ffee00', \
1857
- 6 '#ff7000', \
1858
- 7 '#ee0000', \
1859
- 8 '#7f0000')}
1860
- else
1861
- raise "Unknown palette name"
1862
- end
1863
- end
1864
-
1865
-
1866
- end
1867
-
1868
- class ColorPalette # :nodoc:
1869
-
1870
- def initialize (model, levels, color, scale = nil, continuous = false)
1871
- @model = model
1872
- @levels = levels
1873
- @color = color
1874
- @scale = scale || CArray.int(levels).seq!
1875
- @continuous = continuous
1876
- end
1877
-
1878
- attr_reader :scale, :colors
1879
-
1880
- def min
1881
- return @scale.min
1882
- end
1883
-
1884
- def max
1885
- return @scale.max
1886
- end
1887
-
1888
- def range
1889
- return @scale.min..@scale.max
1890
- end
1891
-
1892
- def to_gnuplot
1893
- out = []
1894
- maxcolors = 1024
1895
- if @continuous
1896
- (@levels).times do |i|
1897
- level = @scale[i]
1898
- out.push([level, *@color[i,nil].to_a])
1899
- end
1900
- else
1901
- (@levels-1).times do |i|
1902
- level = @scale[i]
1903
- level1 = @scale[i+1]
1904
- out.push([level, *@color[i,nil].to_a])
1905
- out.push([level1, *@color[i,nil].to_a])
1906
- end
1907
- maxcolors = @levels-1
1908
- end
1909
- return ["set palette model #{@model} maxcolors #{maxcolors} defined (\\",
1910
- out.map{|list| " " + list.join(" ")}.join(",\\\n") + "\\",
1911
- ")"].join("\n")
1912
- end
1913
-
1914
- end
1915
-
1916
- class ColorPalette
1917
-
1918
- def self.CPT (file, opt = nil)
1919
- options = {:continuous => false}.update(opt)
1920
- continuous = options[:continuous]
1921
- if File.exist?(file)
1922
- text = File.read(file)
1923
- else
1924
- if continuous
1925
- text = IO.popen("makecpt -C#{file} -Z", "r") { |io| io.read }
1926
- else
1927
- text = IO.popen("makecpt -C#{file}", "r") { |io| io.read }
1928
- end
1929
- if text =~ /\A\s*\Z/
1930
- raise "failed to makecpt #{file}"
1931
- end
1932
- end
1933
-
1934
- lines = text.split("\n").map{|line| line.strip }
1935
-
1936
- model = "HSV"
1937
- entries = []
1938
- lines.each do |line|
1939
- case line
1940
- when /\A\#\s*COLOR_MODEL\s*=\s*(.+)\s*\Z/
1941
- model = $1.upcase
1942
- when /\A\Z/, /\A#/, /\A([FBN])\s*(.*)\Z/
1943
- next
1944
- else
1945
- list = line.split(/\s+/)
1946
- list[1] = list[1].split(/\-/)
1947
- list[3] = list[3].split(/\-/)
1948
- list = list.flatten.map{|x| x.to_f}
1949
- entries.push(list)
1950
- end
1951
- end
1952
-
1953
- levels = entries.size * 2
1954
- color = CArray.float(levels, 3)
1955
- scale = CArray.float(levels)
1956
- level = 0
1957
- entries.each do |entry|
1958
- scale[level] = entry[0]
1959
- color[level, nil] = entry[1, 3]
1960
- level += 1
1961
- scale[level] = entry[4]
1962
- color[level, nil] = entry[5, 3]
1963
- level += 1
1964
- end
1965
- if model =~ /HSV/
1966
- color[nil, 0] /= 360
1967
- else
1968
- color[] /= 255
1969
- end
1970
-
1971
- return ColorPalette.new(model, levels, color, scale, options[:contiuous])
1972
- end
1973
-
1974
- def self.GMT (file, continuous = true)
1975
- return CPT(file, :continuous=>continuous)
1976
- end
1977
- end
1978
-
1979
- end
1980
-
1981
- class ColorPalette < CA::Gnuplot::ColorPalette
1982
- end
1983
-
1984
- class CA::Gnuplot
1985
-
1986
- def pi2arc (th0, th1)
1987
- return format("%f:%f", -th1+90, -th0+90)
1988
- end
1989
-
1990
- def xtics_histogram (ticslabels)
1991
- max = ticslabels.size - 1
1992
- return -0.5..max+0.5,
1993
- (0..max).map{|k|[k,ticslabels[k]]} + (0..max-1).map{|k|[k+0.5,"",1]}
1994
- end
1995
-
1996
- def guide_with_x11 (*arg)
1997
- @pause_mouse_mode = arg
1998
- @pause_mouse_term = "x11"
1999
- __guide__
2000
- end
2001
-
2002
- def guide_with_wxt (*arg)
2003
- @pause_mouse_mode = arg
2004
- @pause_mouse_term = "wxt"
2005
- __guide__
2006
- end
2007
-
2008
- def __guide__
2009
- guides = []
2010
- @pause_mouse_mode.each do |mode|
2011
- guide = ""
2012
- if mode.to_s[-1,1] == "!"
2013
- confirm = true
2014
- mode = mode.to_s[0..-2].intern
2015
- else
2016
- confirm = false
2017
- end
2018
- loop do
2019
- begin
2020
- put %{
2021
- set term push
2022
- set term #{@pause_mouse_term}
2023
- replot
2024
- }
2025
- case mode
2026
- when :click
2027
- puts "Wait mouse: 1-mouse click required"
2028
- put "pause mouse"
2029
- mx = evaluate "MOUSE_X"
2030
- my = evaluate "MOUSE_Y"
2031
- when :point
2032
- puts "Draw point: 1-mouse click required"
2033
- put "pause mouse"
2034
- mx = evaluate "MOUSE_X"
2035
- my = evaluate "MOUSE_Y"
2036
- guide = %{set %{ label "" at #{mx},#{my} point }}
2037
- set %{ label "" at #{mx},#{my} point pt 7 ps 2 }
2038
- put %{ refresh }
2039
- when :label, :clabel, :llabel, :rlabel
2040
- puts "Draw label: 1-mouse click required"
2041
- put "pause mouse"
2042
- mx = evaluate "MOUSE_X"
2043
- my = evaluate "MOUSE_Y"
2044
- print "label text: "
2045
- STDOUT.flush
2046
- text = STDIN.gets.chomp
2047
- case mode
2048
- when :label, :llabel
2049
- just = "left"
2050
- when :clabel
2051
- just = "center"
2052
- when :rlabel
2053
- just = "right"
2054
- end
2055
- guide = %{set %{ label #{text.dump} at #{"%.5g" % mx},#{"%.5g" %my} #{just} }}
2056
- set %{ label #{text.dump} at #{mx},#{my} #{just} }
2057
- put %{ refresh }
2058
- when :arrow
2059
- puts "Draw arrow: finish with right click"
2060
- guide = ""
2061
- pairs = []
2062
- i = 0
2063
- loop do
2064
- put "pause mouse"
2065
- mx = evaluate "MOUSE_X"
2066
- my = evaluate "MOUSE_Y"
2067
- mb = evaluate "MOUSE_BUTTON"
2068
- pairs << [mx, my]
2069
- if i >= 1
2070
- if i == 1
2071
- head = ""
2072
- else
2073
- head = "nohead"
2074
- end
2075
- mx1,my1 = pairs[i-1]
2076
- mx2,my2 = pairs[i]
2077
- guide << %{set %{ arrow from #{"%.5g" % mx2},#{"%.5g" % my2} to #{"%.5g" % mx1},#{"%.5g" % my1} #{head}} } << "\n"
2078
- set %{ arrow from #{"%.5g" % mx2},#{"%.5g" % my2} to #{"%.5g" % mx1},#{"%.5g" % my1} #{head}}
2079
- put %{ refresh }
2080
- end
2081
- if mb == "3"
2082
- break
2083
- end
2084
- i += 1
2085
- end
2086
- when :key
2087
- puts "Draw key: 1-mouse click required"
2088
- put "pause mouse"
2089
- mx = evaluate "MOUSE_X"
2090
- my = evaluate "MOUSE_Y"
2091
- guide = %{set %{ key at #{"%.5g" % mx},#{"%.5g" % my} }}
2092
- set %{ key at #{mx},#{my} }
2093
- put %{ refresh }
2094
- else
2095
- raise "invalid pause_mouse_mode #{@pause_mouse_mode}"
2096
- end
2097
- terminal %{ pop }
2098
- if @output
2099
- output @output
2100
- end
2101
- put %{ replot }
2102
- unset %{ output }
2103
- if confirm
2104
- print "ok? (Y/n) "
2105
- STDOUT.flush
2106
- begin
2107
- curstty = %x{stty -g}
2108
- system "stty raw -echo -icanon isig"
2109
- c = STDIN.getc;
2110
- ensure
2111
- system "stty #{curstty}"
2112
- end
2113
- puts
2114
- if c == ?N or c == ?n
2115
- raise
2116
- end
2117
- end
2118
- break
2119
- rescue
2120
- retry
2121
- end
2122
- end
2123
- guides << guide
2124
- end
2125
- @pause_mouse_mode = nil
2126
- puts guides.join("\n")
2127
- pause
2128
- end
2129
-
2130
-
2131
- end