c f77 -o global_merged2_rd -I/usr/local/netcdf/include -O global_merged2_rd.f /usr/local/netcdf/lib/libnetcdf.a program global_merged2_rd implicit none include 'netcdf.inc' ! found in /usr/include C This is the name of the data file we will read. character*(*) FILE_NAME parameter (FILE_NAME='global_merged2.nc') integer ncid C We are reading 2D data. integer NDIMS parameter (NDIMS=2) integer NLATS, NLONS parameter (NLATS = 5400, NLONS = 10800) character*(*) LAT_NAME, LON_NAME parameter (LAT_NAME='latitude', LON_NAME='longitude') integer lat_dimid, lon_dimid C For the lat lon coordinate netCDF variables. real lats(NLATS), lons(NLONS) integer lat_varid, lon_varid C We will read depth. character*(*) DEP_NAME parameter (DEP_NAME='depth') integer dep_varid integer dimids(NDIMS) C To check the units attributes. character*(*) UNITS parameter (UNITS = 'units') character*(*) DEP_UNITS, LAT_UNITS, LON_UNITS parameter (DEP_UNITS = 'meters') parameter (LAT_UNITS = 'degrees_north') parameter (LON_UNITS = 'degrees_east') integer MAX_ATT_LEN parameter (MAX_ATT_LEN = 80) character*(MAX_ATT_LEN) dep_units_in character*(MAX_ATT_LEN) lat_units_in, lon_units_in integer att_len C Read the data into these arrays. real dep_in(NLONS, NLATS) C Loop indices integer lat, lon C Error handling integer retval C Open the file. retval = nf_open(FILE_NAME, nf_nowrite, ncid) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_inq_varid(ncid, LAT_NAME, lat_varid) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_inq_varid(ncid, LON_NAME, lon_varid) if (retval .ne. nf_noerr) call handle_err(retval) C Read the latitude and longitude data. retval = nf_get_var_real(ncid, lat_varid, lats) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_get_var_real(ncid, lon_varid, lons) if (retval .ne. nf_noerr) call handle_err(retval) C Get the varids of the depth netCDF variable. retval = nf_inq_varid(ncid, DEP_NAME, dep_varid) if (retval .ne. nf_noerr) call handle_err(retval) C Read the depth data from the file. C Since we know the contents of the file we know that the data C arrays in this program are the correct size to hold all the data. retval = nf_get_var_real(ncid, dep_varid, dep_in) if (retval .ne. nf_noerr) call handle_err(retval) C Close the file. This frees up any internal netCDF resources C associated with the file. retval = nf_close(ncid) if (retval .ne. nf_noerr) call handle_err(retval) open(100,file='global_merged2.out') C Write the data. do lon = 1, NLONS do lat = 1, NLATS write(100,'(2f10.4,f9.1)')lons(lon),lats(lat),dep_in(lon, lat) end do end do C If we got this far, everything worked as expected. Yipee! print *,'*** SUCCESS writing file global_merged2.out!' end subroutine handle_err(errcode) implicit none include 'netcdf.inc' integer errcode print *, 'Error: ', nf_strerror(errcode) stop 2 end