- 积分
- 1986
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-3-20
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
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
|
评分
-
查看全部评分
|