function [time_vec,data_vec,tbs] = get_shot_from_sdas(uniqueid, shotnumber)
%
% Written by tpereira@ipfn.ist.utl.pt
% IPFN, 21-April-2015
% Octave function to retrieve a shot data from SDAS.
% Uses wget from Linux (works only in Linux).
% To use, copy this file to your working directory.
%
% time_vec :  will be a matrix of scalar, unit will be seconds.
% data_vec :  will be a matrix of scalar, unit will be whatever was recorded in SDAS.
% tbs      :  will be scalar, unit will be seconds.
%
% uniqueid is a string containing the unique identifier of the shot to be retrieved.
% shotnumber is a scalar with the number of the shot to be retrieved.
%
% Examples:
% [t, halpha, tbs] = get_shot_from_sdas("PCIE_ATCA_ADC_TEST.BOARD_1.CHANNEL_032", 29990);
% [t, halpha, tbs] = get_shot_from_sdas("MARTE_NODE_IVO3.DataCollection.Channel_096", 38025);

  if (nargout != 3 || nargin != 2 ||(!isnumeric(shotnumber)) ||(!ischar(uniqueid)))
    printf("\nUSAGE:\n");
    printf("[time_vec,data_vec,tbs] = get_shot_from_sdas(string uniqueid, number shotnumber);\n");
    printf("time_vec and tbs will be seconds\n");
    printf("time_vec and data_vec will be vectors of scalar\n\n");
    printf("Examples:\n");
    printf("[t, halpha, tbs] = get_shot_from_sdas(\"PCIE_ATCA_ADC_TEST.BOARD_1.CHANNEL_032\", 29990);\n");
    printf("[t, halpha, tbs] = get_shot_from_sdas(\"MARTE_NODE_IVO3.DataCollection.Channel_096\", 38025);\n");
    time_vec = data_vec = [];
    tbs = -1;
    return;
  endif

  s1 = strcat("wget -q -O - \"baco.ipfn.ist.utl.pt:8090/?parUID=", uniqueid, "&eventNameID=0x0000&eventNumberID=", num2str(shotnumber), "\"");
  s2 = strcat("wget -q -O - \"baco.ipfn.ist.utl.pt:8090/?parUID=", uniqueid, "&eventNameID=0x0000&eventNumberID=", num2str(shotnumber), "&config=true\"");
  s3 = strcat("wget -q -O - \"baco.ipfn.ist.utl.pt:8090/?parUID=", uniqueid, "&eventNameID=0x0000&eventNumberID=", num2str(shotnumber), "&reqSize=true\"");
  s4 = strcat("wget -q -O - \"baco.ipfn.ist.utl.pt:8090/?parUID=", uniqueid, "&eventNameID=0x0000&eventNumberID=", num2str(shotnumber), "&reqTStart=true\"");
  s5 = strcat("wget -q -O - \"baco.ipfn.ist.utl.pt:8090/?parUID=", uniqueid, "&eventNameID=0x0000&eventNumberID=", num2str(shotnumber), "&reqTEnd=true\"");

  [status1,data_vec] = system(s1, 1);
  [status2,conf] = system(s2, 1);
  [status3,s] = system(s3, 1);
  [status4,tstart] = system(s4, 1);
  [status5,tend] = system(s5, 1);

  if (status1 != 0 || status2 != 0 || status3 != 0 || status4 != 0 || status5 != 0)
    printf("Could not execute wget.\n\n");
    time_vec = data_vec = [];
    tbs = -1;
    return;
  end

  if (isempty(data_vec) || isempty(conf) || isempty(s) || isempty(tstart) || isempty(tend))
    printf("Could not find shot or signal.\n\n");
    time_vec = data_vec = [];
    tbs = -1;
    return;
  end

  data_vec = data_vec+0;
  data_vec = uint8(data_vec);
  data_vec = typecast(swapbytes(typecast(data_vec, 'uint32')), 'single');

  if ((p1 = regexp(conf, "\"DELAY_TIME_MILLIS\" value=\".*\"")))
    #<field uniqueID="DELAY_TIME_MILLIS" value="-1"/>
    p2 = regexp(conf(p1:length(conf)),"\"/>");
    delay = str2num(conf(p1+27:p1+p2(1)-2))/1e3;
  elseif ((p1 = regexp(conf, "\"TRIGGER_DELAY\" value=\".*\"")))
    #
    p2 = regexp(conf(p1:length(conf)),"\"/>");
    delay = str2num(conf(p1+23:p1+p2(1)-2))/1e6;
  elseif ((p1 = regexp(conf, "\"SELF_DELAY\" value=\".*\"")))
    #<field uniqueID="SELF_DELAY" value="5000"/>
    p2 = regexp(conf(p1:length(conf)),"\"/>");
    delay = str2num(conf(p1+20:p1+p2(1)-2))/(-1e6);
  else
    printf("Cannot find delay value. Assuming 1ms.\n\n");
    delay = -1e-3;
  endif

  s = str2num(s);
  tstart = str2num(tstart);
  tend = str2num(tend);

  if ((p1 = regexp(conf, "\"FREQUENCY\" value=\".*\"")))
    #<field uniqueID="FREQUENCY" value="2e+06"/>
    p2 = regexp(conf(p1:length(conf)),"\"/>");
    tbs = 1/str2num(conf(p1+19:p1+p2(1)-2));
  elseif ((p1 = regexp(conf, "\"TBS\" value=\".*\"")))
    #<field uniqueID="TBS" value="40"/>
    p2 = regexp(conf(p1:length(conf)),"\"/>");
    tbs = str2num(conf(p1+13:p1+p2(1)-2))*1e-6;
  elseif ((p1 = regexp(conf, "\"TBS_SELF\" value=\".*\"")))
    #<field uniqueID="TBS_SELF" value="30"/>
    p2 = regexp(conf(p1:length(conf)),"\"/>");
    tbs = str2num(conf(p1+18:p1+p2(1)-2))*1e-6;
  elseif ((p1 = regexp(conf, "\"TIME_SAMPLES\" value=\".*\"")))
    #<field uniqueID="TIME_SAMPLES" value="100"/>
    p2 = regexp(conf(p1:length(conf)),"\"/>");
    tbs = str2num(conf(p1+22:p1+p2(1)-2))*1e-6;
  else
    printf("Cannot find tbs value. Assuming 0.5us.\n\n");
    tbs = 0.5e-6;
  endif

  #printf("delay = %g ms\n", delay/1e-3);
  #printf("tbs = %g us\n", tbs/1e-6);

  time_vec = [delay : tbs : (length(data_vec)-1)*tbs + delay];

  data_vec = double(data_vec);
  time_vec = double(time_vec);

  return;
endfunction
