@rd /S /Q output
@md output
@md output\bglp
@md output\d1namo
@set Merged_File=d:\tmp_merge.csv 
@for %%G in (5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60) do @call :SETUP_DT %%G
rem @for %%G in (5) do @call :SETUP_DT %%G
@del %Merged_File%
@del /s *.raw_stats
@exit /B 0


:SETUP_DT 
@set dt_minutes=%1
@md output\bglp\%dt_minutes%
@md output\d1namo\%dt_minutes%
@if %dt_minutes%==60 (@set scgms_dt=1:00:00) else (@set scgms_dt=0:%dt_minutes%:00)

@echo Processing prediction horizon: %scgms_dt%
@echo Training...
@call :CALL_FILES_TRAINING

@echo Testing...
@call :CALL_FILES_TESTING

@exit /B 0


:CALL_FILES_TRAINING
@echo Training on BGLP...
@cd converted\bglp
@for /R %%G in (???-ws-training.log) do @call :SCGMS_TRAINING_BGLP "%%G" "%%~dpG" "%%~nG"
rem @for /R %%G in (540-ws-training.log) do @call :SCGMS_TRAINING_BGLP "%%G" "%%~dpG" "%%~nG"
@cd ..\..

@echo Training on D1NAMO...
@cd converted\d1namo
@for /R %%G in (*.log) do @call :SCGMS_TRAINING_D1NAMO "%%G" "%%~dpG" "%%~nG"
@cd ..\..

@exit /B 0


:CALL_FILES_TESTING
@echo Testing on BGLP...
@cd converted\bglp
@for /R %%G in (???-ws-testing.log) do @call :SCGMS_TESTING_BGLP "%%G" "%%~dpG" "%%~nG"
rem @for /R %%G in (540-ws-testing.log) do @call :SCGMS_TESTING_BGLP "%%G" "%%~dpG" "%%~nG"

@rem Merge statistics of all subjects to a single file to have a cumulative analysis as well.
copy /b ..\..\output\bglp\%dt_minutes%\*.raw_stats ..\..\output\bglp\%dt_minutes%\all.raw_stats

@rem Analyze the files
@for /R %%G in (..\..\output\bglp\%dt_minutes%\*.raw_stats) do @call :ANALYZE "%%G"  

@cd ..\..


@echo Testing on D1NAMO...
@cd converted\d1namo
@for /R %%G in (*log) do @call :SCGMS_TESTING_D1NAMO "%%G" "%%~dpG" "%%~nG"
copy /b ..\..\output\d1namo\%dt_minutes%\*.raw_stats ..\..\output\d1namo\%dt_minutes%\all.raw_stats
@for /R %%G in (..\..\output\d1namo\%dt_minutes%\*.raw_stats) do @call :ANALYZE "%%G"  

@cd ..\..


@exit /B 0



:SCGMS_SET_COMMON
@set Input_File=%1
@set Input_File=%Input_File:"=%

@set Subject_ID=%3
@set Subject_ID=%Subject_ID:~0,4%

@set Parameters_File=%2..\..\output\%4\%dt_minutes%\%Subject_ID%.ini
@set Parameters_File=%Parameters_File:"=%

@set Out_Stat_File=%2..\..\output\%4\%dt_minutes%\%Subject_ID%.raw_stats
@set Out_Stat_File=%Out_Stat_File:"=%

@set Out_Single_Day_Plot=%2..\..\output\%4\%dt_minutes%\%Subject_ID%.svg
@set Out_Single_Day_Plot=%Out_Single_Day_Plot:"=%


@set Out_Log_File=%2..\..\output\%4\%dt_minutes%\%Subject_ID%.log
@set Out_Log_File=%Out_Log_File:"=%


@echo Processing file: "%Input_File%"
@exit /B 0

:SCGMS_TRAINING_BGLP
@call :SCGMS_SET_COMMON %1 %2 %3 bglp
@c:\programy\glucose3\compiled\console3 ..\..\config\train.ini
@exit /B 0


:SCGMS_TRAINING_D1NAMO
@call :SCGMS_SET_COMMON %1 %2 %3 d1namo
@c:\programy\glucose3\compiled\console3 ..\..\config\d1namo_train.ini
@exit /B 0

:SCGMS_TESTING_BGLP
@call :SCGMS_SET_COMMON %1 %2 %3 bglp

@echo Merging predictions...
@set BGLP_Best_File=%2..\..\best bglp\1_%Subject_ID%_%dt_minutes%.csv
@set BGLP_Best_File=%bglp_best_file:"=%
@echo bglp best file: %bglp_best_file% 
@c:\programy\glucose3\compiled\console3 ..\..\config\bglp_prepare.ini


@echo copying %Input_File%           
@echo to %Out_Log_File%
copy /b %Merged_File% + "%Input_File%"  "%Out_Log_File%"
@rem pause
@rem @set Input_File=%Out_Log_File% 
@c:\programy\glucose3\compiled\console3 ..\..\config\bglp_test.ini
@rem pause
@echo Testing predictions...
@c:\programy\glucose3\compiled\console3 ..\..\config\stats.ini
@rem pause

@exit /B 0


:SCGMS_TESTING_D1NAMO
@call :SCGMS_SET_COMMON %1 %2 %3 d1namo

@echo Merging predictions...
@echo bglp best file: %Input_File% 
@c:\programy\glucose3\compiled\console3 ..\..\config\d1namo_merge.ini

@echo Testing predictions...
@c:\programy\glucose3\compiled\console3 ..\..\config\stats.ini


@exit /B 0



:ANALYZE
@set Out_Stat_File=%1
@set Out_Stat_File=%Out_Stat_File:"=%
@echo Analyzing: %Out_Stat_File% 
@ ..\..\scripts\rundll-ng ..\..\scripts\analysis.dll @analyze

@exit /B 0