plotting positive and negative temperature contours

3 views (last 30 days)
Im trying to plot pos and neg contours but the code I have keeps throwing up errors any ideas?
% ERA5_contourf_1
% Choose netcdf file
nc_file = '../data/ERA/ERA5_2_1979_1984.nc'; % put the full path to the netcdf file between the inverted commas
% Choose what time to plot (the nearest available data will be plotted)
chosen_time_datestr = '1982-08-15-10.00'; % edit this to plot different time-slices
chosen_time_datenum = datenum(chosen_time_datestr, 'yyyy-mm-dd-HH.MM');
%% Look at netcdf file metadata
% ncdisp(nc_file);
%
% Source:
% G:\sheffield_fellowship\teaching\pac_dissertations\2020_2021\abigail_waring\data\ERA\test 1979-2009.nc
% Format:
% 64bit
% Global Attributes:
% Conventions = 'CF-1.6'
% history = '2021-02-10 19:19:08 GMT by grib_to_netcdf-2.16.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -S param -o /cache/data3/adaptor.mars.internal-1612984616.206418-32671-22-77884e71-90b2-4259-9917-b6355929a1cd.nc /cache/tmp/77884e71-90b2-4259-9917-b6355929a1cd-adaptor.mars.internal-1612984616.2070854-32671-10-tmp.grib'
% Dimensions:
% longitude = 240
% latitude = 323
% time = 1575
% Variables:
% longitude
% Size: 240x1
% Dimensions: longitude
% Datatype: single
% Attributes:
% units = 'degrees_east'
% long_name = 'longitude'
% latitude
% Size: 323x1
% Dimensions: latitude
% Datatype: single
% Attributes:
% units = 'degrees_north'
% long_name = 'latitude'
% time
% Size: 1575x1
% Dimensions: time
% Datatype: int32
% Attributes:
% units = 'hours since 1900-01-01 00:00:00.0'
% long_name = 'time'
% calendar = 'gregorian'
% t2m
% Size: 240x323x1575
% Dimensions: longitude,latitude,time
% Datatype: int16
% Attributes:
% scale_factor = 0.001321
% add_offset = 278.4375
% _FillValue = -32767
% missing_value = -32767
% units = 'K'
% long_name = '2 metre temperature'
%% Open netcdf file
disp(['Loading ' nc_file]);
% Get variables from netcdf file
% Extract required variables
longitude = double(ncread(nc_file,'longitude')); % 'degree_east'
latitude = double(ncread(nc_file,'latitude')); % 'degree_north'
time = ncread(nc_file,'time'); % 'hours since 1900-01-01'
t2m = ncread(nc_file,'t2m')-273.15; % 'Centigrade'
t2m = permute(t2m,[2,1,3]);
% Prepare example data to plot
[lon_grid, lat_grid] = meshgrid(longitude, latitude);
t2m_eg = t2m(:,:,1);
% Convert to MATLAB time format
matlab_datetime = datetime(1900,1,1) + hours(time);
matlab_datetime = datenum(matlab_datetime);
% Get the closest data to the time indicated
[min_time_diff, min_time_diff_idx] = min(abs(matlab_datetime - chosen_time_datenum));
%% Load coastline
S = shaperead('../data/gshhs/GSHHS_shp/l/GSHHS_l_L1.shp', 'UseGeoCoords', true);
%% pos and neg contour
%plot negative contours
%not convecneg goes to just less than 0, otherwise the 0 contour and labels gets plotted twice
clear convecpos convecneg;
convecpos=[0:contourspace:contourmax];
convecneg=[contourmin:contourspace:-0.0001];
[c,h1]=contourm(nya2,nxa2,plotdata,convecneg,'--k');
htext0 = clabelm(c,h1,'LabelSpacing',500);
set(htext0,'fontsize',8.0,'linewidth',2.0,'BackgroundColor','none');
%positive contours
[c,h3]=contourm(nya2,nxa2,plotdata,convecpos,'-k');
%clabel(c,h3,'BackgroundColor','none');
htext=clabelm(c,h3,'LabelSpacing',500);
set(htext,'fontsize',8.0,'linewidth',2.0,'BackgroundColor','none');
hold on;
%% Plot example time-slice from netcdf file
disp('Plotting and saving a figure...');
% Make a figure
figure('units','normalized','outerposition',[0 0 1 1],'visible','on');
set(gcf,'color','w');
% Set axis font size
font_size = 12;
set(0, 'DefaultAxesFontSize', font_size);
% Make a map axis to encompass the data
ax = worldmap([nanmin(lat_grid(:)) nanmax(lat_grid(:))], ...
[nanmin(lon_grid(:)) nanmax(lon_grid(:))]);
% Allow overplotting
hold on
% Plot a pseudocolour plot of the chosen time-slice of the data
contourm(lat_grid, lon_grid, t2m(:,:,min_time_diff_idx), 'LevelStep', 5, ...
'LabelSpacing', 244, 'ShowText', 'on', 'fill', 'on', 'LineColor', [0.4 0.4 0.4]);
% Plot the gsshs coastlines using geoshow
geoshow(S, 'DisplayType', 'polygon',...
'FaceColor', 'none', 'EdgeColor', [0.1 0.1 0.1]);
% Make a colourbar
cbar = colorbar('eastoutside','fontsize',font_size);
% Add a colourbar label
set(get(cbar,'Xlabel'),'String','Temperature (^oC)');
% Add a scale bar
scb = scalebar('color',[0.3 0.3 0.3],'FontSize',font_size,'location','southeast');
% Add a title
title(datestr(matlab_datetime(min_time_diff_idx), 'HH.MM-dd-mm-yyyy'));
  9 Comments
Abigail Waring
Abigail Waring on 9 Jun 2021
@Mathieu NOE thats not an option as this is for my masters degree so I am learning on the way
Mathieu NOE
Mathieu NOE on 10 Jun 2021
hello
ok , so gain : what should be the value of contourspace ?

Sign in to comment.

Answers (1)

Shraddha Jain
Shraddha Jain on 14 Jun 2021
Hi Abigail,
You are getting the error Unrecognized function or variable 'contourspace' because the variables have not been defined before using them in your MATLAB script. You need to assign certain value to the variables, for example,
contourspace = 1;
contourmax = 10;
Once these variables are defined, you code below will execute with no errors,
convecpos=[0:contourspace:contourmax];
Refer to the documentation on Create and Edit Variables for more information.
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!