-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/datav5_CrISPCA #77
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
Draft
ADCollard
wants to merge
16
commits into
feature/data_v6
Choose a base branch
from
feature/data_v5_CrISPCA
base: feature/data_v6
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.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ebc604b
Merge branch 'main' into feature/data_v5
rmclaren 55675d1
linter fixes
rmclaren 94681d9
linter fixes
rmclaren 61a1532
added some doc strings
rmclaren 858d9d7
Merge branch 'main' into feature/data_v5
rmclaren 78cc5c1
Merge branch 'main' into feature/data_v5
rmclaren 15a01ea
Branch to add CrIS PCAs
ADCollard 314d3c7
Latest updates
ADCollard 5d928b6
pycodestyle fixes
ADCollard 1d77ee0
merged data_v6
rmclaren 11ac4c2
Merge branch 'feature/data_v5_CrISPCA' of https://github.com/NOAA-EMC…
rmclaren 2d644c2
Version that fails in a more meaningful way
ADCollard cea798c
Add faulthandler
ADCollard 2b62989
Tidy up
ADCollard c5d6818
pycodestyle fixes
ADCollard 8d618f3
Merge branch 'feature/data_v6' into feature/data_v5_CrISPCA
rmclaren 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| import numpy as np | ||
| import xarray as xr | ||
| import bufr | ||
| import yaml | ||
| import faulthandler | ||
|
|
||
| from bufr.obs_builder import ObsBuilder, add_main_functions, map_path | ||
|
|
||
| faulthandler.enable() | ||
| # Encoder YAML (BUFR schema) – separate from any mapping YAML | ||
| ENCODER_YAML = map_path("cris_pca.yaml") | ||
|
|
||
|
|
||
| class CrisPcaObsBuilder(ObsBuilder): | ||
| """ | ||
| CrIS PCA netCDF reader: | ||
|
|
||
| * DOES NOT use an ObsBuilder mapping YAML | ||
| * DOES use a BUFR encoder YAML (cris_pca.yaml) | ||
| * Flattens atrack/xtrack/fov -> location | ||
| * Fills a DataContainer matching encoder variable names | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| print("\n*** CrisPcaObsBuilder CONSTRUCTOR ***") | ||
| print(" ENCODER_YAML =", ENCODER_YAML) | ||
|
|
||
| # --- Load YAML FIRST (before calling super) --- | ||
| with open(ENCODER_YAML, "r") as f: | ||
| full_yaml = yaml.safe_load(f) | ||
|
|
||
| self._encoder_yaml = full_yaml | ||
|
|
||
| # Build dimension map | ||
| # enc = full_yaml.get("encoder", {}) | ||
| dim_path_map = {} | ||
| for dim in full_yaml.get("dimensions", []): | ||
| n = dim["name"] | ||
| p = dim["path"] | ||
| dim_path_map[n] = p | ||
|
|
||
| self._dim_path_map = dim_path_map | ||
|
|
||
| print(" DIM PATH MAP:", self._dim_path_map) | ||
|
|
||
| # NOW call parent (which calls _make_description) | ||
| super().__init__(None, log_name=os.path.basename(__file__)) | ||
|
|
||
| # ----------------------------------------------------- | ||
| # 1) Return a Description using the encoder YAML file | ||
| # ----------------------------------------------------- | ||
| def _make_description(self): | ||
| print("*** _make_description(): using ENCODER_YAML ***") | ||
| return bufr.encoders.Description(ENCODER_YAML) | ||
|
|
||
| # ----------------------------------------------------- | ||
| def load_input(self, filename): | ||
| print(f"*** load_input() CALLED: {filename}") | ||
| ds = xr.open_dataset(filename, decode_times=False) | ||
| print(" dims:", ds.sizes) | ||
| return ds | ||
|
|
||
| # ----------------------------------------------------- | ||
| def preprocess_dataset(self, ds): | ||
| print("*** preprocess_dataset() CALLED ***") | ||
|
|
||
| required = ["atrack", "xtrack", "fov"] | ||
| for d in required: | ||
| if d not in ds.sizes: | ||
| raise RuntimeError(f"Missing dimension {d}") | ||
|
|
||
| na = ds.sizes["atrack"] | ||
| nx = ds.sizes["xtrack"] | ||
| nf = ds.sizes["fov"] | ||
| nlocs = na * nx * nf | ||
|
|
||
| print(f" atrack={na}, xtrack={nx}, fov={nf} -> nlocs={nlocs}") | ||
|
|
||
| # Build indices | ||
| a, x, f = xr.broadcast( | ||
| xr.DataArray(np.arange(na), dims="atrack"), | ||
| xr.DataArray(np.arange(nx), dims="xtrack"), | ||
| xr.DataArray(np.arange(nf), dims="fov"), | ||
| ) | ||
|
|
||
| xtrack = x.values.ravel() | ||
| fov = f.values.ravel() | ||
|
|
||
| scan_pos = 9 * xtrack + fov | ||
|
|
||
| out = xr.Dataset() | ||
| out = out.assign_coords(location=np.arange(nlocs)) | ||
|
|
||
| out["scan_position"] = xr.DataArray(scan_pos, dims=("location",)) | ||
|
|
||
| # Flatten lat/lon into encoder variable names | ||
| for v_in, v_out in [("lat", "latitude"), ("lon", "longitude")]: | ||
| if v_in in ds: | ||
| print(f" flattening {v_in} -> {v_out}") | ||
| out[v_out] = xr.DataArray( | ||
| ds[v_in].values.reshape(nlocs), | ||
| dims=("location",) | ||
| ) | ||
|
|
||
| # Time | ||
| if "obs_time_tai93" in ds: | ||
| print(" converting obs_time_tai93 -> UNIX seconds") | ||
|
|
||
| time3d = xr.broadcast(ds["obs_time_tai93"], ds["lat"])[0] | ||
| time_tai93 = time3d.values.reshape(nlocs) | ||
|
|
||
| TAI93_EPOCH = np.datetime64("1993-01-01T00:00:00") | ||
| UNIX_EPOCH = np.datetime64("1970-01-01T00:00:00") | ||
|
|
||
| offset = (TAI93_EPOCH - UNIX_EPOCH) / np.timedelta64(1, "s") | ||
| time_unix = time_tai93 + offset | ||
|
|
||
| out["time"] = xr.DataArray(time_unix, dims=("location",)) | ||
|
|
||
| # Global PC scores | ||
| if "global_pc_score" in ds: | ||
| npc = ds.sizes["npc_global"] | ||
| print(f" flattening global_pc_score to (location, {npc})") | ||
| out["global_pc_score"] = xr.DataArray( | ||
| ds["global_pc_score"].values.reshape(nlocs, npc), | ||
| dims=("location", "npc_global") | ||
| ) | ||
|
|
||
| print("*** preprocess complete, vars:", list(out.variables)) | ||
| return out | ||
|
|
||
| # ----------------------------------------------------- | ||
| # 2) Build a DataContainer from the flattened Dataset | ||
| # ----------------------------------------------------- | ||
| # ----------------------------------------------------- | ||
| # 2) Build a DataContainer from the flattened Dataset | ||
| # ----------------------------------------------------- | ||
|
|
||
| def _dims_for_var(self, varname, dims): | ||
| """ | ||
| Map xarray dimension names (e.g. ('location', 'npc_global')) | ||
| to BUFR query strings using the 'dimensions' section in cris_pca.yaml. | ||
| """ | ||
| dim_paths = [] | ||
| for d in dims: | ||
| if d not in self._dim_path_map: | ||
| raise RuntimeError( | ||
| f"_dims_for_var: no mapping for dimension '{d}' " | ||
| f"in encoder YAML; known: " | ||
| f"{list(self._dim_path_map.keys())}" | ||
| ) | ||
| dim_paths.append(self._dim_path_map[d]) | ||
|
|
||
| print(f" _dims_for_var({varname}, {dims}) -> {dim_paths}") | ||
| return dim_paths | ||
|
|
||
| def make_obs(self, comm, input_path): | ||
| print("***** Entering make_obs *****") | ||
| ds = self.load_input(input_path) | ||
| ds = self.preprocess_dataset(ds) | ||
|
|
||
| container = bufr.DataContainer() | ||
|
|
||
| # Load YAML once more (or reuse self._encoder_yaml) | ||
| enc = self._encoder_yaml["encoder"] | ||
| variables = enc["variables"] | ||
|
|
||
| print('VARIABLES=', variables) | ||
|
|
||
| for v in variables: | ||
| name = v["name"] | ||
| source = v["source"] | ||
|
|
||
| if source not in ds: | ||
| print(f"WARNING: source '{source}' not in dataset, skipping") | ||
| continue | ||
|
|
||
| xr_dims = ds[source].dims | ||
| dim_paths = self._dims_for_var(name, xr_dims) | ||
|
|
||
| print(f"Adding {name} from {source} with dim_paths {dim_paths}") | ||
| print(" shape =", ds[source].values.shape) | ||
|
|
||
| container.add( | ||
| name, | ||
| ds[source].values, | ||
| dim_paths | ||
| ) | ||
|
|
||
| return container | ||
|
|
||
|
|
||
| add_main_functions(CrisPcaObsBuilder) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| dimensions: | ||
| - name: location | ||
| source: "location" | ||
| path: "*" | ||
|
|
||
| - name: npc_global | ||
| source: npc_global | ||
| path: "*/NPCGLOBAL" | ||
|
|
||
| encoder: | ||
| categories: | ||
| - name: cris_pca | ||
|
|
||
| globals: | ||
| - name: source | ||
| type: string | ||
| value: "CrIS PCA netCDF (test)" | ||
|
|
||
| variables: | ||
| - name: latitude | ||
| source: latitude | ||
| longName: "Latitude" | ||
| units: "degree_north" | ||
|
|
||
| - name: longitude | ||
| source: longitude | ||
| longName: "Longitude" | ||
| units: "degree_east" | ||
|
|
||
| - name: time | ||
| source: time | ||
| longName: "Time" | ||
| units: "seconds since 1970-01-01T00:00:00Z" | ||
|
|
||
| - name: timestamp | ||
| source: time | ||
| longName: "Time" | ||
| units: "seconds since 1970-01-01T00:00:00Z" | ||
|
|
||
| - name: scan_position | ||
| source: scan_position | ||
| longName: "Scan position" | ||
| units: "1" | ||
|
|
||
| - name: global_pc_score | ||
| source: global_pc_score | ||
| longName: "Global PC score" | ||
| units: "1" | ||
|
|
||
This file contains hidden or 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 hidden or 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
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.
"dimensions" section should be inside "encoder". Otherwise it will not be applied,