从Fortran的netcdf文件中难以读取2D字符数组变量

时间:2018-07-05 11:34:20

标签: arrays fortran character netcdf4

我有以下fortran代码,用于读取二维字符数组变量:

program read_metar_station_from_rec
   use netcdf
   implicit none

  ! This is the name of the data file we will create.
   character (len = *), parameter :: FILE_NAME = "metar.nc"

   character (len = *), parameter :: STA_NAME = "stationName"
   integer, dimension(nf90_max_var_dims) :: dimIDs
   integer :: ncid
   integer :: nRecords,maxStaNamLen
   integer :: station_varid
   integer :: i,j
   character, allocatable :: stations(:,:)

  ! Create the netCDF file. The nf90_clobber parameter tells netCDF to
  ! overwrite this file, if it already exists.
  ! Open the file. 
  call check( nf90_open(FILE_NAME, nf90_nowrite, ncid) )

  ! Get the varid of stationName.
  call check( nf90_inq_varid(ncid, STA_NAME, station_varid) )

  ! Get dimensions of stationName.
  call check( nf90_inquire_variable(ncid, station_varid, dimids = dimIDs))
  call check( nf90_Inquire_Dimension(ncid, dimIDs(1), len = maxStaNamLen))
  call check( nf90_Inquire_Dimension(ncid, dimIDs(2), len = nRecords) )

  allocate(stations(maxStaNamLen,nRecords))
  call check( nf90_get_var(ncid, station_varid, stations) )
  do i=1,maxStaNamLen
    do j=1,100
        print *, stations(i,j)
    enddo
  enddo
  ! Close the file. This frees up any internal netCDF resources
  ! associated with the file, and flushes any buffers.
  call check( nf90_close(ncid) )

  print *, "*** SUCCESS reading example file metar.nc! "

contains
  subroutine check(status)
    integer, intent ( in) :: status

    if(status /= nf90_noerr) then 
      print *, trim(nf90_strerror(status))
      stop "Stopped"
    end if
  end subroutine check  
end program read_metar_station_from_rec

我面临的问题是,假设我尝试打印前5条记录,然后仅看到5个字符,如下图所示:

program output 如果我增加记录的数量,那么我会看到相同的5个字符,但它们之间的空行更多。 stationName变量的实际条目在python中如下所示: variable to be read

我在此附上我要读取的数据的链接: netcdf file

如果问题不清楚,请让我知道。

0 个答案:

没有答案