Forum

RE: Scheduled forecast pt II

MM
Mason Marchildon, modified 3 Months ago.

Scheduled forecast pt II

Rookie Crystal gazer Posts: 19 Join Date: 5/1/17 Recent Posts
I've followed the suggestions from my previous thread, thank you Ivo.
My question is regarding updating the system time upon running a windows task (following:  https://publicwiki.deltares.nl/display/FEWSDOC/03+Setting+Up+Scheduled+Forecasts) I see in the one can pass the current system time to the module instance by setting "<time0>%TIME0%</time0>" in the task property file.

My question is: how can I have this task run by passing the current (computer) time to the module instance?
IM
Ivo Miltenburg, modified 3 Months ago.

RE: Scheduled forecast pt II

Rookie Crystal gazer Posts: 13 Join Date: 3/12/13 Recent Posts
Hi Mason,
If your Delft-FEWS SA does not start up with the system time as T0, you will want to provide (hardcode) the date/tim in the time0 field of the task property file. 

Since you are (probably) running a DOS Batch script to call the workflow taskrun, you might want to use some DOS Batch logic to do so. I will provide an example (and files, if this forum allows me to do so) - but this is expertise/knowledge outside Delft-FEWS. DOS Batch scripting is quitte finicky and an art in and of itself. If you know some other low level way of achieving the same result, go for it. If you do want to use the example batch script, you might need to tweak the logic to get the correct datetime format for your application. My example's format is "DD-MM-YYY HH:MM"

Good luck, let me know if you succeeded and what your final approach was...if you can share, the community can benefit as well!

Example taskProperty (template) file, with search_string  "TIMEZERO" (taskProperty_template.xml)
-----
<?xml version="1.0" encoding="UTF-8"?>
<taskProperties xsi:schemaLocation="http://www.wldelft.nl/fews http://fews.wldelft.nl/schemas/version1.0/taskProperties.xsd" xmlns="http://www.wldelft.nl/fews" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <description>ImportNOAAGFSdata_scheduled</description>
    <userId>fews</userId>
    <workflowId>Import_GFS_0.25_Schedule</workflowId>
    <taskSelection>
        <singleTask>
            <time0>TIMEZERO</time0>
        </singleTask>
    </taskSelection>
    <forecastPriority>Normal</forecastPriority>
    <makeForcastCurrent>true</makeForcastCurrent>
</taskProperties>
-----

Example DOS Batch script to replace  "TIMEZERO" and write to newly created taskProperty.xml)
-----
@ECHO OFF
setlocal enabledelayedexpansion

set rootdirpath=%~dp0

cd %rootdirpath%
rem Derive parametername from filedirpath
for /F "tokens=2" %%i in ('DATE /t') do set "DT=%%i"
set TM_HH=%TIME:~0,2%
set TM_MM=%TIME:~3,2%
set TM_HH=%TM_HH: =0%
set TM_MM=%TM_MM: =0%

set DATETIME=%DT% %TM_HH%:%TM_MM%
echo %DATETIME%

set "search=TIMEZERO"
set "replace=%DATETIME%"
del taskProperty.xml
for /F "delims=" %%a in (taskProperty_template.xml) DO (
   set line=%%a
   setlocal EnableDelayedExpansion
   >> taskProperty.xml echo(!line:%search%=%replace%!
   endlocal
)
-----

Resulting taskProperty file, with search_string  "TIMEZERO"  replaced (taskProperty.xml)
----
<?xml version="1.0" encoding="UTF-8"?>
<taskProperties xsi:schemaLocation="http://www.wldelft.nl/fews http://fews.wldelft.nl/schemas/version1.0/taskProperties.xsd" xmlns="http://www.wldelft.nl/fews" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <description>ImportNOAAGFSdata_scheduled</description>
    <userId>fews</userId>
    <workflowId>Import_GFS_0.25_Schedule</workflowId>
    <taskSelection>
        <singleTask>
            <time0>14-06-2021 11:20</time0>
        </singleTask>
    </taskSelection>
    <forecastPriority>Normal</forecastPriority>
    <makeForcastCurrent>true</makeForcastCurrent>
</taskProperties>
----
MM
Mason Marchildon, modified 2 Months ago.

RE: Scheduled forecast pt II

Rookie Crystal gazer Posts: 19 Join Date: 5/1/17 Recent Posts
Thank you Ivo,

Now my solution has been to include a Python script prior to the FEWS call within my batch file.  (Sorry, not going to venture in the batch scripting rabbit holeemoticon

cd C:\FEWS\
python .\WindowsTaskScheduling\refreshT0.py C:/FEWS/WindowsTaskScheduling/TaskRun_Update_Daily_Met_Gob.xml UpdateDailyMetGob
.\bin\windows\Delft-FEWS.exe "runTask=C:\FEWS\WindowsTaskScheduling\TaskRun_Update_Daily_Met_Gob.xml"


Where refreshT0,py:
import sys
import datetime
import pytz

XML_PATH = sys.argv[1]
WORKFLOW_ID = sys.argv[2]

s = '''<?xml version="1.0" encoding="UTF-8"?>
<taskProperties xsi:schemaLocation="http://www.wldelft.nl/fews http://fews.wldelft.nl/schemas/version1.0/taskProperties.xsd" xmlns="http://www.wldelft.nl/fews" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <description>Update daily gobs (scheduled task)</description>
    <userId>mm</userId>
    <workflowId>{}</workflowId>
    <taskSelection>
        <singleTask>
            <time0>{}</time0>
        </singleTask>
    </taskSelection>
    <forecastPriority>Normal</forecastPriority>
    <makeForcastCurrent>true</makeForcastCurrent>
</taskProperties>
'''

cur = datetime.datetime.now(tz=pytz.timezone("EST"))

with open(XML_PATH, "w") as f:
    f.write(s.format(WORKFLOW_ID, cur.isoformat()))


With that I followed the links up above,  Works great! thanks

Mason Marchildon