- 积分
- 1402
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-11-13
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 90后 于 2016-12-5 10:19 编辑
最近,把“Fortran写netcdf文件”做成了一个子程序,里面包含创见nc文件、创建维度、创建坐标变量、添加属性、结束定义、写NC文件,以及关闭文件。在主程序中,第一次调用子程序写nc文件时,ncid = 196608;从第二次开始,ncid都等于32767,即ncid无效。
每次写完netcdf文件,都会用“call check(nf90_close(ncid))”关闭文件,所以,我就不知道错在哪了。麻烦大家帮忙看看~
===================================================================
subroutine output_nc(fileout,varout,attribute,time)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! <<STEPS FOR WRITE NETCD>> !
! NF90_CREATE ! create netCDF dataset: enter define mode !
! NF90_DEF_DIM ! define dimensions: from name and length !
! NF90_DEF_VAR ! define variables: from name, type, dims !
! NF90_PUT_ATT ! assign attribute values !
! NF90_ENDDEF ! end definitions: leave define mode !
! NF90_PUT_VAR ! provide values for variable !
! NF90_CLOSE ! close: save new netCDF dataset !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
use netcdf
use type_dims_att_mod
use type_grib2_keys_mod
implicit none
character*(*) , intent(in) :: fileout
type(var_dims), intent(in) :: varout
type(var_attribute), intent(in) :: attribute
type(grib_time), intent(in) :: time
integer(4) :: ncid
integer :: x_dimid, x_varid
integer :: y_dimid, y_varid
integer :: z_dimid, z_varid
integer :: t_dimid, t_varid
integer :: var_dimid(2), varid
! create the netcdf file.
call check(nf90_create(fileout, nf90_clobber, ncid))
! define the dimensions.
call check(nf90_def_dim(ncid, "time" , 1 , t_dimid))
call check(nf90_def_dim(ncid, "level" , varout%NV, z_dimid))
call check(nf90_def_dim(ncid, "latitude" , varout%Ny, y_dimid))
call check(nf90_def_dim(ncid, "longitude", varout%Nx, x_dimid))
var_dimid = (/ x_dimid, y_dimid /) !! important !!
! define (coordinate) variables => determine "sequence of write in netcdf"
call check(nf90_def_var(ncid, trim(attribute%varname), NF90_FLOAT, var_dimid, varid))
call check(nf90_def_var(ncid, "time" , NF90_INT , t_dimid, t_varid)) ! if no possible.
call check(nf90_def_var(ncid, "level" , NF90_INT , z_dimid, z_varid)) ! change var type,
call check(nf90_def_var(ncid, "latitude" , NF90_FLOAT, y_dimid, y_varid))
call check(nf90_def_var(ncid, "longitude", NF90_FLOAT, x_dimid, x_varid))
! add attribute to coordinate variable
call check(nf90_put_att(ncid, x_varid, "units", "degrees_east" ))
call check(nf90_put_att(ncid, y_varid, "units", "degrees_north" ))
call check(nf90_put_att(ncid, z_varid, "units", trim(attribute%level_units) ))
call check(nf90_put_att(ncid, t_varid, "units", trim(attribute%step_units_long) ))
!call check(nf90_put_att(ncid, t_varid, "units", attribute%step_units_short))
! add attribute to variable
call check(nf90_put_att(ncid, varid , "long_name", trim(attribute%long_name) ))
call check(nf90_put_att(ncid, varid , "units", trim(attribute%units) ))
call check(nf90_put_att(ncid, varid , "_FillValue", attribute%FillValue ))
call check(nf90_put_att(ncid, varid , "discipline_and_category", &
& trim(attribute%discipline_and_category) ))
call check(nf90_put_att(ncid, varid , "template_discipline_category_number", &
& trim(attribute%template_discipline_category_number) ))
call check(nf90_put_att(ncid, varid , "level_type", trim(attribute%level_type) ))
call check(nf90_put_att(ncid, varid , "step_units", attribute%step_units_long ))
call check(nf90_put_att(ncid, varid , "initial_time", trim(attribute%initial_time) ))
! time
call check(nf90_put_att(ncid, t_varid, "long_name", "forecast time" ))
call check(nf90_put_att(ncid, t_varid, "units", &
& trim(attribute%step_units_long)//'s since '//trim(attribute%initial_time)//'.' ))
call check(nf90_put_att(ncid, t_varid, "forecast_range", trim(time%stepRange) ))
! level
!call check(nf90_put_att(ncid, z_varid , "level_type", trim(attribute%level_type) ))
! add global attribute
call check(nf90_put_att(ncid, nf90_global , "centre", trim(attribute%centre) ))
call check(nf90_put_att(ncid, nf90_global , "production_status", &
& trim(attribute%production_status) ))
call check(nf90_put_att(ncid, nf90_global , "production_type", trim(attribute%production_type) ))
! end definitions
call check(nf90_enddef(ncid))
! write data
call check(nf90_put_var(ncid, varid , varout%var ))
call check(nf90_put_var(ncid, t_varid, varout%time ))
call check(nf90_put_var(ncid, z_varid, varout%level))
call check(nf90_put_var(ncid, y_varid, varout%lat ))
call check(nf90_put_var(ncid, x_varid, varout%lon ))
! close netcdf file
call check(nf90_close(ncid))
end subroutine output_nc
===================================================================
|
|