爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1944|回复: 1

[源代码] create netcdf file by fortran 90

[复制链接]
发表于 2013-10-25 21:36:01 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 寒江雪(王训 于 2013-10-26 13:49 编辑

Recently, preparation the data for transport model. So, sharing a script tp creat netcdf file by fortran 90
Enjoy it!


module global
       implicit none
       integer,parameter :: south_north=97,west_east=164
end module

program main
        use netcdf
        use global
        implicit none

        integer :: i,j
        real :: tmp1(south_north*west_east),tmp2(south_north*west_east)

        ! This is the name of data file we will creat
        character(len=100) :: FILE_NAME="landuse_area.nc"
        integer :: ncid
        
        ! we are wrting 2D data, a 97x164 lat-lon grid. 2 dimensions are needed
        integer, parameter :: NDIMS=2
        character (len =100), parameter :: LAT_NAME = "latitude"
        character (len =100), parameter :: LON_NAME = "longitude"
        integer, parameter :: NLATS=south_north,NLONS=west_east
        integer :: lat_dimid, lon_dimid
      
        ! We will write surface 4 vals  fields
        character (len=100),parameter :: RP_NAME="RP"  
        character (len=100),parameter :: PRP_NAME="PRP"  
        character (len=100),parameter :: DL_NAME="DL"  
        character (len=100),parameter :: PDL_NAME="DLP"  
        integer :: rp_varid,prp_varid,dl_varid,pdl_varid
        integer :: dimids(NDIMS)
         
        ! we will create some "units" attribute
        character (len = 100), parameter :: UNITS = "units"
        character (len = 100), parameter :: RP_UNITS = "m2"
        character (len = 100), parameter :: DL_UNITS = "m2"
        character (len = 100), parameter :: PRP_UNITS = "1"
        character (len = 100), parameter :: PDL_UNITS = "1"

        ! we will create some "long_name" attribute
        character (len = 100), parameter :: LONG_NAME = "long_name"
        character (len = 100), parameter :: RP_LNAME = "rice paddy area in each cell"
        character (len = 100), parameter :: DL_LNAME = "dry land area in each cell"
        character (len = 100), parameter :: PRP_LNAME = "precent of rice paddy area in each cell"
        character (len = 100), parameter :: PDL_LNAME = "precent of dry land area in each cell"

        ! We will create values.
        real :: rice_paddy(NLONS, NLATS), dry_land(NLONS, NLATS)
        real :: p_rice_paddy(NLONS, NLATS), p_dry_land(NLONS, NLATS)

        ! obtain the data from the csv file
        open(12,file='dry_land.csv')
        read(12,*) tmp1
        close(12)
      
        open(11,file='rice_paddy_area.csv')
        read(11,*) tmp2
        close(11)
        
        ! transform the data
        i=1
        j=1
        do i=1,97,1
           do j=1,164,1
              dry_land(j,i)=tmp1(west_east*(i-1)+j)
              rice_paddy(j,i)=tmp2(west_east*(i-1)+j)
           end do
        end do

        ! calculate the  landuse per cent in each grid cell
        p_rice_paddy=rice_paddy/(36000*36000)
        p_dry_land=dry_land/(36000*36000)

        ! Create the file.
        call check( nf90_create(FILE_NAME, nf90_clobber, ncid) )

        ! Define the dimensions.
        call check( nf90_def_dim(ncid, LAT_NAME, NLATS, lat_dimid) )
        call check( nf90_def_dim(ncid, LON_NAME, NLONS, lon_dimid) )


        ! Define the netCDF variables. The dimids array is used to pass the
        ! dimids of the dimensions of the netCDF variables.
        dimids = (/ lon_dimid, lat_dimid /)
        call check( nf90_def_var(ncid, RP_NAME, NF90_REAL, dimids, rp_varid) )
        call check( nf90_def_var(ncid, PRP_NAME, NF90_REAL, dimids,prp_varid) )
        call check( nf90_def_var(ncid, DL_NAME, NF90_REAL, dimids, dl_varid) )
        call check( nf90_def_var(ncid, PDL_NAME, NF90_REAL, dimids,pdl_varid) )

        ! Assign units attributes to the variables.
        call check( nf90_put_att(ncid, rp_varid, UNITS, RP_UNITS) )
        call check( nf90_put_att(ncid, prp_varid, UNITS, PRP_UNITS) )
        call check( nf90_put_att(ncid, dl_varid, UNITS, DL_UNITS) )
        call check( nf90_put_att(ncid, pdl_varid, UNITS, PDL_UNITS) )

        ! Assign longname  attributes to the variables.
        call check( nf90_put_att(ncid, rp_varid, LONG_NAME, RP_LNAME) )
        call check( nf90_put_att(ncid, prp_varid,LONG_NAME, PRP_LNAME) )
        call check( nf90_put_att(ncid, dl_varid, LONG_NAME, DL_LNAME) )
        call check( nf90_put_att(ncid, pdl_varid,LONG_NAME, PDL_LNAME) )

        ! End define mode.
        call check( nf90_enddef(ncid) )


        ! Write  the netCDF variables we have defined.
        call check( nf90_put_var(ncid, rp_varid, rice_paddy) )
        call check( nf90_put_var(ncid, prp_varid,p_rice_paddy) )
        call check( nf90_put_var(ncid, dl_varid, dry_land) )
        call check( nf90_put_var(ncid, pdl_varid,p_dry_land) )

        ! Close the file.
        call check( nf90_close(ncid) )
   
        ! If we got this far, everything worked as expected. Yipee!
        print *,"********* SUCCESS !"

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 main

评分

参与人数 2金钱 +30 贡献 +4 收起 理由
言深深 + 20 + 2
lqouc + 10 + 2 感谢分享原创代码

查看全部评分

密码修改失败请联系微信:mofangbao
发表于 2016-7-19 15:51:44 | 显示全部楼层
请问这个 call check( nf90_create(FILE_NAME, nf90_clobber, ncid) )是调用的外部函数吗


密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表