Rony Golder
Rony Golder

Reputation: 121

How to Convert NSIDC Sea Ice Concentration Data to Latitude and Longitude Dimensions in xarray?

I’m working with a dataset cdr_sea_ice_monthly that has dimensions (time, xgrid, ygrid). I want to convert this sea ice concentration variable to have latitude and longitude as dimensions, resulting in (time, latitude, longitude).

Here is what I have tried so far:

import xarray as xr
import numpy as np
from pyproj import CRS
from pyproj import Transformer

# Load dataset
nsidc_ice = xr.open_dataset("https://polarwatch.noaa.gov/erddap/griddap/nsidcG02202v4shmday")
ice_conc = nsidc_ice['cdr_seaice_conc_monthly']

# Select time slice
ice_conc = ice_conc.sel(time=slice("2021-01-01", "2021-12-01"))

# Define CRS (WGS84 and Antarctic Polar Stereographic)
crs_4326 = CRS.from_epsg(4326)
crs_3031 = CRS.from_epsg(3031)

# Create a transformer to convert between x/y grid and lat/lon
transformer = Transformer.from_crs(crs_3031, crs_4326)

# Create a rectangular grid of xgrid and ygrid
x, y = np.meshgrid(ice_conc.xgrid, ice_conc.ygrid)

# Convert x/y grid to lat/lon
lat, lon = transformer.transform(x, y)

# Assign lat/lon coordinates to the DataArray
ice_conc.coords['lat'] = (ice_conc[0][:].dims, lat)
ice_conc.coords['lon'] = (ice_conc[0][:].dims, lon)

# Mask the data to keep only valid sea ice concentration values
ice_wgs84 = ice_conc.where(ice_conc <= 1, np.nan)

However, this gives me an output like:

xarray.DataArray 'cdr_seaice_conc_monthly' (time: 12, ygrid: 332, xgrid: 316)

I want the output to be in the form of (time, lat, lon).

How can I achieve this transformation?

Thanks

Upvotes: 0

Views: 37

Answers (0)

Related Questions