How to read netCDF file ?

l downloaded netCDF master. zip and GEBCO_2020.nc file.
I want to read this netCDF file and plot. But l met this error. What should I do?
Error using internal.matlab.imagesci.nc/openToRead (line 1272)
Could not open GEBCO_2020.nc for reading.
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncdisp (line 62)
ncObj = internal.matlab.imagesci.nc(ncFile);
Error in read_necdf (line 12)
ncdisp(file)
Below is the code I used.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; close all;
% set(0,'Units','pixels');
% scnsize = get(0,'ScreenSize');
% pos1 = [10, 10, 1910, 1070];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
file = 'GEBCO_2020.nc';
%
ncdisp(file)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
time = ncread(file,'time')
%
shot = ncread(file,'shot');
depth = ncread(file,'depth');
SS = ncread(file,'SS',[1 1],[Inf Inf]);
% SS = ncread(file,'temp',[1 1],[Inf Inf]);
% SS = ncread(file,'sal',[1 1],[Inf Inf]);
figure(1);
plot(shot);
figure(3);
plot(depth);
% %
% SS_struct = struct( 'shot', shot, 'depth', depth, 'SS', SS );
% SS_mod = struct('SS',SS_struct);
%
% [xx,yy] = meshgrid(SS_mod.SS_struct.shot,SS_mod.SS_struct.depth);
% zz = SS_mod.SS_struct.SS_struct(:,:,1);
figure(2);
% pcolor(double(xx),double(yy),double(zz))
pcolor( double(SS) );
% caxis([1500 1520]);
colorbar('location','EastOutside'); set(gca,'linewidth',1,'fontsize',16);
shading flat; axis('ij');

7 Comments

Either it could not find the file on the MATLAB path, or else it found the file but you do not have read permission for the file.
What does this line show?
ncdisp(file)
I am downloading the file from https://www.gebco.net/data_and_products/gridded_bathymetry_data/ but it is a bit slow...
ncdisp.m file in the netCDP master.zip. Do you want i show you thid code?
@Walter Roberson
yes this file is 7GB😂
I went to https://www.gebco.net/data_and_products/gridded_bathymetry_data/ and downloaded https://www.bodc.ac.uk/data/open_download/gebco/gebco_2020/zip/ which contains gebco_2020_netcdf.zip which contains a directory gebco_2020_netcdf which contains two .pdf files and GEBCO_2020.nc . The .zip is 4 Gigabytes, expanding to 7 gigabytes.
However.. that nc file does not have any time information, only lat, lon, and elevation.
Which file are you looking at?
Ah... I just thught this funtion reads my netCDF file and plots figure.
I don't need time information.
So l delete time = ncread(file,'time') ?

Sign in to comment.

 Accepted Answer

KSSV
KSSV on 4 May 2021

0 votes

4 Comments

Thank you !
But l met error...
Error: File: gebconetcdf.m Line: 1 Column: 46
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
I followed example's code. GEBCO_2020.nc file in the matlab folder.
l copied location of this file's properties.
% --Example--------------------------------------------------------------
%
% Retrieve the bathymetry data for the Baltic Sea:
%
% [BATHY XGRID YGRID] = gebconetcdf('C:\GISdata\gebco_08.nc',...
% +10.0, +26.0, +52.00, +66.00)
Below code has problem ?
function [BATHY, XGRID, YGRID] = gebconetcdf('C:\Users\wngid\OneDrive\5744~1-DESKTOP-2A49GC5\GEBCO_2020.nc', +10.0, +26.0, +52.00, +66.00);
%[BATHY, XGRID, YGRID] = gebconetcdf(FILE, Wlon, Elon, Slat, Nlat)
%
% This function opens a GEBCO bathymetry NetCDF file and retrieves data
% for a desired latitude/longitude window. Note: Be careful when
% selecting a very large area of the world (e.g. entire oceans) as the
% GEBCO data are stored as 16-bit and memory can run out quickly.
%
% Author: Bryan C. Lougheed. Date: May 2014.
% Written using MatLab version 2012a.
%
% --Input parameters-----------------------------------------------------
%
% FILE is a string specifying the location of the GEBCO bathymetry NetCDF
% file, e.g. 'gebco_08.nc'. Both the half-minute and one-minute datasets
% should be automatically recognised. HOWEVER, to avoid crash, it is best
% to use the half-minute NetCDF for the entire world, as this script is
% optimised for that one, and GEBCO aren't particularly consistent in how
% they design NetCDF files for different resolutions/regions.
%
% Wlon, Elon, Slat and Nlat are the decimal degree values of the
% western, eastern, southern and northern limits of the desired lat/lon
% window, whereby degrees N should be entered as positive, degrees S as
% negative, degrees W as negative and degrees E as positive. The nearest
% possible coordinates in the GEBCO bathymetry NetCDF file will be
% used.
%
% --Output data----------------------------------------------------------
%
% BATHY: Matrix containing the gridded elevation/bathymetry data for the
% desired lat/lon window.
%
% XGRID, YGRID: Matrices of same dimensions as BATHY, containing the
% longitude (XGRID) and latitude (YGRID) coordinates for each datapoint
% in BATHY. Coordinates are in decimal degrees and represent the
% centre-of-pixel coordinates.
%
% --Example--------------------------------------------------------------
%
% Retrieve the bathymetry data for the Baltic Sea:
%
% [BATHY XGRID YGRID] = gebconetcdf('C:\GISdata\gebco_08.nc',...
% +10.0, +26.0, +52.00, +66.00)
%
% Open the NetCDF file
gebconc = netcdf.open(FILE, 'GEBCO_2020.nc');
% Find information about the data contained within the file
west = netcdf.getVar(gebconc,0,0,1);
east = netcdf.getVar(gebconc,0,1,1);
north = netcdf.getVar(gebconc,1,1,1);
south = netcdf.getVar(gebconc,1,0,1);
resdata = netcdf.getVar(gebconc,3,0,1);
% All avaliable lat and lon coordinates in GEBCO file
cols = west+(resdata/2) : resdata : east-(resdata/2);
rows = north-(resdata/2) : -resdata : south+(resdata/2);
% find col and row indexes nearest to desired window
[~, start_col] = min(abs(cols - (Wlon+1*10e-100)));
[~, end_col] = min(abs(cols - (Elon-1*10e-100)));
[~, start_row] = min(abs(rows - (Nlat-1*10e-100)));
[~, end_row] = min(abs(rows - (Slat+1*10e-100)));
% Prep output matrix
BATHY = NaN(length(start_row:1:end_row),length(start_col:1:end_col));
% Read out data row by row
output_row = 0;
for i = start_row:1:end_row;
start_index = ((i-1)*length(cols)) + start_col - 1; %subtract one (zero based indexing in NetCDF)
count = length(start_col:1:end_col);
row_slice = netcdf.getVar(gebconc,5,start_index,count);
output_row = output_row+1;
BATHY(output_row,:) = row_slice(:,:);
end
% Close the NetCDF file
netcdf.close(gebconc);
% Make meshgrid
[XGRID YGRID] = meshgrid( cols(start_col):resdata:cols(end_col) , rows(start_row):-resdata:rows(end_row) );
end
You download the function and save it in a folder. The function will be saved on the name gebconetcdf.m. Now go to the folder, where this function is present. You have to call the function now. Call usig:
file = 'C:\Users\wngid\OneDrive\5744~1-DESKTOP-2A49GC5\GEBCO_2020.nc' ;
[BATHY, XGRID, YGRID] = gebconetcdf(file, +10.0, +26.0, +52.00, +66.00);
You are getting error because, you are inputting the values staright away to the function, this is not allowed.
Thank you!
Out of memory. The likely cause is an infinite recursion within the program.
l met this error..
Reduce the bounding box.... The data which you are trying to read is huge and your RAM cannot support that much size.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 4 May 2021

Commented:

on 5 May 2021

Community Treasure Hunt

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

Start Hunting!