rightso i have a dataset where i am misssing the lsm data because i didnt querry it. but…
- User
rightso i have a dataset where i am misssing the lsm data because i didnt querry it. but the good news is that it doesnt change with time so presumably the same lsm can be used for each time. can you give me a cell which will whck if this is true. and then if it is set all the times where lsm is nan to that value? these are my code snippets that are relevant: import glob import cfgrib
single_level_files = glob.glob('single_levels_*.grib')
single_level_data_2013_to_2024 = []
for file in single_level_files: try: print(f"Loading {file}") datasets = cfgrib.open_datasets(file) for ds in datasets: single_level_data_2013_to_2024.append(ds) except Exception as e: print(f"Error processing {file}: {e}")ds1 = single_level_data_2013_to_2024[1] ds1 = ds1.stack(time_step=('time', 'step')) ds1 = ds1.drop('time_step') ds1 = ds1.rename({'valid_time': 'time'}) ds1 = ds1.swap_dims({'time_step': 'time'})
ds3 = single_level_data_2013_to_2024[3] ds3 = ds3.stack(time_step=('time', 'step')) ds3 = ds3.drop('time_step') ds3 = ds3.rename({'valid_time': 'time'}) ds3 = ds3.swap_dims({'time_step': 'time'})merged_ds = xr.merge([ single_level_data_2013_to_2024[0], ds1, single_level_data_2013_to_2024[2], ds3 ])xarray.Dataset
Dimensions: time: 16068latitude: 61longitude: 71
Coordinates: time (time) datetime64[ns] 2013-01-01 ... 2023-12-31T18:00:00
latitude (latitude) float64 51.0 50.75 50.5 ... 36.5 36.25 36.0
longitude (longitude) float64 -9.5 -9.25 -9.0 ... 7.5 7.75 8.0
number () int64 0
step () timedelta64[ns] 00:00:00
surface () float64 0.0
valid_time (time) datetime64[ns] 2013-01-01 ... 2023-12-31T18:00:00
Data variables: msl (time, latitude, longitude) float32 1.007e+05 1.006e+05 ... 1.02e+05
u10 (time, latitude, longitude) float32 11.64 11.38 10.96 ... -1.061 -1.701
v10 (time, latitude, longitude) float32 -5.532 -5.386 ... -0.6686 -0.3386
t2m (time, latitude, longitude) float32 281.9 281.8 281.6 ... 284.0 283.8
u100 (time, latitude, longitude) float32 13.52 13.15 12.76 ... -1.767 -2.601
v100 (time, latitude, longitude) float32 -6.515 -6.332 ... -0.4309 0.05444
tisr (latitude, longitude, time) float32 0.0 0.0 1.25e+06 ... 2.591e+06 0.0
lsm (time, latitude, longitude) float32 0.0 0.0 0.0 0.0 ... nan nan nan nan
so i need to do this on the merged_ds xarray dataset.
- Assistant
Certainly! Here's a cell that checks if
lsmis constant over time in yourmerged_dsdataset, and if it is, fills all the times wherelsmisNaNwith that constant value:python