-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
very first interim_l3 dataset #121
Open
hgloeckner
wants to merge
8
commits into
Geet-George:main
Choose a base branch
from
hgloeckner:create_interim_l3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21d2827
fix theta and q calculation
hgloeckner 2a205e4
rearrange commands
hgloeckner 9527854
add preparation for interim l3
hgloeckner 0ae196d
interpolation of sondes
hgloeckner a393aa9
prepare l2 for concatenation
hgloeckner 6e09f76
concatenate sondes
hgloeckner 2766381
write l3
hgloeckner 8610e82
remove unnecessary no_launch_remove
hgloeckner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,12 @@ | |
from typing import Any, Optional, List | ||
import os | ||
import subprocess | ||
import warnings | ||
|
||
import numpy as np | ||
import xarray as xr | ||
|
||
|
||
from halodrops.helper import rawreader as rr | ||
import halodrops.helper as hh | ||
|
||
|
@@ -965,11 +967,116 @@ def add_l2_ds(self, l2_dir: str = None): | |
""" | ||
if l2_dir is None: | ||
l2_dir = self.l2_dir | ||
try: | ||
object.__setattr__( | ||
self, "l2_ds", xr.open_dataset(os.path.join(l2_dir, self.l2_filename)) | ||
) | ||
return self | ||
except FileNotFoundError: | ||
return None | ||
|
||
def create_prep_l3(self): | ||
_prep_l3_ds = self.l2_ds.assign_coords( | ||
{"sonde_id": ("sonde_id", [self.l2_ds.sonde_id.values])} | ||
).sortby("time") | ||
object.__setattr__(self, "_prep_l3_ds", _prep_l3_ds) | ||
return self | ||
|
||
def remove_non_mono_incr_alt(self): | ||
""" | ||
This function removes the indices in the | ||
geopotential height ('gpsalt') | ||
""" | ||
_prep_l3_ds = self._prep_l3_ds.load() | ||
gps_alt = _prep_l3_ds.gpsalt | ||
curr_alt = gps_alt.isel(time=0) | ||
for i in range(len(gps_alt)): | ||
if gps_alt[i] > curr_alt: | ||
gps_alt[i] = np.nan | ||
elif ~np.isnan(gps_alt[i]): | ||
curr_alt = gps_alt[i] | ||
_prep_l3_ds["gpsalt"] = gps_alt | ||
|
||
mask = ~np.isnan(gps_alt) | ||
object.__setattr__( | ||
self, "l2_ds", xr.open_dataset(os.path.join(l2_dir, self.l2_filename)) | ||
self, | ||
"_prep_l3_ds", | ||
_prep_l3_ds.sel(time=mask), | ||
) | ||
return self | ||
|
||
def interpolate_alt( | ||
self, | ||
interp_start=-5, | ||
interp_stop=14515, | ||
interp_step=10, | ||
max_gap_fill: int = 50, | ||
method: str = "bin", | ||
): | ||
|
||
interpolation_grid = np.arange(interp_start, interp_stop, interp_step) | ||
|
||
if not (self._prep_l3_ds["gpsalt"].diff(dim="time") < 0).any(): | ||
warnings.warn( | ||
f"your altitude for sonde {self._interim_l3_ds.sonde_id.values} is not sorted." | ||
) | ||
ds = self._prep_l3_ds.swap_dims({"time": "gpsalt"}).load() | ||
|
||
if method == "linear_interpolate": | ||
interp_ds = ds.interp(gpsalt=interpolation_grid) | ||
elif method == "bin": | ||
|
||
interpolation_bins = interpolation_grid.astype("int") | ||
interpolation_label = np.arange( | ||
interp_start + interp_step / 2, | ||
interp_stop - interp_step / 2, | ||
interp_step, | ||
) | ||
interp_ds = ds.groupby_bins( | ||
"gpsalt", | ||
interpolation_bins, | ||
labels=interpolation_label, | ||
).mean(dim="gpsalt") | ||
# somehow coordinates are lost and need to be added again | ||
for coord in ["lat", "lon", "time"]: | ||
interp_ds = interp_ds.assign_coords( | ||
{ | ||
coord: ( | ||
"gpsalt", | ||
ds[coord] | ||
.groupby_bins( | ||
"gpsalt", interpolation_bins, labels=interpolation_label | ||
) | ||
.mean("gpsalt") | ||
.values, | ||
) | ||
} | ||
) | ||
interp_ds = ( | ||
interp_ds.transpose() | ||
.interpolate_na( | ||
dim="gpsalt_bins", max_gap=max_gap_fill, use_coordinate=True | ||
) | ||
.rename({"gpsalt_bins": "gpsalt", "time": "interpolated_time"}) | ||
) | ||
|
||
object.__setattr__(self, "_prep_l3_ds", interp_ds) | ||
|
||
return self | ||
|
||
def prepare_l2_for_gridded(self): | ||
""" | ||
Prepares l2 datasets to be concatenated to gridded. | ||
adds all attributes as variables to avoid conflicts when concatenating because attributes are different | ||
(and not lose information) | ||
|
||
""" | ||
_prep_l3_ds = self._prep_l3_ds | ||
for attr, value in self._prep_l3_ds.attrs.items(): | ||
_prep_l3_ds[attr] = value | ||
|
||
_prep_l3_ds.attrs.clear() | ||
object.__setattr__(self, "_prep_l3_ds", _prep_l3_ds) | ||
return self | ||
|
||
def add_q_and_theta_to_l2_ds(self): | ||
|
@@ -985,14 +1092,65 @@ def add_q_and_theta_to_l2_ds(self): | |
self : object | ||
Returns the sonde object with potential temperature and specific humidity added to the L2 dataset. | ||
""" | ||
if hasattr(self, "_interim_l3_ds"): | ||
ds = self._interim_l3_ds | ||
else: | ||
ds = self.l2_ds | ||
ds = self._prep_l3_ds | ||
|
||
ds = calc_q_from_rh(ds) | ||
ds = calc_theta_from_T(ds) | ||
ds = hh.calc_q_from_rh(ds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a different commit. (that's my bad to not include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes true. I subcontiously changed it at some point ^^' |
||
ds = hh.calc_theta_from_T(ds) | ||
|
||
object.__setattr__(self, "_interim_l3_ds", ds) | ||
|
||
return self | ||
|
||
|
||
@dataclass(order=True) | ||
class Gridded: | ||
sondes: dict | ||
flight_id: str | ||
platform_id: str | ||
|
||
def concat_sondes(self): | ||
""" | ||
function to concatenate all sondes using the combination of all measurement times and launch times | ||
""" | ||
list_of_l2_ds = [sonde._prep_l3_ds for sonde in self.sondes.values()] | ||
combined = xr.combine_by_coords(list_of_l2_ds) | ||
self._interim_l3_ds = combined | ||
return self | ||
|
||
def get_l3_dir(self, l3_dirname: str = None): | ||
if l3_dirname: | ||
self.l3_dir = l3_dirname | ||
elif not self.sondes is None: | ||
self.l3_dir = list(self.sondes.values())[0].l3_dir | ||
else: | ||
raise ValueError("No sondes and no l3 directory given, cannot continue ") | ||
|
||
def get_l3_filename( | ||
self, l3_filename_template: str = None, l3_filename: str = None | ||
): | ||
if l3_filename is None: | ||
if l3_filename_template is None: | ||
l3_filename = hh.l3_filename_template.format( | ||
platform=self.platform_id, | ||
flight_id=self.flight_id, | ||
) | ||
else: | ||
l3_filename = l3_filename_template.format( | ||
platform=self.platform_id, | ||
flight_id=self.flight_id, | ||
) | ||
|
||
self.l3_filename = l3_filename | ||
|
||
return self | ||
|
||
def write_l3(self, l3_dir: str = None): | ||
if l3_dir is None: | ||
l3_dir = self.l3_dir | ||
|
||
if not os.path.exists(l3_dir): | ||
os.makedirs(l3_dir) | ||
|
||
self._interim_l3_ds.to_netcdf(os.path.join(l3_dir, self.l3_filename)) | ||
|
||
return self |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.