Class: IconPlot

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

Constant Summary collapse

VERSION =
'0.0.4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ IconPlot

Returns a new instance of IconPlot.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/iconPlot.rb', line 10

def initialize(*args)
  super(*args)
  
  cdoPath = ENV['CDO'].nil? ? 'cdo' : ENV['CDO']

  defaults = {
    :caller  => Gem.bin_path('iconPlot','nclsh'),
    :plotter => Gem.find_files("icon_plot.ncl")[0],
    :libdir  => File.dirname(Gem.find_files("icon_plot.ncl")[0]),
    :otype   => 'png',
    :display => 'sxiv',
    :cdo     => Cdo.new(cdo: cdoPath),
    :isIcon  => true,
    :debug   => false
  }
  self.each_pair {|k,v| self[k] = defaults[k] if v.nil? }
end

Instance Attribute Details

#callerObject

Returns the value of attribute caller

Returns:

  • (Object)

    the current value of caller



7
8
9
# File 'lib/iconPlot.rb', line 7

def caller
  @caller
end

#cdoObject

Returns the value of attribute cdo

Returns:

  • (Object)

    the current value of cdo



7
8
9
# File 'lib/iconPlot.rb', line 7

def cdo
  @cdo
end

#debugObject

Returns the value of attribute debug

Returns:

  • (Object)

    the current value of debug



7
8
9
# File 'lib/iconPlot.rb', line 7

def debug
  @debug
end

#displayObject

Returns the value of attribute display

Returns:

  • (Object)

    the current value of display



7
8
9
# File 'lib/iconPlot.rb', line 7

def display
  @display
end

#isIconObject

Returns the value of attribute isIcon

Returns:

  • (Object)

    the current value of isIcon



7
8
9
# File 'lib/iconPlot.rb', line 7

def isIcon
  @isIcon
end

#libdirObject

Returns the value of attribute libdir

Returns:

  • (Object)

    the current value of libdir



7
8
9
# File 'lib/iconPlot.rb', line 7

def libdir
  @libdir
end

#otypeObject

Returns the value of attribute otype

Returns:

  • (Object)

    the current value of otype



7
8
9
# File 'lib/iconPlot.rb', line 7

def otype
  @otype
end

#plotterObject

Returns the value of attribute plotter

Returns:

  • (Object)

    the current value of plotter



7
8
9
# File 'lib/iconPlot.rb', line 7

def plotter
  @plotter
end

Instance Method Details

#createData(ifile, varname, operation) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/iconPlot.rb', line 120

def createData(ifile,varname,operation)
  # Temporal file for text output
  dataFile = MyTempfile.path

  # read the date
  IO.popen("echo 'date|time|depth|#{varname}' > #{dataFile}")
  self.cdo.debug = true
  self.cdo.outputkey('date,time,level,value', 
                :input => "-#{operation} -selname,#{varname} #{ifile} >>#{dataFile} 2>/dev/null")

  # postprocessing for correct time values
  data = []
  File.open(dataFile).each_with_index {|line,lineIndex|
    next if line.chomp.empty?
    _t = line.chomp.sub(/^ /,'').gsub(/ +/,'|').split('|')
    if 0 == lineIndex then
      data << _t
      next
    end
    if "0" == _t[1] then
      _t[1] = '00:00:00'
    else
     #time = _t[1].reverse
     #timeStr = ''
     #while time.size > 2 do
     #  timeStr << time[0,2] << ':'
     #  time = time[2..-1]
     #end
     #timeStr << time.ljust(2,'0') unless time.size == 0
     #_t[1] = timeStr.reverse
    end
    data << _t
  }
  data
end

#defaultPlot(ifile, ofile, opts = {}) ⇒ Object



113
114
115
# File 'lib/iconPlot.rb', line 113

def defaultPlot(ifile,ofile,opts={})
  show(scalarPlot(ifile,ofile,'T',opts))
end

#del(file) ⇒ Object



107
108
109
# File 'lib/iconPlot.rb', line 107

def del(file)
  FileUtils.rm(file) if File.exists?(file)
end

#levelPlot(ifile, ofile, varname, opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/iconPlot.rb', line 66

def levelPlot(ifile,ofile,varname,opts={})
  operation = opts[:operation].nil? ? 'fldmin' : opts[:operation]

  data = createData(ifile,varname,operation)
  icon = ExtCsv.new("array","plain",data.transpose)
  setDatetime(icon)

  # Plot data with automatic splitting by depth
  unless icon.datacolumns.include?(varname)
    warn "Variable cannot be found!"
    exit -1
  end
  self.cdo.debug = true
  unit = self.cdo.showunit(:input => "-selname,#{varname} #{ifile}").first
  ExtCsvDiagram.plot_xy(icon,"datetime",varname,
                        "ICON: #{operation} on #{varname} (file:#{ifile})", # Change title here
                      :label_position => 'below',:skipColumnCheck => true,
                      :type => 'lines',:groupBy => ["depth"], :onlyGroupTitle => true,
    #                     :addSettings => ["logscale y"],     # Commend theses out for large scale values like Vert_Mixing_V
    #                     :yrange => '[0.0001:10]',           # Otherwise you'll see nothing reasonable
    :terminal => true ? 'png' : 'x11',
    :ylabel => "#{varname} [#{Shellwords.escape(unit)}]",     # Correct the label if necessary
    :input_time_format => "'%Y-%m-%d %H:%M:%S'",
      :filename => ofile,
      :output_time_format => '"%d.%m.%y \n %H:%M"',:size => "1600,600")
    return "#{ofile}.png"
end

#plot(ifile, ofile, varname, vartype = 'scalar', opts = {}) ⇒ Object



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
53
54
55
56
57
58
59
# File 'lib/iconPlot.rb', line 27

def plot(ifile,ofile,varname,vartype='scalar',opts={})
  unless File.exists?(ifile)
    warn "Input file #{ifile} dows NOT exist!"
    exit
  end
  varIdent = case vartype.to_s
             when 'scalar'  then "-varName=#{varname}"
             when 'vector'  then "-vecVars=#{varname.split(' ').join(',')}"
             when 'scatter' then "-plotMode=scatter -vecVars#{varname.split(' ').join(',')}"
             else
               warn "Wrong variable type #{vartype}"
               raise ArgumentError
             end

  opts[:tStrg] = opts[:tStrg].nil? ? ofile : opts[:tStrg]

  cmd   = [self.caller,self.plotter].join(' ')
  cmd << " -altLibDir=#{self.libdir} #{varIdent} -iFile=#{ifile} -oFile=#{ofile} -oType=#{self.otype}"
  cmd << " -isIcon" if self.isIcon
  cmd << " -DEBUG"  if self.debug
  opts.each {|k,v| 
    v = '"'+v+'"' if (:tStrg == k and not ['"',"'"].include?(v.strip[0]))
    v = 'True' if v == true
    v = 'False' if v == false
    cmd << " -"<< [k,v].join('=')
  }
  puts cmd if self.debug
  out = IO.popen(cmd).read
  puts out if self.debug

  #return [FileUtils.pwd,"#{ofile}.#{self.otype}"].join(File::SEPARATOR)
  return "#{ofile}.#{self.otype}"
end

#scalarPlot(ifile, ofile, varname, opts = {}) ⇒ Object



60
61
62
# File 'lib/iconPlot.rb', line 60

def scalarPlot(ifile,ofile,varname,opts={})
  plot(ifile,ofile,varname,'scalar',opts)
end

#scatterPlot(ifile, ofile, xVarname, yVarname, opts = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/iconPlot.rb', line 93

def scatterPlot(ifile,ofile,xVarname,yVarname,opts={})
  # is there a variable which discribes different regions in the ocean
  regionVar = opts[:regionVar].nil? ? 'rregio_c' : opts[:regionVar]
  hasRegion = self.cdo.showname(:input => ifile).join.split.include?(regionName)
  unless hasRegion
    warn "Variable '#{regionName}' for showing regions is NOT found in the input '#{ifile}'!"
    warn "Going on without plotting regions!"
    varnames = varnames[0,2]
    groupBy = []
  else
    groupBy = [regionName]
  end
end

#setDatetime(extcsvObj) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/iconPlot.rb', line 156

def setDatetime(extcsvObj)
  unless (extcsvObj.respond_to?(:date) and extcsvObj.respond_to?(:time))
    warn "Cannot set datetime due to missing date and time attributes"
    raise ArgumentError
  end
  # Create datetime column for timeseries plot
  extcsvObj.datetime = []
  extcsvObj.date.each_with_index{|date,i| extcsvObj.datetime << [date,extcsvObj.time[i]].join(' ') }
  extcsvObj.datacolumns << "datetime"
end

#show(*files) ⇒ Object



110
111
112
# File 'lib/iconPlot.rb', line 110

def show(*files)
  Parallel.map(files.flatten) {|file| out = IO.popen("#{self.display} #{file}").readlines; puts out.join if self.debug }
end

#showVector(ifile, ofile, vars, opts = {}) ⇒ Object



116
117
118
# File 'lib/iconPlot.rb', line 116

def showVector(ifile,ofile,vars,opts={})
  show(vectorPlot(ifile,ofile,vars,opts))
end

#vectorPlot(ifile, ofile, varname, opts = {}) ⇒ Object



63
64
65
# File 'lib/iconPlot.rb', line 63

def vectorPlot(ifile,ofile,varname,opts={})
  plot(ifile,ofile,varname,'vector',opts)
end