This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Hi Ruslan,
> I've got an issue with writing (and reading) 2-dimensional character
> string arrays in Fortran 90. I found an example how to do it in
> java but it's not quite helpful because the language concept is
> different. Could you provide me with the example for Fortran 90,
> please?
Sure, it looks like our documentation is really lacking in examples of
writing character data using the f90 interface. I'll put the example
inline rather than as an attachment, so it can be found more easily by
others searching for such examples in the future.
----------------------------------
program chartst
use netcdf
integer :: ncid, var1id, var2id, dim1id, dim2id;
character (len = 1), dimension (15) :: ch = (/'a', 'b', 'c', 'd', 'e', &
'f', 'g', 'h', 'i', 'j', &
'k', 'l', 'm', 'n', 'o' /)
character (len = 15) :: ch15 = "abcdefghijklmno";
print *, 'Start: ', ch
call check(nf90_create('CharTest.nc', nf90_clobber, ncid))
call check(nf90_def_dim(ncid, 'Dim1', 5, dim1id))
call check(nf90_def_dim(ncid, 'Dim2', 3, dim2id))
call check(nf90_def_var(ncid, 'Var1', nf90_char, (/dim1id, dim2id/), var1id))
call check(nf90_def_var(ncid, 'Var2', nf90_char, (/dim2id, dim1id/), var2id))
call check(nf90_enddef(ncid))
call check(nf90_put_var(ncid, var1id, ch, start=(/ 1, 1/), count=(/ 5, 3 /)))
call check(nf90_put_var(ncid, var2id, ch15, start=(/ 1, 1/), count=(/ 3, 5 /)))
call check(nf90_close(ncid))
print *, 'End: ', dims
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program chartst
----------------------------------
When you compile and run the program above, it creates CharTest.nc.
Here's the result of running "ncdump CharTest.nc":
$ ncdump CharTest.nc
netcdf CharTest {
dimensions:
Dim1 = 5 ;
Dim2 = 3 ;
variables:
char Var1(Dim2, Dim1) ;
char Var2(Dim1, Dim2) ;
data:
Var1 =
"abcde",
"fghij",
"klmno" ;
Var2 =
"abc",
"def",
"ghi",
"jkl",
"mno" ;
}
--Russ
Russ Rew UCAR Unidata Program
address@hidden http://www.unidata.ucar.edu
Ticket Details
===================
Ticket ID: AOX-617597
Department: Support netCDF
Priority: Normal
Status: Closed