Method: AEMO::NEM12#parse_nem12_300

Defined in:
lib/aemo/nem12.rb

#parse_nem12_300(line, strict: true) ⇒ Array of hashes

Returns the line parsed into a hash of information.

Parameters:

  • line (String)

    A single line in string format

  • strict (Boolean) (defaults to: true)

Returns:

  • (Array of hashes)

    the line parsed into a hash of information

Raises:

  • (TypeError)

Since:

  • 0.1.4

[View source]

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/aemo/nem12.rb', line 202

def parse_nem12_300(line, strict: true) # rubocop:disable Naming/VariableNumber
  csv = line.parse_csv

  if @data_details.last.nil? || @data_details.last[:interval_length].nil?
    raise TypeError,
          'Expected NMI Data Details to exist with IntervalLength specified'
  end

  # ref: AEMO's MDFF Spec NEM12 and NEM13 v1.01 (2014-05-14)
  record_fixed_fields = %w[RecordIndicator IntervalDate QualityMethod ReasonCode ReasonDescription UpdateDatetime MSATSLoadDateTime]
  number_of_intervals = 1440 / @data_details.last[:interval_length]

  raise TypeError, 'Invalid record length' if csv.length != record_fixed_fields.length + number_of_intervals

  intervals_offset = number_of_intervals + 2

  raise ArgumentError, 'RecordIndicator is not 300' if csv[0] != '300'
  raise ArgumentError, 'IntervalDate is not valid' unless AEMO::Time.valid_timestamp8?(csv[1])

  (2..(number_of_intervals + 1)).each do |i|
    raise ArgumentError, "Interval number #{i - 1} is not valid" if csv[i].nil? || csv[i].match(/\d+(\.\d+)?/).nil?
  end

  raise ArgumentError, 'QualityMethod is not valid' unless csv[intervals_offset + 0].instance_of?(String)
  raise ArgumentError, 'QualityMethod does not have valid length' unless [1, 3].include?(csv[intervals_offset + 0].length)

  unless QUALITY_FLAGS.keys.include?(csv[intervals_offset + 0][0])
    raise ArgumentError,
          'QualityMethod does not have valid QualityFlag'
  end

  unless %w[A N V].include?(csv[intervals_offset + 0][0])
    raise ArgumentError, 'QualityMethod does not have valid length' unless csv[intervals_offset + 0].length == 3

    unless METHOD_FLAGS.keys.include?(csv[intervals_offset + 0][1..2].to_i)
      raise ArgumentError,
            'QualityMethod does not have valid MethodFlag'
    end
  end

  raise ArgumentError, 'ReasonCode is not valid' if !%w[A N E].include?(csv[intervals_offset + 0][0]) && !REASON_CODES.keys.include?(csv[intervals_offset + 1].to_i)

  if !csv[intervals_offset + 1].nil? && csv[intervals_offset + 1].to_i.zero? && !(csv[intervals_offset + 2].instance_of?(String) && !csv[intervals_offset + 2].empty?)
    raise ArgumentError,
          'ReasonDescription is not valid'
  end

  if strict
    unless AEMO::Time.valid_timestamp14?(csv[intervals_offset + 3])
      raise ArgumentError,
            'UpdateDateTime is not valid'
    end

    if !csv[intervals_offset + 4].blank? && !AEMO::Time.valid_timestamp14?(csv[intervals_offset + 4])
      raise ArgumentError,
            'MSATSLoadDateTime is not valid'
    end
  end

  # Deal with flags if necessary
  flag = nil
  # Based on QualityMethod and ReasonCode
  if csv[intervals_offset + 0].length == 3 || !csv[intervals_offset + 1].nil?
    flag ||= { quality_flag: nil, method_flag: nil, reason_code: nil }
    if csv[intervals_offset + 0].length == 3
      flag[:quality_flag] = csv[intervals_offset + 0][0]
      flag[:method_flag] = csv[intervals_offset + 0][1, 2].to_i
    end
    flag[:reason_code] = csv[intervals_offset + 1].to_i unless csv[intervals_offset + 1].nil?
  end

  # Deal with updated_at & msats_load_at
  updated_at = nil
  msats_load_at = nil

  if strict
    updated_at = AEMO::Time.parse_timestamp14(csv[intervals_offset + 3]) unless csv[intervals_offset + 3].blank?
    msats_load_at = AEMO::Time.parse_timestamp14(csv[intervals_offset + 4]) unless csv[intervals_offset + 4].blank?
  end

  base_interval = {
    data_details: @data_details.last,
    datetime: AEMO::Time.parse_timestamp8(csv[1]),
    value: nil,
    flag:,
    updated_at:,
    msats_load_at:
  }

  intervals = []
  (2..(number_of_intervals + 1)).each do |i|
    interval = base_interval.dup
    interval[:datetime] += (i - 1) * interval[:data_details][:interval_length] * 60
    interval[:value] = csv[i].to_f
    intervals << interval
  end
  @interval_data += intervals
  intervals
end