INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

list the content of a directory specified by loop indices in fortran

list the content of a directory specified by loop indices in fortran

list the content of a directory specified by loop indices in fortran

(OP)
Hello everybody

0
down vote
favorite


I need some help, well let's get started:

I'm working with numerical weather forecast named as follow:

sub_gfsanl_4_2011MMDD-IIII-FFFF.grb2

-MM stands for month from 01 to 12 -DD stands for days from 01 to 31 -IIII stands for initialisation time, the first and second digits are for hours the third and last are for minutes -FFFF stands for forecast hour , the first and second digits are for hours the third and last are for minutes

in my directory i have several files for a given days of a given months. A day has 4 data, one at every six hours IIII=0000 ,0600,1200,1800.

What i'm trying to do is to list all files of a given days, here the f90 code i wrote:



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 



I would greatly appreciate any help of advises, for some reasons the execute_command_line doesn't seem to like the $ for variable...

Thanks!

RE: list the content of a directory specified by loop indices in fortran

(OP)
Hi i did finally found the solution to my problem:

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_ec

RE: list the content of a directory specified by loop indices in fortran

Minor - your last enddo should be commented out too.

You don't really need a format string - the code can be written as

CODE

10 format(A17,i2.2,i2.2,A1,i4.4,A1,i3.3,A8)
write(filename,10) ... 
or

CODE

write(filename,"(A17,i2.2,i2.2,A1,i4.4,A1,i3.3,A8)") ... 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close