diff --git a/cost_function.py b/cost_function.py index 02b37f55596fe437d478c7d08e6b427e1c2dc33a..8fbe63f38f782c3655c6d86ee537aa95f6f7df12 100755 --- a/cost_function.py +++ b/cost_function.py @@ -120,132 +120,123 @@ for year in range(1993,2019): # change if there is a change over the number of days to average num_of_days_to_avg = 7 -# We iterate on the years because the segments are non sequential in -# time. For each year, we are going to process only the segments that -# begin in that year. -for yr in range(1993, 2019): - print(f'Current year: {yr}') - - # iterate on the vertices - for n in g.vertices(): - # Get the segment and the number of days - segment = g.vp.segment[n] - - # calculate the indexes and dates - first = report_graph.node_to_date_eddy(segment[0], e_overestim) - - # get year - first_date = datetime.date(1950, 1, 1) \ - + datetime.timedelta(first['date_index']) - first_year = first_date.year - - # chech if we are in the current year - - if (first_year == yr): - num_of_days = len(segment) - - # start processing - - last = report_graph.node_to_date_eddy(segment[-1], e_overestim) - last_date = datetime.date(1950, 1, 1) \ - + datetime.timedelta(last['date_index']) - last_year = last_date.year - - # calculate the location in the shapefile - first_loc = util_eddies.comp_ishape(handlers[first_year], - first['date_index'], - first['eddy_index']) - last_loc = util_eddies.comp_ishape(handlers[last_year], - last['date_index'], - last['eddy_index']) - - # grab the centers - - first_pos = handlers[first_year]["readers"]["extremum"]\ - .shape(first_loc).points[0] - last_pos = handlers[last_year]["readers"]["extremum"]\ - .shape(last_loc).points[0] - - ##### STORE POSITIONS IN THE VPS ###### - g.vp.pos_first[n] = first_pos # [deg, deg] - g.vp.pos_last[n] = last_pos # [deg, deg] - - # if the segments are longer than the # of days over which to avg - - if (num_of_days > num_of_days_to_avg): - first_radii = 0 # [m] - last_radii = 0 # [m] - - first_rossby = 0 # [] - last_rossby = 0 # [] - - # First 7 days calculation - first_res = calculate_radii_and_rossby(0, num_of_days_to_avg, - 1, segment, e_overestim, - handlers) - - # average and assign radii - first_radii = first_res['radii'] / num_of_days_to_avg - g.vp.first_av_rad[n] = first_radii - - # grab the days modifier - modifier = first_res['days_modifier'] - - if (num_of_days_to_avg - modifier > 0): - # Average and assign the rossbies: - first_rossby = first_res['rossby'] / (num_of_days_to_avg - modifier) - g.vp.first_av_ros[n] = first_rossby - else: - # there is division by zero, average rossby is undefinied - pass - - # Last 7 days calculation - last_res = calculate_radii_and_rossby(len(segment) - 1, - len(segment) - (num_of_days_to_avg + 1), - -1, - segment, e_overestim, - handlers) - - # Average and assign the last radii - last_radii = last_res['radii'] / num_of_days_to_avg - g.vp.last_av_rad[n] = last_radii - - - # grab the days modifier - modifier = last_res['days_modifier'] - - if (num_of_days_to_avg - modifier > 0): - # Average and assign the rossbies: - last_rossby = last_res['rossby'] / (num_of_days_to_avg - modifier) - g.vp.last_av_ros[n] = last_rossby - else: - # there is division by zero, average rossby is undefinied - pass - # else, the number of eddies in a segment is lower than the # of - # days over which to average, the values will be the same except - # for the positions - else: - res = calculate_radii_and_rossby(0, num_of_days, 1, segment, - e_overestim, handlers) - - # grab the days modifier - modifier = res['days_modifier'] - - if (num_of_days - modifier > 0): - # Average and assign the rossbies: - rossby = res['rossby'] / (num_of_days - modifier) - g.vp.first_av_ros[n] = rossby - g.vp.last_av_ros[n] = rossby - else: - # there is division by zero, average rossby is undefinied - pass - - # Average and assign the radii - - radii = res['radii'] / num_of_days - g.vp.first_av_rad[n] = radii - g.vp.last_av_rad[n] = radii - +# iterate on the vertices +for n in g.vertices(): + # Get the segment and the number of days + segment = g.vp.segment[n] + + # calculate the indexes and dates + first = report_graph.node_to_date_eddy(segment[0], e_overestim) + + # get year + first_date = datetime.date(1950, 1, 1) \ + + datetime.timedelta(first['date_index']) + first_year = first_date.year + + num_of_days = len(segment) + + # start processing + + last = report_graph.node_to_date_eddy(segment[-1], e_overestim) + last_date = datetime.date(1950, 1, 1) \ + + datetime.timedelta(last['date_index']) + last_year = last_date.year + + # calculate the location in the shapefile + first_loc = util_eddies.comp_ishape(handlers[first_year], + first['date_index'], + first['eddy_index']) + last_loc = util_eddies.comp_ishape(handlers[last_year], + last['date_index'], + last['eddy_index']) + + # grab the centers + + first_pos = handlers[first_year]["readers"]["extremum"]\ + .shape(first_loc).points[0] + last_pos = handlers[last_year]["readers"]["extremum"]\ + .shape(last_loc).points[0] + + ##### STORE POSITIONS IN THE VPS ###### + g.vp.pos_first[n] = first_pos # [deg, deg] + g.vp.pos_last[n] = last_pos # [deg, deg] + + # if the segments are longer than the # of days over which to avg + + if (num_of_days > num_of_days_to_avg): + first_radii = 0 # [m] + last_radii = 0 # [m] + + first_rossby = 0 # [] + last_rossby = 0 # [] + + # First 7 days calculation + first_res = calculate_radii_and_rossby(0, num_of_days_to_avg, + 1, segment, e_overestim, + handlers) + + # average and assign radii + first_radii = first_res['radii'] / num_of_days_to_avg + g.vp.first_av_rad[n] = first_radii + + # grab the days modifier + modifier = first_res['days_modifier'] + + if (num_of_days_to_avg - modifier > 0): + # Average and assign the rossbies: + first_rossby = first_res['rossby'] / (num_of_days_to_avg - modifier) + g.vp.first_av_ros[n] = first_rossby + else: + # there is division by zero, average rossby is undefinied + pass + + # Last 7 days calculation + last_res = calculate_radii_and_rossby(len(segment) - 1, + len(segment) - (num_of_days_to_avg + 1), + -1, + segment, e_overestim, + handlers) + + # Average and assign the last radii + last_radii = last_res['radii'] / num_of_days_to_avg + g.vp.last_av_rad[n] = last_radii + + + # grab the days modifier + modifier = last_res['days_modifier'] + + if (num_of_days_to_avg - modifier > 0): + # Average and assign the rossbies: + last_rossby = last_res['rossby'] / (num_of_days_to_avg - modifier) + g.vp.last_av_ros[n] = last_rossby + else: + # there is division by zero, average rossby is undefinied + pass + # else, the number of eddies in a segment is lower than the # of + # days over which to average, the values will be the same except + # for the positions + else: + res = calculate_radii_and_rossby(0, num_of_days, 1, segment, + e_overestim, handlers) + + # grab the days modifier + modifier = res['days_modifier'] + + if (num_of_days - modifier > 0): + # Average and assign the rossbies: + rossby = res['rossby'] / (num_of_days - modifier) + g.vp.first_av_ros[n] = rossby + g.vp.last_av_ros[n] = rossby + else: + # there is division by zero, average rossby is undefinied + pass + + # Average and assign the radii + + radii = res['radii'] / num_of_days + g.vp.first_av_rad[n] = radii + g.vp.last_av_rad[n] = radii + ############################### # Calculate the cost function # ###############################