Skip to content
Snippets Groups Projects
cost_function.py 9.03 KiB
Newer Older
#!/usr/bin/env python3

Lionel GUEZ's avatar
Lionel GUEZ committed
"""A script that takes the graph of segments without cost functions
Lionel GUEZ's avatar
Lionel GUEZ committed
and computes the non-local cost functions applied to edges.
Lionel GUEZ's avatar
Lionel GUEZ committed

Input:

- "node_id_param.json", expected to be in the current directory

Lionel GUEZ's avatar
Lionel GUEZ committed
- the graph of segments without cost functions, "segments.gt" or
  "segments.graphml", expected to be in the current directory
Lionel GUEZ's avatar
Lionel GUEZ committed

- shapefiles, specified as command line arguments

Output: the graph of segments with cost functions,
'segments_cost_functions.gt'

"""

import graph_tool
import time
import json
import math
import report_graph
Lionel GUEZ's avatar
Lionel GUEZ committed
import util_eddies
import bisect
Lionel GUEZ's avatar
Lionel GUEZ committed
import argparse
Omega = 2 * math.pi / 86164.

def calculate_radii_rossby(list_eddies, e_overestim, handlers, array_d_init):
    """Compute average on list_eddies of Rossby number and radius of
    maximum speed contour.

    """

Lionel GUEZ's avatar
Lionel GUEZ committed
    radii = 0 # in m
Lionel GUEZ's avatar
Lionel GUEZ committed
    rossby = 0
    n_valid_Rossby = 0
    n_eddies = len(list_eddies)
Lionel GUEZ's avatar
Lionel GUEZ committed

    for n in list_eddies:
        date_index, eddy_index = report_graph.node_to_date_eddy(n, e_overestim)
        i_SHPC = get_SHPC(array_d_init, date_index)
        ishape = util_eddies.comp_ishape(handlers[i_SHPC], date_index,
                                         eddy_index)
Lionel GUEZ's avatar
Lionel GUEZ committed

Lionel GUEZ's avatar
Lionel GUEZ committed
        # Now that we have the location in the shapefiles, we need to
        # get the radius and the rossby number:
        shapeRec = handlers[i_SHPC]["readers"]["extremum"].shapeRecord(ishape)
Lionel GUEZ's avatar
Lionel GUEZ committed
        lat_in_deg = shapeRec.shape.points[0][1] # in degrees
        f = 2 * Omega * math.sin(math.radians(lat_in_deg)) # in s-1
        V_max = shapeRec.record[4] # in m/s
        R_Vmax = handlers[i_SHPC]["readers"]["max_speed_contour"]\
            .record(ishape)['r_eq_area'] * 1000 # in m
Lionel GUEZ's avatar
Lionel GUEZ committed

        if (V_max < 100):
            rossby += V_max / (f * R_Vmax)
            n_valid_Rossby += 1
Lionel GUEZ's avatar
Lionel GUEZ committed

Lionel GUEZ's avatar
Lionel GUEZ committed
        radii += R_Vmax # in m
Lionel GUEZ's avatar
Lionel GUEZ committed

    radii /= n_eddies
    
    if n_valid_Rossby != 0:
        rossby /= n_valid_Rossby
    else:
        rossby = None

    return {"radii": radii, "rossby": rossby}
def get_SHPC(array_d_ini, date_index):
    i_SHPC = bisect.bisect(array_d_init, date_index)
    assert i_SHPC >= 1
    return i_SHPC - 1

Lionel GUEZ's avatar
Lionel GUEZ committed
t0 = time.perf_counter()
timings = open("timings.txt", "w")
Lionel GUEZ's avatar
Lionel GUEZ committed
parser = argparse.ArgumentParser()
parser.add_argument("SHPC_dir", nargs='+')
parser.add_argument("--graphml", action = "store_true",
                    help = "save to graphml format")
Lionel GUEZ's avatar
Lionel GUEZ committed
args = parser.parse_args()
Lionel GUEZ's avatar
Lionel GUEZ committed
# Grab e_overestim:
with open("node_id_param.json") as f: node_id_param = json.load(f)
e_overestim = node_id_param["e_overestim"]

Lionel GUEZ's avatar
Lionel GUEZ committed
# Set some values needed for the cost function:
Lionel GUEZ's avatar
Lionel GUEZ committed
delta_cent_mean = 3.8481 # [in km]
delta_cent_std = 8.0388

Lionel GUEZ's avatar
Lionel GUEZ committed
delta_ro_mean = -0.0025965
delta_ro_std = 5.2168

Lionel GUEZ's avatar
Lionel GUEZ committed
delta_r_mean = -0.0094709 * 1000 # [in m]
delta_r_std = 8.6953 * 1000

Lionel GUEZ's avatar
Lionel GUEZ committed
# Load the graph_tool file:
Lionel GUEZ's avatar
Lionel GUEZ committed

Lionel GUEZ's avatar
Lionel GUEZ committed
print('Loading graph...')
Lionel GUEZ's avatar
Lionel GUEZ committed
    g = graph_tool.load_graph('segments.gt')
except FileNotFoundError:
Lionel GUEZ's avatar
Lionel GUEZ committed
    g = graph_tool.load_graph('segments.graphml')
Lionel GUEZ's avatar
Lionel GUEZ committed
print('Loading done...')
print("Input graph:")
print("Number of vertices:", g.num_vertices())
print("Number of edges:", g.num_edges())
print("Internal properties:")
g.list_properties()
Lionel GUEZ's avatar
Lionel GUEZ committed
t1 = time.perf_counter()
Lionel GUEZ's avatar
Lionel GUEZ committed
timings.write(f"loading: {t1 - t0:.0f} s\n")
Lionel GUEZ's avatar
Lionel GUEZ committed
t0 = t1
g.vp['pos_first'] = g.new_vp('object')
g.vp['pos_last'] = g.new_vp('object')
g.vp['first_av_rad'] = g.new_vp('float')
g.vp['first_av_ros'] = g.new_vp('float')
g.vp['last_av_rad'] = g.new_vp('float')
g.vp['last_av_ros'] = g.new_vp('float')
g.ep['cost_function'] = g.new_ep('float')
# Set up the list of SHPC:
Lionel GUEZ's avatar
Lionel GUEZ committed
handlers = [util_eddies.open_shpc(shpc_dir) for shpc_dir in args.SHPC_dir]

array_d_init = [handler["d_init"] for handler in handlers]
# (create the list once and for all)
Lionel GUEZ's avatar
Lionel GUEZ committed
num_of_days_to_avg = 7 # number of days to average
Lionel GUEZ's avatar
Lionel GUEZ committed
print("Iterating on vertices...")

Lionel GUEZ's avatar
Lionel GUEZ committed
for n in g.vertices():
    if n.in_degree() != 0 or n.out_degree() != 0:
        segment = g.vp.inst_eddies[n]
        num_of_days = len(segment)

        # Calculate the date index, the eddy index and the SHPC index of
        # the first instantaneous eddy in the segment, then grab the
        # position of the extremum and store it in a vertex property:
        date_index, eddy_index = report_graph.node_to_date_eddy(segment[0],
                                                                e_overestim)
        i_SHPC = get_SHPC(array_d_init, date_index)
        ishape = util_eddies.comp_ishape(handlers[i_SHPC], date_index,
                                         eddy_index)
        g.vp.pos_first[n] = handlers[i_SHPC]["readers"]["extremum"]\
            .shape(ishape).points[0] # [in degrees]

        # Same for last instantaneous eddy in the segment:
        date_index, eddy_index = report_graph.node_to_date_eddy(segment[-1],
                                                                e_overestim)
        i_SHPC = get_SHPC(array_d_init, date_index)
        ishape = util_eddies.comp_ishape(handlers[i_SHPC], date_index,
                                         eddy_index)
        g.vp.pos_last[n] = handlers[i_SHPC]["readers"]["extremum"]\
            .shape(ishape).points[0] # [in degrees]

        if (num_of_days > num_of_days_to_avg):
            # The segment is longer than the number of days over which
            # to average

            # First 7 days calculation
            first_res = calculate_radii_rossby(segment[:num_of_days_to_avg],
                                               e_overestim, handlers,
                                               array_d_init)

            # Average and assign the first radii:
            g.vp.first_av_rad[n] = first_res['radii']

            if first_res['rossby'] is not None:
                # Average and assign the rossbies:
                g.vp.first_av_ros[n] = first_res['rossby']

            # Last 7 days calculation:
            last_res = calculate_radii_rossby(segment[- num_of_days_to_avg:],
                                              e_overestim, handlers,
                                              array_d_init)

            # Average and assign the last radii
            g.vp.last_av_rad[n] = last_res['radii']

            if last_res['rossby'] is not None:
                # Average and assign the rossbies:
                g.vp.last_av_ros[n] = last_res['rossby']
        else:
            # The number of eddies in a segment is lower than the number
            # of days over which to average. The values will be the same
            # except for the positions.
            res = calculate_radii_rossby(segment, e_overestim, handlers,
                                         array_d_init)

            if res['rossby'] is not None:
                # Average and assign the rossbies:
                rossby = res['rossby']
                g.vp.first_av_ros[n] = rossby
                g.vp.last_av_ros[n] = rossby

            # Average and assign the radii
            radii = res['radii']
            g.vp.first_av_rad[n] = radii
            g.vp.last_av_rad[n] = radii
Lionel GUEZ's avatar
Lionel GUEZ committed
t1 = time.perf_counter()
Lionel GUEZ's avatar
Lionel GUEZ committed
timings.write(f"iterating on vertices: {t1 - t0:.0f} s\n")
Lionel GUEZ's avatar
Lionel GUEZ committed
t0 = t1
Lionel GUEZ's avatar
Lionel GUEZ committed
print("Iterating on edges...")

for edge in g.edges():
    source_node = edge.source()
    target_node = edge.target()
Lionel GUEZ's avatar
Lionel GUEZ committed

Lionel GUEZ's avatar
Lionel GUEZ committed
    lat_for_conv = (g.vp.pos_last[source_node][1] +
Lionel GUEZ's avatar
Lionel GUEZ committed
                    g.vp.pos_first[target_node][1]) / 2
    # (latitude needed for conversion of degrees to kilometers)
    lat_for_conv = math.radians(lat_for_conv) # need to convert to radians
Lionel GUEZ's avatar
Lionel GUEZ committed

Lionel GUEZ's avatar
Lionel GUEZ committed
    # because of the wrapping issue (360° wrapping incorrectly to 0°),
    # we check for that here
    lon_diff = abs(g.vp.pos_last[source_node][0] \
                   - g.vp.pos_first[target_node][0])
    if (lon_diff > 300):
        lon_diff = 360 - lon_diff

Lionel GUEZ's avatar
Lionel GUEZ committed
    Delta_Cent = math.sqrt((lon_diff * 111.32 * math.cos(lat_for_conv))**2
                           + ((g.vp.pos_last[source_node][1]
                               - g.vp.pos_first[target_node][1]) * 110.574)**2)
Lionel GUEZ's avatar
Lionel GUEZ committed

    # calculate the first term
    first_term = ((Delta_Cent - delta_cent_mean)/delta_cent_std) ** 2
Lionel GUEZ's avatar
Lionel GUEZ committed

    # Rossbies:
    if (g.vp.first_av_ros[target_node] and g.vp.last_av_ros[source_node]):
Lionel GUEZ's avatar
Lionel GUEZ committed
        Delta_Ro = g.vp.last_av_ros[source_node] \
            - g.vp.first_av_ros[target_node]
Lionel GUEZ's avatar
Lionel GUEZ committed
        # At least one of the rossbies is invalid.
        # Delta_Ro = delta_ro_mean
        Delta_Ro = 0
Lionel GUEZ's avatar
Lionel GUEZ committed

    # Calculate the second term
    second_term = ((Delta_Ro - delta_ro_mean)/delta_ro_std ) ** 2
Lionel GUEZ's avatar
Lionel GUEZ committed

    # R_Vmax 1 and 2 already exist, just get the delta
Lionel GUEZ's avatar
Lionel GUEZ committed

Lionel GUEZ's avatar
Lionel GUEZ committed
    Delta_R_Vmax = g.vp.last_av_rad[source_node] \
        - g.vp.first_av_rad[target_node]
Lionel GUEZ's avatar
Lionel GUEZ committed

    # Calculate the third term
    third_term = ((Delta_R_Vmax - delta_r_mean)/delta_r_std) ** 2
Lionel GUEZ's avatar
Lionel GUEZ committed

    #############################
    # calculate the cost function
    #############################
Lionel GUEZ's avatar
Lionel GUEZ committed

    cf = math.sqrt(first_term + second_term + third_term)
Lionel GUEZ's avatar
Lionel GUEZ committed

    # assign as weight to the edge
    g.ep.cost_function[edge] = cf
Lionel GUEZ's avatar
Lionel GUEZ committed
t1 = time.perf_counter()
Lionel GUEZ's avatar
Lionel GUEZ committed
timings.write(f"iterating on edges: {t1 - t0:.0f} s\n")
Lionel GUEZ's avatar
Lionel GUEZ committed
t0 = t1
Lionel GUEZ's avatar
Lionel GUEZ committed
print("Saving...")

if args.graphml:
    g.save('segments_cost_functions.graphml')
else:
    g.save('segments_cost_functions.gt')

print('All done')
Lionel GUEZ's avatar
Lionel GUEZ committed
t1 = time.perf_counter()
Lionel GUEZ's avatar
Lionel GUEZ committed
timings.write(f"saving: {t1 - t0:.0f} s\n")
Lionel GUEZ's avatar
Lionel GUEZ committed
timings.close()