I have a netCDF 4.4 file which has four dimensions- time, latitude,longitude, level. I want to be able to read the latitude and longitude values from this file and store them in a single dimensional array. However I do not know the size of the latitude array or longitude array and I so cannot allocate the size at the time of the declaration of the two arrays lat and lon. Each netCDF file that will be processed will have it's own lat and lon size and so it is not possible to declare this at compile time. So I did declare this using Fortran's allocate but this does not do what I want it to do. It prints 0 as the values of lats and lons. Where am I going wrong ?
Variables for input Z file : height level
character*80 in_cfn,varname
integer reason,i,in_ndim,ierr
integer ndims_in, nvars_in, ngatts_in, unlimdimid_in
integer lat_varid,lon_varid
character*(*) LAT_NAME, LON_NAME
parameter (LAT_NAME='lat', LON_NAME='lon')
real,allocatable, dimension(:) :: lats
real,allocatable, dimension(:) :: lons
call system('ls hgt_*.nc > hgtFiles.txt')
open(10,file='hgtFiles.txt',action="read")
varname = "hgt"
do
read(10,*,IOSTAT=reason) in_cfn
if (reason/=0) EXIT
print *,in_cfn
retval = nf_open(in_cfn,NF_NOWRITE,ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq(ncid,ndims_in,nvars_in,ngatts_in,unlimdimid_in)
retval = nf_inq_varid(ncid,LAT_NAME,lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_varid(ncid, LON_NAME, lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_get_var_real(ncid, lat_varid, lats)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_get_var_real(ncid, lon_varid, lons)
print *,size(lons)
end do
close(10)
stop
end