carray-netcdf 1.0.13 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +79 -0
- data/CONTRIBUTING.md +75 -0
- data/LICENSE +21 -0
- data/README.md +108 -2
- data/bin/ncheader +223 -0
- data/bin/ncvar +160 -0
- data/carray-netcdf.gemspec +37 -19
- data/docs/00_introduction.md +72 -0
- data/docs/01_getting_started.md +114 -0
- data/docs/02_reading.md +216 -0
- data/docs/03_writing.md +418 -0
- data/docs/04_codec.md +373 -0
- data/docs/05_groups.md +200 -0
- data/docs/06_layer1.md +160 -0
- data/docs/07_cookbook.md +345 -0
- data/docs/08_cheatsheet.md +326 -0
- data/docs/09_cli.md +79 -0
- data/lib/carray/netcdf/codec.rb +536 -0
- data/lib/carray/netcdf/dim.rb +67 -0
- data/lib/carray/netcdf/file.rb +354 -0
- data/lib/carray/netcdf/group.rb +517 -0
- data/lib/carray/netcdf/var.rb +539 -0
- data/lib/carray/netcdf/version.rb +5 -0
- data/lib/carray/netcdf.rb +287 -0
- data/lib/carray-netcdf.rb +3 -1
- metadata +80 -25
- data/API.md +0 -270
- data/Rakefile +0 -12
- data/bin/ncdef2yaml +0 -60
- data/examples/test.nc +0 -0
- data/examples/test.rb +0 -41
- data/examples/time.rb +0 -11
- data/ext/extconf.rb +0 -11
- data/ext/rb_netcdflib.c +0 -2674
- data/lib/io/netcdf.rb +0 -848
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
module CA
|
|
2
|
+
# NetCDF access for CArray. One module, two layers.
|
|
3
|
+
#
|
|
4
|
+
# CA::NetCDF ← this module
|
|
5
|
+
#
|
|
6
|
+
# Layer 1 (this module's functions): a CArray-flavored view of the netcdf
|
|
7
|
+
# gem's `nc_*` API. `extend self` exposes every method both as a module
|
|
8
|
+
# function and as an instance method, so it is usable two ways:
|
|
9
|
+
#
|
|
10
|
+
# CA::NetCDF.nc_get_vara(ncid, varid, start, count) # call style
|
|
11
|
+
#
|
|
12
|
+
# class File
|
|
13
|
+
# include CA::NetCDF # mixin style
|
|
14
|
+
# def read = nc_get_vara(@ncid, @varid, start, count) # bare, CArray
|
|
15
|
+
# end
|
|
16
|
+
#
|
|
17
|
+
# {::NetCDF} is included (not mutated), so every un-overridden `nc_*` and
|
|
18
|
+
# the `NC_*` constants pass through, and `CA::NetCDF` coexists with a
|
|
19
|
+
# future `Numo::NetCDF`. Only the array I/O functions are overridden to
|
|
20
|
+
# speak CArray; scalar (`var1`), definition, inquiry, attribute and group
|
|
21
|
+
# calls fall through to {::NetCDF} verbatim.
|
|
22
|
+
#
|
|
23
|
+
# Layer 1 is raw values only: no convention decoding, no mask handling.
|
|
24
|
+
# Reads return unmasked CArrays; writing a masked CArray raises. The mask
|
|
25
|
+
# / CF story lives in layer 2 (the {File} / {Var} / {Codec} classes).
|
|
26
|
+
module NetCDF
|
|
27
|
+
|
|
28
|
+
include ::NetCDF
|
|
29
|
+
extend self
|
|
30
|
+
|
|
31
|
+
# @!group Layer 1 — array reads
|
|
32
|
+
|
|
33
|
+
# Read an entire variable.
|
|
34
|
+
#
|
|
35
|
+
# Without a buffer: allocate a CArray of the variable's {NC_TO_CA} type
|
|
36
|
+
# and the requested shape, and read into it. With a buffer (a CArray or
|
|
37
|
+
# any MemoryView provider): read into it and return it.
|
|
38
|
+
#
|
|
39
|
+
# @param ncid [Integer] NetCDF file id
|
|
40
|
+
# @param varid [Integer] variable id
|
|
41
|
+
# @param buffer [CArray, MemoryView, nil] optional destination buffer
|
|
42
|
+
# @return [CArray] the read values (a fresh CArray, or `buffer`)
|
|
43
|
+
def nc_get_var(ncid, varid, buffer = nil)
|
|
44
|
+
buffer ||= new_buffer(ncid, varid,
|
|
45
|
+
nc_inq_vardimid(ncid, varid).map { |d| nc_inq_dimlen(ncid, d) })
|
|
46
|
+
::NetCDF.nc_get_var(ncid, varid, buffer)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Read a hyperslab (contiguous block).
|
|
50
|
+
#
|
|
51
|
+
# @param ncid [Integer]
|
|
52
|
+
# @param varid [Integer]
|
|
53
|
+
# @param start [Array<Integer>] per-dim start index
|
|
54
|
+
# @param count [Array<Integer>] per-dim edge count
|
|
55
|
+
# @param buffer [CArray, MemoryView, nil]
|
|
56
|
+
# @return [CArray]
|
|
57
|
+
def nc_get_vara(ncid, varid, start, count, buffer = nil)
|
|
58
|
+
::NetCDF.nc_get_vara(ncid, varid, start, count, buffer || new_buffer(ncid, varid, count))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Read a strided hyperslab.
|
|
62
|
+
#
|
|
63
|
+
# @param ncid [Integer]
|
|
64
|
+
# @param varid [Integer]
|
|
65
|
+
# @param start [Array<Integer>]
|
|
66
|
+
# @param count [Array<Integer>]
|
|
67
|
+
# @param stride [Array<Integer>] per-dim stride (in on-disk elements)
|
|
68
|
+
# @param buffer [CArray, MemoryView, nil]
|
|
69
|
+
# @return [CArray]
|
|
70
|
+
def nc_get_vars(ncid, varid, start, count, stride, buffer = nil)
|
|
71
|
+
::NetCDF.nc_get_vars(ncid, varid, start, count, stride, buffer || new_buffer(ncid, varid, count))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Read a mapped hyperslab.
|
|
75
|
+
#
|
|
76
|
+
# @param ncid [Integer]
|
|
77
|
+
# @param varid [Integer]
|
|
78
|
+
# @param start [Array<Integer>]
|
|
79
|
+
# @param count [Array<Integer>]
|
|
80
|
+
# @param stride [Array<Integer>]
|
|
81
|
+
# @param imap [Array<Integer>] per-dim mapping (in memory elements)
|
|
82
|
+
# @param buffer [CArray, MemoryView, nil]
|
|
83
|
+
# @return [CArray]
|
|
84
|
+
def nc_get_varm(ncid, varid, start, count, stride, imap, buffer = nil)
|
|
85
|
+
::NetCDF.nc_get_varm(ncid, varid, start, count, stride, imap,
|
|
86
|
+
buffer || new_buffer(ncid, varid, count))
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @!endgroup
|
|
90
|
+
|
|
91
|
+
# @!group Layer 1 — array writes
|
|
92
|
+
#
|
|
93
|
+
# Accept a CArray as the buffer. netcdf requires a C-contiguous buffer,
|
|
94
|
+
# so non-entity views are materialized; masked input is rejected (fill
|
|
95
|
+
# encoding is layer 2's job).
|
|
96
|
+
|
|
97
|
+
# Write an entire variable.
|
|
98
|
+
#
|
|
99
|
+
# @param ncid [Integer]
|
|
100
|
+
# @param varid [Integer]
|
|
101
|
+
# @param ca [CArray] source values (must be unmasked)
|
|
102
|
+
# @return [void]
|
|
103
|
+
# @raise [ArgumentError] if `ca` carries a mask
|
|
104
|
+
def nc_put_var(ncid, varid, ca)
|
|
105
|
+
::NetCDF.nc_put_var(ncid, varid, writable_buffer(ca))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Write a hyperslab.
|
|
109
|
+
# @param (see #nc_get_vara)
|
|
110
|
+
# @param ca [CArray]
|
|
111
|
+
# @return [void]
|
|
112
|
+
def nc_put_vara(ncid, varid, start, count, ca)
|
|
113
|
+
::NetCDF.nc_put_vara(ncid, varid, start, count, writable_buffer(ca))
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Write a strided hyperslab.
|
|
117
|
+
# @param (see #nc_get_vars)
|
|
118
|
+
# @param ca [CArray]
|
|
119
|
+
# @return [void]
|
|
120
|
+
def nc_put_vars(ncid, varid, start, count, stride, ca)
|
|
121
|
+
::NetCDF.nc_put_vars(ncid, varid, start, count, stride, writable_buffer(ca))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Write a mapped hyperslab.
|
|
125
|
+
# @param (see #nc_get_varm)
|
|
126
|
+
# @param ca [CArray]
|
|
127
|
+
# @return [void]
|
|
128
|
+
def nc_put_varm(ncid, varid, start, count, stride, imap, ca)
|
|
129
|
+
::NetCDF.nc_put_varm(ncid, varid, start, count, stride, imap, writable_buffer(ca))
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# @!endgroup
|
|
133
|
+
|
|
134
|
+
# A fresh CArray to read `shape` elements of the variable into, typed
|
|
135
|
+
# from its on-disk type through {NC_TO_CA} -- an enum through its base
|
|
136
|
+
# type. A scalar variable (shape []) reads into a 1-element array.
|
|
137
|
+
# @api private
|
|
138
|
+
private def new_buffer(ncid, varid, shape)
|
|
139
|
+
xtype = nc_inq_vartype(ncid, varid)
|
|
140
|
+
if xtype >= NC_FIRSTUSERTYPEID
|
|
141
|
+
_, _, base, _, klass = nc_inq_user_type(ncid, xtype)
|
|
142
|
+
xtype = base if klass == NC_ENUM
|
|
143
|
+
end
|
|
144
|
+
data_type = NC_TO_CA[xtype] or
|
|
145
|
+
raise NotImplementedError, "no CArray type to read nc_type #{xtype} into; pass a buffer"
|
|
146
|
+
CArray.new(data_type, shape.empty? ? [1] : shape)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# A C-contiguous, unmasked CArray ready to hand to libnetcdf:
|
|
150
|
+
# materialize non-entity views, refuse masked input (encoding a fill
|
|
151
|
+
# value is the convention layer's decision, not layer 1's).
|
|
152
|
+
# @api private
|
|
153
|
+
private def writable_buffer(ca)
|
|
154
|
+
return ca unless ca.is_a?(CArray)
|
|
155
|
+
if ca.has_mask?
|
|
156
|
+
raise ArgumentError,
|
|
157
|
+
"cannot write a masked CArray at layer 1; strip the mask " \
|
|
158
|
+
"(e.g. #strip_mask(fill)) in the convention layer first"
|
|
159
|
+
end
|
|
160
|
+
ca.entity? ? ca : ca.copy
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# @!group Type maps (`nc_type` ↔ CArray `data_type`)
|
|
164
|
+
|
|
165
|
+
# On-disk `nc_type` → CArray `data_type` symbol.
|
|
166
|
+
# @return [Hash{Integer => Symbol}]
|
|
167
|
+
NC_TO_CA = {
|
|
168
|
+
NC_BYTE => :int8, NC_UBYTE => :uint8, NC_SHORT => :int16,
|
|
169
|
+
NC_USHORT => :uint16, NC_INT => :int32, NC_UINT => :uint32,
|
|
170
|
+
NC_INT64 => :int64, NC_UINT64 => :uint64, NC_FLOAT => :float32,
|
|
171
|
+
NC_DOUBLE => :float64, NC_CHAR => :uint8,
|
|
172
|
+
}.freeze
|
|
173
|
+
|
|
174
|
+
# {Group#def_var} type argument (Symbol or String) → `nc_type`.
|
|
175
|
+
# @return [Hash{Symbol => Integer}]
|
|
176
|
+
TYPE_TO_NC = {
|
|
177
|
+
double: NC_DOUBLE, float: NC_FLOAT, int: NC_INT, short: NC_SHORT,
|
|
178
|
+
byte: NC_BYTE, char: NC_CHAR, ubyte: NC_UBYTE, ushort: NC_USHORT,
|
|
179
|
+
uint: NC_UINT, int64: NC_INT64, uint64: NC_UINT64,
|
|
180
|
+
# CArray-style aliases
|
|
181
|
+
int8: NC_BYTE, uint8: NC_UBYTE, int16: NC_SHORT, uint16: NC_USHORT,
|
|
182
|
+
int32: NC_INT, uint32: NC_UINT, float32: NC_FLOAT, float64: NC_DOUBLE,
|
|
183
|
+
}.freeze
|
|
184
|
+
|
|
185
|
+
# Coerce a `def_var`-style type to its `nc_type` integer.
|
|
186
|
+
#
|
|
187
|
+
# An Integer is already an `nc_type` (e.g. from {Var#nc_type} on a
|
|
188
|
+
# template source). Pass it through so `def_var(..., template: v)` can
|
|
189
|
+
# preserve `NC_CHAR` vs `NC_UBYTE` — both map to `:uint8` in {NC_TO_CA}
|
|
190
|
+
# and the symbol round-trip would lose the distinction.
|
|
191
|
+
#
|
|
192
|
+
# @param type [Symbol, String, Integer]
|
|
193
|
+
# @return [Integer] an `NC_*` type constant
|
|
194
|
+
# @raise [ArgumentError] on an unrecognized type name
|
|
195
|
+
def nc_type_for(type)
|
|
196
|
+
return type if type.is_a?(Integer)
|
|
197
|
+
TYPE_TO_NC[type.to_s.to_sym] or
|
|
198
|
+
raise ArgumentError, "unknown NetCDF variable type: #{type.inspect}"
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# `nc_type` → typed `nc_put_att_*` suffix.
|
|
202
|
+
# @return [Hash{Integer => Symbol}]
|
|
203
|
+
PUT_ATT_SUFFIX = {
|
|
204
|
+
NC_BYTE => :schar, NC_UBYTE => :uchar, NC_SHORT => :short,
|
|
205
|
+
NC_USHORT => :ushort, NC_INT => :int, NC_UINT => :uint,
|
|
206
|
+
NC_INT64 => :longlong, NC_UINT64 => :ulonglong,
|
|
207
|
+
NC_FLOAT => :float, NC_DOUBLE => :double, NC_CHAR => :text,
|
|
208
|
+
}.freeze
|
|
209
|
+
|
|
210
|
+
# Attributes that NetCDF/CF require to be in the variable's (packed)
|
|
211
|
+
# type. `_FillValue` is enforced by libnetcdf; the rest match by
|
|
212
|
+
# convention and so that masked-value comparison in the codec works in
|
|
213
|
+
# packed space.
|
|
214
|
+
# @return [Array<String>]
|
|
215
|
+
PACKED_SPACE_ATTS = %w[_FillValue missing_value valid_range valid_min valid_max].freeze
|
|
216
|
+
|
|
217
|
+
# Write an attribute forced to `nc_type` (via the typed
|
|
218
|
+
# `nc_put_att_*`), falling back to the generic inference when the type
|
|
219
|
+
# has no typed writer.
|
|
220
|
+
#
|
|
221
|
+
# @param ncid [Integer]
|
|
222
|
+
# @param varid [Integer]
|
|
223
|
+
# @param name [String]
|
|
224
|
+
# @param value [Object] Ruby value or CArray
|
|
225
|
+
# @param nc_type [Integer] target `NC_*` type
|
|
226
|
+
# @return [void]
|
|
227
|
+
def put_att_typed(ncid, varid, name, value, nc_type)
|
|
228
|
+
suffix = PUT_ATT_SUFFIX[nc_type]
|
|
229
|
+
return nc_put_att(ncid, varid, name, value) unless suffix
|
|
230
|
+
fn = "nc_put_att_#{suffix}"
|
|
231
|
+
if put_att_takes_xtype?(fn)
|
|
232
|
+
__send__(fn, ncid, varid, name, nc_type, value)
|
|
233
|
+
else
|
|
234
|
+
__send__(fn, ncid, varid, name, value)
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# A netcdf gem after 0.1.0 takes the on-disk xtype apart from the
|
|
239
|
+
# suffix's memory type, as C does; 0.1.0 has no xtype argument. Read
|
|
240
|
+
# the arity of the binding itself, past any `*args` wrapper prepended
|
|
241
|
+
# over it (a lock, say).
|
|
242
|
+
# @api private
|
|
243
|
+
PUT_ATT_XTYPE = Hash.new do |h, fn|
|
|
244
|
+
m = ::NetCDF.method(fn)
|
|
245
|
+
m = m.super_method while m.arity.negative? && m.super_method
|
|
246
|
+
h[fn] = m.arity == 5
|
|
247
|
+
end
|
|
248
|
+
private_constant :PUT_ATT_XTYPE
|
|
249
|
+
|
|
250
|
+
# @api private
|
|
251
|
+
private def put_att_takes_xtype?(fn) = PUT_ATT_XTYPE[fn]
|
|
252
|
+
|
|
253
|
+
# Write a variable attribute the way {Group#def_var} does:
|
|
254
|
+
#
|
|
255
|
+
# - a CArray keeps its own type (`_FillValue: CA_SHORT([-1])` on a
|
|
256
|
+
# float variable is a short attribute);
|
|
257
|
+
# - a Ruby value for a {PACKED_SPACE_ATTS} name takes the variable's
|
|
258
|
+
# on-disk type `var_type`, which libnetcdf requires of `_FillValue`;
|
|
259
|
+
# - anything else is left to the generic `nc_put_att` inference.
|
|
260
|
+
#
|
|
261
|
+
# @param ncid [Integer]
|
|
262
|
+
# @param varid [Integer]
|
|
263
|
+
# @param name [String]
|
|
264
|
+
# @param value [Object] Ruby value or CArray
|
|
265
|
+
# @param var_type [Integer] the variable's `NC_*` type
|
|
266
|
+
# @return [void]
|
|
267
|
+
# @api private
|
|
268
|
+
def put_var_att(ncid, varid, name, value, var_type)
|
|
269
|
+
if value.is_a?(CArray)
|
|
270
|
+
put_att_typed(ncid, varid, name, value.to_a, TYPE_TO_NC[value.data_type])
|
|
271
|
+
elsif PACKED_SPACE_ATTS.include?(name)
|
|
272
|
+
put_att_typed(ncid, varid, name, value, var_type)
|
|
273
|
+
else
|
|
274
|
+
nc_put_att(ncid, varid, name, value)
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# @!endgroup
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
require_relative "netcdf/version"
|
|
283
|
+
require_relative "netcdf/codec"
|
|
284
|
+
require_relative "netcdf/dim"
|
|
285
|
+
require_relative "netcdf/var"
|
|
286
|
+
require_relative "netcdf/group"
|
|
287
|
+
require_relative "netcdf/file"
|
data/lib/carray-netcdf.rb
CHANGED
metadata
CHANGED
|
@@ -1,39 +1,95 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: carray-netcdf
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
8
|
-
autorequire:
|
|
7
|
+
- himotoyoshi
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: carray
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 3.0.1
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 3.0.1
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: netcdf
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 0.1.0
|
|
33
|
+
- - "<"
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '0.2'
|
|
36
|
+
type: :runtime
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 0.1.0
|
|
43
|
+
- - "<"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0.2'
|
|
46
|
+
description: |
|
|
47
|
+
A NetCDF interface for CArray, built on the netcdf gem. Data is
|
|
48
|
+
exchanged as CArray at every level, through Ruby's MemoryView protocol,
|
|
49
|
+
so libnetcdf works on the array's own memory.
|
|
50
|
+
|
|
51
|
+
Two layers do this. Layer 1 turns the netcdf gem's MemoryView buffers
|
|
52
|
+
into CArray, keeping the nc_* functions as they are. Layer 2 builds the
|
|
53
|
+
object-oriented interface on top, and returns data as the file's
|
|
54
|
+
conventions define it: _FillValue as a mask, scale_factor and add_offset
|
|
55
|
+
applied, CF time as CATime. Pure Ruby, on CArray 3.0.
|
|
56
|
+
email:
|
|
57
|
+
- himotoyoshi@users.noreply.github.com
|
|
15
58
|
executables:
|
|
16
|
-
-
|
|
17
|
-
|
|
18
|
-
|
|
59
|
+
- ncheader
|
|
60
|
+
- ncvar
|
|
61
|
+
extensions: []
|
|
19
62
|
extra_rdoc_files: []
|
|
20
63
|
files:
|
|
21
|
-
-
|
|
64
|
+
- CHANGELOG.md
|
|
65
|
+
- CONTRIBUTING.md
|
|
66
|
+
- LICENSE
|
|
22
67
|
- README.md
|
|
23
|
-
-
|
|
24
|
-
- bin/
|
|
68
|
+
- bin/ncheader
|
|
69
|
+
- bin/ncvar
|
|
25
70
|
- carray-netcdf.gemspec
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
71
|
+
- docs/00_introduction.md
|
|
72
|
+
- docs/01_getting_started.md
|
|
73
|
+
- docs/02_reading.md
|
|
74
|
+
- docs/03_writing.md
|
|
75
|
+
- docs/04_codec.md
|
|
76
|
+
- docs/05_groups.md
|
|
77
|
+
- docs/06_layer1.md
|
|
78
|
+
- docs/07_cookbook.md
|
|
79
|
+
- docs/08_cheatsheet.md
|
|
80
|
+
- docs/09_cli.md
|
|
31
81
|
- lib/carray-netcdf.rb
|
|
32
|
-
- lib/
|
|
82
|
+
- lib/carray/netcdf.rb
|
|
83
|
+
- lib/carray/netcdf/codec.rb
|
|
84
|
+
- lib/carray/netcdf/dim.rb
|
|
85
|
+
- lib/carray/netcdf/file.rb
|
|
86
|
+
- lib/carray/netcdf/group.rb
|
|
87
|
+
- lib/carray/netcdf/var.rb
|
|
88
|
+
- lib/carray/netcdf/version.rb
|
|
33
89
|
homepage: https://github.com/himotoyoshi/carray-netcdf
|
|
34
|
-
licenses:
|
|
90
|
+
licenses:
|
|
91
|
+
- MIT
|
|
35
92
|
metadata: {}
|
|
36
|
-
post_install_message:
|
|
37
93
|
rdoc_options: []
|
|
38
94
|
require_paths:
|
|
39
95
|
- lib
|
|
@@ -41,15 +97,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
41
97
|
requirements:
|
|
42
98
|
- - ">="
|
|
43
99
|
- !ruby/object:Gem::Version
|
|
44
|
-
version: 1.
|
|
100
|
+
version: 3.1.0
|
|
45
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
102
|
requirements:
|
|
47
103
|
- - ">="
|
|
48
104
|
- !ruby/object:Gem::Version
|
|
49
105
|
version: '0'
|
|
50
106
|
requirements: []
|
|
51
|
-
rubygems_version:
|
|
52
|
-
signing_key:
|
|
107
|
+
rubygems_version: 4.0.8
|
|
53
108
|
specification_version: 4
|
|
54
|
-
summary:
|
|
109
|
+
summary: A NetCDF interface for CArray
|
|
55
110
|
test_files: []
|
data/API.md
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
CArray/NetCDF library API document
|
|
2
|
-
==================================
|
|
3
|
-
|
|
4
|
-
1. Loading library
|
|
5
|
-
------------------
|
|
6
|
-
|
|
7
|
-
gem install carray-netcdf
|
|
8
|
-
|
|
9
|
-
require "carray-netcdf"
|
|
10
|
-
|
|
11
|
-
2. Methods
|
|
12
|
-
----------
|
|
13
|
-
|
|
14
|
-
This API provides the way of calling functions
|
|
15
|
-
|
|
16
|
-
include NC
|
|
17
|
-
nc_funcname(...)
|
|
18
|
-
|
|
19
|
-
NC.funcname(...)
|
|
20
|
-
|
|
21
|
-
### 2.1. Data types
|
|
22
|
-
|
|
23
|
-
data_type = NC.ca_type(xtype)
|
|
24
|
-
xtype = NC.nc_type(data_type)
|
|
25
|
-
|
|
26
|
-
### 2.2. NetCDF File
|
|
27
|
-
|
|
28
|
-
fd = nc_create(FILENAME[, mode=NC_CLOBBER])
|
|
29
|
-
|
|
30
|
-
mode : NC_CLOBBER - permit overwrite
|
|
31
|
-
NC_NOCLOBBER - inhibit overwrite
|
|
32
|
-
NC_SHARE - no buffering
|
|
33
|
-
|
|
34
|
-
fd = nc_open(FILENAME[, mode=NC_NOWRITE])
|
|
35
|
-
|
|
36
|
-
mode : NC_NOWRITE - readonly
|
|
37
|
-
NC_WRTIE - writable
|
|
38
|
-
NC_SHARE - no buffering
|
|
39
|
-
|
|
40
|
-
nc_close(fd)
|
|
41
|
-
|
|
42
|
-
nc_redef(fd)
|
|
43
|
-
nc_enddef(fd)
|
|
44
|
-
|
|
45
|
-
nc_sync(fd)
|
|
46
|
-
|
|
47
|
-
ndims = nc_inq_ndims(fd)
|
|
48
|
-
nvars = nc_inq_nvars(fd)
|
|
49
|
-
natts = nc_inq_natts(fd)
|
|
50
|
-
unlimdim = nc_inq_unlimdim(fd)
|
|
51
|
-
|
|
52
|
-
oldmode = nc_set_fill(mode)
|
|
53
|
-
|
|
54
|
-
mode : NC_FILL
|
|
55
|
-
NC_NOFILL
|
|
56
|
-
|
|
57
|
-
### 2.3. NetCDF Dimension
|
|
58
|
-
|
|
59
|
-
dimid = nc_def_dim(fd, dimname, len)
|
|
60
|
-
dimid = nc_inq_dimid(fd, dimname) => Integer | nil
|
|
61
|
-
|
|
62
|
-
dimname = nc_inq_dimname(fd, dimid)
|
|
63
|
-
dimlen = nc_inq_dimlen(fd, dimid)
|
|
64
|
-
|
|
65
|
-
nc_rename_dim(fd, dimid, newname)
|
|
66
|
-
|
|
67
|
-
### 2.4. NetCDF Variable
|
|
68
|
-
|
|
69
|
-
varid = nc_def_var(fd, varname, type, dim)
|
|
70
|
-
varid = nc_inq_varid(fd, varname) => Integer | nil
|
|
71
|
-
|
|
72
|
-
vartype = nc_inq_vartype(fd, varid)
|
|
73
|
-
varndims = nc_inq_varndims(fd, varid)
|
|
74
|
-
vardimid = nc_inq_varndimid(fd, varid) => Array of dimids
|
|
75
|
-
|
|
76
|
-
nc_put_var1(fd, varid, [i,j,..], val)
|
|
77
|
-
nc_put_var(fd, varid, ca)
|
|
78
|
-
nc_put_vara(fd, varid, start, count, ca)
|
|
79
|
-
nc_put_vars(fd, varid, start, count, stride, ca)
|
|
80
|
-
nc_put_varm(fd, varid, start, count, stride, imap, ca)
|
|
81
|
-
|
|
82
|
-
+ with data_type conversion
|
|
83
|
-
|
|
84
|
-
val = nc_get_var1(fd, varid, [i,j,..])
|
|
85
|
-
ca = nc_get_var(fd, varid) [useful]
|
|
86
|
-
ca = nc_get_vara(fd, varid, start, count) [useful]
|
|
87
|
-
ca = nc_get_vars(fd, varid, start, count, stride) [useful]
|
|
88
|
-
ca = nc_get_varm(fd, varid, start, count, stride, imap) [useful]
|
|
89
|
-
|
|
90
|
-
+ no data_type conversion
|
|
91
|
-
|
|
92
|
-
nc_get_var(fd, varid, ca) [pedantic]
|
|
93
|
-
nc_get_vara(fd, varid, start, count, ca) [pedantic]
|
|
94
|
-
nc_get_vars(fd, varid, start, count, stride, ca) [pedantic]
|
|
95
|
-
nc_get_varm(fd, varid, start, count, stride, imap, ca) [pedantic]
|
|
96
|
-
|
|
97
|
-
+ with data_type conversion.
|
|
98
|
-
|
|
99
|
-
nc_rename_var(fd, varid, newname)
|
|
100
|
-
|
|
101
|
-
### 2.5. NetCDF Attribute
|
|
102
|
-
|
|
103
|
-
varnatts = nc_inq_varnatts(fd, varid)
|
|
104
|
-
attid = nc_inq_attid(fd, varid, attname)
|
|
105
|
-
|
|
106
|
-
attname = nc_inq_attname(fd, varid, attid)
|
|
107
|
-
atttype = nc_inq_atttype(fd, varid, attname)
|
|
108
|
-
attlen = nc_inq_attlen(fd, varid, attname)
|
|
109
|
-
|
|
110
|
-
nc_put_att(fd, varid, attname, val)
|
|
111
|
-
|
|
112
|
-
varid : NC_GLOBAL for global att
|
|
113
|
-
val : String -> NC_CHAR
|
|
114
|
-
Integer -> NC_INT
|
|
115
|
-
Float -> NC_DOUBLE
|
|
116
|
-
CArray -> NC.nc_type(val.data_type)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
val = nc_get_att(fd, varid, attname) => String | Numeric | CArray | nil
|
|
120
|
-
|
|
121
|
-
varid : NC_GLOBAL for global att
|
|
122
|
-
|
|
123
|
-
nc_get_att(fd, varid, attname, ca) [pedantic]
|
|
124
|
-
|
|
125
|
-
nc_rename_att(fd, varid, attname, newname)
|
|
126
|
-
nc_del_att(fd, varid, attname)
|
|
127
|
-
|
|
128
|
-
nc_copy_att(fd1, varid1, attname, fd2, varid2)
|
|
129
|
-
|
|
130
|
-
### 2.6 Constants
|
|
131
|
-
|
|
132
|
-
NC_NAT
|
|
133
|
-
NC_BYTE
|
|
134
|
-
NC_SHORT
|
|
135
|
-
NC_INT
|
|
136
|
-
NC_FLOAT
|
|
137
|
-
NC_DOUBLE
|
|
138
|
-
|
|
139
|
-
NC_NOERR - status of API routines
|
|
140
|
-
|
|
141
|
-
NC_CLOBBER - permit overwrite [nc_create]
|
|
142
|
-
NC_NOCLOBBER - inhibit overwrite [nc_create]
|
|
143
|
-
NC_NOWRITE - readonly [nc_open]
|
|
144
|
-
NC_WRITE - writable [nc_open]
|
|
145
|
-
NC_SHARE - no buffering [nc_create, nc_open]
|
|
146
|
-
|
|
147
|
-
NC_GLOBAL - varid for global attributes
|
|
148
|
-
|
|
149
|
-
NC_NOFILL - nc_set_fill(fd, varid, NC_NOFILL)
|
|
150
|
-
NC_FILL - nc_set_fill(fd, varid, NC_FILL)
|
|
151
|
-
|
|
152
|
-
NC_MAX_NAME
|
|
153
|
-
NC_MAX_VAR_DIMS
|
|
154
|
-
NC_MAX_DIMS
|
|
155
|
-
|
|
156
|
-
NC_LOCK - ???
|
|
157
|
-
NC_SIZEHINT_DEFAULT - ???
|
|
158
|
-
|
|
159
|
-
3. NCFile interface
|
|
160
|
-
-------------------
|
|
161
|
-
|
|
162
|
-
NCFile interface provides the way of *READ-ONLY* access to the netcdf file.
|
|
163
|
-
|
|
164
|
-
### 3.1. NCFile
|
|
165
|
-
|
|
166
|
-
nc = NCFile.open(FILENAME)
|
|
167
|
-
|
|
168
|
-
nc = NCFile.new(file_id)
|
|
169
|
-
file_id: return value of nc_open in read-only mode
|
|
170
|
-
|
|
171
|
-
### 3.2. Dimensions
|
|
172
|
-
|
|
173
|
-
nc.has_dim?(DIMNAME)
|
|
174
|
-
nc.dims - Array of dimension
|
|
175
|
-
|
|
176
|
-
dim = nc.dim(DIMNAME) - NCDim object or nil
|
|
177
|
-
dim.to_i - size of dimension
|
|
178
|
-
dim.to_ca
|
|
179
|
-
|
|
180
|
-
### 3.3. Variables
|
|
181
|
-
|
|
182
|
-
nc.has_var?(VARNAME)
|
|
183
|
-
nc.vars - Array of variables
|
|
184
|
-
|
|
185
|
-
var = nc.var(VARNAME) - NCVar object or nil
|
|
186
|
-
var.is_dim? - true if dimension variable
|
|
187
|
-
var.to_ca - get array as CArray object
|
|
188
|
-
var[...] - Cooked value array with CArray-like indexing
|
|
189
|
-
var.get!(...) - same as [...]
|
|
190
|
-
var.get(...) - Non-cooked value array with CArray-like indexing
|
|
191
|
-
|
|
192
|
-
var.get_var1(...) - interface to original get function
|
|
193
|
-
var.get_var()
|
|
194
|
-
var.get_vara(start, count)
|
|
195
|
-
var.get_vars(start, count, stride)
|
|
196
|
-
var.get_varm(start, count, stride, imap)
|
|
197
|
-
var.get_var1!(...)
|
|
198
|
-
var.get_var!()
|
|
199
|
-
var.get_vara!(start, count)
|
|
200
|
-
var.get_vars!(start, count, stride)
|
|
201
|
-
var.get_varm!(start, count, stride, imap)
|
|
202
|
-
|
|
203
|
-
### 3.4. Attributes
|
|
204
|
-
|
|
205
|
-
All attributes are already read in initializing process of NCFile object.
|
|
206
|
-
|
|
207
|
-
nc.attributes - Hash
|
|
208
|
-
dim.attributes
|
|
209
|
-
var.attributes
|
|
210
|
-
|
|
211
|
-
nc.attribute(ATTNAME) - accessor
|
|
212
|
-
dim.attribute(ATTNAME)
|
|
213
|
-
var.attribute(ATTNAME)
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
### 3.5. Iteration
|
|
217
|
-
|
|
218
|
-
No iteration methods is provided, but accessor methods for dims, vars, attributes can be used.
|
|
219
|
-
|
|
220
|
-
ex)
|
|
221
|
-
|
|
222
|
-
nc.attributes.each {|key, value| ... }
|
|
223
|
-
nc.dims.each {|dim| ... }
|
|
224
|
-
nc.vars.each {|var| ... }
|
|
225
|
-
|
|
226
|
-
4. NCFileWriter interface
|
|
227
|
-
-------------------------
|
|
228
|
-
|
|
229
|
-
It provides simple interface to create netcdf file.
|
|
230
|
-
If you want the detailed controling to create netcdf, use nc_function API.
|
|
231
|
-
|
|
232
|
-
out = NCFileWrite.new("test.nc")
|
|
233
|
-
|
|
234
|
-
out.define(
|
|
235
|
-
dimensions: { ### dimension
|
|
236
|
-
lon: 230, ### name: Integer
|
|
237
|
-
lat: 120,
|
|
238
|
-
time: 24
|
|
239
|
-
},
|
|
240
|
-
variables: { ### variables
|
|
241
|
-
temp: { name: definition
|
|
242
|
-
type: int, type: Integer or string (default NC::NC_FLOAT)
|
|
243
|
-
one of byte,short,int,float,double
|
|
244
|
-
dim: ["time", "lat", "lon"], dim: Array of dim names
|
|
245
|
-
long_name: "air temperature", attributes for variable are parallel with type, dim
|
|
246
|
-
units: "degC"
|
|
247
|
-
param1: { float: 1.2 }
|
|
248
|
-
param2: { int: 10 }
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
attributes: {
|
|
252
|
-
creator: "foobar" ### global attributes (Hash)
|
|
253
|
-
}
|
|
254
|
-
)
|
|
255
|
-
|
|
256
|
-
out["lon"] = lon
|
|
257
|
-
out["lat"] = lat
|
|
258
|
-
out["time"] = time
|
|
259
|
-
out["temp"][nil] = temp ### CArray index can be used
|
|
260
|
-
out.write ### call nc_close
|
|
261
|
-
|
|
262
|
-
nc = NCFile.open("test.nc")
|
|
263
|
-
nc.definition ### return definition Hash
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
### template with CDL
|
|
267
|
-
|
|
268
|
-
out = NCFileWrite.new("test.nc", template: cdl_text)
|
|
269
|
-
|
|
270
|
-
|