Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
toargridding
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
esde
toar-public
toargridding
Commits
1714eba0
Commit
1714eba0
authored
1 year ago
by
Simon Grasse
Browse files
Options
Downloads
Patches
Plain Diff
.
parent
20f9c61d
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!11
Creation of first beta release version
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
toargridding/grids.py
+26
-38
26 additions, 38 deletions
toargridding/grids.py
with
26 additions
and
38 deletions
toargridding/grids.py
+
26
−
38
View file @
1714eba0
...
...
@@ -13,8 +13,9 @@ from toargridding.toar_rest_client import (
)
from
toargridding.metadata
import
(
Variables
,
Variable
,
Coordinates
,
Coordinate
,
get_variable_attributes
,
get_global_attributes
,
)
...
...
@@ -55,19 +56,23 @@ class RegularGrid(GridDefinition):
# TODO make sure only sensible resolutions
self
.
lat
=
Coordinate
.
from_resolution
(
Variabl
es
.
latitude
,
lat_resolution
,
min
=-
90
,
max
=
90
Coordinat
es
.
latitude
,
lat_resolution
,
min
=-
90
,
max
=
90
)
self
.
lon
=
Coordinate
.
from_resolution
(
Variabl
es
.
longitude
,
lon_resolution
,
min
=-
180
,
max
=
180
Coordinat
es
.
longitude
,
lon_resolution
,
min
=-
180
,
max
=
180
)
self
.
spatial_shape
=
(
self
.
lon
.
size
,
self
.
lat
.
size
)
spatial_shape
=
(
self
.
lon
.
size
,
self
.
lat
.
size
)
spatial_size
=
self
.
lon
.
size
*
self
.
lat
.
size
self
.
dims
=
[
Coordinates
.
latitude
.
name
,
Coordinates
.
longitude
.
name
,
Coordinates
.
time
.
name
,
]
self
.
_as_xy_index
=
np
.
dstack
(
np
.
meshgrid
(
range
(
self
.
lat
.
size
),
range
(
self
.
lon
.
size
))
).
reshape
(
-
1
,
2
)
self
.
_as_i_index
=
(
np
.
arange
(
self
.
lon
.
size
*
self
.
lat
.
size
).
reshape
(
self
.
spatial_shape
).
T
)
self
.
_as_i_index
=
np
.
arange
(
spatial_size
).
reshape
(
spatial_shape
).
T
def
as_xarray
(
self
,
timeseries_per_statistic
,
metadata
):
datasets
=
dict
()
...
...
@@ -79,7 +84,6 @@ class RegularGrid(GridDefinition):
datasets
[
statistic
]
=
self
.
create_dataset
(
cell_statistics
,
metadata
)
return
datasets
print
(
cls
)
def
clean_coords
(
self
,
coords
:
pd
.
DataFrame
):
valid_coords
=
coords
.
notna
().
all
(
axis
=
1
)
...
...
@@ -103,13 +107,13 @@ class RegularGrid(GridDefinition):
def
get_cell_statistics
(
self
,
groups
):
stats
=
{
Variables
.
mean
.
name
:
groups
.
mean
(),
Variables
.
std
.
name
:
groups
.
std
(),
Variables
.
n
.
name
:
groups
.
count
(),
Variables
.
mean
:
groups
.
mean
(),
Variables
.
std
:
groups
.
std
(),
Variables
.
n
:
groups
.
count
(),
}
idx
=
stats
[
Variables
.
n
.
name
]
==
0
stats
[
Variables
.
mean
.
name
][
idx
]
=
self
.
fill_value
stats
[
Variables
.
std
.
name
][
idx
]
=
self
.
fill_value
idx
=
stats
[
Variables
.
n
]
==
0
stats
[
Variables
.
mean
][
idx
]
=
self
.
fill_value
stats
[
Variables
.
std
][
idx
]
=
self
.
fill_value
return
stats
...
...
@@ -118,34 +122,18 @@ class RegularGrid(GridDefinition):
time
=
pd
.
DatetimeIndex
(
random_df
.
columns
)
gridded_ds
=
self
.
get_empty_grid
(
time
,
metadata
)
for
cell_statistic
,
aggregated_timeseries
in
cell_statistics
.
items
():
gridded_ds
=
self
.
assign_statistic_to_grid
(
gridded_ds
,
cell_statistic
,
aggregated_timeseries
,
time
)
for
variable
,
aggregated_data
in
cell_statistics
.
items
():
data_array_dict
=
self
.
get_data_array_dict
(
time
,
aggregated_data
,
variable
)
gridded_ds
=
gridded_ds
.
assign
(
data_array_dict
)
return
gridded_ds
def
assign_statistic_to_grid
(
self
,
gridded_ds
:
xr
.
Dataset
,
cell_statistic
,
grouped_timeseries
,
time
):
values
=
self
.
create_gridded_statistics
(
time
,
grouped_timeseries
)
print
(
values
.
shape
)
gridded_ds
=
gridded_ds
.
assign
(
# maybe better all in one ? (memory)
{
cell_statistic
:
(
[
Variables
.
latitude
.
name
,
Variables
.
longitude
.
name
,
Variables
.
time
.
name
,
],
values
,
)
}
)
return
gridded_ds
def
get_data_array_dict
(
self
,
time
,
aggregated_data
,
variable
):
gridded_data
=
self
.
create_gridded_data
(
time
,
aggregated_data
)
gridded_variable
=
Variable
.
from_data
(
gridded_data
,
variable
)
return
{
variable
.
name
:
gridded_variable
.
as_data_array
(
self
.
dims
)}
def
create_gridded_
statistics
(
self
,
time
,
grouped_timeseries
):
def
create_gridded_
data
(
self
,
time
,
grouped_timeseries
):
values
=
np
.
empty
((
self
.
lat
.
size
,
self
.
lon
.
size
,
time
.
size
))
values
[...]
=
self
.
fill_value
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment