fortran实例_挑选LLJs
2019-05-30 本文已影响0人
Aerosols
Frotran 删除字符串空格
Trim 删除字符串结尾处的空格
AdjustL 删除字符串开头的空格,左对齐
AdjustR 删除字符串结尾的空格,右对齐
write/read(UNIT=number,FMT=format,NML=namelist,REC=record,IOSTAT=stat,ERR=errlabel,END=endlabel)
(1)UNIT=number:指定read/write所使用的输入/出位置
(2)FMT=format,:指定输入输出格式
(3)NML=namelist:读写某个namelist
(4)REC=record:在直接读取文件中,设置索要读写的文件模块位置
(5)IOSTAT=stat:设置一个整数值给变量stat,用来说明文件的读写状态
stat>0,表示读取操作发生错误
stat=0,表示读取操作正常
stat<0,表示文件终了
(6)ERR=errlabel:在读写过程中发生错误时,转移到某个行代码来继续执行程序。
(7)END=endlabel:在读写到文件末尾时,转移到某个行代码来继续执行程序。
实例1
功能:挑出NLLJs的个例。
program main
implicit none
integer,parameter:: nline=100
integer i,hght(nline),relh(nline),drct(nline),sknt(nline)
integer max_lev,min_lev
real wind_max,wind_min
real pres(nline),temp(nline),dwpt(nline),mixr(nline),thta(nline),thte(nline),thtv(nline)
integer yr,imon,iday,ihr,days(12)
character(len=160) filename
character(len=4) cyr
character(len=2) cmon,cday,hours(2)
data hours/"00","12"/ !!!数组赋初值
data days/31,28,31,30,31,30,31,31,30,31,30,31/
open(13,file="lsd_select.log")
do yr = 2017, 2018
write(cyr,fmt='(i4.4)') yr
do imon = 1, 12
write(cmon,fmt='(i2)') imon
do iday = 1,days(imon)
write(cday,fmt='(i2)') iday
do ihr = 1,2
filename = "seasia_54511_sounding_"//cyr//"_"//trim(adjustl(cmon))//"_"//trim(adjustl(cday))//"_"//hours(ihr)//".txt"
!!!读取filename
open(12,file="/home/lsd/sound/"//filename)
do i=1,6
read(12,*,end=300) !!!!读写到文件末尾时,转移到300行代码继续执行
enddo
do i= 1,nline
read(12,200,err=100) pres(i),hght(i),temp(i),dwpt(i),relh(i),mixr(i),drct(i),sknt(i),thta(i),thte(i),thtv(i)
!write(*,200) pres(i),hght(i),temp(i),dwpt(i),relh(i),mixr(i),drct(i),sknt(i),thta(i),thte(i),thtv(i)
! error=100,读写过程中出现错误时,转移到100行代码继续执行.
enddo
200 FORMAT(1x,f6.1,1x,i6,2(1x,f6.1),1x,i6,1x,f6.1,2(1x,i6),3(1x,f6.1))
100 continue
close(12) !!!读取文件结束
!!! 判断3000m以下最大风速及最大风速的位置
wind_max=sknt(1)
max_lev=1
do i=1,nline
if(hght(i)>3000) exit
if(sknt(i)>wind_max) then
wind_max = sknt(i)
max_lev=i
endif
enddo
!!!在最大风速高度以上3000m以下找最小风速及高度
wind_min=wind_max
min_lev=max_lev
do i=max_lev,nline
if(hght(i)>3000) exit
if(sknt(i)<wind_min) then
wind_min = sknt(i)
min_lev=i
endif
enddo
print*,filename
print *,wind_max,hght(max_lev)
print *,wind_min,hght(min_lev)
print *,"----------------------------------"
if(wind_max * 0.514 >= 6 .and. wind_min <= wind_max*0.5) write(13,fmt='(A40,1x,2(f6.1,1x,i6))') filename,wind_max*0.514,hght(max_lev),wind_min*0.514,hght(min_lev)
300 continue
enddo
enddo
enddo
enddo
stop
end program main
实例2:
!数据:ASCII码
!目的:将单个站点的历史信息写入一个文件
program testio
implicit none
integer yr,imon,iday,ihr,iocode,num,ista,target,search_code
integer k,i,hr(4)
real days(12)
real lsd(25)
character(len=2) cmon,cdays,hours(4),chr
character(len=120) filename,dirname,ctarget
character(len=4) cyr
data days/31,28,31,30,31,30,31,31,30,31,30,31/
data hours/'02','08','14','20'/
data hr/2,8,14,20/
num = 20000
call getarg(1,ctarget)
read(ctarget,*) target
!target=40811
dirname = "."
open(13,file=trim(ctarget)//'.log') !!!trim将字符串末尾(右端)的空格删掉
open(22,file='./quece/'//trim(ctarget)//'_quece.log')
do yr = 2017, 2017
write(cyr,fmt='(i4.4)') yr
do imon = 1,12
write(cmon,fmt='(i2.2)') imon
do iday = 1,days(imon)
write (cdays,fmt='(i2.2)') iday
do ihr = 0,23
write (chr,fmt='(i2.2)') ihr
filename = cyr//cmon//cdays//chr//'00.000'
print*,filename
search_code=0
open(12,file=trim(dirname)//'/'//trim(filename), status='old',form='formatted',iostat=iocode)
if(iocode==0) then
do k=1,2
read(12,*,end=88) !如果文件为空,跳到88行语句执行
enddo
do k=1,num
read(12,*,end=88) ista, (lsd(i),i=1,25) !一共26列
if(ista==target) then
search_code=1
exit
endif
enddo
else
write(22,*) trim(filename)
endif
88 continue
if(search_code/=1) lsd=9999
write(13,fmt='(A,1x,25(f6.1,3x))') cyr//cmon//cdays//chr, (lsd(i),i=1,25)
close(12)
!call sleep( 1 )
enddo
enddo
enddo
enddo
end program testio