COSIMA / cosima-recipes

A cookbook of recipes (i.e., examples) for analysing ocean and sea ice model output
https://cosima-recipes.readthedocs.io
Apache License 2.0
46 stars 66 forks source link

Inefficient distance along contour calculation in `Cross-contour_transport.ipynb` #403

Closed adele-morrison closed 3 months ago

adele-morrison commented 4 months ago

The code that calculates the distance along the contour in the Cross-contour_transport.ipynb script is very slow (~10 minutes). Probably we could come up with a better way of doing this. Maybe use the lat and lon along the contour instead to compute the shortest distance between each set of coordinates on the contour?

anton-seaice commented 4 months ago

I suspect this for loop (currently cell 38) is the problem :

for count in range(1, num_points):
    if skip == True:
        skip = False
        continue
    if count in y_indices:
        if count + 1 in y_indices:
            # note dxu and dyt do not vary in x:
            jj = np.where(mask_y_transport_numbered==count)[0]
            distance_along_contour[count-1] = (dxu[jj])[0]
        else:
            jj0 = np.where(mask_y_transport_numbered==count)[0]
            jj1 = np.where(mask_x_transport_numbered==count+1)[0]
            half_diagonal_distance = 0.5 * np.sqrt((dxu[jj0])[0]**2 + (dyt[jj1])[0]**2)
            distance_along_contour[count-1] = half_diagonal_distance
            distance_along_contour[count] = half_diagonal_distance
            # skip to next count:
            skip = True
            ...

which leads to this line (and the other similar lines) :

np.where(mask_y_transport_numbered==count)[0]

which hopefully can be done as a whole array rather than one at a time ...

which leads back to fixing https://github.com/COSIMA/cosima-recipes/issues/179 first and then flowing the changes through the rest of the workbook

adele-morrison commented 4 months ago

I completely replaced that loop in this PR: https://github.com/COSIMA/cosima-recipes/pull/406.