Class: CTioga2::Data::Backends::GnuplotBackend

Inherits:
Backend
  • Object
show all
Includes:
Log, Dobjects
Defined in:
lib/ctioga2/data/backends/backends/gnuplot.rb

Instance Method Summary collapse

Methods included from Log

context, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Methods inherited from Backend

#dataset, describe, #description, #expand_sets, #has_set?, #initialize, list_backends, #set_param_from_string, #sets_available

Methods included from BackendDescriptionExtend

#base_description, #create_factory, #describe, #description, #factory_class, #factory_description, #factory_description_hash, #factory_description_list, #has_factory?, #inherit_parameters, #param, #param_accessor, #param_reader, #param_writer, #register_class, #set_description

Constructor Details

This class inherits a constructor from CTioga2::Data::Backends::Backend

Instance Method Details

#query_dataset(set) ⇒ Object

This is called by the architecture to get the data. It splits the set name into filename@cols, reads the file if necessary and calls get_data



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ctioga2/data/backends/backends/gnuplot.rb', line 58

def query_dataset(set)
  set =~ /^(.*?)(?:@(\d+))?(?::(.*))?$/
  filename = $1
  if $2
    number = $2.to_i - 1
  else
    number = 0
  end
  if $3
    overrides = "#{@variable_overrides};#{$3}"
  else
    overrides = @variable_overrides
  end
  plots = run_gnuplot(filename, overrides)
  return Dataset.new(set,plots[number])
end

#run_gnuplot(filename, overrides = @variables_overrides) ⇒ Object

Runs gnuplot on the file, and returns all datasets found inside it.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ctioga2/data/backends/backends/gnuplot.rb', line 77

def run_gnuplot(filename, overrides = @variables_overrides)
  date = File::mtime(filename)
  # Get it from the cache !
  debug { "Running gnuplot on file #{filename}" }
  f = Utils::open(filename)
  # We open a bidirectionnal connection to gnuplot:
  gnuplot = IO.popen("gnuplot", "r+")
  output = ""
  ## \todo determine gnuplot version for choosing which one we
  ## want to use.
  # gnuplot.puts "set term table"
  gnuplot.puts "set table"
  if @samples
    overrides ||= ""
    overrides += ";set samples #{@samples}"
  end
  for line in f
    next if line =~ /set\s+term/
    if overrides and line =~ /plot\s+/
      debug { 
        "Found a plot, inserting variable overrides: #{overrides}" 
      }
      line.gsub!(/plot\s+/, "#{overrides};plot ")
    end
    if @range and line =~ /plot\s+/
      debug { 
        "Found a plot, inserting range: #{@range}" 
      }
      line.gsub!(/plot\s+(\[[^\]]+\])?/, 
                 "plot [#{@range}]")
    end
    gnuplot.print line 
    gnuplot.flush
    output += slurp(gnuplot)
  end
  
  # Output a "\n" in the end.
  
  gnuplot.puts ""
  gnuplot.flush
  gnuplot.close_write
  # Then we get all that is remaining:
  output += gnuplot.read
  gnuplot.close
  
  # Now, interaction with gnuplot is finished, and we want to
  # parse that:
  outputs = output.split("\n\n")
  plots = []
  for data in outputs
    plots << Dvector.fancy_read(StringIO.new(data), [0,1])
  end
  # This block evaluates to plots:
  return plots
end

#slurp(f, size = 10240) ⇒ Object

Gets all data from the given file until it blocks, and returns it.



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ctioga2/data/backends/backends/gnuplot.rb', line 134

def slurp(f, size = 10240)
  str = ""
  begin
    while IO::select([f],[],[],0)
      ret = f.readpartial(size)
      if ret.empty?
        return str
      end
      str << ret 
    end
  end
  return str
end