__init__.py 3.89 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
from shapely import geometry
from shapely.geometry import LineString
from shapely.strtree import STRtree

def storetree():
Tomas Pettersson's avatar
Tomas Pettersson committed
19
20
21
22
23
24
25
  try:
    with open('../coastline_HELCOM_82214.dat',"r") as coastlinefile:
      lines = coastlinefile.readlines()
      section = []
      sections = []
      
      for line in lines:
Tomas Pettersson's avatar
Tomas Pettersson committed
26
27
28
        # if len(section) == 1000:
        #   sections.append(LineString(section))
        #   section = []
Tomas Pettersson's avatar
Tomas Pettersson committed
29
        if '-999999' in line:
Tomas Pettersson's avatar
Tomas Pettersson committed
30
          if len(section) > 0: #this will skip some points where section will end with only one coord. Possibly create a line from previous section on ending point
Tomas Pettersson's avatar
Tomas Pettersson committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
            sections.append(LineString(section))
          section = []
          continue
        split = line.split('   ')
        section.append([float(split[2]),float(split[1])])
      print('Number of lines: '+str(len(sections)))
      with open('resources/strtree.pickle', 'wb') as handle:
        pickle.dump(sections, handle, protocol=pickle.HIGHEST_PROTOCOL)
      # verify
      tree = STRtree(sections)
      queryline = LineString([[57.5,17.5],[57.5,18.5]])
      matches = tree.query(queryline);
      print('Number of matches: '+str(len(matches)))

  except Exception as error:
    print_exc()
    sys.exit(1)  
Tomas Pettersson's avatar
Tomas Pettersson committed
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

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
70
71
72
73
74
75

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

Tomas Pettersson's avatar
Tomas Pettersson committed
78
79
def run():
  try: 
80
    writeStatus("INITIAL")
Tomas Pettersson's avatar
Tomas Pettersson committed
81
82
83
    strtree = STRtree(pickle.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.pickle')))))
    m = Model()()
    with open('input.json',"r") as inputfile:
Tomas Pettersson's avatar
Tomas Pettersson committed
84
      print("PARTIAL")
Tomas Pettersson's avatar
Tomas Pettersson committed
85
86
87
88
89
90
91
92
93
      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'])
Tomas Pettersson's avatar
Tomas Pettersson committed
94
          multipointoutlet, pDist = m.createOutlet(outletgeom)
Tomas Pettersson's avatar
Tomas Pettersson committed
95
96
        if ('auxiliary' in inputfeature['properties'] and inputfeature['properties']['auxiliary'] == 'exercise'):
          exercisefeature = inputfeature
Tomas Pettersson's avatar
Tomas Pettersson committed
97
      outputfeaturecollection = m.createOutput(outletproperties['simulation']['startDate'],pDist,multipointoutlet,exercisefeature, strtree)
Tomas Pettersson's avatar
Tomas Pettersson committed
98
99
100
101
102
      outputfeaturecollection['properties'] = {}
      outputfeaturecollection['properties']['uuid'] = os.getcwd().split(os.sep)[-1]
      outputfeaturecollection['properties']['status'] = 'COMPLETE'
      outputfeaturecollection['properties']['simulation'] = outletproperties['simulation']

Tomas Pettersson's avatar
Tomas Pettersson committed
103
104
105
      Cloudtrack.write(outputfeaturecollection)
      Particletrack.write(outputfeaturecollection)
      # Output.write(outputfeaturecollection)
Tomas Pettersson's avatar
Tomas Pettersson committed
106
      print("COMPLETE")
107

Tomas Pettersson's avatar
Tomas Pettersson committed
108
109
110
    sys.exit(0)
  except Exception as error:
    print_exc()
Tomas Pettersson's avatar
Tomas Pettersson committed
111
    writeStatus("ERROR")
Tomas Pettersson's avatar
Tomas Pettersson committed
112
113
    sys.exit(1)
'''
114
  To run in terminal call with python __init__.py 
Tomas Pettersson's avatar
Tomas Pettersson committed
115
116
117
118
'''
if __name__ == "__main__": 
  storetree()