__init__.py 3.32 KB
Newer Older
Tomas Pettersson's avatar
Tomas Pettersson committed
1 2 3 4 5 6 7
#!/usr/bin/python
# coding: utf-8

import os
import sys
import json
import pickle
Tomas Pettersson's avatar
Tomas Pettersson committed
8
import pkg_resources
Tomas Pettersson's avatar
Tomas Pettersson committed
9 10
from traceback import print_exc
from model import Model
11 12 13
from output import Output
from output import Particletrack
from output import Cloudtrack
Tomas Pettersson's avatar
Tomas Pettersson committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
from shapely import geometry
from shapely.geometry import LineString
from shapely.strtree import STRtree

def storetree():
  with open('../coastline_HELCOM_82214.dat',"r") as coastlinefile:
    lines = coastlinefile.readlines()
    section = []
    sections = []
    for line in lines:
      if '-999999' in line:
        if len(section) > 0:
          sections.append(LineString(section))
        section = []
        continue
      split = line.split('   ')
      section.append([float(split[2]),float(split[1])])
Tomas Pettersson's avatar
Tomas Pettersson committed
31
    with open('resources/strtree.pickle', 'wb') as handle:
Tomas Pettersson's avatar
Tomas Pettersson committed
32 33
      pickle.dump(sections, handle, protocol=pickle.HIGHEST_PROTOCOL)

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

def read_properties(filepath, sep='=', comment_char='#'):
  props = {}
  try:
    with open(filepath, "rt") as f:
      for line in f:
        l = line.strip()
        if l and not l.startswith(comment_char):
          key_value = l.split(sep)
          key = key_value[0].strip()
          value = sep.join(key_value[1:]).strip().strip('"') 
          props[key] = value 
  except IOError:
    return props
  return props

def write_properties(filepath, props):
  with open(filepath, "wt") as f:
    for key, value in props.iteritems():
      f.write(key+"="+value+"\n")

Tomas Pettersson's avatar
Tomas Pettersson committed
55 56 57 58 59 60

def writeStatus(status):
  props = read_properties('taskinfo.properties')
  if props.has_key("status"):
    props['status'] = status
    write_properties('taskinfo.properties', props)
61
  print(status)
Tomas Pettersson's avatar
Tomas Pettersson committed
62

Tomas Pettersson's avatar
Tomas Pettersson committed
63 64
def run():
  try: 
65
    # writeStatus("INITIAL")
Tomas Pettersson's avatar
Tomas Pettersson committed
66 67 68
    strtree = STRtree(pickle.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.pickle')))))
    m = Model()()
    with open('input.json',"r") as inputfile:
69
      # writeStatus("PARTIAL")
Tomas Pettersson's avatar
Tomas Pettersson committed
70 71 72 73 74 75 76 77 78 79 80 81
      inputfeaturecollection = json.load(inputfile)
      output = {}
      multipointoutlet = {} 
      outletproperties = {}
      exercisefeature = {}
      for inputfeature in inputfeaturecollection['features']:
        if ('primary' in inputfeature['properties'] and inputfeature['properties']['primary'] == 'PADM'):
          outletproperties = inputfeature['properties']
          outletgeom = geometry.shape(inputfeature['geometry'])
          multipointoutlet = m.createOutlet(outletgeom)
        if ('auxiliary' in inputfeature['properties'] and inputfeature['properties']['auxiliary'] == 'exercise'):
          exercisefeature = inputfeature
Tomas Pettersson's avatar
Tomas Pettersson committed
82
      outputfeaturecollection = m.createOutput(outletproperties['simulation']['startDate'],multipointoutlet,exercisefeature, strtree)
Tomas Pettersson's avatar
Tomas Pettersson committed
83 84 85 86 87
      outputfeaturecollection['properties'] = {}
      outputfeaturecollection['properties']['uuid'] = os.getcwd().split(os.sep)[-1]
      outputfeaturecollection['properties']['status'] = 'COMPLETE'
      outputfeaturecollection['properties']['simulation'] = outletproperties['simulation']

88 89
      Cloudtrack.write(outputfeaturecollection)
      Particletrack.write(outputfeaturecollection)
90 91
      # Output.write(outputfeaturecollection)
      # writeStatus("COMPLETE")
92

Tomas Pettersson's avatar
Tomas Pettersson committed
93 94 95
    sys.exit(0)
  except Exception as error:
    print_exc()
96
    # writeStatus("ERROR")
Tomas Pettersson's avatar
Tomas Pettersson committed
97 98
    sys.exit(1)
'''
99
  To run in terminal call with python __init__.py 
Tomas Pettersson's avatar
Tomas Pettersson committed
100 101 102 103
'''
if __name__ == "__main__": 
  storetree()