Useful MATLAB Code [2026.09.21]¶
Seawater property toolboxes and code for handling gridded data.
Sea water toolbox¶
-
TEOS-10 (GSW)
Gibbs SeaWater Oceanographic Toolbox. The GitHub repository is identical to the toolbox distributed on the official site.
-
Properties of seawater (SIO 210)
MATLAB seawater property code from the UCSD SIO 210 course
-
SEA-MAT
Collection of MATLAB tools for oceanography
Extract / Convert grid¶
Extract specific area from global data¶
Extract specific area from global data
Extract specific area from global data
%% Extract the data in specific area from Global data.
% Edit ==================================
% specific area range where you want.
lo = [117, 130]; la = [23 43];
% allocate the variables of global or larger area.
originlon = ?;
originlat = ?
originVar = ?;
% You don't need to edit below. =======================================
xstart=knnsearch(originlon, lo(1)); xend=knnsearch(originlon, lo(2));
ystart=knnsearch(originlat, la(1)); yend=knnsearch(originlat, la(2));
newlon = originlon( xstart:xend );
newlat = originlat( ystart:yend );
newVar = originVar( xstart:xend , ystart:yend );
% Now you just use "newlon", "newlat", "newVar"
Extract value at a specific point from a curved grid¶
Extract value at a specific point from a curved grid
Extract value at a specific point from a curved grid
clc; clear; close all;
% Edit ================================================================
% load data (Curved Grid)
fnm = 'somxl030_control_monthly_highres_2D_199501_CONS_v0.1.nc';
lat = ncread(fnm, 'nav_lat');
lon = ncread(fnm, 'nav_lon');
somxl030 = squeeze(ncread(fnm, 'somxl030'));
% drifter position (Specific Point where you want to extract value)
drf = [125 30];
% You don't need to edit below. =======================================
% reshape arraw -> input parameter for function "dsearchn"
lat2 = reshape(lat, [], 1);
lon2 = reshape(lon, [], 1);
lonlat = cat(2, lon2, lat2);
% k : index of nearlest point in grid
k = dsearchn(lonlat, drf);
% check the lon, lat of the nearest point in grid
check = lonlat(k, :);
check_lon = unique(check(:,1));
check_lat = unique(check(:,2));
fprintf('Longitude of nearest point : %f \n', check_lon)
fprintf('Latitude of nearest point : %f \n', check_lat)
% find index in 2d grid
id1 = lon == check_lon; id2 = lat == check_lat;
id = all(cat(3, id1, id2), 3);
fprintf('MLD of nearest point : %f \n', somxl030(id))
Convert curvilinear grid into regular grid¶
Convert curvilinear grid into regular grid
Convert curvilinear grid into regular grid
clc; clear; close all;
% Curvilinear grid
hycbathy = "Bathy\depth_GLBb0.08_09m11.nc";
% ncdisp(hycbathy)
hyclon = double(ncread(hycbathy, 'Longitude'));
hyclat = double(ncread(hycbathy, 'Latitude'));
hycdepth = double(ncread(hycbathy, 'depth'));
% Set the lon and lat range
lo = [117, 128]; la = [32 42];
% new query grid
[xq,yq] = meshgrid(lo(1):0.08:lo(2), la(1):0.08:la(2));
% Regular grid
newdep = griddata(hyclon,hyclat,hycdepth,xq,yq);