10    !  GETWATR
20    !-------------------------------------------------------------------------
30    ! HPBASIC/IBASIC Programs; Octave Waterfall Dump
40    !
50    ! this program is divided into three parts. (1) set up the source,(2) set up
60    ! and take a measurement,(3) dump the data out of the instrument and print
70    ! out the information. The information is in dBSLPrms units
80    !
90    ! Some items of note.
100   !          1. default is 1 engineering unit per volt
110   !          2. engineering unit is defined in Pascals
120   !          3. 20E-6 pascals = 1 SPL  or 0 dBVrms = approx 94 dBSPLrms
130   !          4. waterfall can only be dumped if the measurement is PAUSED
140   !          5. Connect the SOURCE to CHANNEL 1
150   !          6. Overall power and A-weight Filter data is always outputted
160   !          7. Data in the array A(*) is from Oldest to newest data.
170   !          8. This is the opposite of the display which has oldest on
180   !             the bottom
190   !
200   ! This program will also distinguish between IBASIC and HPBASIC run modes.
210   !-------------------------------------------------------------------------
220   ON ERROR GOTO Not_ibasic     ! auto selection of proper select code
230   Device=800                   ! device select code from IBASIC
240   CLEAR Device
245   CLEAR SCREEN
250   GOTO Start_prog
260   !
270 Not_ibasic: !
280   Device=711                   ! device select code from external computer
290 Start_prog: !
300   OFF ERROR
310   !
320   ASSIGN @Analyzer TO Device   ! allows code to be address independent
330   !
340   ! set aside a large space for freq and amplitude data
350   DIM A(20000),X(100)
360   !
370   !
380   OUTPUT @Analyzer;"*RST"              ! Reset the analyzer
390   OUTPUT @Analyzer;"INST OCT"          ! activate realtime octave analysis
395   OUTPUT @Analyzer;"FREQ:RES:OCT THIR" ! Default is 1/3 Octave
400 ! OUTPUT @Analyzer;"FREQ:RES:OCT FULL" ! UNCOMMENT To get 1/1 Octave
410 ! OUTPUT @Analyzer;"FREQ:RES:OCT TWEL" ! UNCOMMENT To get 1/12 Octave
420 !
430   OUTPUT @Analyzer;"OUTP ON"           ! Turn the source ON
440   OUTPUT @Analyzer;"SOUR:VOLT  1 VRMS" ! set to 1 VOLT RMS
450   OUTPUT @Analyzer;"SOUR:FREQ 1000 HZ" ! set to 1000 HZ
460   !
470   OUTPUT @Analyzer;"DISP:FORM ULOW"    ! select upper/lower display
480   OUTPUT @Analyzer;"DISP:WIND1:WAT ON" ! Turn on the waterfall display
490                                        ! for trace A
492   !
494   ! display the weighted power band
496   OUTPUT @Analyzer;"DISP:WIND1:TRAC:APOW ON"
500   !
510   ! put channel 1 power spectrum in trace A waterfall
520   OUTPUT @Analyzer;"CALC1:FEED 'XFR:POW 1';MATH:STAT OFF;*WAI"
530   !
540   ! set the channel 1 units to Pascals
550   OUTPUT @Analyzer;"VOLT1:RANG:UNIT:XDCR:LAB PA"
560   OUTPUT @Analyzer;"VOLT1:RANG:UNIT:USER ON"
570   !
580   ! start a measurement and wait for the finish
590   OUTPUT @Analyzer;"ABOR;:INIT;*WAI"
600   !
610   ! pause the measurement for data dumps
620   OUTPUT @Analyzer;"INIT:CONT OFF; *WAI"
630   !
640   ! Start of the data dumping routines. Freq and amplitude data are
650   ! dumped separately
660   !
670   ! assign an i/o path for binary transfers
680   ASSIGN @Analyzer_bin TO Device;FORMAT OFF
690   !
700   ! command the analyzer to output block data in floating point binary
710   ! representation.
720   OUTPUT @Analyzer;"FORM:DATA REAL,64"
740   !
750   ! command the analyzer to output the x values for trace A
760   OUTPUT @Analyzer;"CALC1:X:DATA?"
770   !
780   ! read the header of the data block.  first read the '#n' to
790   ! find out how many digits are in the byte count then read the
800   ! number of bytes.
810   ENTER @Analyzer USING "%,A,D";A$,X_digits
820   !
830   ENTER @Analyzer USING "%,"&VAL$(X_digits)&"D";Num_of_bytes
840   X_num_points=Num_of_bytes DIV 8 ! 8 data bytes per number
850   !
860   ! Use the number of bytes to determine the amount of data.
870   ! Redimension the array so it's the same size as the data block
880   ! being read in so we can read the data in the fastest possible way.
890   REDIM X(X_num_points-1)
900   !
910   ! read the x values
920   ENTER @Analyzer_bin;X(*)
930   ENTER @Analyzer;A$        ! read the line feed character (IEEE 488.2)
932 FOR I=0 TO X_num_points-1
933   IF X(I)>100000 THEN X(I)=100000
940   !
945 NEXT I
960   ! command the analyzer output the y values for the waterfall
970   OUTPUT @Analyzer;"CALC1:WAT:DATA?"
980   !
990   ! read the block header
1000  ENTER @Analyzer USING "%,A,D";A$,Y_digits
1010  ENTER @Analyzer USING "%,"&VAL$(Y_digits)&"D";Num_of_bytes
1020  !
1030  Y_num_points=Num_of_bytes DIV 8 ! 8 data bytes per number
1040  !
1050  ! Redimension the array so it's the same size as the data block.
1060  REDIM A(Y_num_points-1)     ! allocate just the space needed
1070  !
1080  ! read the y data for all the traces in the waterfall
1090  ENTER @Analyzer_bin;A(*)
1100  ENTER @Analyzer;A$        ! read the line feed character
1110  !
1120  !
1130  ! to remove the possible large minus numbers(-3.40E38), run
1140  ! the following code.  This is useful to limit the image
1150  ! statements to the +- 1000 range
1160  FOR I=0 TO Y_num_points-1            ! array starts at Zero
1170    IF A(I)<-99 THEN A(I)=-99      ! I wanted to set a minimum level
1172    IF A(I)>99 THEN A(I)=99        ! I wanted to set a minimum level
1180  NEXT I
1190  !
1200  ! open up a display window if in IBASIC
1210  IF Device=800 THEN OUTPUT @Analyzer;"DISP:PROG LOW"
1220  !
1230  ! Start of the print routines
1240  ! Note - last piece of data is "overall" data
1250  ! Note - second last piece of data is "A-Weight" data!
1260  PRINT "CENTER FREQ          SCAN1      SCAN2       SCAN3"
1270  PRINT 
1280  FOR I=0 TO X_num_points-3
1290 Im_1:    IMAGE   7D.D,11X,M4D.DD,3X,M4D.DD,3X,M4D.DD
1300    PRINT USING Im_1;X(I),A(I),A(I+X_num_points),A(I+2*X_num_points)
1301  IF Device=800 AND (I MOD 10=9) THEN
1302    BEEP 200,.2
1303    DISP "press continue to see more values"
1304    PAUSE
1305    DISP ""
1306  END IF 
1310  NEXT I
1320  PRINT 
1330 Im_2:  IMAGE   16A,4X,M4D.2D,3X,M4D.2D,3X,M4D.2D
1340  PRINT USING Im_2;" A WEIGHT DATA     ",A(X_num_points-2),A(2*X_num_points-2),A(3*X_num_points-2)
1350  PRINT USING 1330;" OVERALL POWER     ",A(X_num_points-1),A(2*X_num_points-1),A(3*X_num_points-1)
1360  END
