## Copyright (C) 2009 Thomas Weidenfeller
## 
## This file is part of gds2000tools/gds-octave.
## 
## gds2000tools/gds-octave is free software: you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## version 3 as published by the Free Software Foundation.
## 
## gds2000tools/gds-octave is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with gds2000tools/gds-octave.  If not,
## see <http://www.gnu.org/licenses/>.
##

## usage: gds_plot(GDSSTRUCT, ...)
##
## Plots the data in the provided GDSSTRUCTs.
##
## See also: gds_csvread, gds_plotspectrum.

## Author: Thomas Weidenfeller
## Created: 2009-07-08
## Version: $Id: gds_plot.m,v 1.9 2010/12/31 13:05:40 tw Exp $
## Keywords: Oscilloscope, GDS-2000, gds2000tools, plot

function gds_plot(varargin)

	N = length(varargin);
	if(N < 1)
		print_usage();
	endif

	prows = ceil(sqrt(N));
	pcolumns = ceil(N / prows);

	for i = 1:N
		gs = varargin{i};
		if(!isstruct(gs))
			print_usage();
		endif

		##
		## Determine sampling period
		##
		dt = __gds_sampling_period(gs);

		##
		## Check if we have the data we should have
		##
		L = __gds_memory_length(gs);

		##
		## Construct a time vector for labeling the x axis.
		##
		if(rows(gs.Horizontal_Position) == 1)
			off = gs.Horizontal_Position;
		else
			off = 0;
		endif
		t = ([0:L - 1] * dt) - off;

		##
		## Construct a vector for marking the trigger level
		##
		##trig = ones(1, L) * gs.Trigger_Level;

		##
		## Plot and label axis
		##
		## plot(t, gds_struct2val(gs), [ ";" gs.Source ";" ], t, trig, ";Trigger;");
		if(N > 1)
			subplot(prows, pcolumns, i);
		endif
		plot(t, gds_struct2val(gs));

		title([gs.Source " " gs.Horizontal_Mode " " gs.Time ]);
		if(strcmp(gs.Horizontal_Units, "s"))
			xlabel("Time / s");
		else
			xlabel(gs.Horizontal_Units);
		endif
		ylabel([ "Amplitude / " gs.Vertical_Units ]);
		axis("on");
		grid("on");
		##grid("minor", "on"); # fails in octave 3.0.1
	endfor
endfunction

%!demo
%! ##gs = gds_csvread("DS0008.CSV"); ## read waveform from file
%! gs = __gds_demo_gs();      ## for now get a demo gds waveform data structure
%! subplot(1,1,1);
%! gds_plot(gs);
