- 积分
- 7456
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-9-20
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 虫儿飞 于 2014-12-12 16:01 编辑
先给程序
subroutine generate_ctl(data_name_str,xdef_str,ydef_str,zdef_str,tdef_str,vars,var)
implicit none
character*150 :: data_name_str,title_str
character*100 :: xdef_str,ydef_str,zdef_str,tdef_str
integer*4 :: vars
character*1 :: vars_str
character*100 :: var(vars)
character*150 :: out_file_str
character*25, parameter :: path_str = '**************************'
integer*4 :: i
out_file_str = trim(data_name_str)//'.ctl'
open(1001,file=trim(path_str)//trim(out_file_str))
title_str = data_name_str
call sub_replace_character(title_str,'.',' ')
write(vars_str,'(i1)')vars
write(1001,'(a)') adjustl('dset '//trim(path_str)//trim(data_name_str)//'.dat')
write(1001,'(a)') adjustl('undef -9.99E+08')
write(1001,'(a)') adjustl('title '//trim(title_str))
write(1001,'(a)') adjustl(xdef_str)
write(1001,'(a)') adjustl(ydef_str)
write(1001,'(a)') adjustl(zdef_str)
write(1001,'(a)') adjustl(tdef_str)
write(1001,'(a)') adjustl('vars '//trim(vars_str))
do i = 1,vars
write(1001,'(a)') adjustl(var(i))
enddo
write(1001,'(a)') adjustl('endvars')
close(1001)
end
subroutine sub_replace_character(OutStr,OldChar,NewChar)
!!!--- 说明:InStr是从非‘空格’字符开始的,对于其余三个变量没有要求
implicit none
character(len=*) :: OutStr
character(len=*) :: OldChar,NewChar
character*150 :: InStr
integer*4 :: AllCount,SubCountOld,SubCountNew,Count
integer*4 :: i
InStr = OutStr
OutStr = ''
AllCount = len_trim(InStr)
SubCountOld = len(OldChar)
SubCountNew = len(NewChar)
if(InStr(1:SubCountOld).eq.OldChar) then
OutStr(1:SubCountNew) = NewChar
Count = SubCountNew
i = 1+SubCountOld
else
OutStr(1:1) = InStr(1:1)
Count = 1
i = 2
endif
do while(i.le.AllCount-SubCountOld+1)
if(InStr(i:i+SubCountOld-1).eq.OldChar) then
OutStr = OutStr(1:count)//NewChar
Count = Count + SubCountNew
i = i+SubCountOld
else
OutStr = OutStr(1:Count)//InStr(i:i)
Count = Count + 1
i = i+1
endif
enddo
if(i.le.AllCount) OutStr = OutStr(1:Count)//InStr(i:AllCount)
end
再给示例用法
变量定义阶段
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ctl
character*150 :: data_name_str
character*100 :: xdef_str,ydef_str,zdef_str,tdef_str
integer*4 :: vars
character*100,allocatable :: var(:)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ctl
程序调用阶段
out_file_str = 'corr.between.sst.index.of.key.area.and.hgt.dat'
open(100,file=trim(out_file_str),form='unformatted',access='direct',recl=4*lats*lons)
irec = 0
do k = 1,m
irec = irec + 1
write(100,rec=irec) ((rr(i,j,k),i=1,lons),j=1,lats)
enddo
close(100)
vars = 1
allocate(var(vars))
var(1) = 'cc 0 99 correlation coefficient'
data_name_str = 'corr.between.sst.index.of.key.area.and.hgt'
xdef_str = 'xdef 144 linear 0 2.5'
ydef_str = 'ydef 73 linear -90.0 2.5'
zdef_str = 'zdef 1 linear 0 1'
tdef_str = 'tdef 13 linear 01Jan1981 1mon'
call generate_ctl(data_name_str,xdef_str,ydef_str,zdef_str,tdef_str,vars,var)
deallocate(var)
经过我自己长达2-3年的实践检验,没有问题。
但是有一个细节需要注意,需要在subroutine generate_ctl里修改路径
character*25, parameter :: path_str = '**************************'
一般而言数据在哪儿,ctl就在哪儿,也可以不一样,只要脑袋转得过弯来。
在我的示例中,可以看出我生成的文件名,一般都可以根据名字把这个文件表征的大致内容看出来,并且用圆点想链接,在subroutine generate_ctl里将圆点换成空格,作为数据名称。这个是第二个子程序的用途。
|
评分
-
查看全部评分
|