Class: DirectoryCreator

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

Class Method Summary collapse

Class Method Details

.create_directories(files) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/directory_creator.rb', line 55

def self.create_directories(files)

  files.each do |file|
    subdirs = DirectoryCreator.subdirs_for_cmor_file file
    system "mkdir -p '#{File.join subdirs}'"
    cmd = "ln #{file} #{File.join(subdirs, File.basename(file))}"
    puts cmd
    system cmd
  end
end

.subdirs_for_cmor_file(f) ⇒ Object



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
48
49
50
51
52
# File 'lib/directory_creator.rb', line 21

def self.subdirs_for_cmor_file(f)
  names = %w(mip_era activity_id institution_id source_id experiment_id sub_experiment_id variant_label table_id variable_id grid_label creation_date)

  ncdump_h_txt = %x(ncdump -h #{f})
  global_attr_txt = ncdump_h_txt.split('// global attributes:').last
  values = {}

  names.each do |n|
    match = /:#{n} = "(?<val>.*?)" ;/.match(global_attr_txt)
    raise "can not find global attribute \"#{n}\"" unless match
    values[n] = match[:val]
  end

  dir_templates = %w(mip_era activity_id institution_id source_id experiment_id member_id table_id variable_id grid_label version)
  dir_names = dir_templates.map do |t|
    if t == "member_id"
      if(values['sub_experiment_id'] == "none")
        values['variant_label']
      else
        "#{values['sub_experiment_id']}-#{values['variant_label']}"
      end
    elsif t == "version"
      (Date.parse values['creation_date']).to_time.strftime "v%Y%m%d"
    elsif t == "activity_id" # an exception for this exists: "If multiple activities are listed in the global attribute, the first one is used in the directory structure."
      values[t].split.first
    else
      values[t]
    end
  end

  dir_names
end