program simple_xy_wr implicit none include 'netcdf.inc' character*(*) FILE_NAME parameter (FILE_NAME='simple_xy.nc') integer NDIMS parameter (NDIMS=2) integer NX, NY parameter (NX = 6, NY = 12) integer ncid, varid, dimids(NDIMS) integer x_dimid, y_dimid integer data_out(NY, NX) integer x, y, retval do x = 1, NX do y = 1, NY data_out(y, x) = (x - 1) * NY + (y - 1) end do end do retval = nf_create(FILE_NAME, NF_CLOBBER, ncid) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_def_dim(ncid, "x", NX, x_dimid) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_def_dim(ncid, "y", NY, y_dimid) if (retval .ne. nf_noerr) call handle_err(retval) C The dimids array is used to pass the IDs of the dimensions of C the variables. Note that in fortran arrays are stored in C column-major format. dimids(2) = x_dimid dimids(1) = y_dimid retval = nf_def_var(ncid, "data", NF_INT, NDIMS, dimids, varid) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_enddef(ncid) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_put_var_int(ncid, varid, data_out) if (retval .ne. nf_noerr) call handle_err(retval) retval = nf_close(ncid) if (retval .ne. nf_noerr) call handle_err(retval) end subroutine handle_err(errcode) implicit none include 'netcdf.inc' integer errcode print *, 'Error: ', nf_strerror(errcode) stop 2 end