Change longitude coordinate convention from 0E-360W to -180E,180W in a NetCDF File
13 views (last 30 days)
Show older comments
Hi all,
I have a NetCDF file that plots a global map of the salinity, on top of which I superimpose the coastline coordinates from MATLAB's map toolbox such that I can have a clearer definition of my world map.
However, the MATLAB map has a -180E to 180W longitude convention while the NetCDF map has a 0E to 360W longitude convention. When I try to convert the NetCDF map (which seems like a very simple operation) such that its convention is the same as that of MATLAB (i.e. -180E to 180E), the plot seems to be coming awfully wrong every time.
Any suggestions?
Thanks in advance for your help.
(below is the code)
%%URL & Display
%Load URL(s)
url = 'https://podaac- opendap.jpl.nasa.gov:443/opendap/allData/smap/L3/RSS/V2/8day_running/SCI/2018/131/RSS_smap_SSS_8day_running_20 18_135_v02.0.nc' ;
%Display variables
%ncdisp(url)
%%Call Variables
%Time
time_start = ncreadatt(url, '/', 'time_coverage_start') ;
time_end = ncreadatt(url, '/', 'time_coverage_end') ;
%Coordinates
lat = ncread(url, 'lat') ;
lon360 = ncread(url, 'lon') ;
%Convert longitude to 180W - 180E
%First way to do it
lon = rem( (lon360 + 180), 360) - 180 ;
lon_idx = lon > 180 ;
lon(lon_idx) = -lon(lon_idx) + 180 ;
% %Second way to do it
% lon = rem((lon360 + 180), 360) + 180 ;
%Salinity
sal = ncread(url, 'sss_smap') ;
%%Plot
%Prep Data
[LON, LAT] = meshgrid(lon, lat) ;
v = linspace(min(sal(:)), max(sal(:)), 50) ;
%Figure
fig = figure ;
fig.Position = [ 100 200 1000 500 ] ;
%Plot Data
[~, h] = contourf(LON, LAT, sal', v) ;
h.LineStyle = 'none' ;
hold on
%Plot World Map
load coastlines
plot(coastlon,coastlat)
%Colorbar
c = colorbar ;
%Grid
grid on
ax = gca ;
ax.GridColor = [1 0 0] ;
%Labeling
c.Label.String = 'Salinity (PSU)' ; %Colorbar Label
title( strcat({'Recorded: '}, {time_start(1:10)}, {' to '}, {time_end(1:10)} ) ) ; %Graph Title
ax.FontSize = 14 ;
2 Comments
Augusto Gabriel da Costa Pereira
on 29 Jun 2022
Edited: Augusto Gabriel da Costa Pereira
on 29 Jun 2022
Thanks!
KSSV
on 30 Jun 2022
You can add 180 to -180 to +180 or substract 180 from 0 to 360. Won't this work?
Answers (1)
Marina
on 6 May 2023
Hi Konstantinos,
I have the same problem and I found this functions that maybe could help you solve the longitud units:
If you want more information I found it at:
I hope its help you and others.
0 Comments
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!