爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7318|回复: 7

FORTRAN 写 NC 格式————为了ncl画图方便

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

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

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

x
本想上传到 FORTRAN 板块,但是觉得 ncl 这儿比较需要 贴在这儿

为了方便 已经上手ncl 画图的兄弟们,
找到一篇以前的 FORTRAN程序写nc 的程序》
提供 麻雀程序一篇,供大家 玩味 

FORTRAN  写 ncl


program CREATENCF

!C... include the usefully NetCDF include file
include 'netcdf.inc'

!C... define the variables used in this program !
integer status, ncid          ! status is a test flag and ncid is the unit of open(creat) files
integer LATID, LONID, TIMEID                !dimention's ID
integer PM10ID,PM2_5ID                   ! variable's ID
integer PM10dim(3), PM2_5dim(3)
integer nlon, nlat, ntime
parameter (nlon=10, nlat=10, ntime=10)
real PM10array(nlon, nlat, ntime)      ! variable's array ==>this is data array
real PM2_5array(nlon, nlat, ntime)
integer start(3), count(3)
data start /1,1,1/
data count /nlon, nlat, ntime/

!C... get the library version of NetCDF in this mashine
write(*,*), 'The NetCDFs Library version is ==> ', nf_inq_libvers()

!C... begin to creat a new NetCDF file
status=nf_create('create.nc', nf_clobber, ncid)
if (status .NE. nf_noerr) call handle_err(status)
write(*,*), 'create an NetCDF file, and its ID is => ', ncid

!C... begin to define your dimentions
status=nf_def_dim(ncid, 'lat', 10, LATID)
if (status .NE. nf_noerr) call handle_err(status)

status=nf_def_dim(ncid, 'lon', 10, LONID)
if (status .NE. nf_noerr) call handle_err(status)

status=nf_def_dim(ncid, 'time', nf_unlimited, TIMEID)  ! the time is unlimited in this program
if (status .NE. nf_noerr) call handle_err(status)
write(*,*), 'define the dimentions lat, lon, and time, the ID is => ',LATID, LONID, TIMEID

!C... begin to define your varibles
PM10dim(1)=lonid
PM10dim(2)=latid
PM10dim(3)=timeid
PM2_5dim(1)=lonid
PM2_5dim(2)=latid
PM2_5dim(3)=timeid
status=nf_def_var(ncid, 'PM10', nf_float, 3, PM10dim, PM10ID)
status=nf_def_var(ncid, 'PM2_5', nf_float, 3, PM2_5dim, PM2_5ID)
if (status .NE. nf_noerr) call handle_err(status)
write(*,*), 'define varialbes, and the ID is => ', PM10ID, PM2_5ID

!C... Put your attribute of your variables
status=nf_put_att_text(ncid, PM10ID, 'long_name', 35, 'particulate metter with dp < 10    ')
status=nf_put_att_text(ncid, PM10ID, 'unit', 35, 'ug/m3')
status=nf_put_att_text(ncid, PM2_5ID, 'long_name', 35, 'particulate metter with dp < 2.5    ')
status=nf_put_att_text(ncid, PM2_5ID, 'unit', 35, 'ug/m3 ')
status=nf_put_att_text(ncid, nf_global, 'Title_of_this_program', 30, 'A NetCDF test program.     ')
if (status .NE. nf_noerr) call handle_err(status)

!C... end of define mode (this function is must be used)
status=nf_enddef(ncid)
if (status .NE. nf_noerr) call handle_err(status)
print *, 'end_define'

!C... To get your data !
do 10 ilon=1, nlon
do 10 ilat=1, nlat
do 10 itime=1, ntime
PM10array(ilon, ilat, itime)=50.0
PM2_5array(ilon, ilat, itime)=30.0
10      continue
print *, 'get data'

!C... begin to put your data in to this file!
status=nf_put_vara_real(ncid, PM10ID, start, count, PM10array)
status=nf_put_vara_real(ncid, PM2_5ID, start, count, PM2_5array)
if (status .NE. nf_noerr) call handle_err(status)
print *, 'put variables'

!C... close this file..
status=nf_close(ncid)
if (status .NE. nf_noerr) call handle_err(status)

write(*,*), 'OK! your have created a NetCDF file, you can use the command to look your file'
write(*,*), 'ncdump create.ncf|more'
end

!C... Make the error control program!
subroutine handle_err(status)
integer status
if (status .NE. NF_NOERR) then
write(*,*), NF_STRERROR(status), status
stop 'program stoped, and you must find the error in your source code.'
endif
end



运行  :
ifort  2_example.f90  -L/opt/soft-install/intel_soft/netcdf362/lib -lnetcdf -lm   -I/opt/soft-install/intel_soft/netcdf362/include -o gui.out

评分

参与人数 1金钱 +15 贡献 +5 收起 理由
mofangbao + 15 + 5

查看全部评分

密码修改失败请联系微信:mofangbao
0
早起挑战累计收入
发表于 2013-10-25 08:22:20 | 显示全部楼层
谢谢楼主分享,希望楼主把需要的依赖库也列一下~
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2013-10-25 08:37:31 | 显示全部楼层
密码修改失败请联系微信:mofangbao
发表于 2013-12-13 22:18:25 | 显示全部楼层
太复杂了,不过谢谢楼主分享
密码修改失败请联系微信:mofangbao
发表于 2014-10-31 20:39:41 | 显示全部楼层
谢谢楼主  强大
密码修改失败请联系微信:mofangbao
发表于 2015-11-18 16:43:36 | 显示全部楼层
请问楼主nf_clobber这个是什么意思?
密码修改失败请联系微信:mofangbao
发表于 2015-11-18 17:07:23 | 显示全部楼层
谢谢楼主分享
密码修改失败请联系微信:mofangbao
发表于 2016-5-25 15:41:42 | 显示全部楼层
感谢楼主,已经试验成功了,不过一旦生成的nc大小超过2G,程序就没有输出了,不知道楼主遇到过吗?
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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