% Numerical Modelling in Geosciences % by Manuele Faccenda, Università di Padova % % Practice 1b % % Produce, plot, save, load 2D data clear all; close all; %% Produce 2D data x=-10:0.1:10; y=-10:0.1:10; xnum=size(x,2); ynum=size(y,2); z=zeros(xnum,ynum); for i = 1:xnum for j = 1:ynum z(i,j) = sin(x(i))+sin(y(j)); end end %Plot 2D data figure(1) subplot(2,2,1) %pcolor(x,y,z) surf(x,y,z) shading interp colorbar title('Plot function z') xlabel('X') ylabel('Y') %% Text files: human readable %Save data in ascii format to text file fid=fopen('test.txt','w'); fprintf(fid,'%d \n',xnum); fprintf(fid,'%d \n',ynum); for i = 1:xnum fprintf(fid,'%e \n',x(i)); end for i = 1:ynum fprintf(fid,'%e \n',y(i)); end for i = 1:xnum for j = 1:ynum fprintf(fid,'%e \n',z(i,j)); end end fclose(fid); clear all; %Read data in ascii format from text file fid=fopen('test.txt','r'); xnum=fscanf(fid,'%d',1); ynum=fscanf(fid,'%d',1); x=zeros(1,xnum); for i = 1:xnum x(i)=fscanf(fid,'%e',1); end y=zeros(1,ynum); for i = 1:ynum y(i)=fscanf(fid,'%e',1); end z1=zeros(xnum,ynum); for i = 1:xnum for j = 1:ynum z1(i,j)=fscanf(fid,'%e',1); end end fclose(fid); subplot(2,2,2) %pcolor(x,y,z1) surf(x,y,z1) shading interp colorbar title('Read and plot z1') xlabel('X') ylabel('Y') %% Binary files: machine readable, much faster than in ascii format %Save data in binary format fid=fopen('test.bin','w'); fwrite(fid,xnum,'uint8'); fwrite(fid,ynum,'uint8'); for i = 1:xnum fwrite(fid,x(i),'single'); end for i = 1:ynum fwrite(fid,y(i),'single'); end for i = 1:xnum for j = 1:ynum fwrite(fid,z1(i,j),'single'); end end fclose(fid); clear all; %Read data in binary format fid=fopen('test.bin','r'); xnum=fread(fid,1,'uint8'); ynum=fread(fid,1,'uint8'); x=zeros(1,xnum); for i = 1:xnum x(i)=fread(fid,1,'single'); end y=zeros(1,ynum); for i = 1:ynum y(i)=fread(fid,1,'single'); end z2=zeros(xnum,ynum); for i = 1:xnum for j = 1:ynum z2(i,j)=fread(fid,1,'single'); end end fclose(fid); subplot(2,2,3) %pcolor(x,y,z2) surf(x,y,z2) shading interp colorbar title('Read and plot z2 ') xlabel('X') ylabel('Y') %% HDF5: extremely useful when many different datasets must be saved/loaded % Path where to save/load data must be specified. It can be used also in C % and Fortran codes %Save data in binary format to a hdf5 file hdf5write('test.h5','/xnum',xnum) hdf5write('test.h5','/ynum',ynum,'WriteMode','append') hdf5write('test.h5','/Coordinates/x',x,'WriteMode','append') hdf5write('test.h5','/Coordinates/y',y,'WriteMode','append') hdf5write('test.h5','/Functions/z',z2,'WriteMode','append') clear all; %Read data from a hdf5 file xnum=hdf5read('test.h5','/xnum'); ynum=hdf5read('test.h5','/ynum'); x=hdf5read('test.h5','/Coordinates/x'); y=hdf5read('test.h5','/Coordinates/y'); z3=hdf5read('test.h5','/Functions/z'); subplot(2,2,4) %pcolor(x,y,z3) surf(x,y,z3) shading interp colorbar title('Read and plot z3') xlabel('X') ylabel('Y') %Save image filename = 'test.png'; printmod = 1; if printmod>0 print ('-dpng', '-r300','-zbuffer ',filename); end %% MatLab save/load functions: extremely easy to use, no need to specify path, but works only on MatLab %Save data in bynary MatLab-file (but also in ascii format to text file, see manual) save('test','z3','xnum','ynum') clear all; load('test','xnum') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% %%% %%% Homework %%% %%% %%% %%% %%% %%% Study all the new functions by using the MatLab manual, many different %%% %%% options are associated with these functions. Exercise producing/saving/ %%% %%% %%% loading/plotting 2D data %%% %%% %%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%