24 views (last 30 days)
Show older comments
Daryna Butash on 24 Feb 2021
Edited: Rik on 14 Mar 2021
Accepted Answer: Chad Greene
Open in MATLAB Online
Hello! Thank you for looking at my question I need a little bit of help with my code I am trying to plot sea surface temperature in the Baltic Sea for my uni coursework and plot it on a map on matlab with colour change gradients and contours however, I have been having very little luck with making the geoshow function work. My SST data ends up 3 dimensional and wont combine with my latitude and longtitude which are 2D variable and im not sure why that is I will attach my code here. my sst variable ends up being 383 x 523 x 56 double and my latitude one is 523x383 single, I have tried reshaping as well as putting it into just the two columns but then its not the same size. Any help would be much appreciated, thank you in advance! I have had a few attempts at reshaping which are percentaged below.
clc; close all ;
ncfile = 'CMEMS_BAL_PHY_reanalysis_monthlymeans_200809.nc' ; %loads temperature file
ncdisp(ncfile) %info about ncfile
sst = ncread(ncfile, 'thetao');
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
% Read in the sea_ice_fraction data and store in a new variable called ice:
%gets sea surface temperature value
time = ncread(ncfile,'time') ; nt = length(time) ; %reads the time variable and obtains its size
[X,Y] = meshgrid(lon,lat) ; % transform longitudes and latitudes into a grid of lon long x lat dimension
%% in case salinty wants to be used
%ncfileS = 'woa18_decav_s00_04.nc' ; %loads salinity file
sal = ncread(ncfile,'so'); %defines salinity variable
%%generates map
figure('Color','white'); %creates figure
ax=worldmap([45 90],[-45 45]); % loads world map with the limits for BALTICS
%newx= reshape(X, 383, 523);
%
%nsst = permute(sst,[1 3 2]);
%nsst = reshape(nsst,[],size(sst,2),1);
%nsst= reshape(sst, []);
%setm(ax,'mapprojection','mercator','Origin', [180 0 180]) %changes projection and changes origin reference for coordinates
levels=[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ]; % creates levels for contours
geoshow(double(Y),double(X),sst,'DisplayType','texturemap') %creates surface map of temperature
cb = contourcbar('peer',ax,'Location','northoutside'); %creates colorbar
caxis([15 35]) %defines limits for colorbar
colormap(jet) %sets color scheme
set(get(cb,'XLabel'),'String','SST (^oC)') %title for color bar
set(cb,'Position',[0.23 0.2417 0.5750 0.0335]) %adjust location and size of color bar
%levels=[28]; %sets level for contour defining specific
geoshow(double(Y),double(X),nsst,'DisplayType', 'contour','LevelList',levels,'LineColor','black') %plots temperature contour defining the western Pacific Warm Pool
hold on
land = shaperead('landareas.shp', 'UseGeoCoords', true); %define land areas
geoshow(land, 'FaceColor', [0 0 0]) % plots land areas in black
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Rik on 14 Mar 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/754784-mapping-sea-surface-temp-with-specific-location-struggling-with-geoshow-function#comment_1390913
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Chad Greene on 2 Mar 2021
Open in MATLAB Online
For 3D data cubes of things like ocean temperature, you most often need to permute the first two dimensions when reading in from a netcdf. That would look like this for your data:
sst = permute(ncread(ncfile, 'thetao'),[2 1 3]);
To make a 2D map of this 3D data, you'll need to either pick a single time slice and plot it like this:
pcolorm(Lat,Lon,sst(:,:,3))
to plot the third map of SST data in the time series. Or perhaps you wish to plot the average SST map for all of your data. That would look like this:
pcolorm(Lat,Lon,mean(sst,3,'omitnan'))
which calculates the mean long the third dimension (time) and omits any missing data values if there are any.
A side note: As a matter of style, it's generally best if you can avoid using the variable names X and Y to mean longitude and latitude, because X and Y often refer to projected coordinates which are in meters, rather than degrees. In my own work, I typically use lowercase lon and lat for 1D arrays and uppercase Lon and Lat for 2D grids of longitdue and latitude. That would look like
[Lon,Lat] = meshgrid(lon,lat);
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Daryna Butash on 4 Mar 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/754784-mapping-sea-surface-temp-with-specific-location-struggling-with-geoshow-function#comment_1368201
This is super helpful thank you so much! I don't think I will plot in 3d just because of ease of relaying my figures but I will definitely give it a try! Your suggestion helped my code work thank you again for the help.
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationMapping Toolbox
RadarMapping Toolbox
Find more on Mapping Toolbox in Help Center and File Exchange
Tags
- geoshow
- function
- matlab
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office