grib2io.utils.rotate

 1#from math import *
 2import math
 3import numpy as np
 4
 5def rotated_grid_transform(lats, lons, lat_sp, lon_sp, option=2):
 6    #lon = grid_in[0]
 7    #lat = grid_in[1]
 8
 9    lons = (lons*math.pi)/180.0 # Convert degrees to radians
10    lats = (lats*math.pi)/180.0
11
12    #SP_lon = SP_coor[0]
13    #SP_lat = SP_coor[1]
14
15    theta = 90.0 + lat_sp # Rotation around y-axis
16    phi = lon_sp # Rotation around z-axis
17
18    theta = (theta*math.pi) / 180.0
19    phi = (phi*math.pi) / 180.0 # Convert degrees to radians
20
21    x = np.cos(lons)*np.cos(lats) # Convert from spherical to cartesian coordinates
22    y = np.sin(lons)*np.cos(lats)
23    z = np.sin(lats)
24
25    if option == 1: # Regular -> Rotated
26
27        x_new = np.cos(theta)*np.cos(phi)*x + np.cos(theta)*np.sin(phi)*y + np.sin(theta)*z
28        y_new = -np.sin(phi)*x + np.cos(phi)*y
29        z_new = -np.sin(theta)*np.cos(phi)*x - np.sin(theta)*np.sin(phi)*y + np.cos(theta)*z
30
31    elif option == 2:  # Rotated -> Regular
32
33        phi = -phi
34        theta = -theta
35
36        x_new = np.cos(theta)*np.cos(phi)*x + np.sin(phi)*y + np.sin(theta)*np.cos(phi)*z
37        y_new = -np.cos(theta)*np.sin(phi)*x + np.cos(phi)*y - np.sin(theta)*np.sin(phi)*z
38        z_new = -np.sin(theta)*x + np.cos(theta)*z
39
40    lons_new = np.arctan2(y_new,x_new) # Convert cartesian back to spherical coordinates
41    lats_new = np.arcsin(z_new)
42
43    lons_new = (lons_new*180.0) / math.pi # Convert radians back to degrees
44    lats_new = (lats_new*180.0) / math.pi
45
46    return lats_new, lons_new
def rotated_grid_transform(lats, lons, lat_sp, lon_sp, option=2):
 6def rotated_grid_transform(lats, lons, lat_sp, lon_sp, option=2):
 7    #lon = grid_in[0]
 8    #lat = grid_in[1]
 9
10    lons = (lons*math.pi)/180.0 # Convert degrees to radians
11    lats = (lats*math.pi)/180.0
12
13    #SP_lon = SP_coor[0]
14    #SP_lat = SP_coor[1]
15
16    theta = 90.0 + lat_sp # Rotation around y-axis
17    phi = lon_sp # Rotation around z-axis
18
19    theta = (theta*math.pi) / 180.0
20    phi = (phi*math.pi) / 180.0 # Convert degrees to radians
21
22    x = np.cos(lons)*np.cos(lats) # Convert from spherical to cartesian coordinates
23    y = np.sin(lons)*np.cos(lats)
24    z = np.sin(lats)
25
26    if option == 1: # Regular -> Rotated
27
28        x_new = np.cos(theta)*np.cos(phi)*x + np.cos(theta)*np.sin(phi)*y + np.sin(theta)*z
29        y_new = -np.sin(phi)*x + np.cos(phi)*y
30        z_new = -np.sin(theta)*np.cos(phi)*x - np.sin(theta)*np.sin(phi)*y + np.cos(theta)*z
31
32    elif option == 2:  # Rotated -> Regular
33
34        phi = -phi
35        theta = -theta
36
37        x_new = np.cos(theta)*np.cos(phi)*x + np.sin(phi)*y + np.sin(theta)*np.cos(phi)*z
38        y_new = -np.cos(theta)*np.sin(phi)*x + np.cos(phi)*y - np.sin(theta)*np.sin(phi)*z
39        z_new = -np.sin(theta)*x + np.cos(theta)*z
40
41    lons_new = np.arctan2(y_new,x_new) # Convert cartesian back to spherical coordinates
42    lats_new = np.arcsin(z_new)
43
44    lons_new = (lons_new*180.0) / math.pi # Convert radians back to degrees
45    lats_new = (lats_new*180.0) / math.pi
46
47    return lats_new, lons_new