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.



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

def initialize(*args)
  super(*args)
  
  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     => ENV['CDO'].nil? ? 'cdo' : ENV['CDO'],
    :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



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

def caller
  @caller
end

#cdoObject

Returns the value of attribute cdo

Returns:

  • (Object)

    the current value of cdo



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

def cdo
  @cdo
end

#debugObject

Returns the value of attribute debug

Returns:

  • (Object)

    the current value of debug



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

def debug
  @debug
end

#displayObject

Returns the value of attribute display

Returns:

  • (Object)

    the current value of display



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

def display
  @display
end

#isIconObject

Returns the value of attribute isIcon

Returns:

  • (Object)

    the current value of isIcon



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

def isIcon
  @isIcon
end

#libdirObject

Returns the value of attribute libdir

Returns:

  • (Object)

    the current value of libdir



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

def libdir
  @libdir
end

#otypeObject

Returns the value of attribute otype

Returns:

  • (Object)

    the current value of otype



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

def otype
  @otype
end

#plotterObject

Returns the value of attribute plotter

Returns:

  • (Object)

    the current value of plotter



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

def plotter
  @plotter
end

Instance Method Details

#createData(ifile, varname, operation) ⇒ Object



112
113
114
115
116
117
118
119
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
# File 'lib/iconPlot.rb', line 112

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

  # read the date
  IO.popen("echo 'date|time|depth|#{varname}' > #{dataFile}")
  Cdo.debug = true
  Cdo.outputkey('date,time,level,value', 
                :in => "-#{operation} -selname,#{varname} #{ifile} >>#{dataFile} 2>&1")

  # postprocessing for correct time values
  data = []
  File.open(dataFile).each_with_index {|line,lineIndex|
    next if line.chomp.empty?
    _t = line.chomp.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



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

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

#del(file) ⇒ Object



99
100
101
# File 'lib/iconPlot.rb', line 99

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

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



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
83
84
# File 'lib/iconPlot.rb', line 58

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
  Cdo.debug = true
  unit = Cdo.showunit(:in => "-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



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
# File 'lib/iconPlot.rb', line 24

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] =ofile if opts[:tStrg].nil?

  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| 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



52
53
54
# File 'lib/iconPlot.rb', line 52

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

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



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/iconPlot.rb', line 85

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 = Cdo.showname(:in => 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



148
149
150
151
152
153
154
155
156
157
# File 'lib/iconPlot.rb', line 148

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



102
103
104
# File 'lib/iconPlot.rb', line 102

def show(*files)
  files.flatten.each {|file| IO.popen("#{self.display} #{file} &") }
end

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



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

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

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



55
56
57
# File 'lib/iconPlot.rb', line 55

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