Class: SciYAG::Backends::GnuplotBackend

Inherits:
Backend
  • Object
show all
Includes:
Dobjects
Defined in:
lib/SciYAG/Backends/gnuplot.rb

Instance Method Summary collapse

Methods inherited from Backend

#base_line=, #clear_xy_filters, default_state, describe, #expand_sets, #get_cached_entry, #has_set?, #initialize, list_backends, list_descriptions, logger=, #meta_data, #pop_xy_filter, #push_xy_filter, #set_type, #sets_available, #xy_data, #xyz_data

Methods included from MetaBuilder::DescriptionExtend

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

Methods included from MetaBuilder::DescriptionInclude

#description, #get_param, #get_param_raw, #long_name, #option_parser_banner, #option_parser_fill, #option_parser_options, #parameter, #restore_state, #save_state, #set_param, #set_param_raw

Constructor Details

This class inherits a constructor from SciYAG::Backends::Backend

Instance Method Details

#query_xy_data(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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/SciYAG/Backends/gnuplot.rb', line 53

def query_xy_data(set)
  if set =~ /(.*)@(\d+)/
    filename = $1
    number = $2.to_i - 1
  else
    filename = set
    number = 0
  end
  plots = run_gnuplot(filename)
  return Function.new(*(plots[number]))
end

#run_gnuplot(filename) ⇒ Object

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



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/SciYAG/Backends/gnuplot.rb', line 67

def run_gnuplot(filename)
  date = File::mtime(filename)
  # Get it from the cache !
  get_cached_entry(filename, [], {:date => date}) do
    debug "Running gnuplot on file #{filename}"
    f = File.open(filename)
    # We open a bidirectionnal connection to gnuplot:
    gnuplot = IO.popen("gnuplot", "r+")
    output = ""
    gnuplot.puts "set term table"
    for line in f
      next if line =~ /set\s+term/
      if @variables_overrides and line =~ /plot\s+/
        debug "Found a plot, inserting variable overrides :#{@variables_overrides}"
        line.gsub!(/plot\s+/, "#{@variables_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:
    plots
    # DO NOT USE return !!!
  end
end

#slurp(f, size = 10240) ⇒ Object

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



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/SciYAG/Backends/gnuplot.rb', line 114

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