Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split an array and create files

Status
Not open for further replies.

Catalina0421

Programmer
Oct 5, 2010
5
US
Hi everyone,

I am working with Fortran 90 for almost a year and I have a problem with two parts.

I have an array and I have to split in several parts. Each time the array is different so I can't change the code everytime. The only way I found it's split the array and transfer the small part data to different files. But I don't know how create a serie of files or a "standard" for this, because in my limited knowledge of Fortran you always generate the variable before.

Thanks for your help guys... :D
 
Sorry but what you want to do is not clear enough.

Is the array a result of your code which has to write it onto several files ?

Is the array an input of your code which has to read it from several files ?

Could you show us the code itself (or your attempt to create such a code) ?
 
OK! Sorry if I was not clear with the information...

The array is an input with many data (for example, 10 experiments) and I want to create small arrays or files (per each experiment). The problem is the array will change, it means, sometimes will be 20 experiments, sometimes 4. I don't want to modify the code everytime. I want generate the smalls arrays (and obviously, the array's names) without change the code everytime. I know how split the array but I don't have any idea how generate "generic" names.

I attempt to create the code...

Thanks for your answer... :D
 
But when the data of all experiments (sometimes 4 sometimes 10,...) are stored in the same array, then how are they separated, i.e. how do you know that for example a[1],...,a[k] are from the experiment 1, a[k+1],...,a[n] from the experiment 2, ... etc.

Catalina0421 said:
I know how split the array but I don't have any idea how generate "generic" names.
Where is the problem?
When you have n experiments, then you can generate n text files, e.g. with names:
"experiment_1.dat",..,"experiment_n.dat".
 
Do you want something like that :
- a small input deck, read on standard input unit, enables to provide the list of experiment names,
- each name is translated into a file name,
- each file is opened and loaded into an array,
- all arrays are grouped together in a list.
- each experiment is associated to its owns number of values.

If yes than the following program is a good start :

Code:
program read_experiments

  implicit none
  
  type experiment
    character(255)               :: name
    integer                      :: nvalues=0
    double precision,allocatable :: values(:)
  end type
  
  type(experiment),allocatable   :: list(:)
  integer                        :: i,n,istat
  character(255)                 :: filename
  
  read(*,*) n ! reading the number of experiments
  
  allocate(list(n))
  
  do i=1,n
    read(*,'(a)') list(i)%name
    filename=TRIM(list(i)%name)//'.txt' ! or any other transformation
    open(15,file=filename,status='old',iostat=istat)
    if(istat /= 0) then
      write(*,*) 'error : the file ',TRIM(filename),' does not exist'
    else
      read(15,*) list(i)%nvalues
      allocate(list(i)%values(list(i)%nvalues))
      read(15,*) list(i)%values
      close(15)
    endif
  enddo
  
  ! writing what has been read, just for verification
    
  do i=1,size(list)
    write(*,*) 'experiment file ',TRIM(list(i)%name)
    write(*,*) 'number of values ',list(i)%nvalues
    write(*,'(10(1x,1pe12.5))') list(i)%values
  enddo
  
end program

Input file "d" :

Code:
2
e1
e2

File "e1.txt" :

Code:
2
3. 4.

File "e2.txt" :

Code:
3
7. 8. 9.

Test :

Code:
[lcoul@localhost test]$ ifort read_experiments.f90
[lcoul@localhost test]$ ./a.out < d
 experiment file e1
 number of values            2
  3.00000E+00  4.00000E+00
 experiment file e2
 number of values            3
  7.00000E+00  8.00000E+00  9.00000E+00

Of course, it is possible to extend this program in analyzing more complicated experiment files which are often under the format CSV (comma separated variables) and understandable by tools like EXCEL. In that case, the vector "values" must be replaced by a matrix, each column of that matrix has a name and the number of columns must be obtained in scanning the first line and extracting the column names. The number of rows is obtained in reading the file twice.

If you are interested, I can provide a subroutine able to read a CSV file. I should be able to find it somewhere...

 
Hi Mikrom for your answer... For me, the problem is generate the n text files... Maybe it's the simplest thing in the world but I can't figure it out....

Thanks again FJacq! I just wondering, could I use iostat in Ifort for Linux? Sorry if I say something stupid...
 
Thanks again FJacq! I just wondering, could I use iostat in Ifort for Linux? Sorry if I say something stupid...

Yes ! iostat may be used on any operating system.

I tested this small program on Linux and I am sure that it is also valid on any computer having a F90 compiler.

 
Hi Catalina0421,

If you want to thank then click above at
star.gif
Thank FJacq
for this valuable post!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top