Skip to main content

9.10) Obtaining Data From The Web: Detailed Example


We want to download the ETOPO2 global topography dataset (in NetCDF format), uncompress it, load the data and then save the UK topography to a NetCDF file. To understand all of the commands in this example, you will need to have read about NetCDF Files.
[matlab]
% First let's download the NetCDF from the website and unzip it
full_url='http://www.ngdc.noaa.gov/mgg/global/relief/ETOPO2/ETOPO2v2-2006/ETOPO2v2c/netCDF/'
filename='ETOPO2v2c_F4_netcdf.zip';
urlwrite([full_url, filename], filename);
unzip(filename)
filename=[filename(1,1:end-11), '.nc']; % Let's remove '_netcdf.zip' from the end of the filename and and '.nc'
% Now let's load the NetCDF and extract the UK topography to an array
ncdisp(filename)
topo=ncread(filename, 'z');
UK_topo=topo(5000:5500,4150:4600);
% Let's also extract the longitude and latitude vectors
longitude=ncread(filename, 'x');
latitude=ncread(filename, 'y');
% Now crop the longitude and latitude vectors to correspond to the UK coverage
longitude=longitude(5000:5500, 1);
latitude=latitude(4150:4600, 1);
% Now let's save the UK topography to a NetCDF file
out_filename='uk.nc';
nccreate(out_filename, 'UK_topo', 'Dimensions', {'lon' 501 'lat' 451};, 'Format', 'classic);
ncwrite(out_filename, 'UK_topo', UK_topo);
[/matlab]