1 MODULE module_get_file_names
3 ! This module is used by the ndown program. We can have multiple output
4 ! files generated from the wrf program. To remove the what-are-the-
5 ! files-to-input-to-ndown task from the user, we use a couple of UNIX
6 ! commands. These are activated from either the "system" command or
7 ! the "exec" command. Neither is part of the Fortran standard.
9 INTEGER :: number_of_eligible_files
10 CHARACTER(LEN=132) , DIMENSION(:) , ALLOCATABLE :: eligible_file_name
14 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16 SUBROUTINE system(cmd)
18 CHARACTER (LEN=*) , INTENT(IN) :: cmd
20 call pxfsystem(cmd, len(cmd), ierr)
25 SUBROUTINE unix_ls ( root , id )
30 CHARACTER (LEN=*) , INTENT(IN) :: root
31 INTEGER , INTENT(IN) :: id
33 CHARACTER (LEN=132) :: command
34 INTEGER :: ierr , loop , loslen , strlen
35 #ifdef NONSTANDARD_SYSTEM_FUNC
36 INTEGER , EXTERNAL :: SYSTEM
38 LOGICAL :: unix_access_ok
39 LOGICAL, EXTERNAL :: wrf_dm_on_monitor
42 ! This is to make sure that we successfully use one of the available methods
43 ! for getting at a UNIX command. This is an initialized flag.
45 unix_access_ok = .FALSE.
47 ! Build a UNIX command, and "ls", of all of the files mnatching the "root*" prefix.
49 monitor_only_code : IF ( wrf_dm_on_monitor() ) THEN
51 loslen = LEN ( command )
52 CALL all_spaces ( command , loslen )
53 WRITE ( command , FMT='("ls -1 ",A,"*d",I2.2,"* > .foo")' ) TRIM ( root ) , id
55 ! We stuck all of the matching files in the ".foo" file. Now we place the
56 ! number of the those file (i.e. how many there are) in ".foo1". Also, if we
57 ! do get inside one of these CPP ifdefs, then we set our access flag to true.
59 #ifdef NONSTANDARD_SYSTEM_SUBR
60 CALL SYSTEM ( TRIM ( command ) )
61 CALL SYSTEM ( '( cat .foo | wc -l > .foo1 )' )
62 unix_access_ok = .TRUE.
64 #ifdef NONSTANDARD_SYSTEM_FUNC
65 ierr = SYSTEM ( TRIM ( command ) )
66 ierr = SYSTEM ( '( cat .foo | wc -l > .foo1 )' )
67 unix_access_ok = .TRUE.
70 ! Test to be sure that we did indeed hit one of the ifdefs.
72 IF ( .NOT. unix_access_ok ) THEN
73 PRINT *,'Oops, how can I access UNIX commands from Fortran?'
74 CALL wrf_error_fatal ( 'system_or_exec_only' )
77 ! Read the number of files.
79 OPEN (FILE = '.foo1' , &
82 ACCESS = 'SEQUENTIAL' , &
85 READ ( 112 , * ) number_of_eligible_files
88 ! If there are zero files, we are toast.
90 IF ( number_of_eligible_files .LE. 0 ) THEN
91 PRINT *,'Oops, we need at least ONE input file (wrfout*) for the ndown program to read.'
92 CALL wrf_error_fatal ( 'need_wrfout_input_data' )
95 ENDIF monitor_only_code
97 ! On the monitor proc, we got the number of files. We use that number to
98 ! allocate space on all of the procs.
100 CALL wrf_dm_bcast_integer ( number_of_eligible_files, 1 )
102 ! Allocate space for this many files.
103 ! GAC 20140321 - Addition to prevent attempts to reallocate same variable.
104 ! This used to be a bug when running convert_emiss for nested domains
105 ! a while back, now it is probably just a paranoid check.
107 IF ( ALLOCATED ( eligible_file_name ) ) DEALLOCATE ( eligible_file_name )
108 ALLOCATE ( eligible_file_name(number_of_eligible_files) , STAT=ierr )
110 ! Did the allocate work OK?
112 IF ( ierr .NE. 0 ) THEN
113 print *,'tried to allocate ',number_of_eligible_files,' eligible files, (look at ./foo)'
114 WRITE(message,*)'module_get_file_names: unix_ls: unable to allocate filename array Status = ',ierr
115 CALL wrf_error_fatal( message )
118 ! Initialize all of the file names to blank.
120 CALL init_module_get_file_names
122 ! Now we go back to a single monitor proc to read in the file names.
124 monitor_only_code2: IF ( wrf_dm_on_monitor() ) THEN
126 ! Open the file that has the list of filenames.
128 OPEN (FILE = '.foo' , &
131 ACCESS = 'SEQUENTIAL' , &
134 ! Read all of the file names and store them.
136 DO loop = 1 , number_of_eligible_files
137 READ ( 111 , FMT='(A)' ) eligible_file_name(loop)
138 print *,TRIM(eligible_file_name(loop))
142 ! We clean up our own messes.
144 #ifdef NONSTANDARD_SYSTEM_SUBR
145 CALL SYSTEM ( '/bin/rm -f .foo' )
146 CALL SYSTEM ( '/bin/rm -f .foo1' )
148 #ifdef NONSTANDARD_SYSTEM_FUNC
149 ierr = SYSTEM ( '/bin/rm -f .foo' )
150 ierr = SYSTEM ( '/bin/rm -f .foo1' )
153 ENDIF monitor_only_code2
155 ! Broadcast the file names to everyone on all of the procs.
157 DO loop = 1 , number_of_eligible_files
158 strlen = LEN( TRIM( eligible_file_name(loop) ) )
159 CALL wrf_dm_bcast_string ( eligible_file_name(loop) , strlen )
162 END SUBROUTINE unix_ls
164 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
166 SUBROUTINE all_spaces ( command , length_of_char )
170 INTEGER :: length_of_char
171 CHARACTER (LEN=length_of_char) :: command
174 DO loop = 1 , length_of_char
175 command(loop:loop) = ' '
178 END SUBROUTINE all_spaces
180 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
182 SUBROUTINE init_module_get_file_names
185 eligible_file_name = ' ' // &
189 END SUBROUTINE init_module_get_file_names
191 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
193 END MODULE module_get_file_names
196 !USE module_get_file_names
197 !call init_module_get_file_names
198 !call unix_ls ( 'wrf_real' , 1 )