function zvl3_sa_lan_meas

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Takes a spectrum using the ZVL3 SA mode Stores the Data files with 
% Time Stamps on the ZVL3 hard disk on dat file format 
%Hamdi Mani 08/9/2011 (hamdi.mani@asu.edu)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% User Settings 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% IP address of the ZVL3
% IP_address = '10.206.155.99'; % ASU Network IP Address 

%Set execution period in Seconds: this is the sampling period
execution_period= 60;  % = 1 minute


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
close all;
% % Number of samples to be captured.
% NSamples = 10;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%SA SETTINGS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start LAN connection to the ZVL3
zvl3 = visa('ni', 'TCPIP::10.206.155.99::INSTR');
fopen(zvl3);
% Get ID string from ZVL3
% rs_sa_getID(zvl3);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SA Settings 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%Set the Center Frequency 
%fprintf(zvl3,'FREQ:CENT %sMHz; WAI','1420');

%Set the frequency SPAN
%fprintf(zvl3,'FREQ:SPAN %sMHz; WAI','100');

%Set the start frequency
fprintf(zvl3,'FREQ:START %sMHz; WAI','10');

%Set the STOP frequency
fprintf(zvl3,'FREQ:STOP %sMHz; WAI','300');

%Set the number of freq points per sweep
fprintf(zvl3,'SWE:POIN 1001'); %100 KHz resolution

%Set the Resolution bandwidth Filter
fprintf(zvl3,'BAND:RES %sMHz; WAI','1');

%Set the Video bandwidth Filter
fprintf(zvl3,'BAND:VID %sKHz; WAI','0.1');

%Set the Reference Level
fprintf(zvl3,'DISP:TRAC:Y:RLEV %sdBm; WAI','-50');


%Set the POWER UNIT
fprintf(zvl3,'CALC:UNIT:POW %s; WAI','dBm');

%Set the Input RF Attenuation
fprintf(zvl3,'INP:ATT %sdB; WAI','10');%needed to avoid overload.

%Set the sweep mode (Continuous/Single Sweep)
%fprintf(zvl3,'INIT:CONT %s; WAI','OFF')%Single sweep mode
fprintf(zvl3,'INIT:CONT %s; WAI','ON');

%Set the marker ON/OFF
fprintf(zvl3,'CALC:MARK %s; WAI','ON');
%fprintf(zvl3,'CALC:MARK:X %sMHZ; WAI','1000')
%fprintf(zvl3,'CALC:MARK:MAX')
%fprintf(zvl3,'CALC:MARK:MIN')
%fprintf(zvl3,'CALC:MARK:TRACE  %s; WAI','1')

%Set the trigger mode 
fprintf(zvl3,'TRIG:MODE %s; WAI','IMM');

%Set the trace mode (AVER/MINH/MAXH/WRIT/VIEW/BLANK)
%fprintf(zvl3,'DISP:TRAC4:MODE %s; WAI','VIEW');
fprintf(zvl3,'DISP:TRAC:MODE %s; WAI','writ');

%Select the detector mode:APE/POS/NEG/SAMP/RMS/AVER/QPE
fprintf(zvl3,'DET AVER');



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% %SET FILE EXPORT FORMAT (ASC:ASCII)
% fprintf(zvl3,'FORM ASC');
% 
% %set decimal separator for ascii format POIN/COMM
% fprintf(zvl3,'FORM:DEXP:DSEP POIN');

while (true)
    
%Define filename and store location
k=datestr(now);% Output should be like this: 15-Aug-2011 23:50:55
%replace the ':' by '-' to be able to save.
for i=1:length(k)
    if k(i)==':'
        k(i)='-';
    end
end

filename=strcat('c:\your_folder\Spectrum',k,'.dat');

%Turn OFF Averaging    
fprintf(zvl3,'AVER OFF ');

%Set Averaging Factor
fprintf(zvl3,'AVER:COUN 10');% average 10 sweeps

%Turn ON averaging
fprintf(zvl3,'AVER ON');

pause(30);

%Save the Files
fprintf(zvl3,'MMEM:STOR:TRAC 1 , ''%s''',filename);

% Make sure that the averaging is done before storing data
% the most reliable way to do is to check the sweep time SWT  with your settings
% and pause for SWT + 2 seconds for safety on line #130
pause(execution_period);
end


end
