Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagebash
themeDJango
import os

####SETTINGS####

localrules: create_sims, grid, locations # these rules are not submitted


wind_speed_## TODO this list also in the script create sims. How to access this list in a python script?
wind_speed_list = [30, 20]

win #= variations
True

####End result####

rule all:
    input:
        output_bot=os.path.join('data','3-input','vzm-j19_6-v1a_20m.bot'),
        output_fxw=path_fig=expand("reports/U{wind_speed}.png", wind_speed=wind_speed_list),
rule analyse:
input:
path="data/4-output/U{wind_speed}/U{wind_speed}_p1.tab"
output:
path_fig="reports/U{wind_speed}.png"
script:
os.path.join('datasrc','34-inputanalyze','vzm-j19_6-v1a_20m.fxwanalyse.py'),

        output_p1rule run:
input:
output_bot=os.path.join('data','3-input','p1bed.xynbot'),
        output_p2p1=os.path.join('data','3-input','p2p1.xyn'),
        path_sims=expand("data/4-output/U{wind_speed}/U{wind_speed}.swn",  wind_speed=wind_speed_list),
        
output:
res=expand("data/4-output/U{wind_speed}/U{wind_speed}_p1.tab",  wind_speed=wind_speed_list)

rule run:
if not   output:
        res=win:
shell("data/4-output/U{wildcards.wind_speed}/run_SWAN.sh U{wildcards.wind_speed}_p1.tab"
    shell: ")
else:
shell("cd data/4-output/U{wildcards.wind_speed}/swan4131A1c7.sh && copy U{wildcards.wind_speed}.swn INPUT && call ..\\..\\..\\bin\\swan_4131A_1_del_w64_i18_omp.exe ")

rule create_sims:
    input:
        bot=os.path.join('data','3-input','vzm-j19_6-v1a_20mbed.bot'),
        p1=os.path.join('data','3-input','p1.xyn'),
        path_template = os.path.join('config','template')
    output:
        path_sims=expand("data/4-output/U{wind_speed}/U{wind_speed}.swn",  wind_speed=wind_speed_list)
    script:
        os.path.join('src','1-prepare','create_sims.py')



rule grid:
    input:
        path_bot=os.path.join('data','1-external','vzm-j19_6-v1a_20mbed.bot'),
        path_fxw=os.path.join('data','1-external','vzm-j19_6-v1a_20mobs.fxw'),
    output:
        output_bot=os.path.join('data','3-input','vzm-j19_6-v1a_20mbed.bot'),
        output_fxw=os.path.join('data','3-input','vzm-j19_6-v1a_20mobs.fxw'),
    script:
        os.path.join('src','1-prepare','create_grid.py')


rule locations:
    input:
        path_p1=os.path.join('data','1-external','p1.xyn'),
        path_p2=os.path.join('data','1-external','p2.xyn'),
    output:
        output_p1=os.path.join('data','3-input','p1.xyn'),
        output_p2=os.path.join('data','3-input','p2.xyn'),
    script:
        os.path.join('src','1-prepare','create_output_locations.py')


This dummy project required the execution of the following python scripts before the simulations are submitted.

...

Code Block
languagebash
themeDJango
import os
import numpy as np
from shutil import copyfile
from mako.template import Template

def render_template(data,template):
    '''
    Render template
    input:
        data: dictionary with variables to render
        template: template file
    '''
    with open(template) as f:
        template = f.read()
    ##
    tmpl = Template(text=template)

    with open(os.path.join(data['output_path'], '{}.{}'.format(data['fname'],data['extension']) ),mode='w') as f:
        f.write(tmpl.render(**data))




path_sims = snakemake.output.path_sims
path_template = snakemake.input.path_template

## if file exists overwrite
overwrite            = False
swan_template        = 'swan_template_final.swn'

# =============================================================================
#  wave conditions
# =============================================================================

wind_speed_list           = [30, 20]

# =============================================================================
#  create sims
# =============================================================================


## template file
template        = os.path.join(path_template,swan_template)

run_condition = {'water_level':[],
                 'wind_speed':[],
                 'wind_dir':[]}
## make combinations
for ii, item in enumerate(wind_speed_list):
    ## get conditions
    wind_speed  = item

    fname = 'U{}'.format(wind_speed)

    
    ## data for template
    data = {'output_path':os.path.dirname(path_sims[ii]),
            'extension':'swn',
            'variant':'A',
            'fname':fname,
            'wind_speed': wind_speed,
            'wind_dir': 180,
            'water_level': 0}
    
    ## skip if file already exists
    if os.path.exists(os.path.join(path_sims[ii], '{}.swn'.format(fname))) and overwrite:
        print('skipped {}'.format(fname))
        continue
    ## render template
    render_template(data,template)
    ## copy swan_run

    copyfile(os.path.join(path_template,'swan4131A1c7run_SWAN.sh'), os.path.join(os.path.dirname(path_sims[ii]),'swan4131A1c7run_SWAN.sh'))