我正在处理数字天气预报,其名称如下:
sub_gfsanl_4_2011MMDD-IIII-FFFF.grb2-MM表示从01到12月份的月份
-DD代表从01到31天的时间
-IIII表示初始化时间,第一个和第二个数字表示小时,第三个和最后一个数字表示分钟。
-FFFF代表预报时间,第一位和第二位代表小时,第三位和最后一位代表分钟。
在我的目录中,我有几个给定月份中给定天数的文件。一天有4个数据,每6小时一次,IIII=0000 ,0600,1200,1800。
我要做的是列出给定日期的所有文件,这里是我编写的f90代码:
program test_ec
implicit none
!==variable declaration==
integer :: mi,di,dil,mil
character*3 :: temp
!==Program instructions==
mil=1
write(temp,'(i2.2)') mil
read(temp,'(i2.2)') mi
!convert the month into a two digit value mi=01
! change to directory where the data are stored
CALL chdir('/media/Hello/ncfiles/GFS' )
do dil=1,31
!loop over days
write(temp,'(i2.2)') dil
read(temp,'(i2.2)') di
! converting day number into a two digit number, store this value into di. ex dil=9 then di=09
CALL execute_command_line( 'ls sub_gfsanl_4_2011${mi}${di}*.nc > yes.txt' )
!list all files with the correct month and days and store it to yes.txt
end do
end program test_ec由于某些原因,execute_command_line似乎不喜欢$ For变量.
发布于 2016-10-18 21:08:53
不能在shell中使用${mi}${di}。它们是Fortran变量,而不是shell变量。您必须将数字放入Fortran内部的字符串中。使用一些已建立的方法。他们在这里多次在Convert integers to strings to create output filenames at run time和它的重复治疗(见右侧相关)。
发布于 2016-10-19 16:13:55
多亏了弗拉基米尔的建议,我才解决了我的问题!
program test_ec
implicit none
integer :: mi,di,iiii,fff
character(len=1024) :: filename
character(len=1024) :: format_string
logical exist
mi=1
CALL chdir('/media/Hello/ncfiles/GFS' )
!do di=1,10
do iiii=0,18,6
do fff=0,18,6
format_string = "(A17,i2.2,i2.2,A1,i4.4,A1,i3.3,A8)"
write (filename,format_string) "sub_gfsanl_4_2011",mi,di,"_",iiii,"_",fff,".grb2.nc"
inquire(file=trim(filename), exist=exist)
if (exist) then
write(*,*) 'file exists i can do do whatever i want with this file'
else
write(*,*) 'I did not find that file.'
end if
enddo
enddo
enddo
end program test_echttps://stackoverflow.com/questions/40117357
复制相似问题