fortran使用netcdf 读写NC文件



先安装VS2010
再安装 intel visual fortran2011

1、设置netcdf Path环境变量

去下载别人已编译好的netcdf库, http://bbs.lasg.ac.cn/bbs/viewthread.php?tid=955
NetCDF3.6.1Beta1.Win32.for.CVF.IVF.rar
为了简单操作,我将 NetCDF3.6.1Beta1.Win32.for.CVF.IVF.rar里面文件结构根据说明重新调整了一下。

解压到C盘
C:\NetCDF3.6.1Beta1.Win32.for.IVF
得到3个文件夹:
bin,lib,include, 开始设置visual studio。

将bin目录加入path
计算机——》右键,属性——》高级系统设置——》环境变量——》系统变量——》
在Path里面追加: C:\NetCDF3.6.1Beta1.Win32.for.IVF\bin;

注意修改path后,需要重启计算机或者先注销后登陆才会起作用。

2、设置Intel Visual Fortran Compiler环境

工具——》选项——》Intel Visual Fortran——》compilers
Libraries:
C:\NetCDF3.6.1Beta1.Win32.for.IVF\lib
Includes:
C:\NetCDF3.6.1Beta1.Win32.for.IVF\include

如图:


fortran使用netcdf 读写NC文件_第1张图片 fortran使用netcdf 读写NC文件_第2张图片

3、设置每个项目的依赖lib

项目上右键——》属性——》配置属性——》Linker——》General
Addtional Library Directories:
C:\NetCDF3.6.1Beta1.Win32.for.IVF\lib


4、读写文件Demo:

读NC文件:

program Console11
  use typeSizes
  use netcdf90
  implicit none
  include 'netcdf.inc'
    ! Variables
    character(len=50) filename
    integer ncid,ierr
    ! Body of Console3
    filename ='G:/si_monthly_1850-2012_r2.5x2.5.nc'
    print *, 'Hello World'
    
    ierr = nf90_open(filename,nf_nowrite,ncid)
    print *, ncid
end program Console11

写NC文件

! This program provides an elementary check of some of the parts of the 
!   Fortran 90 interface to netCDF 3.5. It is a Fortran 90 implementation
!   of the nctst.cpp program provided with the C++ interface to netcdf
!   (in the src/cxx directory of the netcdf distribution). 
!
program netcdfTest
  use typeSizes
  use netcdf90
  implicit none
  include 'netcdf.inc'
  
  ! netcdf related variables
  integer :: ncFileID,                                   &
             latDimID, lonDimID, frTimeDimID, timeDimID, &
             pressVarID, latVarID, lonVarID, frTimeVarID, refTimeVarID, scalarVarID
             
  ! Local variables
  integerparameter :: numLats = 4, numLons = 3, &
                        numFrTimes = 2, timeStringLen = 20
  character (len = *), parameter :: fileName = "example.nc"
  integer :: counter                      
  realdimension(numLons, numLats, numFrTimes) :: pressure
  integer (kind = FourByteInt), dimension(numFrTimes) :: frTimeVals
  real (kind = FourByteReal) fillVal;
  real (kind = FourByteReal), dimension(2) :: validRange;
  
  ! --------------------
  ! Code begins
  ! --------------------
  if(.not. byteSizesOK()) then
    print *, "Compiler does not appear to support required kinds of variables."
    stop
  end if
    
  ! Create the file
  call check(nf90_create(path = trim(fileName), cmode = nf90_clobber, ncid = ncFileID))
  
  ! Define the dimensions
  call check(nf90_def_dim(ncid = ncFileID, name = "lat",     len = numLats,        dimid = latDimID))
  call check(nf90_def_dim(ncid = ncFileID, name = "lon",     len = numLons,        dimid = lonDimID))
  call check(nf90_def_dim(ncid = ncFileID, name = "frtime",  len = nf90_unlimited, dimid = frTimeDimID))
  call check(nf90_def_dim(ncid = ncFileID, name = "timelen", len = timeStringLen,  dimid = timeDimID))
  ! Create variables and attributes
  call check(nf90_def_var(ncid = ncFileID, name = "P", xtype = nf90_float,     &
                     dimids = (/ lonDimID, latDimID, frTimeDimID /), varID = pressVarID) )
  call check(nf90_put_att(ncFileID, pressVarID, "long_name",   "pressure at maximum wind"))
  call check(nf90_put_att(ncFileID, pressVarID, "units",       "hectopascals") )
  ! Use 4-byte reals explicitly, to match 4-byte attribute type in test file
  validRange(1) = 0.
  validRange(2) = 1500
  call check(nf90_put_att(ncFileID, pressVarID, "valid_range", validRange))
  ! Use a 4-byte float constant, to match variable type
  fillVal = -9999.0
  call check(nf90_put_att(ncFileID, pressVarID,  "_FillValue", fillVal ) )
                      
  call check(nf90_def_var(ncFileID, "lat", nf90_float, dimids = latDimID, varID = latVarID) )
  call check(nf90_put_att(ncFileID, latVarID, "long_name""latitude"))
  call check(nf90_put_att(ncFileID, latVarID, "units""degrees_north"))
  call check(nf90_def_var(ncFileID, "lon", nf90_float, lonDimID, lonVarID) )
  call check(nf90_put_att(ncFileID, lonVarID, "long_name""longitude"))
  call check(nf90_put_att(ncFileID, lonVarID, "units",     "degrees_east"))
  call check(nf90_def_var(ncFileID, "frtime", nf90_int, frTimeDimID, frTimeVarID) )
  call check(nf90_put_att(ncFileID, frTimeVarID, "long_name""forecast time"))
  call check(nf90_put_att(ncFileID, frTimeVarID, "units",     "hours"))
  call check(nf90_def_var(ncFileID, "reftime", nf90_char, timeDimID, refTimeVarID) )
  call check(nf90_put_att(ncFileID, refTimeVarID, "long_name""reference time"))
  call check(nf90_put_att(ncFileID, refTimeVarID, "units",     "text_time"))
                     
  ! In the C++ interface the define a scalar variable - do we know how to do this? 
  call check(nf90_def_var(ncFileID, "ScalarVariable", nf90_real, scalarVarID))
  
  ! Global attributes
  call check(nf90_put_att(ncFileID, nf90_global, "history", &
                     "created by Unidata LDM from NPS broadcast"))
  call check(nf90_put_att(ncFileID, nf90_global, "title", &
                     "NMC Global Product Set: Pressure at Maximum Wind"))
  
  ! Leave define mode
  call check(nf90_enddef(ncfileID))
  
  ! Write the dimension variables
  call check(nf90_put_var(ncFileID, latVarId,     (/ -90., -87.5, -85., -82.5 /)) )
  call check(nf90_put_var(ncFileID, lonVarId,     (/ -180, -175, -170 /)      ) )
  ! Don't use anonymous array here, in case platform has 8-byte integers
  frTimeVals(1) = 12
  frTimeVals(2) = 18
  call check(nf90_put_var(ncFileID, frTimeVarId,  frTimeVals                  ) )
  call check(nf90_put_var(ncFileID, reftimeVarID, "1992-3-21 12:00"           ) )
  
  ! Write the pressure variable. Write a slab at a time to check incrementing.
  pressure = 949. + real(reshape( (/ (counter, counter = 1, numLats * numLons * numFrTimes) /),  &
                                    (/ numLons, numLats, numFrTimes /) ) )
  call check(nf90_put_var(ncFileID, pressVarID, pressure(:, :, 1:1)) )
  call check(nf90_put_var(ncFileID, pressVarID, pressure(:, :, 2:2), start = (/ 1, 1, 2 /)) )
  
  call check(nf90_put_var(ncFileID, scalarVarID, 10))
  call check(nf90_close(ncFileID))
contains
  ! Internal subroutine - checks error status after each netcdf, prints out text message each time
  !   an error code is returned. 
  subroutine check(status)
    integerintent ( in) :: status
    
    if(status /= nf90_noerr) then 
      print *, trim(nf90_strerror(status))
    end if
  end subroutine check  
end program netcdfTest


配置中出现问题

问题1:

1>LIBCMT.lib(invarg.obj) : error LNK2005: __initp_misc_invarg 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: __call_reportfault 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: __set_invalid_parameter_handler 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: __get_invalid_parameter_handler 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: "void __cdecl _invoke_watson(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invoke_watson@@YAXPBG00II@Z) 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: __invalid_parameter 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: "void __cdecl _invalid_parameter(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invalid_parameter@@YAXPBG00II@Z) 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LIBCMT.lib(invarg.obj) : error LNK2005: ___pInvalidArgHandler 已ò?经-在ú LIBCMTD.lib(invarg.obj) 中D定¨义ò?
1>LINK : warning LNK4098: 默?认è?库a“°LIBCMT”±与ó?其?他?库a的μ?使ê1用ó?冲3?突í?;£?请?使ê1用ó? /NODEFAULTLIB:library
1>Debug\Console1.exe : fatal error LNK1169: 找ò到μ?一ò?个?或ò多à个?多à重?定¨义ò?的μ?符·?号o?

解决办法:
项目上右键——》属性——》配置属性——》fortran——》General——》Input
Ignore specific library:LIBCMT.lib


问题2:

出现 "无法定位程序输入点_NF_ABORT@4于动态链接库netcdf.dll"
或 "无法启动此程序,因为计算机中丢失netcdf.dll ..."

fortran使用netcdf 读写NC文件_第3张图片 fortran使用netcdf 读写NC文件_第4张图片


解决办法:
将C:\NetCDF3.6.1Beta1.Win32.for.IVF\bin加入系统环境变量Path中
或者 C:\NetCDF3.6.1Beta1.Win32.for.IVF\bin\netcdf.dll 拷贝到工程的Debug目录下

注意将bin路径加入path后,需要重启计算机或者先注销后登陆才会起作用。

参考:
http://bbs.lasg.ac.cn/bbs/viewthread.php?tid=955
http://blog.sciencenet.cn/blog-1245939-765800.html

你可能感兴趣的:(fortran)