;P75/P175 automated FM local scan script, (C) Pira.cz 2016
;FM Scope version required: 1.3c rev. 4 or later

;*************************************************************
;*** User configurable parameters:                         ***
;*************************************************************

set (DelayMeasuring,600)		;how long to measure each station [sec]
set (DelayRDS,120)			;how long to read RDS data [sec]
set (MaxNoise,60)			;Max. noise allowed to start monitoring the station
set (AMLimit,30)			;Max. % of AM modulation allowed to start monitoring the station
set (SkipFreqs,)			;frequencies to skip

;*************************************************************
;*** Main program:                                         ***
;*************************************************************

executionspeed (4)
frequencydeviation.histogram (1)
frequencydeviation.online (0)
mpx.doubletimebase(0)

SetProperty(HistogramBitmapWidth,800)
SetProperty(HistogramBitmapHeight,600)
SetProperty(PmBitmapWidth,800)
SetProperty(PmBitmapHeight,600)
SetProperty(MPXBitmapWidth,800)
SetProperty(MPXBitmapHeight,600)
SetProperty(RFBitmapWidth,800)
SetProperty(RFBitmapHeight,600)
SetProperty(BandscanBitmapWidth,1024)
SetProperty(BandscanBitmapHeight,600)
SetProperty(OthersBitmapWidth,800)
SetProperty(OthersBitmapHeight,600)
SetProperty(ScanRDS,1)
SetProperty(DetailsInGraphs,1)
Bandscan.showfrequency(1)
Bandscan.showpi(1)
Bandscan.showps(1)
textwindow.clear
page.show(6)

textwindow.addline(Select the output folder and enter the name for the local scan.)
savedialog(*.)
if (%inputvalid!=1)
  textwindow.addtext( Aborting.)
  stop
  endif

set (DataPath,%inputtext)
textwindow.addtext( OK. The output will be saved as %DataPath-<filetype>)

location.load (localscan)
location.unlock
call (ProcessBand)			;*Proceed the monitoring*

stop

;*************************************************************
;*** Band processing:                                      ***
;*************************************************************

ProcessBand:

set (NewBandscan,0)

textwindow.addline(Take a new bandscan?)
input(Take new bandscan? 0 for No)
if (%inputtext!=0)
  textwindow.addtext( Yes)
  textwindow.addline(Getting bandscan, please wait...)
  bandscan.acquire
  bandscan.savebitmap (%DataPath-bandscan.jpg)
  set (NewBandscan,1)
  goto (ProcessBand2)
  endif
textwindow.addtext( No)

ProcessBand2:

set (stationindex,1)
textwindow.addline(%stationcount stations detected)

Loop0:

if (%stationindex>%stationcount)
  goto (SelectStation)
  endif

textwindow.addline([%stationindex] %station.frequency %station.rdsps %station.rdspi)
inc(stationindex)
goto (Loop0)

SelectStation:

Set(FirstStation,1)

if (NewBandscan=1)
  goto (SkipSelect)
  endif

textwindow.addline(Start this scan on the station number: )
input(Enter the station number or click Cancel for 1)
if (%inputvalid=1)
  set(FirstStation,%inputtext)
  if (%FirstStation<0)
    Set (FirstStation,1)
    endif
  endif
textwindow.addtext(%FirstStation)

SkipSelect:

set (stationindex,%FirstStation)

Loop1:					;loop through stations from bandscan

if (%stationindex>%stationcount)
  textwindow.addline({%time} Done.)
  RETURN
  endif

set (tempfreq,%station.frequency)

if (%station.noise>%MaxNoise)
  textwindow.addline({%time} Station %tempfreq skipped [%stationindex of %stationcount] {noisy reception})
  goto (Next1)
  endif

if (%station.freq@%SkipFreqs)
  textwindow.addline({%time} Station %tempfreq skipped by user [%stationindex of %stationcount])
  goto (Next1)
  endif

call (ProcessStation)			;try mearuring the station

Next1:

inc (stationindex)
goto (Loop1)

;*************************************************************
;*** Station measuring:                                    ***
;*************************************************************

ProcessStation:

setmode (0)
tune (%station.frequency)
send (*1)
send (*0)

textwindow.addline({%time} Tuning to %station.frequency [%stationindex of %stationcount])
call (CheckStation)			;check the station's reception quality
if (%Result=0)
  RETURN
  endif

imf.get
mpx.run
modulationpower.run
set (EndTime,%timestamp)
inc (EndTime,%DelayMeasuring)
call (Wait)
modulationpower.stop
mpx.stop

getquality
send (?U)
modulationpower.savebitmap (%DataPath-%freq-pm.jpg)
frequencydeviation.acquire
frequencydeviation.savebitmap (%DataPath-%freq-hi.jpg)
mpx.savebitmap (%DataPath-%freq-mx.jpg)

setmode (1)
send (*6)
send (*0)
rawdata.run
sleep (%DelayRDS)
rawdata.stop
rawdata.save (%DataPath-%freq-rw.spy)
rds.getdata
rds.getstatistics

textwindow.addline({%time} Saving data for %station.frequency)
createreport (%DataPath-%freq-rp.txt)

RETURN

;*************************************************************
;*** Additional routines:                                  ***
;*************************************************************

Wait:

sleep(5)
if (%timestamp>=%EndTime)
  RETURN
  endif

goto (Wait)

;*************************************************************

CheckStation:

set (Result,0)
set (WaitCnt,22)
set (AModCnt,0)
getfrequency

Loop2:

sleep (2)
inc (WaitCnt,-1)
if (%WaitCnt<0)
  goto (ContinueStation)
  endif

getamod
if (%amod>%AMLimit)
  goto (AMFailed)
  endif

goto (Loop2)

AMFailed:

inc (AModCnt,1)
if (%AModCnt>4)
  textwindow.addline({%time} Station %tempfreq skipped {AM modulation})
  RETURN
  endif
goto (Loop2)

ContinueStation: 

frequencydeviation.acquire
if (%totalsamples<50)
  textwindow.addline({%time} Station %tempfreq skipped {noisy reception})
  RETURN
  endif

if (%totalsamples<600)
  if (%abovelimit>1)
    textwindow.addline({%time} Station %tempfreq skipped {noisy reception})
    RETURN
    endif
  endif

set (Result,1)
RETURN

