I want to open SST data from NOAA opendap, clip the region that I am interested in, load the variables of interest, do some summary stats, and then close the file. Because I want to do it for daily data over several years, I do not want to save the .nc files on my computer. I would prefer just open it with python, clip, summarise, and export.
This is how I do it without the Shapefile clipping part but with a ''box'' region. It works fine. However, I would like to define the region with Shapefile.
```
import netCDF4
#-- Define URLs for data access
ncfile = 'https://podaac-opendap.jpl.nasa.gov/opendap/hyrax/allData/ghrsst/data/GDS2/L4/GLOB/JPL/MUR/v4.1/2023/066/20230307090000-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc'
fh = netCDF4.Dataset(ncfile)
time = fh.variables['time'][:]
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
#-- latitude and longitude boundaries
latbounds = [xxx, xxx]
lonbounds = [xxx, xxx]
latli = np.argmin( np.abs( lats - latbounds[0] ) )
latui = np.argmin( np.abs( lats - latbounds[1] ) )
lonli = np.argmin( np.abs( lons - lonbounds[0] ) )
lonui = np.argmin( np.abs( lons - lonbounds[1] ) )
sst_subset = fh.variables['analysed_sst'][ 0 , latli:latui , lonli:lonui ]
sst_subset = sst_subset - 273.15
mean_sst = np.mean(sst_subset)
lons_subset = fh.variables['lon'][lonli:lonui]
lats_subset = fh.variables['lat'][latli:latui]
fh.close()
```
I have tried the following code that I found on a similar topics here. It works if I download and save the .nc file on my computer but it does not work if I open it directly from the url.
```
import xarray as xr
import rioxarray
import geopandas as gpd
from shapely.geometry import mapping
# Load Netcdf file
ncfile = 'https://podaac-opendap.jpl.nasa.gov/opendap/hyrax/allData/ghrsst/data/GDS2/L4/GLOB/JPL/MUR/v4.1/2023/066/20230307090000-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc'
fh = xr.open_dataset(ncfile)
fh.rio.set_spatial_dims(x_dim='lon', y_dim='lat', inplace=True)
fh.rio.write_crs('EPSG:4326', inplace=True)
# Load shapefile
lme = gpd.read_file('LMEs66.shp') # source: https://www.sciencebase.gov/catalog/item/55c77722e4b08400b1fd8244
lme = lme[lme['LME_NUMBER'] == 10] # Subsetting LME
# Clip Netcdf (SST) by shapefile (LME)
clipped = fh.rio.clip(lme.geometry.apply(mapping), lme.crs)
clipped.to_netcdf('mytest_clipped.nc')
```
I get this error when opening from the url:
"oc_open: server error retrieving url: code=? message="Error {
code = 500;
message = "Unable to process <BESError> object in stream.";
}
Thanks for you help.