From 2e60b5f39005ae2350198b83552d9ee934fbda5d Mon Sep 17 00:00:00 2001 From: Souza-Junior Date: Sat, 6 Apr 2024 18:09:43 -0300 Subject: [PATCH 1/5] Account extra coordinates in the function grid_to_table Add lines to search for extra coordinates and append the information into the list of coordinates and coordinates_name --- verde/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/verde/utils.py b/verde/utils.py index 2761aea04..c082d29ee 100644 --- a/verde/utils.py +++ b/verde/utils.py @@ -637,6 +637,12 @@ def grid_to_table(grid): # Need to flip the coordinates because the names are in northing and # easting order coordinates = [i.ravel() for i in np.meshgrid(east, north)][::-1] + # Identify and add extra coordinates + all_coordinate_names = list(grid.coords.keys()) + for coord in all_coordinate_names: + if coord not in coordinate_names: + coordinates.append(grid[coord].values.ravel()) + coordinate_names.append(coord) data_dict = dict(zip(coordinate_names, coordinates)) data_dict.update(dict(zip(data_names, data_arrays))) return pd.DataFrame(data_dict) From 633c73cbc3fe064f7f151318837b182b4099b0f2 Mon Sep 17 00:00:00 2001 From: Souza-Junior Date: Sat, 6 Apr 2024 19:39:58 -0300 Subject: [PATCH 2/5] Solve PR issues --- verde/utils.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/verde/utils.py b/verde/utils.py index c082d29ee..3496ec75b 100644 --- a/verde/utils.py +++ b/verde/utils.py @@ -638,11 +638,10 @@ def grid_to_table(grid): # easting order coordinates = [i.ravel() for i in np.meshgrid(east, north)][::-1] # Identify and add extra coordinates - all_coordinate_names = list(grid.coords.keys()) - for coord in all_coordinate_names: - if coord not in coordinate_names: - coordinates.append(grid[coord].values.ravel()) - coordinate_names.append(coord) + extra = [coord for coord in grid.coords.keys() if coord not in coordinate_names] + for coord in extra: + coordinates.append(grid[coord].values.ravel()) + coordinate_names.append(coord) data_dict = dict(zip(coordinate_names, coordinates)) data_dict.update(dict(zip(data_names, data_arrays))) return pd.DataFrame(data_dict) From bb3d7625398c1420d0cfc260e9a606a868c07042 Mon Sep 17 00:00:00 2001 From: Souza-Junior Date: Mon, 8 Apr 2024 14:29:55 -0300 Subject: [PATCH 3/5] Add test for grid with extra coordinates --- verde/utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/verde/utils.py b/verde/utils.py index 3496ec75b..4aaa3db25 100644 --- a/verde/utils.py +++ b/verde/utils.py @@ -620,7 +620,18 @@ def grid_to_table(grid): [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] >>> print(table.wind_speed.values) [20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39] - + >>> # Non-dimensional coordinates are also handled + >>> temperature = xr.DataArray( + ... np.arange(20).reshape((4, 5)), + ... coords=(np.arange(4), np.arange(5, 10)), + ... dims=['northing', 'easting'] + ... ) + >>> temperature = temperature.assign_coords(upward=(("northing", "easting"), np.arange(20).reshape((4, 5)))) + >>> table = grid_to_table(temperature) + >>> list(sorted(table.columns)) + ['easting', 'northing', 'scalars', 'upward'] + >>> print(table.upward.values) + [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] """ if hasattr(grid, "data_vars"): # It's a Dataset From fddc70b8f6cbcfa509815d9baf6962e731ae354c Mon Sep 17 00:00:00 2001 From: Leonardo Uieda Date: Thu, 11 Apr 2024 09:38:45 -0300 Subject: [PATCH 4/5] Fix documentation line too long --- verde/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/verde/utils.py b/verde/utils.py index 4aaa3db25..1e5cc668f 100644 --- a/verde/utils.py +++ b/verde/utils.py @@ -626,7 +626,12 @@ def grid_to_table(grid): ... coords=(np.arange(4), np.arange(5, 10)), ... dims=['northing', 'easting'] ... ) - >>> temperature = temperature.assign_coords(upward=(("northing", "easting"), np.arange(20).reshape((4, 5)))) + >>> temperature = temperature.assign_coords( + ... upward=( + ... ("northing", "easting"), + ... np.arange(20).reshape((4, 5)) + ... ) + ... ) >>> table = grid_to_table(temperature) >>> list(sorted(table.columns)) ['easting', 'northing', 'scalars', 'upward'] From 52ef281d393f320c2e94726d4914be36573214c6 Mon Sep 17 00:00:00 2001 From: Leonardo Uieda Date: Thu, 11 Apr 2024 09:41:28 -0300 Subject: [PATCH 5/5] Fix formatting --- verde/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/verde/utils.py b/verde/utils.py index 1e5cc668f..a12748d0d 100644 --- a/verde/utils.py +++ b/verde/utils.py @@ -628,7 +628,7 @@ def grid_to_table(grid): ... ) >>> temperature = temperature.assign_coords( ... upward=( - ... ("northing", "easting"), + ... ("northing", "easting"), ... np.arange(20).reshape((4, 5)) ... ) ... )