grib2io.utils.rotated_grid

Tools for working with Rotated Lat/Lon Grids.

 1"""
 2Tools for working with Rotated Lat/Lon Grids.
 3"""
 4
 5import math
 6import numpy as np
 7
 8def rotated_grid_transform(lats, lons, lat_sp, lon_sp, option=2):
 9    """
10    Transform latitude and longitude from regular to rotated and vice-versa.
11
12    Adapted from: https://www.mathworks.com/matlabcentral/fileexchange/43435-rotated-grid-transform
13
14    Parameters
15    ----------
16
17    **`lats : array_like`**:
18
19    Input latitudes in degrees.
20    
21    **`lats : array_like`**:
22
23    Input longitudes in degrees.
24
25    **`lat_sp : float`**:
26
27    Latitude of the Southern Pole.
28
29    **`lon_sp : float`**:
30
31    Longitude of the Southern Pole.
32
33    **`option : int`**:
34
35    Transform regular to rotated (1) or rotated to regular (2) [DEFAULT].
36
37    Returns
38    -------
39    
40    Sequence of transformed latitude, longitude in degrees.
41    """
42
43    # Convert lats and lons degress to radians
44    lons = (lons*math.pi) / 180.0
45    lats = (lats*math.pi) / 180.0
46
47    theta = 90.0 + lat_sp # Rotation around y-axis
48    phi = lon_sp # Rotation around z-axis
49
50    # Convert to radians
51    theta = (theta*math.pi) / 180.0
52    phi = (phi*math.pi) / 180.0
53
54    # Convert from spherical to cartesian coordinates
55    x = np.cos(lons)*np.cos(lats)
56    y = np.sin(lons)*np.cos(lats)
57    z = np.sin(lats)
58
59    if option == 1: # Regular -> Rotated
60
61        x_new = np.cos(theta)*np.cos(phi)*x + np.cos(theta)*np.sin(phi)*y + np.sin(theta)*z
62        y_new = -np.sin(phi)*x + np.cos(phi)*y
63        z_new = -np.sin(theta)*np.cos(phi)*x - np.sin(theta)*np.sin(phi)*y + np.cos(theta)*z
64
65    elif option == 2:  # Rotated -> Regular
66
67        phi = -phi
68        theta = -theta
69
70        x_new = np.cos(theta)*np.cos(phi)*x + np.sin(phi)*y + np.sin(theta)*np.cos(phi)*z
71        y_new = -np.cos(theta)*np.sin(phi)*x + np.cos(phi)*y - np.sin(theta)*np.sin(phi)*z
72        z_new = -np.sin(theta)*x + np.cos(theta)*z
73
74    # Convert cartesian back to spherical coordinates
75    lons_new = np.arctan2(y_new,x_new)
76    lats_new = np.arcsin(z_new)
77
78    # Convert radians back to degrees
79    lons_new = (lons_new*180.0) / math.pi # Convert radians back to degrees
80    lats_new = (lats_new*180.0) / math.pi
81
82    return lats_new, lons_new
def rotated_grid_transform(lats, lons, lat_sp, lon_sp, option=2):
 9def rotated_grid_transform(lats, lons, lat_sp, lon_sp, option=2):
10    """
11    Transform latitude and longitude from regular to rotated and vice-versa.
12
13    Adapted from: https://www.mathworks.com/matlabcentral/fileexchange/43435-rotated-grid-transform
14
15    Parameters
16    ----------
17
18    **`lats : array_like`**:
19
20    Input latitudes in degrees.
21    
22    **`lats : array_like`**:
23
24    Input longitudes in degrees.
25
26    **`lat_sp : float`**:
27
28    Latitude of the Southern Pole.
29
30    **`lon_sp : float`**:
31
32    Longitude of the Southern Pole.
33
34    **`option : int`**:
35
36    Transform regular to rotated (1) or rotated to regular (2) [DEFAULT].
37
38    Returns
39    -------
40    
41    Sequence of transformed latitude, longitude in degrees.
42    """
43
44    # Convert lats and lons degress to radians
45    lons = (lons*math.pi) / 180.0
46    lats = (lats*math.pi) / 180.0
47
48    theta = 90.0 + lat_sp # Rotation around y-axis
49    phi = lon_sp # Rotation around z-axis
50
51    # Convert to radians
52    theta = (theta*math.pi) / 180.0
53    phi = (phi*math.pi) / 180.0
54
55    # Convert from spherical to cartesian coordinates
56    x = np.cos(lons)*np.cos(lats)
57    y = np.sin(lons)*np.cos(lats)
58    z = np.sin(lats)
59
60    if option == 1: # Regular -> Rotated
61
62        x_new = np.cos(theta)*np.cos(phi)*x + np.cos(theta)*np.sin(phi)*y + np.sin(theta)*z
63        y_new = -np.sin(phi)*x + np.cos(phi)*y
64        z_new = -np.sin(theta)*np.cos(phi)*x - np.sin(theta)*np.sin(phi)*y + np.cos(theta)*z
65
66    elif option == 2:  # Rotated -> Regular
67
68        phi = -phi
69        theta = -theta
70
71        x_new = np.cos(theta)*np.cos(phi)*x + np.sin(phi)*y + np.sin(theta)*np.cos(phi)*z
72        y_new = -np.cos(theta)*np.sin(phi)*x + np.cos(phi)*y - np.sin(theta)*np.sin(phi)*z
73        z_new = -np.sin(theta)*x + np.cos(theta)*z
74
75    # Convert cartesian back to spherical coordinates
76    lons_new = np.arctan2(y_new,x_new)
77    lats_new = np.arcsin(z_new)
78
79    # Convert radians back to degrees
80    lons_new = (lons_new*180.0) / math.pi # Convert radians back to degrees
81    lats_new = (lats_new*180.0) / math.pi
82
83    return lats_new, lons_new

Transform latitude and longitude from regular to rotated and vice-versa.

Adapted from: https://www.mathworks.com/matlabcentral/fileexchange/43435-rotated-grid-transform

Parameters

lats : array_like:

Input latitudes in degrees.

lats : array_like:

Input longitudes in degrees.

lat_sp : float:

Latitude of the Southern Pole.

lon_sp : float:

Longitude of the Southern Pole.

option : int:

Transform regular to rotated (1) or rotated to regular (2) [DEFAULT].

Returns

Sequence of transformed latitude, longitude in degrees.