program main
include 'netcdf.inc'
* error status return
integer stat
* netCDF ncid
integer ncid
* dimension lengths
integer lat_len
parameter (lat_len = 6)
integer lon_len
parameter (lon_len = 4)
integer time_len
parameter (time_len = NF_UNLIMITED)
* dimension ids
integer lat_dim
integer lon_dim
integer time_dim
* variable ids
integer lat_id;
integer lon_id;
integer time_id;
integer pr_id;
* rank (number of dimensions) for each variable
integer lat_rank
parameter (lat_rank = 1)
integer lon_rank
parameter (lon_rank = 1)
integer time_rank
parameter (time_rank = 1)
integer pr_rank
parameter (pr_rank = 3)
* variable shapes
integer lat_dims(lat_rank)
integer lon_dims(lon_rank)
integer time_dims(time_rank)
integer pr_dims(pr_rank)
* variable declarations
real lat(6)
real lon(4)
* attribute vectors
integer textval(1)
* enter define mode
stat = nf_create('mslp.nc', nf_clobber, ncid);
call check_err(stat)
* define dimensions
stat = nf_def_dim(ncid, 'lat', lat_len, lat_dim);
call check_err(stat)
stat = nf_def_dim(ncid, 'lon', lon_len, lon_dim);
call check_err(stat)
stat = nf_def_dim(ncid, 'time', time_len, time_dim);
call check_err(stat)
* define variables
lat_dims(1) = lat_dim
stat = nf_def_var(ncid, 'lat', nf_float, lat_rank, lat_dims, lat_i
1d);
call check_err(stat)
lon_dims(1) = lon_dim
stat = nf_def_var(ncid, 'lon', nf_float, lon_rank, lon_dims, lon_i
1d);
call check_err(stat)
time_dims(1) = time_dim
stat = nf_def_var(ncid, 'time', nf_double, time_rank, time_dims, t
1ime_id);
call check_err(stat)
pr_dims(1) = lon_dim
pr_dims(2) = lat_dim
pr_dims(3) = time_dim
stat = nf_def_var(ncid, 'pr', nf_float, pr_rank, pr_dims, pr_id);
call check_err(stat)
* assign global attributes
* define title
stat = nf_put_att_text(ncid, NF_GLOBAL, 'title', 20, 'example for
1workshop')
call check_err(stat)
* assign per-variable attributes
* define units
stat = nf_put_att_text(ncid, lat_id, 'units', 13, 'degrees_north')
call check_err(stat)
* define units
stat = nf_put_att_text(ncid, lon_id, 'units', 12, 'degrees_east')
call check_err(stat)
* define units
stat = nf_put_att_text(ncid, time_id, 'units', 24, 'seconds since
12009-01-01')
call check_err(stat)
* define standard_name
stat = nf_put_att_text(ncid, pr_id, 'standard_name', 25, 'air_pres
1sure_at_sea_level')
call check_err(stat)
* define units
stat = nf_put_att_text(ncid, pr_id, 'units', 3, 'hPa')
call check_err(stat)
* leave define mode
stat = nf_enddef(ncid);
call check_err(stat)
* assign scalar and fixed dimension variable data
* perform variable data writes
call write_lat(ncid,lat_id)
call write_lon(ncid,lon_id)
call write_time(ncid,time_id)
call write_pr(ncid,pr_id)
stat = nf_close(ncid)
call check_err(stat)
end
subroutine write_lat(ncid,lat_id)
integer ncid
integer lat_id
include 'netcdf.inc'
integer stat
integer lat_start(1)
integer lat_count(1)
real lat(6)
data lat /25, 30, 35, 40, 45, 50/
lat_start(1) = 1
lat_count(1) = 6
stat = nf_put_vara_real(ncid, lat_id, lat_start, lat_count, lat)
call check_err(stat)
end
subroutine write_lon(ncid,lon_id)
integer ncid
integer lon_id
include 'netcdf.inc'
integer stat
integer lon_start(1)
integer lon_count(1)
real lon(4)
data lon /-125, -110, -95, -80/
lon_start(1) = 1
lon_count(1) = 4
stat = nf_put_vara_real(ncid, lon_id, lon_start, lon_count, lon)
call check_err(stat)
end
subroutine write_time(ncid,time_id)
integer ncid
integer time_id
include 'netcdf.inc'
integer stat
integer time_start(1)
integer time_count(1)
double precision time(2)
data time /7776000, 15552000/
time_start(1) = 1
time_count(1) = 2
stat = nf_put_vara_double(ncid, time_id, time_start, time_count, t
1ime)
call check_err(stat)
end
subroutine write_pr(ncid,pr_id)
integer ncid
integer pr_id
include 'netcdf.inc'
integer stat
integer pr_start(3)
integer pr_count(3)
real pr(4,6,2)
data pr /900.5, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910,
1911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 9
172, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 98
15, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995/
pr_start(1) = 1
pr_start(2) = 1
pr_start(3) = 1
pr_count(1) = 4
pr_count(2) = 6
pr_count(3) = 2
stat = nf_put_vara_real(ncid, pr_id, pr_start, pr_count, pr)
call check_err(stat)
end
subroutine check_err(stat)
integer stat
include 'netcdf.inc'
if (stat .ne. NF_NOERR) then
print *, nf_strerror(stat)
stop
endif
end