Class: DataRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/data_request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ DataRequest

Returns a new instance of DataRequest.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/data_request.rb', line 58

def initialize(paths)
  @tables = paths.map {|x| DataRequestTable.new(x)}
  @tables.each {|x| raise "tables have different data request versions (#{@tables.first.version}@#{@tables.first.path} vs #{x.version}@#{x.path})" if @tables.first.version != x.version}
  @tables = @tables.sort_by {|t| t.table_id}
  
  # merge variables with identical variable_id and frequency which may appear in multiple tables
  # sort by name+interval+frequency, so we the following order:
  #      var1 0.125 3hr   [table1]
  #      var2 0.125 3hr   [table42]
  #      var2 0.125 3hrPt [table1]
  vars = @tables.collect_concat {|t| t.variable_entries}
  merged_vars = []
  # sort vars by merge criterium (variable_id, unit, time_method) and additionally by interval and table_id
  vars = vars.sort_by {|v| "#{v.variable_id} #{v.unit} #{v.time_method} #{v.table.approx_interval} #{v.table.table_id}"}
  vars.each do |v|
    # merge vars with equal variable_id, unit, time_method
    if(merged_vars.last && merged_vars.last.variable_id == v.variable_id && merged_vars.last.unit == v.unit && merged_vars.last.time_method == v.time_method)
      merged_vars.last.merge_table_var_entry(v)
    else
      merged_vars << DataRequestVariable.new_from_table_var_entry(v)
    end
  end
  
  @variables = merged_vars
end

Instance Attribute Details

#variablesObject (readonly)

Returns the value of attribute variables.



6
7
8
# File 'lib/data_request.rb', line 6

def variables
  @variables
end

Class Method Details

.approx_interval_for_table(table) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/data_request.rb', line 9

def self.approx_interval_for_table(table)
  approx_interval = {"3hr" => 3.0/24,
  "6hrLev" => 6.0/24,
  "6hrPlev" => 6.0/24,
  "6hrPlevPt" => 6.0/24,
  "AERday" => 1.0,
  "AERhr" => 1.0/24, # note, data request 01.00.27 says "1.0" here, but this seems to be wrong
  "AERmon" => 30.0,
  "AERmonZ" => 30.0,
  "Amon" => 30.0,
  "CF3hr" => 3.0/24,
  "CFday" => 1.0,
  "CFmon" => 30.0,
  "day" => 1.0,
  "E3hr" => 3.0/24,
  "E3hrPt" => 3.0/24,
  "E6hrZ" => 6.0/24,
  "Eday" => 1.0,
  "EdayZ" => 1.0,
  "Emon" => 30.0,
  "EmonZ" => 30.0,
  "Eyr" => 365.0,
  "ImonAnt" => 30.0,
  "ImonGre" => 30.0,
  "IyrAnt" => 365.0,
  "IyrGre" => 365.0,
  "LImon" => 30.0,
  "Lmon" => 30.0,
  "Oclim" => 30.0,
  "Oday" => 1.0,
  "Odec" => 3650.0,
  "Omon" => 30.0,
  "Oyr" => 365.0,
  "SIday" => 1.0,
  "SImon" => 30.0}[table]

  raise "can not find approx_interval for table #{table}" unless approx_interval
  approx_interval
end

.new_from_tables_dir(path) ⇒ Object

create from all tables in given dir, omitting non-table files from the default tables directory at github.com/PCMDI/cmip6-cmor-tables



51
52
53
54
55
# File 'lib/data_request.rb', line 51

def self.new_from_tables_dir(path)
  eliglible_files = Dir["#{path}/CMIP6_*.json"]-["#{path}/CMIP6_CV_test.json", "#{path}/CMIP6_coordinate.json", "#{path}/CMIP6_CV.json", "#{path}/CMIP6_formula_terms.json", "#{path}/CMIP6_grids.json", "#{path}/CMIP6_input_example.json"]
  raise "no eliglible json tables found at path <#{path}>" if eliglible_files.empty?
  DataRequest.new eliglible_files
end

Instance Method Details

#find(variable_id, frequency_name) ⇒ Object



85
86
87
# File 'lib/data_request.rb', line 85

def find(variable_id, frequency_name)
  @variables.find {|v| variable_id == v.variable_id && v.frequencies.include?(frequency_name)}
end

#find_variable_id_in_table_id(variable_id, table_id) ⇒ Object



90
91
92
# File 'lib/data_request.rb', line 90

def find_variable_id_in_table_id(variable_id, table_id)
  @variables.find {|v| variable_id == v.variable_id && v.table_ids.include?(table_id)}
end

#table_idsObject



105
106
107
# File 'lib/data_request.rb', line 105

def table_ids
  @tables.collect_concat {|t| t.table_id}
end

#to_sObject

all variables and frequencies as string, so one knows which data to generate for a simulation (table names are appended) sorted by name+interval+frequency, so we the following order:

var1 0.125 3hr   [table1]
var2 0.125 3hr   [table42]
var2 0.125 3hrPt [table1]


115
116
117
118
119
120
121
# File 'lib/data_request.rb', line 115

def to_s
  s = "=== #{version} ===\n"
  @variables.each do |v|
    s += "#{v}\n"
  end
  s
end

#variable_idsObject



95
96
97
# File 'lib/data_request.rb', line 95

def variable_ids
  @variables.map {|v| v.variable_id}
end

#versionObject



100
101
102
# File 'lib/data_request.rb', line 100

def version
  @tables.first.version
end