Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
enricofer committed Nov 21, 2021
1 parent 31efc77 commit ace7cc2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 18 deletions.
13 changes: 2 additions & 11 deletions wind_forecast_routing_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ def __init__(self, gribLayer, wind_idx, destinationCrs=None):
def getWindAt(self, t, lat, lon):
""" Returns (twd, tws) for the given point (lat, lon) at time t """
if t >= self.start_time and t<=self.end_time:
delta = t - self.start_time #self.grib.dataProvider().temporalCapabilities().referenceTime().toPyDateTime()
delta = t - self.start_time
interval = QgsInterval(delta.total_seconds())
lon_lat = QgsPointXY(lon, lat)
interval = self.grib.datasetIndexAtRelativeTime (interval, self.wind_idx)
wind_value = self.grib.datasetValue(interval, lon_lat)
twd = math.radians( heading( wind_value.x(), wind_value.y() ) )
tws = wind_value.scalar()#*1.943844
tws = wind_value.scalar()
return (twd,tws)
else:
print ("OUT_OF_RANGE", t, self.start_time, self.end_time)
Expand Down Expand Up @@ -231,28 +231,20 @@ def processAlgorithm(self, parameters, context, feedback):
"""
grib_layerfile = self.parameterAsFile(parameters, self.GRIB, context)
grib_layer = self.parameterAsLayer(parameters, self.GRIB, context)
print ("GRIB DETAILS:", grib_layerfile, grib_layer, grib_layer.id())
wind_ds = self.parameterAsInt(parameters, self.WIND_DATASET_INDEX, context)
polar_filename = self.polar_names[self.parameterAsEnum(parameters, self.POLAR, context)]
polar = Polar(os.path.join(self.polars_dir,self.polars[polar_filename]))
start = self.parameterAsDateTime(parameters, self.START_TIME, context)
start_point = self.parameterAsPoint(parameters, self.START_POINT, context, crs=grib_layer.crs())
end_point = self.parameterAsPoint(parameters, self.END_POINT, context, crs=grib_layer.crs())

print ("parameters", parameters)

track = ((start_point.y(), start_point.x()), (end_point.y(), end_point.x()))
geo_context = QgsRectangle(start_point.x(),start_point.y(), end_point.x(),end_point.y())
track_dist = QgsPointXY(start_point.x(),start_point.y()).sqrDist(end_point.x(), end_point.y())
geo_context.grow( (track_dist/2 ) if track_dist < 1 else 0.5 ) #limit grow to 0.5 degree
checkValidity = in_sea_checker(geo_context)
grib_reader = grib_sampler(grib_layer,wind_ds)

#test
#print ("TESTGRIB",grib_reader.getWindAt(datetime.strptime("02/02/21 18:00", "%d/%m/%y %H:%M"),39.31064,5.06086))

print ("track", track)

route_process = Routing(LinearBestIsoRouter, polar, track, grib_reader, start.toPyDateTime(), lineValidity = checkValidity.path_in_sea_xy,)
step = 1
execution = "ok"
Expand Down Expand Up @@ -289,7 +281,6 @@ def processAlgorithm(self, parameters, context, feedback):
if execution == "ok":
tr.append((*track[-1], dateutil.parser.parse(tr[-1][2])+timedelta(hours=1), 0, 0, 0, 0))

print (tr)
route_polyline = []


Expand Down
7 changes: 0 additions & 7 deletions wind_forecast_routing_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def initAlgorithm(self, config):
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
print ("CONFIG LAUNCH", config)
if "nooutput" in config:
self.allow_output = False
else:
Expand All @@ -191,7 +190,6 @@ def initAlgorithm(self, config):
for wm in wind_models:
wind_models_labels.append(wm["context"])

print (wind_models_labels)
# We add the input vector features source. It can have any kind of
# geometry.
self.addParameter(QgsProcessingParameterEnum(self.MODEL, 'Wind Model', options=wind_models_labels, defaultValue=0, allowMultiple=False))
Expand Down Expand Up @@ -275,7 +273,6 @@ def processAlgorithm(self, parameters, context, feedback):


model = self.parameterAsEnum(parameters, self.MODEL, context)
print (model)
wind_model_def = None
if model == 0:
for wm in wind_models:
Expand Down Expand Up @@ -306,15 +303,13 @@ def processAlgorithm(self, parameters, context, feedback):
rawReplyObject = manager.blockingGet(request)
j = QJsonDocument.fromJson(rawReplyObject.content())
replyObject = j.toVariant()
print("replyObject", replyObject)
if replyObject["status"]:

download_params = {
'URL': replyObject["message"]["url"],
'OUTPUT': grib_output
}

print ("download_params:",download_params)
output_download = processing.run('native:filedownloader', download_params, context=context, feedback=feedback, is_child_algorithm=True)
else:
print ("ERROR", replyObject)
Expand All @@ -341,9 +336,7 @@ def processAlgorithm(self, parameters, context, feedback):
"extent": QgsMeshLayer(output_download['OUTPUT'],"grib","mdal").extent(),
"Inverted_clip": output_context
}
print ("context_land_params", context_land_params)
context_land_results = processing.run('sailtools:inverted_clip', context_land_params, context=context, feedback=feedback, is_child_algorithm=True)
print ("context_land_results", context_land_results)
self.output_routing[self.OUTPUT_CONTEXT] = context_land_results['OUTPUT']

self.output_routing["elab_name"] = "wind routing %s %s %s %.2f,%.1f,%.1f,%.1f" % (
Expand Down

0 comments on commit ace7cc2

Please sign in to comment.