Class: TCD::Header
- Inherits:
-
Object
- Object
- TCD::Header
- Defined in:
- lib/tcd/header.rb
Overview
Parser for TCD ASCII header section. The header contains [KEY] = VALUE pairs defining all encoding parameters.
Constant Summary collapse
- REQUIRED_KEYS =
%i[ header_size number_of_records constituents start_year number_of_years ].freeze
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access header parameters by symbol key.
- #amplitude_bits ⇒ Object
- #amplitude_scale ⇒ Object
-
#compute_checksum(io) ⇒ Object
CRC-32 of the ASCII header padded to header_size bytes, as libtcd's header_checksum() computes it.
- #confidence_value_bits ⇒ Object
- #constituent_bits ⇒ Object
- #constituent_size ⇒ Object
- #constituents ⇒ Object
- #countries ⇒ Object
- #country_bits ⇒ Object
- #country_size ⇒ Object
- #date_bits ⇒ Object
- #datum_bits ⇒ Object
- #datum_offset_bits ⇒ Object
- #datum_offset_scale ⇒ Object
- #datum_size ⇒ Object
- #datum_types ⇒ Object
- #direction_bits ⇒ Object
- #direction_unit_bits ⇒ Object
- #direction_unit_size ⇒ Object
- #direction_unit_types ⇒ Object
- #end_of_file ⇒ Object
- #epoch_bits ⇒ Object
- #epoch_scale ⇒ Object
- #equilibrium_bits ⇒ Object
- #equilibrium_offset ⇒ Object
- #equilibrium_scale ⇒ Object
-
#header_size ⇒ Object
Convenience accessors for commonly used parameters.
-
#initialize(io) ⇒ Header
constructor
A new instance of Header.
-
#key?(key) ⇒ Boolean
Check if key exists.
-
#keys ⇒ Object
All parameter keys.
- #last_modified ⇒ Object
- #latitude_bits ⇒ Object
- #latitude_scale ⇒ Object
- #legalese_bits ⇒ Object
- #legalese_size ⇒ Object
- #legalese_types ⇒ Object
- #level_add_bits ⇒ Object
- #level_add_scale ⇒ Object
- #level_multiply_bits ⇒ Object
- #level_multiply_scale ⇒ Object
-
#level_unit_bits ⇒ Object
Lookup table parameters.
- #level_unit_size ⇒ Object
- #level_unit_types ⇒ Object
- #longitude_bits ⇒ Object
- #longitude_scale ⇒ Object
- #major_rev ⇒ Object
- #minor_rev ⇒ Object
- #months_on_station_bits ⇒ Object
- #node_bits ⇒ Object
- #node_offset ⇒ Object
- #node_scale ⇒ Object
- #number_of_records ⇒ Object
- #number_of_years ⇒ Object
- #record_size_bits ⇒ Object
-
#record_type_bits ⇒ Object
Record field parameters.
- #restriction_bits ⇒ Object
- #restriction_size ⇒ Object
- #restriction_types ⇒ Object
-
#speed_bits ⇒ Object
Bit field parameters.
- #speed_offset ⇒ Object
- #speed_scale ⇒ Object
- #start_year ⇒ Object
- #station_bits ⇒ Object
-
#stored_checksum(io) ⇒ Object
The CRC-32 stored in the 4 bytes after the header (big-endian).
- #time_bits ⇒ Object
- #tzfile_bits ⇒ Object
- #tzfile_size ⇒ Object
- #tzfiles ⇒ Object
-
#verify_checksum!(io) ⇒ Object
Raises ChecksumError unless the stored checksum matches the header.
- #version ⇒ Object
Constructor Details
#initialize(io) ⇒ Header
Returns a new instance of Header.
16 17 18 19 20 |
# File 'lib/tcd/header.rb', line 16 def initialize(io) @params = {} parse(io) validate! end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
14 15 16 |
# File 'lib/tcd/header.rb', line 14 def params @params end |
Instance Method Details
#[](key) ⇒ Object
Access header parameters by symbol key
23 24 25 |
# File 'lib/tcd/header.rb', line 23 def [](key) @params[key] end |
#amplitude_bits ⇒ Object
59 |
# File 'lib/tcd/header.rb', line 59 def amplitude_bits; @params[:amplitude_bits]; end |
#amplitude_scale ⇒ Object
60 |
# File 'lib/tcd/header.rb', line 60 def amplitude_scale; @params[:amplitude_scale]; end |
#compute_checksum(io) ⇒ Object
CRC-32 of the ASCII header padded to header_size bytes, as libtcd's header_checksum() computes it.
111 112 113 114 115 116 117 118 |
# File 'lib/tcd/header.rb', line 111 def compute_checksum(io) io.seek(0) data = io.read(header_size) if data.nil? || data.bytesize < header_size raise FormatError, "file is shorter than its #{header_size}-byte header" end Zlib.crc32(data) end |
#confidence_value_bits ⇒ Object
76 |
# File 'lib/tcd/header.rb', line 76 def confidence_value_bits; @params[:confidence_value_bits]; end |
#constituent_bits ⇒ Object
100 |
# File 'lib/tcd/header.rb', line 100 def constituent_bits; @params[:constituent_bits]; end |
#constituent_size ⇒ Object
101 |
# File 'lib/tcd/header.rb', line 101 def constituent_size; @params[:constituent_size]; end |
#constituents ⇒ Object
40 |
# File 'lib/tcd/header.rb', line 40 def constituents; @params[:constituents]; end |
#countries ⇒ Object
106 |
# File 'lib/tcd/header.rb', line 106 def countries; @params[:countries]; end |
#country_bits ⇒ Object
105 |
# File 'lib/tcd/header.rb', line 105 def country_bits; @params[:country_bits]; end |
#country_size ⇒ Object
107 |
# File 'lib/tcd/header.rb', line 107 def country_size; @params[:country_size]; end |
#date_bits ⇒ Object
74 |
# File 'lib/tcd/header.rb', line 74 def date_bits; @params[:date_bits]; end |
#datum_bits ⇒ Object
94 |
# File 'lib/tcd/header.rb', line 94 def datum_bits; @params[:datum_bits]; end |
#datum_offset_bits ⇒ Object
72 |
# File 'lib/tcd/header.rb', line 72 def datum_offset_bits; @params[:datum_offset_bits]; end |
#datum_offset_scale ⇒ Object
73 |
# File 'lib/tcd/header.rb', line 73 def datum_offset_scale; @params[:datum_offset_scale]; end |
#datum_size ⇒ Object
96 |
# File 'lib/tcd/header.rb', line 96 def datum_size; @params[:datum_size]; end |
#datum_types ⇒ Object
95 |
# File 'lib/tcd/header.rb', line 95 def datum_types; @params[:datum_types]; end |
#direction_bits ⇒ Object
82 |
# File 'lib/tcd/header.rb', line 82 def direction_bits; @params[:direction_bits]; end |
#direction_unit_bits ⇒ Object
88 |
# File 'lib/tcd/header.rb', line 88 def direction_unit_bits; @params[:direction_unit_bits]; end |
#direction_unit_size ⇒ Object
90 |
# File 'lib/tcd/header.rb', line 90 def direction_unit_size; @params[:direction_unit_size]; end |
#direction_unit_types ⇒ Object
89 |
# File 'lib/tcd/header.rb', line 89 def direction_unit_types; @params[:direction_unit_types]; end |
#end_of_file ⇒ Object
47 |
# File 'lib/tcd/header.rb', line 47 def end_of_file; @params[:end_of_file]; end |
#epoch_bits ⇒ Object
61 |
# File 'lib/tcd/header.rb', line 61 def epoch_bits; @params[:epoch_bits]; end |
#epoch_scale ⇒ Object
62 |
# File 'lib/tcd/header.rb', line 62 def epoch_scale; @params[:epoch_scale]; end |
#equilibrium_bits ⇒ Object
53 |
# File 'lib/tcd/header.rb', line 53 def equilibrium_bits; @params[:equilibrium_bits]; end |
#equilibrium_offset ⇒ Object
55 |
# File 'lib/tcd/header.rb', line 55 def equilibrium_offset; @params[:equilibrium_offset]; end |
#equilibrium_scale ⇒ Object
54 |
# File 'lib/tcd/header.rb', line 54 def equilibrium_scale; @params[:equilibrium_scale]; end |
#header_size ⇒ Object
Convenience accessors for commonly used parameters
38 |
# File 'lib/tcd/header.rb', line 38 def header_size; @params[:header_size]; end |
#key?(key) ⇒ Boolean
Check if key exists
28 29 30 |
# File 'lib/tcd/header.rb', line 28 def key?(key) @params.key?(key) end |
#keys ⇒ Object
All parameter keys
33 34 35 |
# File 'lib/tcd/header.rb', line 33 def keys @params.keys end |
#last_modified ⇒ Object
46 |
# File 'lib/tcd/header.rb', line 46 def last_modified; @params[:last_modified]; end |
#latitude_bits ⇒ Object
66 |
# File 'lib/tcd/header.rb', line 66 def latitude_bits; @params[:latitude_bits]; end |
#latitude_scale ⇒ Object
67 |
# File 'lib/tcd/header.rb', line 67 def latitude_scale; @params[:latitude_scale]; end |
#legalese_bits ⇒ Object
97 |
# File 'lib/tcd/header.rb', line 97 def legalese_bits; @params[:legalese_bits]; end |
#legalese_size ⇒ Object
99 |
# File 'lib/tcd/header.rb', line 99 def legalese_size; @params[:legalese_size]; end |
#legalese_types ⇒ Object
98 |
# File 'lib/tcd/header.rb', line 98 def legalese_types; @params[:legalese_types]; end |
#level_add_bits ⇒ Object
78 |
# File 'lib/tcd/header.rb', line 78 def level_add_bits; @params[:level_add_bits]; end |
#level_add_scale ⇒ Object
79 |
# File 'lib/tcd/header.rb', line 79 def level_add_scale; @params[:level_add_scale]; end |
#level_multiply_bits ⇒ Object
80 |
# File 'lib/tcd/header.rb', line 80 def level_multiply_bits; @params[:level_multiply_bits]; end |
#level_multiply_scale ⇒ Object
81 |
# File 'lib/tcd/header.rb', line 81 def level_multiply_scale; @params[:level_multiply_scale]; end |
#level_unit_bits ⇒ Object
Lookup table parameters
85 |
# File 'lib/tcd/header.rb', line 85 def level_unit_bits; @params[:level_unit_bits]; end |
#level_unit_size ⇒ Object
87 |
# File 'lib/tcd/header.rb', line 87 def level_unit_size; @params[:level_unit_size]; end |
#level_unit_types ⇒ Object
86 |
# File 'lib/tcd/header.rb', line 86 def level_unit_types; @params[:level_unit_types]; end |
#longitude_bits ⇒ Object
68 |
# File 'lib/tcd/header.rb', line 68 def longitude_bits; @params[:longitude_bits]; end |
#longitude_scale ⇒ Object
69 |
# File 'lib/tcd/header.rb', line 69 def longitude_scale; @params[:longitude_scale]; end |
#major_rev ⇒ Object
44 |
# File 'lib/tcd/header.rb', line 44 def major_rev; @params[:major_rev]; end |
#minor_rev ⇒ Object
45 |
# File 'lib/tcd/header.rb', line 45 def minor_rev; @params[:minor_rev]; end |
#months_on_station_bits ⇒ Object
75 |
# File 'lib/tcd/header.rb', line 75 def months_on_station_bits; @params[:months_on_station_bits]; end |
#node_bits ⇒ Object
56 |
# File 'lib/tcd/header.rb', line 56 def node_bits; @params[:node_bits]; end |
#node_offset ⇒ Object
58 |
# File 'lib/tcd/header.rb', line 58 def node_offset; @params[:node_offset]; end |
#node_scale ⇒ Object
57 |
# File 'lib/tcd/header.rb', line 57 def node_scale; @params[:node_scale]; end |
#number_of_records ⇒ Object
39 |
# File 'lib/tcd/header.rb', line 39 def number_of_records; @params[:number_of_records]; end |
#number_of_years ⇒ Object
42 |
# File 'lib/tcd/header.rb', line 42 def number_of_years; @params[:number_of_years]; end |
#record_size_bits ⇒ Object
70 |
# File 'lib/tcd/header.rb', line 70 def record_size_bits; @params[:record_size_bits]; end |
#record_type_bits ⇒ Object
Record field parameters
65 |
# File 'lib/tcd/header.rb', line 65 def record_type_bits; @params[:record_type_bits]; end |
#restriction_bits ⇒ Object
91 |
# File 'lib/tcd/header.rb', line 91 def restriction_bits; @params[:restriction_bits]; end |
#restriction_size ⇒ Object
93 |
# File 'lib/tcd/header.rb', line 93 def restriction_size; @params[:restriction_size]; end |
#restriction_types ⇒ Object
92 |
# File 'lib/tcd/header.rb', line 92 def restriction_types; @params[:restriction_types]; end |
#speed_bits ⇒ Object
Bit field parameters
50 |
# File 'lib/tcd/header.rb', line 50 def speed_bits; @params[:speed_bits]; end |
#speed_offset ⇒ Object
52 |
# File 'lib/tcd/header.rb', line 52 def speed_offset; @params[:speed_offset]; end |
#speed_scale ⇒ Object
51 |
# File 'lib/tcd/header.rb', line 51 def speed_scale; @params[:speed_scale]; end |
#start_year ⇒ Object
41 |
# File 'lib/tcd/header.rb', line 41 def start_year; @params[:start_year]; end |
#station_bits ⇒ Object
71 |
# File 'lib/tcd/header.rb', line 71 def station_bits; @params[:station_bits]; end |
#stored_checksum(io) ⇒ Object
The CRC-32 stored in the 4 bytes after the header (big-endian).
121 122 123 124 125 126 |
# File 'lib/tcd/header.rb', line 121 def stored_checksum(io) io.seek(header_size) bytes = io.read(4) raise FormatError, "file ends before the header checksum" if bytes.nil? || bytes.bytesize < 4 bytes.unpack1("N") end |
#time_bits ⇒ Object
77 |
# File 'lib/tcd/header.rb', line 77 def time_bits; @params[:time_bits]; end |
#tzfile_bits ⇒ Object
102 |
# File 'lib/tcd/header.rb', line 102 def tzfile_bits; @params[:tzfile_bits]; end |
#tzfile_size ⇒ Object
104 |
# File 'lib/tcd/header.rb', line 104 def tzfile_size; @params[:tzfile_size]; end |
#tzfiles ⇒ Object
103 |
# File 'lib/tcd/header.rb', line 103 def tzfiles; @params[:tzfiles]; end |
#verify_checksum!(io) ⇒ Object
Raises ChecksumError unless the stored checksum matches the header. libtcd refuses to open such a file.
130 131 132 133 134 135 136 137 |
# File 'lib/tcd/header.rb', line 130 def verify_checksum!(io) stored = stored_checksum(io) computed = compute_checksum(io) return if stored == computed raise ChecksumError, format("header checksum mismatch: stored 0x%08x, computed 0x%08x " \ "(the ASCII header was modified or the file is corrupt)", stored, computed) end |
#version ⇒ Object
43 |
# File 'lib/tcd/header.rb', line 43 def version; @params[:version]; end |