carray 1.3.5 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSES +22 -0
  3. data/README.md +18 -21
  4. data/Rakefile +31 -0
  5. data/carray.gemspec +13 -26
  6. data/{ca_iter_block.c → ext/ca_iter_block.c} +13 -13
  7. data/{ca_iter_dimension.c → ext/ca_iter_dimension.c} +16 -16
  8. data/{ca_iter_window.c → ext/ca_iter_window.c} +10 -10
  9. data/{ca_obj_array.c → ext/ca_obj_array.c} +60 -55
  10. data/{ca_obj_bitarray.c → ext/ca_obj_bitarray.c} +12 -12
  11. data/{ca_obj_bitfield.c → ext/ca_obj_bitfield.c} +7 -7
  12. data/{ca_obj_block.c → ext/ca_obj_block.c} +42 -42
  13. data/{ca_obj_fake.c → ext/ca_obj_fake.c} +7 -7
  14. data/{ca_obj_farray.c → ext/ca_obj_farray.c} +18 -18
  15. data/{ca_obj_field.c → ext/ca_obj_field.c} +15 -15
  16. data/{ca_obj_grid.c → ext/ca_obj_grid.c} +27 -27
  17. data/{ca_obj_mapping.c → ext/ca_obj_mapping.c} +9 -9
  18. data/{ca_obj_object.c → ext/ca_obj_object.c} +37 -37
  19. data/{ca_obj_reduce.c → ext/ca_obj_reduce.c} +1 -1
  20. data/{ca_obj_refer.c → ext/ca_obj_refer.c} +33 -33
  21. data/{ca_obj_repeat.c → ext/ca_obj_repeat.c} +43 -43
  22. data/{ca_obj_select.c → ext/ca_obj_select.c} +2 -2
  23. data/{ca_obj_shift.c → ext/ca_obj_shift.c} +23 -23
  24. data/{ca_obj_transpose.c → ext/ca_obj_transpose.c} +23 -23
  25. data/{ca_obj_unbound_repeat.c → ext/ca_obj_unbound_repeat.c} +55 -55
  26. data/{ca_obj_window.c → ext/ca_obj_window.c} +26 -26
  27. data/{carray.h → ext/carray.h} +77 -51
  28. data/{carray_access.c → ext/carray_access.c} +133 -91
  29. data/{carray_attribute.c → ext/carray_attribute.c} +12 -12
  30. data/{carray_call_cfunc.c → ext/carray_call_cfunc.c} +0 -0
  31. data/{carray_cast.c → ext/carray_cast.c} +6 -6
  32. data/{carray_cast_func.rb → ext/carray_cast_func.rb} +0 -0
  33. data/{carray_class.c → ext/carray_class.c} +0 -0
  34. data/{carray_conversion.c → ext/carray_conversion.c} +11 -13
  35. data/{carray_copy.c → ext/carray_copy.c} +19 -19
  36. data/{carray_core.c → ext/carray_core.c} +2 -2
  37. data/ext/carray_data_type.c +66 -0
  38. data/{carray_element.c → ext/carray_element.c} +9 -9
  39. data/{carray_generate.c → ext/carray_generate.c} +67 -1
  40. data/{carray_iterator.c → ext/carray_iterator.c} +40 -38
  41. data/{carray_loop.c → ext/carray_loop.c} +24 -24
  42. data/{carray_mask.c → ext/carray_mask.c} +17 -6
  43. data/{carray_math.rb → ext/carray_math.rb} +25 -25
  44. data/ext/{mathfunc/carray_mathfunc.c → carray_mathfunc.c} +0 -0
  45. data/{carray_numeric.c → ext/carray_numeric.c} +1 -1
  46. data/{carray_operator.c → ext/carray_operator.c} +32 -9
  47. data/{carray_order.c → ext/carray_order.c} +2 -153
  48. data/{carray_sort_addr.c → ext/carray_sort_addr.c} +0 -0
  49. data/{carray_stat.c → ext/carray_stat.c} +5 -5
  50. data/{carray_stat_proc.rb → ext/carray_stat_proc.rb} +23 -23
  51. data/{carray_test.c → ext/carray_test.c} +22 -21
  52. data/{carray_undef.c → ext/carray_undef.c} +0 -0
  53. data/{carray_utils.c → ext/carray_utils.c} +0 -0
  54. data/{extconf.rb → ext/extconf.rb} +0 -5
  55. data/{mkmath.rb → ext/mkmath.rb} +12 -2
  56. data/{ruby_carray.c → ext/ruby_carray.c} +9 -2
  57. data/{ruby_ccomplex.c → ext/ruby_ccomplex.c} +0 -0
  58. data/{ruby_float_func.c → ext/ruby_float_func.c} +0 -0
  59. data/{version.h → ext/version.h} +5 -5
  60. data/{version.rb → ext/version.rb} +0 -0
  61. data/lib/carray.rb +49 -7
  62. data/lib/carray/{base/autoload.rb → autoload.rb} +48 -6
  63. data/lib/carray/autoload/autoload_base.rb +1 -5
  64. data/lib/carray/autoload/autoload_gem_cairo.rb +9 -0
  65. data/lib/carray/autoload/autoload_gem_ffi.rb +9 -0
  66. data/lib/carray/autoload/autoload_gem_gnuplot.rb +2 -0
  67. data/lib/carray/autoload/autoload_gem_io_csv.rb +14 -0
  68. data/lib/carray/autoload/autoload_gem_io_pg.rb +6 -0
  69. data/lib/carray/autoload/autoload_gem_io_sqlite3.rb +12 -0
  70. data/lib/carray/autoload/autoload_gem_narray.rb +10 -0
  71. data/lib/carray/autoload/autoload_gem_numo_narray.rb +15 -0
  72. data/lib/carray/autoload/autoload_gem_opencv.rb +16 -0
  73. data/lib/carray/autoload/autoload_gem_random.rb +8 -0
  74. data/lib/carray/autoload/autoload_gem_rmagick.rb +23 -0
  75. data/lib/carray/autoload/{autoload_graphics_zimg.rb → autoload_gem_zimg.rb} +0 -0
  76. data/lib/carray/basic.rb +193 -0
  77. data/lib/carray/compose.rb +291 -0
  78. data/lib/carray/construct.rb +445 -0
  79. data/lib/carray/convert.rb +115 -0
  80. data/lib/carray/{base/inspect.rb → inspect.rb} +6 -6
  81. data/lib/carray/io/imagemagick.rb +1 -1
  82. data/lib/carray/{base/iterator.rb → iterator.rb} +3 -3
  83. data/lib/carray/mask.rb +91 -0
  84. data/lib/carray/{base/math.rb → math.rb} +17 -47
  85. data/lib/carray/math/histogram.rb +7 -7
  86. data/lib/carray/mkmf.rb +8 -0
  87. data/lib/carray/object/ca_obj_pack.rb +8 -8
  88. data/lib/carray/obsolete.rb +272 -0
  89. data/lib/carray/ordering.rb +157 -0
  90. data/lib/carray/{base/serialize.rb → serialize.rb} +28 -53
  91. data/lib/carray/string.rb +190 -0
  92. data/lib/carray/{base/struct.rb → struct.rb} +20 -20
  93. data/lib/carray/{io/table.rb → table.rb} +1 -9
  94. data/lib/carray/testing.rb +56 -0
  95. data/lib/carray/time.rb +78 -0
  96. data/lib/carray/transform.rb +100 -0
  97. data/misc/Methods.ja.md +182 -0
  98. data/{NOTE → misc/NOTE} +0 -0
  99. data/test/test_ALL.rb +0 -2
  100. data/test/test_order.rb +7 -7
  101. data/test/test_ref_store.rb +13 -13
  102. data/test/test_stat.rb +7 -15
  103. data/{devel → utils}/guess_shape.rb +0 -0
  104. data/utils/{diff_method.rb → monkey_patch_methods.rb} +17 -7
  105. metadata +100 -254
  106. data/COPYING +0 -56
  107. data/GPL +0 -340
  108. data/Gemfile +0 -8
  109. data/Gemfile.lock +0 -33
  110. data/LEGAL +0 -50
  111. data/TODO +0 -5
  112. data/carray_random.c +0 -531
  113. data/ext/calculus/carray_calculus.c +0 -931
  114. data/ext/calculus/carray_interp.c +0 -358
  115. data/ext/calculus/extconf.rb +0 -12
  116. data/ext/calculus/lib/autoload/autoload_math_calculus.rb +0 -2
  117. data/ext/calculus/lib/math/calculus.rb +0 -119
  118. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +0 -31
  119. data/ext/fortio/extconf.rb +0 -3
  120. data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +0 -5
  121. data/ext/fortio/lib/carray/io/fortran_format.rb +0 -43
  122. data/ext/fortio/lib/fortio.rb +0 -3
  123. data/ext/fortio/lib/fortio/fortran_format.rb +0 -605
  124. data/ext/fortio/lib/fortio/fortran_format.tab.rb +0 -536
  125. data/ext/fortio/lib/fortio/fortran_format.y +0 -215
  126. data/ext/fortio/lib/fortio/fortran_namelist.rb +0 -151
  127. data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +0 -470
  128. data/ext/fortio/lib/fortio/fortran_namelist.y +0 -213
  129. data/ext/fortio/lib/fortio/fortran_sequential.rb +0 -345
  130. data/ext/fortio/ruby_fortio.c +0 -182
  131. data/ext/fortio/test/test_H.rb +0 -5
  132. data/ext/fortio/test/test_T.rb +0 -7
  133. data/ext/fortio/test/test_fortran_format.rb +0 -86
  134. data/ext/fortio/test/test_namelist.rb +0 -25
  135. data/ext/fortio/test/test_namelist_write.rb +0 -10
  136. data/ext/fortio/test/test_sequential.rb +0 -13
  137. data/ext/fortio/test/test_sequential2.rb +0 -13
  138. data/ext/fortio/work/test.rb +0 -10
  139. data/ext/fortio/work/test_e.rb +0 -19
  140. data/ext/fortio/work/test_ep.rb +0 -10
  141. data/ext/fortio/work/test_parse.rb +0 -12
  142. data/ext/imagemap/carray_imagemap.c +0 -495
  143. data/ext/imagemap/doc/call_graph.dot +0 -28
  144. data/ext/imagemap/draw.c +0 -567
  145. data/ext/imagemap/extconf.rb +0 -13
  146. data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +0 -1
  147. data/ext/imagemap/lib/graphics/imagemap.rb +0 -273
  148. data/ext/imagemap/lib/image_map.rb +0 -4
  149. data/ext/imagemap/test/swath_index.rb +0 -83
  150. data/ext/imagemap/test/swath_warp.rb +0 -99
  151. data/ext/imagemap/test/test.rb +0 -23
  152. data/ext/imagemap/test/test_image.rb +0 -42
  153. data/ext/imagemap/test/test_line.rb +0 -14
  154. data/ext/imagemap/test/test_rotate.rb +0 -17
  155. data/ext/imagemap/test/test_triangle.rb +0 -20
  156. data/ext/imagemap/test/test_warp.rb +0 -26
  157. data/ext/mathfunc/extconf.rb +0 -18
  158. data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +0 -1
  159. data/ext/mathfunc/lib/math/mathfunc.rb +0 -15
  160. data/ext/mathfunc/test/test_hypot.rb +0 -5
  161. data/ext/mathfunc/test/test_j0.rb +0 -22
  162. data/ext/mathfunc/test/test_jn.rb +0 -8
  163. data/ext/mathfunc/test/test_sph.rb +0 -9
  164. data/ext/narray/README +0 -22
  165. data/ext/narray/ca_wrap_narray.c +0 -500
  166. data/ext/narray/carray_narray.c +0 -21
  167. data/ext/narray/extconf.rb +0 -57
  168. data/ext/narray/lib/autoload/autoload_math_narray.rb +0 -1
  169. data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +0 -11
  170. data/ext/narray/lib/math/narray.rb +0 -17
  171. data/ext/narray/lib/math/narray_miss.rb +0 -45
  172. data/lib/carray/autoload/autoload_graphics_gnuplot.rb +0 -2
  173. data/lib/carray/autoload/autoload_io_csv.rb +0 -14
  174. data/lib/carray/autoload/autoload_io_pg.rb +0 -6
  175. data/lib/carray/autoload/autoload_io_sqlite3.rb +0 -12
  176. data/lib/carray/autoload/autoload_io_table.rb +0 -1
  177. data/lib/carray/autoload/autoload_math_interp.rb +0 -4
  178. data/lib/carray/base/basic.rb +0 -1090
  179. data/lib/carray/base/obsolete.rb +0 -131
  180. data/lib/carray/graphics/gnuplot.rb +0 -2131
  181. data/lib/carray/graphics/zimg.rb +0 -296
  182. data/lib/carray/io/csv.rb +0 -572
  183. data/lib/carray/io/pg.rb +0 -101
  184. data/lib/carray/io/sqlite3.rb +0 -215
  185. data/lib/carray/math/interp.rb +0 -57
  186. data/lib/carray/math/interp/adapter_gsl_spline.rb +0 -47
  187. data/mt19937ar.c +0 -182
  188. data/mt19937ar.h +0 -86
  189. data/rdoc_main.rb +0 -27
  190. data/rdoc_math.rb +0 -5
  191. data/rdoc_stat.rb +0 -31
  192. data/test/test_narray.rb +0 -64
  193. data/test/test_random.rb +0 -15
  194. data/utils/create_rdoc.sh +0 -9
  195. data/utils/make_tgz.sh +0 -3
@@ -1,296 +0,0 @@
1
- # ----------------------------------------------------------------------------
2
- #
3
- # carray/graphics/zimg.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
-
14
- =begin
15
-
16
- option
17
-
18
- :debug => (true|false)
19
- :output => filename
20
- :complex => (abs|length|phase|real|imaginary)
21
- :colormap => (red|blue|gray) | "filename" | [r,g,b]
22
- :invert => (true|false)
23
- :crange => [min,max]
24
- :nda_color => color
25
- :scale => [x[,y]]
26
- :align => [h,[,v,bcol]]
27
- :quality => quality
28
- :interlace => (true|false)
29
- :colorbox => (true|false)
30
- :cbox_tics => n
31
- :cbox_label => string
32
- :cbox_format => string
33
- :display => (true|false) | "program"
34
- :legend => string
35
- :label => [[x1,y1,s1], [x2,y2,s2], ...]
36
- :vlabel => [[x1,y1,s1], [x2,y2,s2], ...]
37
- :textfont => n
38
- :textcolor => "xxx"|"xxxxxx"
39
- :smooth => threshold
40
- :contors => [levels,"log",bg,fg] ### Not Implemented
41
-
42
- =end
43
-
44
- class CArray
45
-
46
- @@zimg_count = 0
47
-
48
- def zimg (option={}) # :nodoc:
49
-
50
- tempfile = "%zimg_#{$$}_#{@@zimg_count}.dat"
51
- @@zimg_count += 1
52
-
53
- if self.rank != 2
54
- raise "zimg accept only 2-D data"
55
- end
56
-
57
- option.each do |k, v|
58
- if k.kind_of?(String)
59
- option[k.intern] = v
60
- end
61
- end
62
-
63
- if option[:output]
64
- output = option[:output]
65
- case output
66
- when /.ppm$/
67
- outfmt = "--ppm"
68
- when /.pgm$/
69
- outfmt = "--pgm"
70
- when /.jpg$/i, /.jpeg$/i
71
- if option[:quality]
72
- outfmt = "--jpeg=" + option[:quality].to_s
73
- else
74
- outfmt = "--jpeg"
75
- end
76
- else
77
- outfmt = ""
78
- end
79
- else
80
- output = "-"
81
- outfmt = ""
82
- end
83
-
84
- if option[:interlace]
85
- interlace = "--interlace"
86
- else
87
- interlace = ""
88
- end
89
-
90
- case self.data_type
91
- when CA_UINT8
92
- type_spec = "--unsigned-char"
93
- when CA_INT8, CA_BOOLEAN
94
- type_spec = "--char"
95
- when CA_UINT16
96
- type_spec = "--unsigned-short"
97
- when CA_INT16
98
- type_spec = "--short"
99
- when CA_UINT32
100
- type_spec = "--unsigned-int"
101
- when CA_INT32
102
- type_spec = "--int"
103
- when CA_FLOAT32
104
- type_spec = "--float"
105
- when CA_FLOAT64
106
- type_spec = "--double"
107
- when CA_CMPLX64
108
- type_spec = "--complex-float=" + ( option[:complex] || "abs" )
109
- when CA_CMPLX128
110
- type_spec = "--complex-double=" + ( option[:complex] || "abs" )
111
- else
112
- raise "can't create image for invalid numerical data"
113
- end
114
-
115
- case option[:colormap]
116
- when "red", "blue", "gray"
117
- colormap = "--" + option[:colormap]
118
- when String
119
- if File.exist?(option[:colormap])
120
- colormap = "--colormap=" + option[:colormap]
121
- else
122
- raise "can't find colomap file"
123
- end
124
- when Array
125
- colormap = "--colormap=" + option[:colormap].join(",")
126
- else
127
- colormap = ""
128
- end
129
-
130
- if option[:crange]
131
- crange = "--crange=" + option[:crange].join(',')
132
- else
133
- crange = ''
134
- end
135
-
136
- if option[:nda_color]
137
- if option[:fill_value]
138
- nda = "--nda=" + [option[:fill_value], option[:nda_color]].join(',')
139
- else
140
- nda = "--nda=" + ["Infinity", option[:nda_color]].join(',')
141
- end
142
- else
143
- nda = ""
144
- end
145
-
146
- if option[:scale]
147
- scale = "--scale=" + option[:scale].join(',')
148
- else
149
- scale = ''
150
- end
151
-
152
- if option[:align]
153
- if option[:align].last.kind_of?(String)
154
- align = "--align=" + option[:align][0..-2].join('x')
155
- "," + option[:align].last
156
- else
157
- align = "--align=" + option[:align].join('x')
158
- end
159
- else
160
- align = ''
161
- end
162
-
163
- if option[:colorbox]
164
- if option[:cbox_tics]
165
- colorbox = "--colorbox=" + option[:cbox_tics].to_s
166
- else
167
- colorbox = "--colorbox"
168
- end
169
- else
170
- colorbox = ""
171
- end
172
-
173
- if option[:cbox_format]
174
- cbox_format = option[:cbox_format]
175
- else
176
- cbox_format = ""
177
- end
178
-
179
- if option[:cbox_label]
180
- cbox_label = option[:cbox_label]
181
- else
182
- cbox_label = ""
183
- end
184
-
185
- geom = lambda{|x,y|
186
- [ (x > 0 ? "+" : '') + x.to_s, (y > 0 ? '+' :'') + y.to_s].join
187
- }
188
-
189
- label = ""
190
- if option[:label]
191
- option[:label].each do |x, y, s|
192
- label << "--label=" + [geom[x, y],s].join(',') + " "
193
- end
194
- end
195
-
196
- vlabel = ""
197
- if option[:vlabel]
198
- option[:vlabel].each do |x, y, s|
199
- vlabel << "--vlabel=" + [geom[x, y],s].join(',') + " "
200
- end
201
- end
202
-
203
- if option[:textfont]
204
- textfont = "--font=" + option[:textfont].to_s
205
- else
206
- textfont = ""
207
- end
208
-
209
- if option[:textcolor]
210
- textcolor = "--textcolor=" + option[:textcolor].to_s
211
- else
212
- textcolor = ""
213
- end
214
-
215
- case option[:smooth]
216
- when Numeric
217
- smooth = "--smooth=" + option[:smooth].to_s
218
- else
219
- if option[:smooth]
220
- smooth = "--smooth"
221
- else
222
- smooth = ""
223
- end
224
- end
225
-
226
- command = [
227
- "zimg",
228
- "--output=#{output}",
229
- outfmt,
230
- interlace,
231
- type_spec,
232
- "--size=" + self.dim.reverse.join(","),
233
- scale,
234
- align,
235
- colormap,
236
- option[:invert] ? "--invert" : "",
237
- crange,
238
- nda,
239
- colorbox,
240
- cbox_label,
241
- cbox_format,
242
- label,
243
- vlabel,
244
- textfont,
245
- textcolor,
246
- smooth,
247
- tempfile
248
- ].join(" ")
249
-
250
- if option[:nda_color] and self.has_mask?
251
- if option[:fill_value]
252
- out = self.unmask(option[:fill_value])
253
- else
254
- out = self.unmask(nan)
255
- end
256
- else
257
- out = self
258
- end
259
-
260
- open(tempfile, "w"){ |io|
261
- out.dump_binary(io)
262
- }
263
-
264
- if option[:debug]
265
- puts command
266
- end
267
-
268
- if option[:output]
269
- system(command)
270
- case option[:display]
271
- when String
272
- system([option[:display], output].join(" "))
273
- else
274
- if option[:display]
275
- system(["display", output].join(" "))
276
- end
277
- end
278
- else
279
- case option[:display]
280
- when String
281
- system(command + ["|", option[:display], "-"].join(" "))
282
- else
283
- if option[:display]
284
- system(command + ["|", "display -"].join(" "))
285
- end
286
- end
287
- end
288
-
289
- ensure
290
- if File.exist?(tempfile)
291
- File.unlink(tempfile)
292
- end
293
- end
294
-
295
- end
296
-
@@ -1,572 +0,0 @@
1
- # ----------------------------------------------------------------------------
2
- #
3
- # carray/io/csv.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
- # CSV data reader/writer for CArray (in DSL approach)
14
- #
15
- # For reading (explicit form)
16
- #
17
- # csv = CA::CSVReader.new {
18
- # header # read a line as "names" header
19
- # header :units # read a line as "units" header
20
- # skip 1 # skip a line
21
- # body 10 # read values
22
- # process { |name, column| # post processing
23
- # case name # name : name of columns (string|integer)
24
- # when "Date" # column : column carray
25
- # column.map!{|x| Date.parse(x) }
26
- # else
27
- # column[] = column.double
28
- # end
29
- # }
30
- # }
31
- #
32
- # data1 = csv.read_file(file1)
33
- # data2 = csv.read_file(file2)
34
- # data3 = csv.read_file(file3)
35
- #
36
- # For reading (implicit form)
37
- #
38
- # data = CArray.load_csv(file) { ... definitions ... }
39
- # data = CArray.from_csv(io|string) { ... definitions ... }
40
- #
41
- # For writing (explict form)
42
- #
43
- # csv = CA::CSVWriter.new {
44
- # names ["Date", "a", "b", "c"] # set name of columns
45
- # process { |name, column| # pre processing
46
- # case name # name : name of column
47
- # when "Date" # column : column carray
48
- # column.map!{|x| x.to_s }
49
- # else
50
- # column.map!{|x| "%.2f" % x }
51
- # end
52
- # }
53
- # puts "sample CSV data" # print any string
54
- # header # write names in header
55
- # header ["","mm","cm","m"] # write units in header
56
- # write # write values
57
- # }
58
- #
59
- # data1.write_file(file1)
60
- # data2.write_file(file2)
61
- # data3.write_file(file3)
62
- #
63
- # For writing (implicit form)
64
- #
65
- # data.save_csv(file) { ... definitions ... }
66
- # data.to_csv([io|string]) { ... definitions ... }
67
- #
68
-
69
- require "stringio"
70
- require "strscan"
71
- require "carray/io/table"
72
-
73
- module CA
74
-
75
- class CSVReader # :nodoc:
76
-
77
- def initialize (sep=",", rs=$/, &block)
78
- @block = block
79
- @sep = sep
80
- @rs = rs
81
- end
82
-
83
- def read_io (io)
84
- return Processor.new(io, @sep, @rs, &@block).run
85
- end
86
-
87
- def read_string (string)
88
- return read_io(StringIO.new(string))
89
- end
90
-
91
- def read_file (filename)
92
- open(filename) { |io|
93
- return read_io(io)
94
- }
95
- end
96
-
97
- class Processor # :nodoc:
98
-
99
- def initialize (io, sep, rs=$/, &block)
100
- @io = io
101
- @sep = sep
102
- @rs = rs
103
- @sc = StringScanner.new("")
104
- @block = block || proc { body }
105
- @namelist = nil
106
- @names = nil
107
- @headerlist = []
108
- @header = {}
109
- @note = ""
110
- @table = nil
111
- @regexp_simple1 = /#{@sep}/
112
- @regexp_simple2 = / *#{@sep} */
113
- @regexp_pat1 = /\A([^"#{@sep}][^#{@sep}]*) *#{@sep} */
114
- @regexp_pat2 = /\A"([^"]+)" *#{@sep} */
115
- @regexp_pat3 = /\A"((?:[^"]+|"")+)" *#{@sep} */
116
- @regexp_pat4 = /\A(?:""|) *#{@sep} */
117
- end
118
-
119
- def run
120
- case @block.arity
121
- when 1
122
- @block.call(self)
123
- when -1, 0
124
- instance_exec(&@block)
125
- else
126
- raise "invalid block paramter"
127
- end
128
- header = @header
129
- note = @note
130
- @table.instance_exec{
131
- @names = header["names"]
132
- @header = header
133
- @note = note
134
- }
135
- @table.extend(CA::TableMethods)
136
- @table.column_names = header["names"]
137
- class << @table
138
- attr_reader :note
139
- def header (name=nil)
140
- if name
141
- return @header[name.to_s]
142
- else
143
- return @header
144
- end
145
- end
146
- end
147
- return @table
148
- end
149
-
150
- def column_names (*namelist)
151
- if @header.has_key?("names")
152
- warn "override header['names']"
153
- end
154
- @names = namelist.map(&:to_s)
155
- @header["names"] = @names
156
- return namelist
157
- end
158
-
159
- def header (name = "names")
160
- name = name.to_s
161
- list = csv_feed()
162
- if name == "names"
163
- if @names
164
- raise "already 'names' defined"
165
- end
166
- @names = list
167
- end
168
- @header[name] = list
169
- @headerlist.push(name)
170
- return list
171
- end
172
-
173
- attr_reader :names
174
-
175
- def note (n=1)
176
- list = []
177
- n.times { list << @io.gets(@rs) }
178
- @note << (text = list.join)
179
- return text
180
- end
181
-
182
- def skip (n=1)
183
- n.times { @io.gets(@rs) }
184
- end
185
-
186
- def body (n=nil, cols=nil)
187
- unless n
188
- n = 0x80000000
189
- end
190
- data = []
191
- count = 0
192
-
193
- if cols
194
- @cols = cols
195
- elsif @names
196
- @cols = @names.size
197
- else
198
- list = csv_feed()
199
- if list.nil?
200
- @rows = 0
201
- @table = CArray.object(@rows, @cols)
202
- return
203
- end
204
- data.push(list)
205
- count += 1
206
- @cols = list.size
207
- end
208
-
209
- lsize = nil
210
- while count < n and list = csv_feed(@cols)
211
- lsize = list.size
212
- if lsize == @cols
213
- data.push(list)
214
- elsif lsize <= @cols
215
- record = Array.new(@cols, nil)
216
- record[0,lsize] = list
217
- data.push(record)
218
- else
219
- extra = Array.new(lsize - @cols, nil)
220
- data.each do |row|
221
- row.push(*extra)
222
- end
223
- data.push(list)
224
- @cols = lsize
225
- # raise "csv parse error : too large column number at line #{@io.lineno}"
226
- end
227
- count += 1
228
- end
229
-
230
- @rows = data.size
231
- @table = CArray.object(@rows, @cols){ data }
232
- @table[:eq,""] = nil
233
- end
234
-
235
- def select (*namelist)
236
- @namelist = namelist.empty? ? nil : namelist
237
- case @namelist
238
- when nil
239
- when Array
240
- index = (0...@cols).map.to_a
241
- index_list = @namelist.map{ |x|
242
- case x
243
- when Integer
244
- x
245
- when Range
246
- index[x]
247
- when String, Symbol
248
- if @names and i = @names.index(x.to_s)
249
- i
250
- else
251
- raise "invalid argument #{x}"
252
- end
253
- else
254
- raise "invalid argument"
255
- end
256
- }.flatten
257
- @table = @table[nil, CA_INT(index_list)].to_ca
258
- @header.keys.each do |k|
259
- @header[k] = @header[k].values_at(*index_list)
260
- end
261
- else
262
- raise
263
- end
264
- end
265
-
266
- def process
267
- if @namelist
268
- @namelist.each_with_index do |name, i|
269
- yield(name, @table[nil, i])
270
- end
271
- elsif @names
272
- @names.each_with_index do |name, i|
273
- yield(name, @table[nil, i])
274
- end
275
- else
276
- @table.dim1.times do |i|
277
- yield(i, @table[nil,i])
278
- end
279
- end
280
- end
281
-
282
- def convert (data_type, options={}, &block)
283
- if block_given?
284
- if data_type.is_a?(Class) and data_type < CA::Struct
285
- @table = @table[:i, nil].convert(data_type, &block)
286
- else
287
- @table = @table.convert(data_type, options, &block)
288
- end
289
- else
290
- if data_type.is_a?(Class) and data_type < CA::Struct
291
- @table = @table[:i,nil].convert(data_type) { |b|
292
- data_type.new(*b[0,nil])
293
- }
294
- else
295
- @table = @table.to_type(data_type, options)
296
- end
297
- end
298
- end
299
-
300
- private
301
-
302
- def csv_feed (cols=nil)
303
- if @io.eof?
304
- return nil
305
- end
306
- line = nil
307
- loop do
308
- if newline = @io.gets(@rs)
309
- if line
310
- line << newline
311
- else
312
- line = newline
313
- end
314
- count_quote = line.count('"')
315
- else
316
- line = ""
317
- count_quote = 0
318
- end
319
- if count_quote == 0
320
- line.chomp!
321
- if line.count(' ') == 0
322
- return line.split(@sep, -1) ### /#{@sep}/
323
- else
324
- return line.split(@regexp_simple2, -1) ### / *#{@sep} */
325
- end
326
- end
327
- if count_quote % 2 == 0
328
- line.chomp!
329
- return csv_split(line, cols)
330
- else
331
- if newline
332
- next
333
- else
334
- raise "csv parse error"
335
- end
336
- end
337
- end
338
- end
339
-
340
- def csv_split (text, cols=nil)
341
- if cols
342
- csv = Array.new(cols)
343
- else
344
- csv = []
345
- end
346
- text << @sep
347
- @sc.string = text
348
- i = 0
349
- begin
350
- case
351
- when @sc.scan(@regexp_pat1)
352
- ### /\A([^"#{@sep}][^#{@sep}]*) *#{@sep} */
353
- csv[i] = @sc[1]
354
- when @sc.scan(@regexp_pat2)
355
- ### /\A"([^"]+)" *#{@sep} */
356
- csv[i] = @sc[1]
357
- when @sc.scan(@regexp_pat3)
358
- ### /\A"((?:[^"]+|"")+)" *#{@sep} */
359
- s = @sc[1]
360
- if s =~ /"/
361
- csv[i] = s.gsub(/""/, '"')
362
- else
363
- csv[i] = s
364
- end
365
- when @sc.scan(@regexp_pat4)
366
- ### /\A(?:""|) *#{@sep} */
367
- csv[i] = nil
368
- else
369
- raise "csv parse error"
370
- end
371
- i += 1
372
- end until @sc.eos?
373
- return csv
374
- end
375
-
376
- end
377
- end
378
- end
379
-
380
- module CA
381
-
382
- class CSVWriter # :nodoc:
383
-
384
- def initialize (sep=",", rs=$/, fill="", &block)
385
- @block = block
386
- @sep = sep
387
- @rs = rs
388
- @fill = fill
389
- end
390
-
391
- def write_io (table, io)
392
- return Processor.new(table, io, @sep, @rs, @fill, &@block).run
393
- end
394
-
395
- def write_string (table, string)
396
- write_io(table, StringIO.new(string))
397
- return string
398
- end
399
-
400
- def write_file (table, filename, mode="w")
401
- open(filename, mode) { |io|
402
- return write_io(table, io)
403
- }
404
- end
405
-
406
- class Processor # :nodoc:
407
-
408
- def initialize (table, io, sep, rs, fill, &block)
409
- @io = io
410
- @sep = sep
411
- @rs = rs
412
- @fill = fill
413
- @block = block || proc { body }
414
- if table.has_data_class?
415
- @names = table.members
416
- @table = CArray.merge(CA_OBJECT, table[nil].fields)
417
- else
418
- @names = table.instance_exec{ @names }
419
- if @names.nil?
420
- @names = table.instance_exec{ @column_names }
421
- end
422
- case
423
- when table.rank > 2
424
- @table = table.reshape(false,nil).object
425
- when table.rank == 1
426
- @table = table[:%,1].object ### convert to CA_OBJECT
427
- else
428
- @table = table.object ### convert to CA_OBJECT
429
- end
430
- end
431
- if @table.has_mask?
432
- @table.unmask(@fill)
433
- end
434
- @regexp_simple = /#{@sep}/o
435
- end
436
-
437
- def csv_quote (text)
438
- text = text.dup
439
- if text.gsub!(/"/, '""') or text =~ @regexp_simple ### /#{@sep}|"/
440
- text = '"' + text + '"'
441
- end
442
- return text
443
- end
444
-
445
- def run
446
- case @block.arity
447
- when 1
448
- @block.call(self)
449
- when -1, 0
450
- instance_exec(&@block)
451
- else
452
- raise "invalid block parameter"
453
- end
454
- end
455
-
456
- # set @names
457
- def names (list)
458
- @names = list
459
- end
460
-
461
- # puts header
462
- def header (list = @names)
463
- @io.write list.map{|s| csv_quote(s)}.join(@sep)
464
- @io.write(@rs)
465
- end
466
-
467
- # puts any strings
468
- def puts (*argv)
469
- @io.print(*argv)
470
- @io.write(@rs)
471
- end
472
-
473
- # write value
474
- # If option :strict is set, do csv_quote for string element
475
- def body (strict: true, format: nil)
476
- if strict
477
- case @table.data_type
478
- when CA_OBJECT
479
- table = @table.to_ca
480
- table[:is_kind_of, String].map! { |s| csv_quote(s) }
481
- when CA_FIXLEN
482
- table = @table.object
483
- table.map! { |s| csv_quote(s) }
484
- else
485
- table = @table.object
486
- end
487
- else
488
- table = @table
489
- end
490
- if format
491
- table.dim0.times do |i|
492
- @io.write Kernel::format(format,*table[i,nil].to_a)
493
- @io.write(@rs)
494
- end
495
- else
496
- table.dim0.times do |i|
497
- @io.write table[i,nil].to_a.join(@sep)
498
- @io.write(@rs)
499
- end
500
- end
501
- end
502
-
503
- # pre processing data
504
- def process (namelist = @names)
505
- if namelist
506
- namelist.each_with_index do |name, i|
507
- yield(name, @table[nil, i])
508
- end
509
- else
510
- @table.dim1.times do |i|
511
- yield(i, @table[nil,i])
512
- end
513
- end
514
- end
515
- end
516
- end
517
- end
518
-
519
- class CArray
520
-
521
- def self.load_csv (file, option={}, rs: $/, sep: ",", &block)
522
- option = {:sep=>sep, :rs=>rs}.update(option)
523
- reader = CA::CSVReader.new(option[:sep], option[:rs], &block)
524
- return reader.read_file(file)
525
- end
526
-
527
- def self.from_csv (io, option={}, rs: $/, sep: ",", &block)
528
- option = {:sep=>sep, :rs=>rs}.update(option)
529
- reader = CA::CSVReader.new(option[:sep], option[:rs], &block)
530
- case io
531
- when IO, StringIO
532
- return reader.read_io(io)
533
- when String
534
- return reader.read_string(io)
535
- else
536
- raise "invalid argument"
537
- end
538
- end
539
-
540
- def save_csv (file, option = {}, rs: $/, sep: ",", fill: "", mode: "w", &block)
541
- option = {:sep=>sep, :rs=>rs, :fill=>fill, :mode=>mode}.update(option)
542
- writer = CA::CSVWriter.new(option[:sep], option[:rs], option[:fill], &block)
543
- return writer.write_file(self, file, option[:mode])
544
- end
545
-
546
- def to_csv (io="", option ={}, rs: $/, sep: ",", fill: "", &block)
547
- option = {:sep=>sep, :rs=>rs, :fill=>fill}.update(option)
548
- writer = CA::CSVWriter.new(option[:sep], option[:rs], option[:fill], &block)
549
- case io
550
- when IO, StringIO
551
- return writer.write_io(self, io)
552
- when String
553
- return writer.write_string(self, io)
554
- end
555
- end
556
-
557
- def to_tabular (option = {})
558
- option = {:sep=>" ", :names=>nil}.update(option)
559
- if option[:names]
560
- names = option[:names]
561
- elsif self.respond_to?(:names)
562
- names = self.names
563
- end
564
- sep = option[:sep]
565
- data = self.to_ca.map! {|s| s.to_s }
566
- table = CArray.join([names.to_ca], [data])
567
- length = table.convert{|s| s.length}.max(0)
568
- table.map_with_index! {|s, idx| s.rjust(length[idx[1]]) }.to_csv.gsub(/,/,sep)
569
- end
570
-
571
- end
572
-