使用gFortran 进行NetCDF文件的读取,需要netcdf库的支持。
检查netcdf是否可用:

shell
$ ncdump  

如果提示不存在命令,就需要自己安装了,可以参考Compilling WRF 安装步骤。
Note: 此处介绍的安装方法可能不再适用,请参考最新的NETCDF/HDF5安装说明 — 2023-11-15

  • 使用F90脚本读取NC文件
    脚本内容如下,读取Hamawari-8 L2CLP文件的经纬度参数:
fortran
include 'netcdf.inc'

!!main
character*100 fnameout,tmpf,h8file,geoh8
logical alive

!!!!H-8 参数
integer iopen,ioquire,iovar,latid,lonid,ncid,varid,len_file,ioclose
integer, parameter :: dize=2401!定义维数
real*4 latitude(dize),longitude(dize)


!!! open one h8-file for geolocation extraction
geoh8="useful.h8/NC_H08_20150704_0820_FLDK.RealNC.nc" !!非常普通的一个H8文件而已
iopen=nf_open(trim(adjustl(geoh8)),nf_nowrite,ncid) !打开netcdf文件,获取文件的ID号(ncid)
if (iopen .ne. 0) then
print*,"H8-Geolocation extraction: invalid H8 file, open failure!"
stop
end if

ioquire=nf_inq_varid (ncid, 'latitude', latid)
ioquire=ioquire+nf_inq_varid (ncid, 'longitude', lonid)
if (ioquire .ne. 0) then
print*,"H8-Geolocation extraction: variable not found in H8 file!"
stop
end if

iovar=nf_get_var_real(ncid,latid,latitude)
iovar=iovar+nf_get_var_real(ncid,lonid,longitude)
if (iovar .ne. 0) then
print*,"H8-Geolocation extraction: variable extract failed!"
stop
end if
iopen=nf_close(ncid)
end

  • 运行脚本
    shell
    $bash bash.sh #编译
    $./read.h8.geo.exe

bash.sh文件内容如下:

shell
gfortran read.h8.geo.f90 -I/home/hjh/netcdf/include  -o read.h8.geo.exe -L/home/hjh/netcdf/lib -lnetcdff -lnetcdf