Dear Folks,
I am a newbie in netcdf, even in fortran77 programming.
I try to make a netcdf file in f77 to support my Phd research.
I modify an example f77 program as describe below:
==============================================
program convert_U1M_netcdf
implicit none
include '/home/endros/netcdf/include/netcdf.inc'
C This is the name of the data file we will create.
character*(*) FILE_NAME
parameter (FILE_NAME = 'home/endros/U1MP1d_MTH.nc')
integer ncid
C We are writing data, .....
C timesteps of data.
integer NDIMS, NRECS
parameter (NDIMS = 3)
integer NLATS, NLONS
parameter (NLATS = 620, NLONS = 866, NRECS = 120)
character*(*) LAT_NAME, LON_NAME, REC_NAME
parameter (LAT_NAME = 'y', LON_NAME = 'x')
parameter (REC_NAME = 'time')
integer lon_dimid, lat_dimid, lvl_dimid, rec_dimid, nrec_dimid
C The start and count arrays will tell the netCDF library where to
C write our data.
integer start(NDIMS), count(NDIMS)
C These program variables hold the latitudes and longitudes.
real lats(NLATS), lons(NLONS), day(NRECS)
integer lon_varid, lat_varid, lvl_varid, rec_varid
C We will create two netCDF variables, one each for temperature and
C pressure fields.
character*(*) ECM_NAME, NREC_NAME
parameter (ECM_NAME='ssu')
parameter (NREC_NAME='day')
integer ecm_varid, nrec_varid
integer dimids(NDIMS)
C We recommend that each variable carry a "units" attribute.
character*(*) UNITS
parameter (UNITS = 'units')
character*(*) ECM_UNITS,LAT_UNITS,LON_UNITS,REC_UNITS,NREC_UNITS
parameter(ECM_UNITS='days',REC_UNITS='months',NREC_UNITS='mths')
parameter (LAT_UNITS = 'degrees_north')
parameter (LON_UNITS = 'degrees_east')
character cyears*4, cmonths*2, cdays*2
C Program variables to hold the data we will write out.
real ecm_out(NLONS, NLATS)
C Use these to construct some latitude and longitude data for this
C example.
integer START_LAT, START_LON, START_DAY
parameter (START_LAT = 10.42, START_LON = 107.92)
parameter (START_DAY = 1)
C Loop indices.
integer rc, lat, lon, rec,iyy,isyy,ieyy,imm,i,j,tss,nrec
C Error handling.
integer retval
C Create pretend data. If this wasn't an example program, we would
C have some real data to write, for example, model output.
do lat = 1, NLATS
lats(lat) = START_LAT + (lat - 1) * (1/111.12)
end do
do lon = 1, NLONS
lons(lon) = START_LON + (lon - 1) * (1/111.12)
end do
do rc = 1, NRECS
day(rc) = START_DAY + (rc - 1)
end do
C Create the file.
retval = nf_create(FILE_NAME, nf_clobber, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the dimensions.
retval = nf_def_dim(ncid, LAT_NAME, NLATS, lat_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, LON_NAME, NLONS, lon_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, REC_NAME, NRECS, rec_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the coordinate variables.
retval = nf_def_var(ncid, LAT_NAME, NF_DOUBLE, 1, lat_dimid,
+ lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, LON_NAME, NF_DOUBLE, 1, lon_dimid,
+ lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Assign units attributes to coordinate variables.
retval = nf_put_att_text(ncid, lat_varid, UNITS, len(LAT_UNITS),
+ LAT_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, lon_varid, UNITS, len(LON_UNITS),
+ LON_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
C The dimids array is used to pass the dimids of the dimensions of
C the netCDF variables.
dimids(1) = lon_dimid
dimids(2) = lat_dimid
dimids(3) = rec_dimid
C Define the netCDF variables for the pressure and temperature data.
retval = nf_def_var(ncid, ECM_NAME, NF_DOUBLE, NDIMS, dimids,
+ ecm_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, NREC_NAME, NF_DOUBLE, NDIMS, dimids,
+ nrec_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Assign units attributes to the netCDF variables.
retval = nf_put_att_text(ncid, ecm_varid, UNITS, len(ECM_UNITS),
+ ECM_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, nrec_varid, UNITS, len(NREC_UNITS),
+ NREC_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
C End define mode.
retval = nf_enddef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Write the coordinate variable data.
retval = nf_put_var_real(ncid, lat_varid, lats)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_var_real(ncid, lon_varid, lons)
if (retval .ne. nf_noerr) call handle_err(retval)
C These settings tell netcdf to write one timestep of data.
count(1) = NLONS
count(2) = NLATS
count(3) = 1
start(1) = 1
start(2) = 1
C Write the pretend data.
c----collect variable output in (i,j)--------------
isyy=1993
ieyy=2002
do 9000 iyy=isyy,ieyy
do 8900 imm=1,12
write(cyears,'(i4.4)') iyy
write(cmonths,'(i2.2)') imm
print *,'proc: mo-',cmonths,'yr-',cyears
open(45,file='/work/endros/CSCR'
& //'/U1M_'
& //cyears//cmonths//'.dat'
& ,form='unformatted'
& ,access='direct'
& ,recl=NLONS*NLATS)
read(45,rec=1) ((ecm_out(i,j),i=1,NLONS),j=1,NLATS)
close(45)
print *, 'start saving netcdf...wait...!!!'
do rec=1,NRECS
start(3) = rec
retval = nf_put_vara_real(ncid, ecm_varid, start, count,
+ ecm_out)
if (retval .ne. nf_noerr) call handle_err(retval)
enddo
8900 enddo
9000 enddo
C Close the file.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
print *,'*** SUCCESS writing .....: ', FILE_NAME, '!'
end
subroutine handle_err(errcode)
implicit none
include '/home/endros/netcdf/include/netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
=========================================
Actually, this works well and I got a nc file. After I check the header
$ncdump -h U1MP1d_MTH.nc
netcdf U1MP1d_MTH {
dimensions:
y = 620 ;
x = 866 ;
time = 120 ;
variables:
double y(y) ;
y:units = "degrees_north" ;
double x(x) ;
x:units = "degrees_east" ;
double ssu(time, y, x) ;
ssu:units = "days" ;
double *day(time, y, x)* ;
day:units = "mths" ;
}
--->> a question is how to modify the above program in which I will get the
variable "day" is only depend on "time". I want to make *"day(time)"* only.
Sorry, My english limited that why i cannot really understand the example
f77 program.
Any helps and clues. I am really apreciate it.
Endro
Phd Student, Japan
--
View this message in context:
http://netcdf-group.1586084.n2.nabble.com/writing-netcdf-by-f77-tp7574833.html
Sent from the NetCDF Group mailing list archive at Nabble.com.