110 likes | 344 Views
NetCDF and binary read in MATLAB. Pierre Chien 2009/03/19. NetCDF in FORTRAN 77. NetCDF (network Common Data Form) is a set of software libraries and machine-independent data formats that support the creation, access, and sharing of array-oriented scientific data. Use NetCDF in MATLAB.
E N D
NetCDFand binary read in MATLAB Pierre Chien 2009/03/19
NetCDF (network Common Data Form) is a set of software libraries and machine-independent data formats that support the creation, access, and sharing of array-oriented scientific data
Use NetCDF in MATLAB • Two path were required to be added before using NetCDF • addpath mexnc • addpath matlab_netCDF_OPeNDAP • Two common way to read NetCDF • inqnc(‘file’) • To see what do they have in this nc file • getnc(‘file’,‘variable_name’); • To get the variable in the nc file
Use NetCDF in Linux • ncdump -h file • Output the title for this nc file • ncdump -v variable file • Output the value in this variable for this nc file
PROGRAM MAIN include ‘netcdf.inc’ INTEGER start(3),count(3),strid(3) INTEGER ncid,varid,status REAL TEMP(No,Na,itime) … DATA start /1,1,1/ DATA count /1,1,1/ DATA strid /1,1,1/ … status=nf_open(input, nf_nowrite, ncid) If (status .ne. nf_noerr)print *, nf_strerror(status) status = nf_inq_varid (ncid, ‘U10’, varid) status = nf_get_varS_real (ncid, varid, start, count, strid, TEMP) … nfclose = nf_close(ncid) Use NetCDF in FORTRAN
PROGRAM MAIN include ‘netcdf.inc’ INTEGER start(3),count(3) INTEGER ncid,varid,status REAL TEMP(No,Na,itime) … DATA start /1,1,1/ DATA count /1,1,1/ … status=nf_open(input, nf_write, ncid) status = nf_inq_varid (ncid, ‘U10’, varid) status = nf_get_varA_real (ncid, varid, start, count, TEMP) … status = nf_put_varA_real(ncid, varid, start, count, TEMP) … nfclose = nf_close(ncid) Write NetCDF in FORTRAN
ASCII file in FORTRAN OPEN(174, file='T2_test', form='formatted') WRITE(174,*) 'ITF=', ITF-1 DO J = 1, J0 write(174,999) ((T2(I,J,1)+T2(I,J,2))*.5, I = 1, I0) ENDDO 999 format(122f8.3)
Binary file in FORTRAN OPEN(175, file='T2_testa', form='unformatted') WRITE(175) ((REAL(T2(I,J,1)+T2(I,J,2))*.5,I=1,I0),J=1,J0)
Reading binary file in MATLAB INTSIZE4=‘int32’; REALSIZE = ‘real*4’; fid = fopen(‘file’,‘r’,‘b’); fread(fid,1,INTSIZE4) TEMP = fread(fid,I0*J0,REALSIZE); SST = reshape(TEMP,I0,J0); fread(fid,1,INTSIZE4) fclose(fid);