3434
3535import numpy as np
3636from datatree import DataTree
37- from xarray import open_dataset
37+ from xarray import merge , open_dataset
3838from xarray .backends import NetCDF4DataStore
3939from xarray .backends .common import BackendEntrypoint
4040from xarray .backends .store import StoreBackendEntrypoint
@@ -128,13 +128,20 @@ def _get_sweep_groups(
128128 ray_start_index = root .get ("ray_start_index" , False )
129129
130130 # strip variables and attributes
131- anc_dims = list (set (root .dims ) ^ {"time" , "range" , "sweep" })
131+ anc_dims = set (root .dims ) ^ {"time" , "range" , "sweep" , "n_points" }
132+ anc_dims &= set (root .dims )
133+
132134 root = root .drop_dims (anc_dims )
133135
134136 root = root .rename ({"fixed_angle" : "sweep_fixed_angle" })
135137
136138 # conform to cfradial2 standard
137139 data = conform_cfradial2_sweep_group (root , optional , "time" )
140+ data_vars = {
141+ k
142+ for k , v in data .data_vars .items ()
143+ if any (d in v .dims for d in ["range" , "n_points" ])
144+ }
138145
139146 # which sweeps to load
140147 # sweep is assumed a list of strings with elements like "sweep_0"
@@ -172,23 +179,16 @@ def _get_sweep_groups(
172179 rslice = slice (0 , current_ray_n_gates [0 ].values .astype (int ))
173180 ds = ds .isel (range = rslice )
174181 ds = ds .isel (n_points = nslice )
175- ds = ds .stack (n_points = [dim0 , "range" ])
176- ds = ds .unstack ("n_points" )
177- # fix elevation/time additional range dimension in coordinate
178- ds = ds .assign_coords ({"elevation" : ds .elevation .isel (range = 0 , drop = True )})
179-
180- # handling first dimension
181- # for CfRadial1 first dimension is time
182- if first_dim == "auto" :
183- ds = ds .swap_dims ({"time" : dim0 })
184- ds = ds .sortby (dim0 )
185-
186- # reassign azimuth/elevation coordinates
187- ds = ds .assign_coords ({"azimuth" : ds .azimuth })
188- ds = ds .assign_coords ({"elevation" : ds .elevation })
182+ ds_vars = ds [data_vars ]
183+ ds_vars = merge ([ds_vars , ds [[dim0 , "range" ]]])
184+ ds_vars = ds_vars .stack (n_points = [dim0 , "range" ])
185+ ds_vars = ds_vars .unstack ("n_points" )
186+ ds = ds .drop_vars (ds_vars .data_vars )
187+ ds = merge ([ds , ds_vars ])
189188
190189 # assign site_coords
191190 if site_coords :
191+
192192 ds = ds .assign_coords (
193193 {
194194 "latitude" : root .latitude ,
@@ -197,6 +197,20 @@ def _get_sweep_groups(
197197 }
198198 )
199199
200+ # handling first dimension
201+ # for CfRadial1 first dimension is time
202+ if first_dim == "auto" :
203+ if "time" in ds .dims :
204+ ds = ds .swap_dims ({"time" : dim0 })
205+ ds = ds .sortby (dim0 )
206+ else :
207+ if "time" not in ds .dims :
208+ ds = ds .swap_dims ({dim0 : "time" })
209+ ds = ds .sortby ("time" )
210+
211+ # reassign azimuth/elevation coordinates
212+ ds = ds .set_coords (["azimuth" , "elevation" ])
213+
200214 sweep_groups [sw ] = ds
201215
202216 return sweep_groups
0 commit comments