7b. All-season gridded data wrangling

7b. All-season gridded data wrangling#

This notebook prepares the all-season gridded datasets used across the book.

# Regular Python library imports 
import xarray as xr 
import numpy as np
import pandas as pd
import pyproj
import scipy.interpolate
import matplotlib.pyplot as plt
import glob
from datetime import datetime
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import os

# Helper functions for reading the data from the bucket and plotting
from utils.read_data_utils import read_IS2SITMOGR4, read_book_data
from utils.plotting_utils import static_winter_comparison_lineplot, staticArcticMaps, interactiveArcticMaps, compute_gridcell_winter_means, interactive_winter_comparison_lineplot # Plotting utils 
from utils.extra_funcs import read_IS2SITMOGR4S, regrid_ubris_to_is2, get_cs2is2_snow, apply_interpolation_time
# Plotting dependencies
#%config InlineBackend.figure_format = 'retina'
import matplotlib as mpl

# Remove warnings to improve display
import warnings 
warnings.filterwarnings('ignore') 

# Get the current working directory
current_directory = os.getcwd()
# Load the winter IS-2 thickness data from zarr/s3
IS2SITMOGR4_v3 = read_IS2SITMOGR4(data_type='netcdf-local', version='V4') 
#IS2SITMOGR4_v3
# Get some IS2 map projection/domain info needed for later functions
out_proj = 'EPSG:3411'
mapProj = pyproj.Proj("+init=" + out_proj)

xIS2 = IS2SITMOGR4_v3.x.values
yIS2 = IS2SITMOGR4_v3.y.values
xptsIS2, yptsIS2 = np.meshgrid(xIS2, yIS2)
out_lons = IS2SITMOGR4_v3.longitude.values
out_lats = IS2SITMOGR4_v3.latitude.values
IS2SITMOGR4_v3
<xarray.Dataset> Size: 361MB
Dimensions:                      (time: 22, y: 448, x: 304)
Coordinates:
  * time                         (time) datetime64[ns] 176B 2018-11-01 ... 20...
    longitude                    (y, x) float32 545kB 168.3 168.1 ... -9.999
    latitude                     (y, x) float32 545kB 31.1 31.2 ... 34.58 34.47
  * x                            (x) float32 1kB -3.838e+06 ... 3.738e+06
  * y                            (y) float32 2kB 5.838e+06 ... -5.338e+06
Data variables: (12/31)
    crs                          (time) int32 88B -2147483647 ... -2147483647
    ice_thickness                (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    snow_depth                   (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_J22            (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    snow_density                 (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_SM_E5dist      (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ...                           ...
    ice_density_J22              (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_mW99rdist      (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    snow_depth_mW99rdist_int     (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_mW99rdist_int  (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    sea_ice_conc                 (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    region_mask                  (time, y, x) float32 12MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
Attributes:
    contact:      Alek Petty (akpetty@umd.edu)
    description:  Aggregated IS2SITMOGR4 V4 dataset.
    reference:    Official NSIDC data doi: 10.5067/CV6JEXEE31HF. Derived data...
    history:      Created 06/08/25
rename_dict = {
    'snow_depth_SM_E5dist': 'snow_depth_sm_e5',
    'snow_depth_SM_E5dist_int': 'snow_depth_sm_e5_int',
    'snow_depth_SM_M2dist': 'snow_depth_sm_m2',
    'snow_depth_SM_M2dist_int': 'snow_depth_sm_m2_int',
    'ice_thickness_SM_E5dist': 'ice_thickness_sm_e5',
    'ice_thickness_SM_E5dist_int': 'ice_thickness_sm_e5_int',
    'ice_thickness_SM_M2dist': 'ice_thickness_sm_m2',
    'ice_thickness_SM_M2dist_int': 'ice_thickness_sm_m2_int',
    'snow_depth_mW99rdist': 'snow_depth_mw99r',
    'snow_depth_mW99rdist_int': 'snow_depth_mw99r_int',
    'ice_thickness_mW99rdist': 'ice_thickness_mw99r',
    'ice_thickness_mW99rdist_int': 'ice_thickness_mw99r_int',
    'snow_density_W99r': 'snow_density_w99r',
    'snow_density_SM_E5': 'snow_density_sm_e5',
    'snow_density_SM_M2': 'snow_density_sm_m2',
    'ice_density_J22': 'ice_density_j22',
    'ice_thickness_J22': 'ice_thickness_j22',
    'ice_thickness_J22_int': 'ice_thickness_j22_int',
    'ice_density_1': 'ice_density'

    # Add more if needed
}
IS2SITMOGR4_v3 = IS2SITMOGR4_v3.rename(rename_dict)
# interpolate variables unique to the winter dataset (the sm data are already interpolated in summer and summer has no modified warren data)

#IS2SITMOGR4_v3 = apply_interpolation_time(IS2SITMOGR4_v3, xptsIS2, yptsIS2, ['snow_depth_sm', 'ice_thickness_sm', 'snow_depth_mw99', 'ice_thickness_mw99'])
# Read in the new summer IS-2 thickness data locally (not on s3 yet)
IS2SITMOGR4_summer_v0 = read_IS2SITMOGR4S(local_data_path="./data/IS2SITMOGR4S/")
./data/IS2SITMOGR4S/V1/*.nc
IS2SITMOGR4_summer_v0.variables
Frozen({'crs': <xarray.Variable (time: 11)> Size: 44B
array([-2147483647, -2147483647, -2147483647, -2147483647, -2147483647,
       -2147483647, -2147483647, -2147483647, -2147483647, -2147483647,
       -2147483647], dtype=int32)
Attributes: (12/23)
    long_name:                              NSIDC Sea Ice Polar Stereographic...
    proj4text:                              +proj=stere +lat_0=90 +lat_ts=70 ...
    spatial_ref:                            PROJCS["NSIDC Sea Ice Polar Stere...
    grid_mapping_name:                      polar_stereographic
    srid:                                   urn:ogc:def:crs:EPSG::3411
    GeoTransform:                           -3850000.0 25000.0 0 5850000.0 0 ...
    ...                                     ...
    parent_grid_cell_row_subset_end:        448.0
    parent_grid_cell_column_subset_start:   0.0
    parent_grid_cell_column_subset_end:     304.0
    units:                                  meters
    semimajor_radius:                       6378273.0
    semiminor_radius:                       6356889.449, 'ice_thickness_sm_e5': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-E5 sea ice thickness
    description:   Monthly mean gridded sea ice thickness calculated using re...
    grid_mapping:  crs, 'ice_thickness_sm_m2': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM sea ice thickness
    description:   Monthly mean gridded sea ice thickness calculated using re...
    grid_mapping:  crs, 'ice_thickness_unc': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     sea ice thickness uncertainty
    description:   Monthly mean gridded total sea ice thickness uncertainty a...
    grid_mapping:  crs, 'num_segments': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    description:   Number of valid thickness segments in the given monthly gr...
    units:         counts
    long_name:     number of segments
    grid_mapping:  crs, 'mean_day_of_month': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    description:   Mean day of the month represented by a given grid-cell bas...
    units:         day
    long_name:     mean day of month
    grid_mapping:  crs, 'snow_depth_sm_e5': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-E5 snow depth
    description:   Monthly mean gridded SnowModel-LG with ERA5 forcing (Listo...
    source:        Liston, G. E., J. Stroeve, and P. Itkin. (2021). Lagrangia...
    grid_mapping:  crs, 'snow_density_sm_e5': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         kilograms per meters cubed
    long_name:     SM-E5 snow density
    description:   Monthly mean gridded SnowModel-LG with ERA5 forcing (Listo...
    source:        Liston, G. E., J. Stroeve, and P. Itkin. (2021). Lagrangia...
    grid_mapping:  crs, 'snow_depth_sm_m2': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>, 'snow_density_sm_m2': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         kilograms per meters cubed
    long_name:     SM-M@ snow density
    description:   Monthly mean gridded SnowModel-LG with MERRA-2 forcing (Li...
    source:        Liston, G. E., J. Stroeve, and P. Itkin. (2021). Lagrangia...
    grid_mapping:  crs, 'freeboard': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     total freeboard
    description:   Monthly mean gridded total freeboard from rel006 ATL10 (AT...
    grid_mapping:  crs, 'ice_density': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         kilograms per meters cubed
    long_name:     sea ice density
    description:   Bulk sea ice density (constant value of 916 kg/m^3)
    grid_mapping:  crs, 'snow_depth_w99r': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     W99r snow depth
    description:   Monthly mean gridded snow depths from the Warren et al., (...
    source:        Warren, S. G., Rigor, I. G., Untersteiner, N., Radionov, V...
    grid_mapping:  crs, 'snow_density_w99r': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         kilograms per meters cubed
    long_name:     W99 snow density
    description:   Monthly mean gridded snow density from the Warren et al., ...
    grid_mapping:  crs, 'ice_density_j22': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         kilograms per meters cubed
    long_name:     J22 sea ice density
    description:   Monthly mean gridded bulk ice density estimates calculated...
    source:        Jutila, A., Hendricks, S., Ricker, R., von Albedyll, L., K...
    grid_mapping:  crs, 'ice_thickness_sm_e5_j22': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-E5/J22 sea ice thickness
    description:   Monthly mean gridded sea ice thickness calculated using re...
    grid_mapping:  crs, 'ice_thickness_sm_m2_j22': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-M2/J22 sea ice thickness
    description:   Monthly mean gridded sea ice thickness calculated using re...
    grid_mapping:  crs, 'ice_thickness_w99r': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     W99r sea ice thickness
    description:   Monthly mean gridded sea ice thickness calculated using re...
    grid_mapping:  crs, 'freeboard_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     total freeboard (interpolated)
    description:   Monthly mean gridded and smoothed/interpolated total freeb...
    grid_mapping:  crs, 'snow_depth_sm_e5_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-E5 snow depth (interpolated)
    description:   Monthly mean gridded and smoothed/interpolated redistribut...
    grid_mapping:  crs, 'snow_depth_sm_m2_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>, 'snow_depth_w99r_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     W99r snow depth (interpolated)
    description:   Monthly mean gridded and smoothed/interpolated snow depths...
    source:        Warren, S. G., Rigor, I. G., Untersteiner, N., Radionov, V...
    grid_mapping:  crs, 'ice_thickness_sm_e5_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-E5 sea ice thickness (interpolated)
    description:   Monthly mean gridded and smoothed/interpolated sea ice thi...
    grid_mapping:  crs, 'ice_thickness_sm_m2_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-M2 sea ice thickness (interpolated)
    description:   Monthly mean gridded and smoothed/interpolated sea ice thi...
    grid_mapping:  crs, 'ice_thickness_sm_e5_j22_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     SM-E5/J22 sea ice thickness (interpolated)
    description:   Monthly mean gridded and smoothed/interpolated sea ice thi...
    grid_mapping:  crs, 'ice_thickness_sm_m2_j22_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>, 'ice_thickness_w99r_int': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     W99r sea ice thickness (interpolated)
    description:   Monthly mean gridded and interpolated/smoothed sea ice thi...
    grid_mapping:  crs, 'ice_thickness_unc_freeboard': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     sea ice thickness uncertainty from freeboard uncertainty
    description:   Contribution to total sea ice thickness uncertainty from f...
    grid_mapping:  crs, 'ice_thickness_unc_ice_density': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     sea ice thickness uncertainty from ice density uncertainty
    description:   Contribution to total sea ice thickness uncertainty from i...
    grid_mapping:  crs, 'ice_thickness_unc_snow_density': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     sea ice thickness uncertainty from snow density uncertainty
    description:   Contribution to total sea ice thickness uncertainty from s...
    grid_mapping:  crs, 'ice_thickness_unc_snow_depth': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    units:         meters
    long_name:     sea ice thickness uncertainty from snow depth uncertainty
    description:   Contribution to total sea ice thickness uncertainty from s...
    grid_mapping:  crs, 'sea_ice_conc': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    source:        https://nsidc.org/data/G02202/
    description:   Monthly mean gridded ice concentration from the NOAA/NSIDC...
    units:         Concentration (0 to 1)
    long_name:     sea ice concentration
    grid_mapping:  crs, 'region_mask': <xarray.Variable (time: 11, y: 448, x: 304)> Size: 6MB
dask.array<getitem, shape=(11, 448, 304), dtype=float32, chunksize=(1, 448, 304), chunktype=numpy.ndarray>
Attributes:
    source:         https://doi.org/10.5067/CYW3O8ZUNIWC
    flag_values:    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,...
    flag_meanings:  Outside_of_defined_regions Central_Arctic Beaufort_Sea Ch...
    description:    NSIDC Northern Hemisphere region mask from Meier and Stew...
    long_name:      Northern Hemisphere region mask
    grid_mapping:   crs, 'time': <xarray.IndexVariable 'time' (time: 11)> Size: 88B
array(['2019-05-01T00:00:00.000000000', '2019-06-01T00:00:00.000000000',
       '2019-08-01T00:00:00.000000000', '2020-05-01T00:00:00.000000000',
       '2020-06-01T00:00:00.000000000', '2020-07-01T00:00:00.000000000',
       '2020-08-01T00:00:00.000000000', '2021-05-01T00:00:00.000000000',
       '2021-06-01T00:00:00.000000000', '2021-07-01T00:00:00.000000000',
       '2021-08-01T00:00:00.000000000'], dtype='datetime64[ns]'), 'longitude': <xarray.Variable (y: 448, x: 304)> Size: 545kB
array([[168.32042 , 168.14876 , 167.97641 , ..., 102.71974 , 102.544685,
        102.370316],
       [168.4334  , 168.26143 , 168.08879 , ..., 102.608604, 102.433235,
        102.258545],
       [168.54704 , 168.37479 , 168.20184 , ..., 102.49679 , 102.3211  ,
        102.146095],
       ...,
       [-80.970955, -80.79312 , -80.61448 , ..., -10.108099,  -9.926246,
         -9.745198],
       [-80.842575, -80.665   , -80.48663 , ..., -10.234803, -10.053245,
         -9.872486],
       [-80.71499 , -80.53768 , -80.35958 , ..., -10.360703, -10.179442,
         -9.998976]], dtype=float32), 'latitude': <xarray.Variable (y: 448, x: 304)> Size: 545kB
array([[31.102673, 31.199413, 31.295801, ..., 31.677736, 31.582802,
        31.4875  ],
       [31.25011 , 31.34728 , 31.444096, ..., 31.82774 , 31.73238 ,
        31.63665 ],
       [31.397564, 31.495165, 31.592411, ..., 31.977772, 31.881985,
        31.785826],
       ...,
       [34.345894, 34.45253 , 34.55881 , ..., 34.980267, 34.87546 ,
        34.77028 ],
       [34.198696, 34.304863, 34.41067 , ..., 34.830242, 34.725906,
        34.621197],
       [34.05146 , 34.157154, 34.262493, ..., 34.680187, 34.576324,
        34.472084]], dtype=float32), 'x': <xarray.IndexVariable 'x' (x: 304)> Size: 1kB
array([-3837500., -3812500., -3787500., ...,  3687500.,  3712500.,  3737500.],
      dtype=float32)
Attributes:
    units:          meters
    long_name:      center values of projection grid in x direction
    standard_name:  projection_x_coordinate, 'y': <xarray.IndexVariable 'y' (y: 448)> Size: 2kB
array([ 5837500.,  5812500.,  5787500., ..., -5287500., -5312500., -5337500.],
      dtype=float32)
Attributes:
    units:          meters
    long_name:      center values of projection grid in y direction
    standard_name:  projection_y_coordinate})
#rename_dict = {
#    'snow_depth_w99r': 'snow_depth_W99r',
#    'snow_depth_w99r_int': 'snow_depth_W99r_int',
#    'snow_density_w99r': 'snow_density_W99r',
#    'ice_thickness_w99r': 'ice_thickness_W99r',
#    'ice_thickness_w99r_int': 'ice_thickness_W99r_int',
#    # Add more if needed
#}
#IS2SITMOGR4_summer_v0 = IS2SITMOGR4_summer_v0.rename(rename_dict)
IS2SITMOGR4_summer_v0.snow_depth_sm_m2_int.isel(time=0).plot()
<matplotlib.collections.QuadMesh at 0x312f1d040>
../_images/da33186d427a3df8a03d35afc9e84477268bc224eb380d0e2be4215e4b4d37c7.png
IS2SITMOGR4_summer_v0
<xarray.Dataset> Size: 193MB
Dimensions:                         (time: 11, y: 448, x: 304)
Coordinates:
  * time                            (time) datetime64[ns] 88B 2019-05-01 ... ...
    longitude                       (y, x) float32 545kB 168.3 168.1 ... -9.999
    latitude                        (y, x) float32 545kB 31.1 31.2 ... 34.47
  * x                               (x) float32 1kB -3.838e+06 ... 3.738e+06
  * y                               (y) float32 2kB 5.838e+06 ... -5.338e+06
Data variables: (12/33)
    crs                             (time) int32 44B -2147483647 ... -2147483647
    ice_thickness_sm_e5             (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_sm_m2             (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc               (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    num_segments                    (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    mean_day_of_month               (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ...                              ...
    ice_thickness_unc_freeboard     (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc_ice_density   (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc_snow_density  (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc_snow_depth    (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    sea_ice_conc                    (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    region_mask                     (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
Attributes:
    contact:      Alek Petty (akpetty@umd.edu)
    description:  Aggregated IS2SITMOGR4 summer V1 dataset.
    history:      Created 23/07/25
# Interpolate variables that still need it in the summer dataset
#interp_vars=[
# 'snow_depth_sm_m2',
# 'ice_thickness_sm_m2']

#IS2SITMOGR4_summer_v0 = apply_interpolation_time(IS2SITMOGR4_summer_v0, xptsIS2, yptsIS2, interp_vars)
IS2SITMOGR4_summer_v0
<xarray.Dataset> Size: 193MB
Dimensions:                         (time: 11, y: 448, x: 304)
Coordinates:
  * time                            (time) datetime64[ns] 88B 2019-05-01 ... ...
    longitude                       (y, x) float32 545kB 168.3 168.1 ... -9.999
    latitude                        (y, x) float32 545kB 31.1 31.2 ... 34.47
  * x                               (x) float32 1kB -3.838e+06 ... 3.738e+06
  * y                               (y) float32 2kB 5.838e+06 ... -5.338e+06
Data variables: (12/33)
    crs                             (time) int32 44B -2147483647 ... -2147483647
    ice_thickness_sm_e5             (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_sm_m2             (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc               (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    num_segments                    (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    mean_day_of_month               (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ...                              ...
    ice_thickness_unc_freeboard     (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc_ice_density   (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc_snow_density  (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc_snow_depth    (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    sea_ice_conc                    (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    region_mask                     (time, y, x) float32 6MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
Attributes:
    contact:      Alek Petty (akpetty@umd.edu)
    description:  Aggregated IS2SITMOGR4 summer V1 dataset.
    history:      Created 23/07/25
#rename_dict = {
#    'snow_depth_sm_e5': 'snow_depth_sm',
#    'snow_depth_sm_e5_int': 'snow_depth_sm_int',
#    'snow_density_sm_e5': 'snow_density_sm',
#    'ice_thickness_sm_e5': 'ice_thickness_sm',
#    'ice_thickness_sm_e5_int': 'ice_thickness_sm_int',
#    'ice_thickness_sm_e5_j22': 'ice_thickness_sm_j22',
#    'ice_thickness_sm_e5_j22_int': 'ice_thickness_sm_j22_int',
#    # Add more if needed
#}
#IS2SITMOGR4_summer_v0 = IS2SITMOGR4_summer_v0.rename(rename_dict)
# Quick check it looks interpolated..!
IS2SITMOGR4_summer_v0.region_mask.isel(time=0).plot()
<matplotlib.collections.QuadMesh at 0x313f507c0>
../_images/2b3191a09e9f5899198077550be71fa34680c9d6ccf35b3c92271eeda406929a.png
# Merge winter and summer IS-2 thickness data into one dataset
IS2SITMOGR4_allseason = IS2SITMOGR4_summer_v0.merge(IS2SITMOGR4_v3)
IS2SITMOGR4_allseason
<xarray.Dataset> Size: 774MB
Dimensions:                         (time: 33, x: 304, y: 448)
Coordinates:
  * time                            (time) datetime64[ns] 264B 2018-11-01 ......
  * x                               (x) float32 1kB -3.838e+06 ... 3.738e+06
  * y                               (y) float32 2kB 5.838e+06 ... -5.338e+06
    longitude                       (y, x) float32 545kB 168.3 168.1 ... -9.999
    latitude                        (y, x) float32 545kB 31.1 31.2 ... 34.47
Data variables: (12/44)
    crs                             (time) float64 264B -2.147e+09 ... -2.147...
    ice_thickness_sm_e5             (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_sm_m2             (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc               (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    num_segments                    (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    mean_day_of_month               (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ...                              ...
    snow_depth_int                  (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_j22_int           (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    snow_depth_mw99r                (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_mw99r             (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    snow_depth_mw99r_int            (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_mw99r_int         (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
Attributes:
    contact:      Alek Petty (akpetty@umd.edu)
    description:  Aggregated IS2SITMOGR4 summer V1 dataset.
    history:      Created 23/07/25
# Interpolate variables that still need it in the all-season dataset
interp_vars=[
 'snow_density',
 'snow_density_sm_e5',
 'snow_density_sm_m2',
 'snow_density_w99r',
 #'ice_thickness_sm_m2',
 #'ice_thickness_sm_j22',
 #'ice_thickness_sm_m2_j22',
 'ice_density_j22']

IS2SITMOGR4_allseason = apply_interpolation_time(IS2SITMOGR4_allseason, xptsIS2, yptsIS2, interp_vars)
Creating/forcing new variable snow_density_int
snow_density 0
Interpolated.
snow_density 1
Interpolated.
snow_density 2
Interpolated.
snow_density 3
Interpolated.
snow_density 4
Interpolated.
snow_density 5
Interpolated.
snow_density 6
no data or issue with gridding, so skipping...
snow_density 7
no data or issue with gridding, so skipping...
snow_density 8
no data or issue with gridding, so skipping...
snow_density 9
Interpolated.
snow_density 10
Interpolated.
snow_density 11
Interpolated.
snow_density 12
Interpolated.
snow_density 13
Interpolated.
snow_density 14
Interpolated.
snow_density 15
Interpolated.
snow_density 16
Interpolated.
snow_density 17
no data or issue with gridding, so skipping...
snow_density 18
no data or issue with gridding, so skipping...
snow_density 19
no data or issue with gridding, so skipping...
snow_density 20
no data or issue with gridding, so skipping...
snow_density 21
Interpolated.
snow_density 22
Interpolated.
snow_density 23
Interpolated.
snow_density 24
Interpolated.
snow_density 25
Interpolated.
snow_density 26
Interpolated.
snow_density 27
Interpolated.
snow_density 28
Interpolated.
snow_density 29
no data or issue with gridding, so skipping...
snow_density 30
no data or issue with gridding, so skipping...
snow_density 31
no data or issue with gridding, so skipping...
snow_density 32
no data or issue with gridding, so skipping...
Creating/forcing new variable snow_density_sm_e5_int
snow_density_sm_e5 0
Interpolated.
snow_density_sm_e5 1
Interpolated.
snow_density_sm_e5 2
Interpolated.
snow_density_sm_e5 3
Interpolated.
snow_density_sm_e5 4
Interpolated.
snow_density_sm_e5 5
Interpolated.
snow_density_sm_e5 6
Interpolated.
snow_density_sm_e5 7
Interpolated.
snow_density_sm_e5 8
Interpolated.
snow_density_sm_e5 9
Interpolated.
snow_density_sm_e5 10
Interpolated.
snow_density_sm_e5 11
Interpolated.
snow_density_sm_e5 12
Interpolated.
snow_density_sm_e5 13
Interpolated.
snow_density_sm_e5 14
Interpolated.
snow_density_sm_e5 15
Interpolated.
snow_density_sm_e5 16
Interpolated.
snow_density_sm_e5 17
Interpolated.
snow_density_sm_e5 18
Interpolated.
snow_density_sm_e5 19
Interpolated.
snow_density_sm_e5 20
Interpolated.
snow_density_sm_e5 21
Interpolated.
snow_density_sm_e5 22
Interpolated.
snow_density_sm_e5 23
Interpolated.
snow_density_sm_e5 24
Interpolated.
snow_density_sm_e5 25
Interpolated.
snow_density_sm_e5 26
Interpolated.
snow_density_sm_e5 27
Interpolated.
snow_density_sm_e5 28
Interpolated.
snow_density_sm_e5 29
Interpolated.
snow_density_sm_e5 30
Interpolated.
snow_density_sm_e5 31
Interpolated.
snow_density_sm_e5 32
no data or issue with gridding, so skipping...
Creating/forcing new variable snow_density_sm_m2_int
snow_density_sm_m2 0
Interpolated.
snow_density_sm_m2 1
Interpolated.
snow_density_sm_m2 2
Interpolated.
snow_density_sm_m2 3
Interpolated.
snow_density_sm_m2 4
Interpolated.
snow_density_sm_m2 5
Interpolated.
snow_density_sm_m2 6
Interpolated.
snow_density_sm_m2 7
Interpolated.
snow_density_sm_m2 8
Interpolated.
snow_density_sm_m2 9
Interpolated.
snow_density_sm_m2 10
Interpolated.
snow_density_sm_m2 11
Interpolated.
snow_density_sm_m2 12
Interpolated.
snow_density_sm_m2 13
Interpolated.
snow_density_sm_m2 14
Interpolated.
snow_density_sm_m2 15
Interpolated.
snow_density_sm_m2 16
Interpolated.
snow_density_sm_m2 17
Interpolated.
snow_density_sm_m2 18
Interpolated.
snow_density_sm_m2 19
Interpolated.
snow_density_sm_m2 20
Interpolated.
snow_density_sm_m2 21
Interpolated.
snow_density_sm_m2 22
Interpolated.
snow_density_sm_m2 23
Interpolated.
snow_density_sm_m2 24
Interpolated.
snow_density_sm_m2 25
Interpolated.
snow_density_sm_m2 26
Interpolated.
snow_density_sm_m2 27
Interpolated.
snow_density_sm_m2 28
Interpolated.
snow_density_sm_m2 29
Interpolated.
snow_density_sm_m2 30
Interpolated.
snow_density_sm_m2 31
Interpolated.
snow_density_sm_m2 32
no data or issue with gridding, so skipping...
Creating/forcing new variable snow_density_w99r_int
snow_density_w99r 0
Interpolated.
snow_density_w99r 1
Interpolated.
snow_density_w99r 2
Interpolated.
snow_density_w99r 3
Interpolated.
snow_density_w99r 4
Interpolated.
snow_density_w99r 5
Interpolated.
snow_density_w99r 6
Interpolated.
snow_density_w99r 7
Interpolated.
snow_density_w99r 8
Interpolated.
snow_density_w99r 9
Interpolated.
snow_density_w99r 10
Interpolated.
snow_density_w99r 11
Interpolated.
snow_density_w99r 12
Interpolated.
snow_density_w99r 13
Interpolated.
snow_density_w99r 14
Interpolated.
snow_density_w99r 15
Interpolated.
snow_density_w99r 16
Interpolated.
snow_density_w99r 17
Interpolated.
snow_density_w99r 18
Interpolated.
snow_density_w99r 19
Interpolated.
snow_density_w99r 20
Interpolated.
snow_density_w99r 21
Interpolated.
snow_density_w99r 22
Interpolated.
snow_density_w99r 23
Interpolated.
snow_density_w99r 24
Interpolated.
snow_density_w99r 25
Interpolated.
snow_density_w99r 26
Interpolated.
snow_density_w99r 27
Interpolated.
snow_density_w99r 28
Interpolated.
snow_density_w99r 29
Interpolated.
snow_density_w99r 30
Interpolated.
snow_density_w99r 31
Interpolated.
snow_density_w99r 32
no data or issue with gridding, so skipping...
Creating/forcing new variable ice_density_j22_int
ice_density_j22 0
Interpolated.
ice_density_j22 1
Interpolated.
ice_density_j22 2
Interpolated.
ice_density_j22 3
Interpolated.
ice_density_j22 4
Interpolated.
ice_density_j22 5
Interpolated.
ice_density_j22 6
Interpolated.
ice_density_j22 7
Interpolated.
ice_density_j22 8
Interpolated.
ice_density_j22 9
Interpolated.
ice_density_j22 10
Interpolated.
ice_density_j22 11
Interpolated.
ice_density_j22 12
Interpolated.
ice_density_j22 13
Interpolated.
ice_density_j22 14
Interpolated.
ice_density_j22 15
Interpolated.
ice_density_j22 16
Interpolated.
ice_density_j22 17
Interpolated.
ice_density_j22 18
Interpolated.
ice_density_j22 19
Interpolated.
ice_density_j22 20
Interpolated.
ice_density_j22 21
Interpolated.
ice_density_j22 22
Interpolated.
ice_density_j22 23
Interpolated.
ice_density_j22 24
Interpolated.
ice_density_j22 25
Interpolated.
ice_density_j22 26
Interpolated.
ice_density_j22 27
Interpolated.
ice_density_j22 28
Interpolated.
ice_density_j22 29
Interpolated.
ice_density_j22 30
Interpolated.
ice_density_j22 31
Interpolated.
ice_density_j22 32
no data or issue with gridding, so skipping...
done, returning new dataset
IS2SITMOGR4_allseason
<xarray.Dataset> Size: 864MB
Dimensions:                         (time: 33, x: 304, y: 448)
Coordinates:
  * time                            (time) datetime64[ns] 264B 2018-11-01 ......
  * x                               (x) float32 1kB -3.838e+06 ... 3.738e+06
  * y                               (y) float32 2kB 5.838e+06 ... -5.338e+06
    longitude                       (y, x) float32 545kB 168.3 168.1 ... -9.999
    latitude                        (y, x) float32 545kB 31.1 31.2 ... 34.47
Data variables: (12/49)
    crs                             (time) float64 264B -2.147e+09 ... -2.147...
    ice_thickness_sm_e5             (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_sm_m2             (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ice_thickness_unc               (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    num_segments                    (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    mean_day_of_month               (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    ...                              ...
    ice_thickness_mw99r_int         (time, y, x) float32 18MB dask.array<chunksize=(1, 448, 304), meta=np.ndarray>
    snow_density_int                (time, y, x) float32 18MB nan nan ... nan
    snow_density_sm_e5_int          (time, y, x) float32 18MB nan nan ... nan
    snow_density_sm_m2_int          (time, y, x) float32 18MB nan nan ... nan
    snow_density_w99r_int           (time, y, x) float32 18MB nan nan ... nan
    ice_density_j22_int             (time, y, x) float32 18MB nan nan ... nan
Attributes:
    contact:      Alek Petty (akpetty@umd.edu)
    description:  Aggregated IS2SITMOGR4 summer V1 dataset.
    history:      Created 23/07/25
# Get UBRIS/UIT CS-2 data
start_date_cs2 = "Oct 2018"
end_date_cs2 = "July 2021"
# NB: MS indicates a time frequency of start of the month
date_range_cs2 = pd.date_range(start=start_date_cs2, end=end_date_cs2, freq='MS') 

# Re-grid the UBRIS/UIT CS-2 data to the IS-2 grid.
# Includes monthly resampling of the biweekly data
cs2_ubris = regrid_ubris_to_is2(mapProj, xIS2, yIS2, out_lons, out_lats, date_range_cs2,
                             dataPathCS2=current_directory+'/data/', dataset='uit_cryosat2_seaicethickness_nh_80km_v1p7.nc')

# Relabel the cs2_sea_ice_thickness_UBRIS variable to ice_thickness_cs2_ubris
cs2_ubris = cs2_ubris.rename({'cs2_sea_ice_thickness_UBRIS': 'ice_thickness_cs2_ubris'})
# Get the preliminary IS-2/CS-2 winter snow depths, downlaoded from Zenodo
cs2is2_snow_regridded_da = get_cs2is2_snow(mapProj, xIS2, yIS2, dataPathCS2=current_directory+'/data/uit_cs2-is2-ak_snow_depth_25km_v3.nc')
# Merge with other CS-2 data
cs2_ubris = cs2_ubris.merge({'cs2is2_snow_depth': cs2is2_snow_regridded_da})
# Merge all season IS-2 data and CS-2 datasets
IS2_CS2_allseason = IS2SITMOGR4_allseason.merge(cs2_ubris)

# Adjust all times to the middle (15th) of the month for plotting purposes
# (generally time defaults to the start of the month, with the MS function)
IS2_CS2_allseason['time'] = IS2_CS2_allseason['time'] + pd.Timedelta(days=14)
# Remove data outside of the period coincident with the SM-LG/IS-2 data
IS2_CS2_allseason = IS2_CS2_allseason.where(IS2_CS2_allseason['time'] <= pd.Timestamp('2021-07-20'), drop=True)
IS2_CS2_allseason = IS2_CS2_allseason.where(IS2_CS2_allseason['time'] >= pd.Timestamp('2018-11-01'), drop=True)
#IS2_CS2_allseason
# Only need the region mask for the first time step (missing from time=0 for some reason...)
IS2_CS2_allseason['region_mask'] = IS2_CS2_allseason['region_mask'].isel(time=1, drop=True)
# Save the dataset to netCDF file
output_path = './data/book_data_allseason_v2.nc'
IS2_CS2_allseason.to_netcdf(output_path)

print(f"Dataset saved to {output_path}")
Dataset saved to ./data/book_data_allseason_v2.nc
# Test loading the saved file
test_load = xr.open_dataset('./data/book_data_allseason_v2.nc')
print("Successfully loaded saved dataset")
Successfully loaded saved dataset