#!/usr/bin/python # coding: utf-8 import os import sys import json import pickle import pkg_resources from traceback import print_exc from model import Model from output import Output from output import Particletrack from output import Cloudtrack from shapely import geometry from shapely.geometry import LineString from shapely.strtree import STRtree def storetree(): try: with open('../coastline_HELCOM_82214.dat',"r") as coastlinefile: lines = coastlinefile.readlines() section = [] sections = [] for line in lines: # if len(section) == 1000: # sections.append(LineString(section)) # section = [] if '-999999' in line: 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 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) 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") def writeStatus(status): props = read_properties('taskinfo.properties') if props.has_key("status"): props['status'] = status write_properties('taskinfo.properties', props) print(status) def run(): try: writeStatus("INITIAL") strtree = STRtree(pickle.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.pickle'))))) m = Model()() with open('input.json',"r") as inputfile: print("PARTIAL") 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, pDist = m.createOutlet(outletgeom) if ('auxiliary' in inputfeature['properties'] and inputfeature['properties']['auxiliary'] == 'exercise'): exercisefeature = inputfeature outputfeaturecollection = m.createOutput(outletproperties['simulation']['startDate'],pDist,multipointoutlet,exercisefeature, strtree) outputfeaturecollection['properties'] = {} outputfeaturecollection['properties']['uuid'] = os.getcwd().split(os.sep)[-1] outputfeaturecollection['properties']['status'] = 'COMPLETE' outputfeaturecollection['properties']['simulation'] = outletproperties['simulation'] Cloudtrack.write(outputfeaturecollection) Particletrack.write(outputfeaturecollection) # Output.write(outputfeaturecollection) print("COMPLETE") sys.exit(0) except Exception as error: print_exc() writeStatus("ERROR") sys.exit(1) ''' To run in terminal call with python __init__.py ''' if __name__ == "__main__": storetree()