Net CDF and binary read in MATLAB Pierre
Net. CDF and binary read in MATLAB Pierre Chien 2009/03/19
Net. CDF in FORTRAN 77
• Net. CDF (network Common Data Form) is a set of software libraries and machineindependent data formats that support the creation, access, and sharing of arrayoriented scientific data
Use Net. CDF in MATLAB • Two path were required to be added before using Net. CDF – addpath mexnc – addpath matlab_net. CDF_OPe. NDAP • Two common way to read Net. CDF – 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 Net. CDF 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
Use Net. CDF in FORTRAN 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, ‘U 10’, varid) status = nf_get_var. S_real (ncid, varid, start, count, strid, TEMP) … nfclose = nf_close(ncid)
Write Net. CDF 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, ‘U 10’, varid) status = nf_get_var. A_real (ncid, varid, start, count, TEMP) … status = nf_put_var. A_real(ncid, varid, start, count, TEMP) … nfclose = nf_close(ncid)
Read and Write binary file in MATLAB
ASCII file in FORTRAN OPEN(174, file='T 2_test', form='formatted') WRITE(174, *) 'ITF=', ITF-1 DO J = 1, J 0 write(174, 999) ((T 2(I, J, 1)+T 2(I, J, 2))*. 5, I = 1, I 0) ENDDO 999 format(122 f 8. 3)
Binary file in FORTRAN OPEN(175, file='T 2_testa', form='unformatted') WRITE(175) ((REAL(T 2(I, J, 1)+T 2(I, J, 2))*. 5, I=1, I 0), J=1, J 0)
Reading binary file in MATLAB INTSIZE 4=‘int 32’; REALSIZE = ‘real*4’; fid = fopen(‘file’, ‘r’, ‘b’); fread(fid, 1, INTSIZE 4) TEMP = fread(fid, I 0*J 0, REALSIZE); SST = reshape(TEMP, I 0, J 0); fread(fid, 1, INTSIZE 4) fclose(fid);
- Slides: 11