%returns average orbit mesured from devices devdescr on basis of given number (navg) of measuremns
function [hmean,vmean,currmean,spos] = readorbit(devdescr, prefix, navg)



if (navg < 1)
 error('navg parameter smaller then 1');
 return;
end;

readprop='AQN2'; 

n = length(devdescr);

debug = 0;
if (debug) fprintf(1,'There is %d devices\n',n); end;

itreshold = .5;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%                                        %%%%%%%%%
%%%%%%%  Copy positions from the definitions   %%%%%%%%%
%%%%%%%                                        %%%%%%%%%
%%%%%%%  This way it is easier for plotting    %%%%%%%%%
%%%%%%% and we know nuber of working monitors  %%%%%%%%%
%%%%%%%                                        %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

k = 0;
for i=1:n
  if (devdescr(i).faulty)
    continue;
  end
  k = k + 1;
  spos(k) = devdescr(i).spos;
end

global testmode

if (testmode > 0)
 [hmean,vmean,currmean] = genorbit()
 return;
end




h =  zeros(k,navg);
v =  zeros(k,navg);
current =  zeros(k,navg);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%                                        %%%%%%%%%
%%%%%%%      read the orbit navg times         %%%%%%%%%
%%%%%%%                                        %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for j=1:navg
  k=0;
  for i=1:n

    if (devdescr(i).faulty)
      continue;
    end

    k = k + 1;

    asigname = [ prefix 'SV' devdescr(i).name 'S-C'];
    if(debug) fprintf(1,'Reading %s',devicename); end;


    current(k,j) = 0;
    fprintf(1,'%d %s Current is %f\n',i,asigname,current(k,1));

    while (current(k,1) > itreshold),
      current(k,j) = Jgetcontrolval(asigname,readprop);    
      fprintf(1,'%d %s Current is %f\n',i,asigname,current(k,1));
      if (current(k,j) > itreshold)
          fprintf(1,'%d %s Current is TOO LOW %f waiting\n',i,asigname,current(k,1));
          pause(1.2);
      end
    end


    asigname = [ prefix 'SV' devdescr(i).name 'H-C'];

    h(k,j) = nan;
    while (isnan(h(k,j))),
      h(k,j) = Jgetcontrolval(asigname,readprop);
      fprintf(1,'%d %s Xpos %f\n',i,asigname,h(k,j));
      %h(k,j) = 0.5; fprintf(1,'Warning: readorbit: Fixed orbit\n',h(i,j));
    end  

%    fprintf(dhf,'%f ',h(k,j));

    asigname = [ prefix 'SV' devdescr(i).name 'V-C'];

    v(k,j) = nan;
    while (isnan(v(k,j))),
       v(k,j) = Jgetcontrolval(asigname,readprop);
       fprintf(1,'%d %s Xpos %f\n',i,asigname,v(i,j));
       %v(k,j) = 0.5; fprintf(1,'Warning: readorbit: Fixed orbit\n',v(i,j));
    end 
%    fprintf(dvf,'%f ',v(i,j));

  end

  pause(1.2);

end

hmean = zeros(k,2);
vmean = zeros(k,2);
currmean = zeros(k,2);

hmean(:,1)    = mean(h,2);
vmean(:,1)    = mean(v,2);
currmean(:,1) = mean(current,2);

hmean(:,2)    = std(h,0,2)/sqrt(navg); %rms = sqrt(sum(x_mean^2 -x_i)/(n*(n-1))) = stddev/sqrt(n)
vmean(:,2)    = std(v,0,2)/sqrt(navg);
currmean(:,2) = std(current,0,2)/sqrt(navg);


