Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DART-WRF-delete_byApril
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
DataAssimilation
DART-WRF-delete_byApril
Commits
3f5f5767
Commit
3f5f5767
authored
2 years ago
by
lkugler
Browse files
Options
Downloads
Patches
Plain Diff
method to get model grid indices
parent
e5360c8f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
dartwrf/obs/obsseq.py
+76
-8
76 additions, 8 deletions
dartwrf/obs/obsseq.py
with
76 additions
and
8 deletions
dartwrf/obs/obsseq.py
+
76
−
8
View file @
3f5f5767
...
@@ -141,6 +141,61 @@ class ObsRecord(pd.DataFrame):
...
@@ -141,6 +141,61 @@ class ObsRecord(pd.DataFrame):
assert
np
.
allclose
(
Hx
.
mean
(
axis
=
1
),
self
[
what
+
'
ensemble mean
'
])
assert
np
.
allclose
(
Hx
.
mean
(
axis
=
1
),
self
[
what
+
'
ensemble mean
'
])
return
Hx
.
values
return
Hx
.
values
def
get_model_grid_indices
(
self
,
wrf_file_with_grid
):
"""
Retrieve the grid indices closest to the observations
Note:
Only the horizontal grid is considered
Args:
wrf_file_with_grid (str): path to wrf file with grid information
Returns:
pd.DataFrame (n_obs, 2) columns: i, j
"""
from
scipy
import
spatial
import
xarray
as
xr
def
find_index_from_coords_tree
(
tree
,
len_latitudes
,
lat
=
45.
,
lon
=
0.
):
"""
Find Lat & Lon indices in array
to find the state space values nearest to the observation
Args:
len_latitudes (int) : usually xlat.shape[0]
actually this could also have to be len of longitudes (i dont know!)
but it works if len(xlon)==len(xlat)
Returns:
ilat, ilon (int)
"""
dd
,
ii
=
tree
.
query
([[
lat
,
lon
],])
ilat
=
int
(
ii
/
len_latitudes
)
ilon
=
int
(
ii
%
len_latitudes
)
return
ilat
,
ilon
# load coordinates of wrf grid
grid
=
xr
.
open_dataset
(
wrf_file_with_grid
)
xlat
=
grid
.
XLAT_M
.
values
.
squeeze
()
xlon
=
grid
.
XLONG_M
.
values
.
squeeze
()
# build search tree
tree
=
spatial
.
KDTree
(
np
.
c_
[
xlat
.
ravel
(),
xlon
.
ravel
()])
# get lat lon of observations
lon_lat
=
self
.
get_lon_lat
()
ilat_ilon
=
np
.
empty
((
len
(
lon_lat
),
2
),
np
.
int32
)
# find indices of observations in wrf grid
for
i
,
row
in
lon_lat
.
iterrows
():
ilat_ilon
[
i
,:]
=
find_index_from_coords_tree
(
tree
,
xlat
.
shape
[
0
],
row
.
lat
,
row
.
lon
)
return
pd
.
DataFrame
(
index
=
self
.
index
,
data
=
dict
(
wrf_i
=
ilat_ilon
[:,
0
],
wrf_j
=
ilat_ilon
[:,
1
]))
def
get_lon_lat
(
self
):
def
get_lon_lat
(
self
):
"""
Retrieve longitude and latitude of observations
"""
Retrieve longitude and latitude of observations
...
@@ -190,21 +245,26 @@ class ObsRecord(pd.DataFrame):
...
@@ -190,21 +245,26 @@ class ObsRecord(pd.DataFrame):
return
nlayers
return
nlayers
def
superob
(
self
,
window_km
):
def
superob
(
self
,
window_km
):
"""
Select subset, average, overwrite existing obs with average
"""
Create super-observations (averaged observations)
TODO: allow different obs types (KIND)
TODO: loc3d overwrite with mean
Metadata is copied from the first obs in a superob-box
Note:
Note:
This routine discards observations (round off)
This routine discards observations (round off)
e.g. 31 obs with 5 obs-window => obs #31 is not processed
e.g. 31 obs with 5 obs-window => obs #31 is not processed.
Metadata is copied from the first observation in a superob-box
The location (loc3d) of new observation is taken from the center observation
TODO: allow different obs types (KIND)
Args:
Args:
window_km (numeric): horizontal window edge length
window_km (numeric): horizontal window edge length
includes obs on edge
includes obs on edge
25x25 km with 5 km obs density
25x25 km with 5 km obs density
= average 5 x 5 observations
= average 5 x 5 observations
Returns:
ObsRecord
"""
"""
def
calc_deg_from_km
(
distance_km
,
center_lat
):
def
calc_deg_from_km
(
distance_km
,
center_lat
):
"""
Approximately calculate distance in degrees from meters
"""
Approximately calculate distance in degrees from meters
...
@@ -265,8 +325,16 @@ class ObsRecord(pd.DataFrame):
...
@@ -265,8 +325,16 @@ class ObsRecord(pd.DataFrame):
boxes
=
[]
boxes
=
[]
for
i
in
range
(
0
,
nx
+
1
-
win_obs
,
win_obs
):
for
i
in
range
(
0
,
nx
+
1
-
win_obs
,
win_obs
):
# i is the number of observations in x direction
# but in steps of "number of observations in superob window"
# i.e. i = 0, win_obs, 2*win_obs, 3*win_obs, ...
for
j
in
range
(
0
,
nx
+
1
-
win_obs
,
win_obs
):
for
j
in
range
(
0
,
nx
+
1
-
win_obs
,
win_obs
):
# same as i but in y direction
for
k
in
range
(
0
,
nlayers
):
for
k
in
range
(
0
,
nlayers
):
# k is the index of the vertical layer
if
debug
:
print
(
i
,
j
,
k
)
if
debug
:
print
(
i
,
j
,
k
)
# find indices of observations within superob window
# find indices of observations within superob window
...
@@ -301,7 +369,7 @@ class ObsRecord(pd.DataFrame):
...
@@ -301,7 +369,7 @@ class ObsRecord(pd.DataFrame):
# average spread and other values
# average spread and other values
for
key
in
obs_box
:
for
key
in
obs_box
:
if
key
in
[
'
loc3d
'
,
'
kind
'
,
'
metadata
'
,
'
time
'
]:
if
key
in
[
'
loc3d
'
,
'
kind
'
,
'
metadata
'
,
'
time
'
]:
pass
pass
# these parameters are not averaged
elif
'
spread
'
in
key
:
elif
'
spread
'
in
key
:
# stdev of mean of values = sqrt(mean of variances)
# stdev of mean of values = sqrt(mean of variances)
obs_mean
.
at
[
key
]
=
np
.
sqrt
((
obs_box
[
key
]
**
2
).
mean
())
obs_mean
.
at
[
key
]
=
np
.
sqrt
((
obs_box
[
key
]
**
2
).
mean
())
...
...
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