@@ -49,126 +49,17 @@ def add_coordinates(
49
49
50
50
"""
51
51
52
- from scipy .interpolate import interp1d , interpn
53
-
54
- logging .debug ("start adding coordinates from cube" )
55
-
56
- # pull time as datetime object and timestr from input data and add it to DataFrame:
57
- features ["time" ] = None
58
- features ["timestr" ] = None
59
-
60
- logging .debug ("adding time coordinate" )
61
-
62
- time_in = variable_cube .coord ("time" )
63
- time_in_datetime = time_in .units .num2date (time_in .points )
64
-
65
- features ["time" ] = time_in_datetime [features ["frame" ]]
66
- features ["timestr" ] = [
67
- x .strftime ("%Y-%m-%d %H:%M:%S" ) for x in time_in_datetime [features ["frame" ]]
68
- ]
69
-
70
- # Get list of all coordinates in input cube except for time (already treated):
71
- coord_names = [coord .name () for coord in variable_cube .coords ()]
72
- coord_names .remove ("time" )
73
-
74
- logging .debug ("time coordinate added" )
75
-
76
- # chose right dimension for horizontal axis based on time dimension:
77
- ndim_time = variable_cube .coord_dims ("time" )[0 ]
78
- if ndim_time == 0 :
79
- hdim_1 = 1
80
- hdim_2 = 2
81
- elif ndim_time == 1 :
82
- hdim_1 = 0
83
- hdim_2 = 2
84
- elif ndim_time == 2 :
85
- hdim_1 = 0
86
- hdim_2 = 1
87
-
88
- # create vectors to use to interpolate from pixels to coordinates
89
- dimvec_1 = np .arange (variable_cube .shape [hdim_1 ])
90
- dimvec_2 = np .arange (variable_cube .shape [hdim_2 ])
91
-
92
- # loop over coordinates in input data:
93
- for coord in coord_names :
94
- logging .debug ("adding coord: " + coord )
95
- # interpolate 2D coordinates:
96
- if variable_cube .coord (coord ).ndim == 1 :
97
- if variable_cube .coord_dims (coord ) == (hdim_1 ,):
98
- f = interp1d (
99
- dimvec_1 ,
100
- variable_cube .coord (coord ).points ,
101
- fill_value = "extrapolate" ,
102
- )
103
- coordinate_points = f (features ["hdim_1" ])
104
-
105
- if variable_cube .coord_dims (coord ) == (hdim_2 ,):
106
- f = interp1d (
107
- dimvec_2 ,
108
- variable_cube .coord (coord ).points ,
109
- fill_value = "extrapolate" ,
110
- )
111
- coordinate_points = f (features ["hdim_2" ])
112
-
113
- # interpolate 2D coordinates:
114
- elif variable_cube .coord (coord ).ndim == 2 :
115
- if variable_cube .coord_dims (coord ) == (hdim_1 , hdim_2 ):
116
- points = (dimvec_1 , dimvec_2 )
117
- values = variable_cube .coord (coord ).points
118
- xi = np .column_stack ((features ["hdim_1" ], features ["hdim_2" ]))
119
- coordinate_points = interpn (points , values , xi , bounds_error = False )
120
-
121
- if variable_cube .coord_dims (coord ) == (hdim_2 , hdim_1 ):
122
- points = (dimvec_2 , dimvec_1 )
123
- values = variable_cube .coord (coord ).points
124
- xi = np .column_stack ((features ["hdim_2" ], features ["hdim_1" ]))
125
- coordinate_points = interpn (points , values , xi , bounds_error = False )
126
-
127
- # interpolate 3D coordinates:
128
- # mainly workaround for wrf latitude and longitude (to be fixed in future)
129
-
130
- elif variable_cube .coord (coord ).ndim == 3 :
131
- if variable_cube .coord_dims (coord ) == (ndim_time , hdim_1 , hdim_2 ):
132
- points = (dimvec_1 , dimvec_2 )
133
- values = variable_cube [0 , :, :].coord (coord ).points
134
- xi = np .column_stack ((features ["hdim_1" ], features ["hdim_2" ]))
135
- coordinate_points = interpn (points , values , xi , bounds_error = False )
136
-
137
- if variable_cube .coord_dims (coord ) == (ndim_time , hdim_2 , hdim_1 ):
138
- points = (dimvec_2 , dimvec_1 )
139
- values = variable_cube [0 , :, :].coord (coord ).points
140
- xi = np .column_stack ((features ["hdim_2" ], features ["hdim_1" ]))
141
- coordinate_points = interpn (points , values , xi , bounds_error = False )
142
-
143
- if variable_cube .coord_dims (coord ) == (hdim_1 , ndim_time , hdim_2 ):
144
- points = (dimvec_1 , dimvec_2 )
145
- values = variable_cube [:, 0 , :].coord (coord ).points
146
- xi = np .column_stack ((features ["hdim_1" ], features ["hdim_2" ]))
147
- coordinate_points = interpn (points , values , xi , bounds_error = False )
148
-
149
- if variable_cube .coord_dims (coord ) == (hdim_1 , hdim_2 , ndim_time ):
150
- points = (dimvec_1 , dimvec_2 )
151
- values = variable_cube [:, :, 0 ].coord (coord ).points
152
- xi = np .column_stack ((features ["hdim_1" ], features ["hdim_2" ]))
153
- coordinate_points = interpn (points , values , xi , bounds_error = False )
154
-
155
- if variable_cube .coord_dims (coord ) == (hdim_2 , ndim_time , hdim_1 ):
156
- points = (dimvec_2 , dimvec_1 )
157
- values = variable_cube [:, 0 , :].coord (coord ).points
158
- xi = np .column_stack ((features ["hdim_2" ], features ["hdim_1" ]))
159
- coordinate_points = interpn (points , values , xi , bounds_error = False )
160
-
161
- if variable_cube .coord_dims (coord ) == (hdim_2 , hdim_1 , ndim_time ):
162
- points = (dimvec_2 , dimvec_1 )
163
- values = variable_cube [:, :, 0 ].coord (coord ).points
164
- xi = np .column_stack ((features ["hdim_2" ], features ["hdim_1" ]))
165
- coordinate_points = interpn (points , values , xi , bounds_error = False )
166
-
167
- # write resulting array or list into DataFrame:
168
- features [coord ] = coordinate_points
169
-
170
- logging .debug ("added coord: " + coord )
171
- return features
52
+ if isinstance (variable_cube , iris .cube .Cube ):
53
+ return internal_utils .iris_utils .add_coordinates (features , variable_cube )
54
+ if isinstance (variable_cube , xr .DataArray ):
55
+ return internal_utils .xr_utils .add_coordinates_to_features (
56
+ features ,
57
+ variable_cube ,
58
+ preserve_iris_datetime_types = preserve_iris_datetime_types ,
59
+ )
60
+ raise ValueError (
61
+ "add_coordinates only supports xarray.DataArray and iris.cube.Cube"
62
+ )
172
63
173
64
174
65
def add_coordinates_3D (
0 commit comments