Silverfrost Forums

Welcome to our forums

Data representation in an array format

27 Jun 2008 11:47 #3425

Hi,

I can´t find a solution for the following:

I´m working with a fortran 95 that generates a file in which the result is a file with 48 datas presented in a (7X7) Matrix. The point is: I would like to get this datas in just an array for representing it in Excel. I can see in the fortran code that the write statment is presented in this way:

open(10, file='C:\Documents and Settings\MARCAL\Escritorio\Grafics.txt')

write(10, *) 'Forecast:' write(10, *) Frct(1:siz) write(10, *) 'Observation:' write(10, *) Obs(1:siz)

close (10)

I have tried to short this out by myself but I can´t find the solution, would anyone mind to tell me how to write the format in order to get the datas presented in just an array???

PD: I use Excel to represent datas, because the use of macros and so, but there is any other better program for representing purposes?? thanks

Thanks very much in advance BR

30 Jun 2008 9:59 #3428

Hi kingkastle,

I'm not sure if I can help you. If you want to use data in Excel, it might be good to store them in the CSV format which can directly be used in that software. Here comes a little example:

   character*100 string

c string must be terminated by zero

   string = '      No. 1 ;      No. 2 ;      No. 3 ;      No. 4  '
   string(52:52) = char(0)
   write(20,'(A)',err=100)string(1:52)

   write(string( 1:12),'(F12.3)',err=100)frct(1)
   write(string(14:25),'(F12.3)',err=100)frct(2)
   write(string(27:38),'(F12.3)',err=100)frct(3)
   write(string(40:51),'(F12.3)',err=100)frct(4)
   write(20,'(A)',err=100)string(1:52)

   write(string( 1:12),'(F12.3)',err=100)obs(1)
   write(string(14:25),'(F12.3)',err=100)obs(2)
   write(string(27:38),'(F12.3)',err=100)obs(3)
   write(string(40:51),'(F12.3)',err=100)obs(4)
   write(20,'(A)',err=100)string(1:52)

100 close(20)

Write all data belonging to one 'row' like the Forecast and the Observation you mentioned. The first line of the output file contains the field names (in my example No. 1, No. 2 ...). Make sure that the output string is one character longer then needed, and terminate the string with zero. Store the file with the extension CSV.

Regards Wilfried

30 Jun 2008 11:41 #3430

Don't forget that Excel comma separated files require a full stop decimal point and a comma between variables, if your Windows International settings specify the British/american method. For continental Europe, the decimal point is a comma and the comma separator becomes a semi-colon. FTN95 always uses a full stop as the decimal, unless someone wants to tell me otherwise. The alternate method is to use a tab delimited text file, but you will probably still have to make the conversion for the decimal point.

Typically, you could use the following for tab delimited:

character*1 separator,from_decimal_point, to_decimal_point
character*20 format_string
character*2000 big_output_buffer
separator                = char(9)
from_decimal_point = '.'
to_decimal_point     = ','
big_output_buffer    = ' '
format_string='(f12.3,a)'
write(big_output_buffer,format_string)(frct(i),separator,i=1,siz)
ilen=leng(big_output_buffer)
do i=1,ilen
  if(big_output_buffer(i:i) .eq. from_decimal_point )then
    big_output_buffer(i:i) = to_decimal_point 
  endif
enddo
write(1000)trim(big_output_buffer)
1000 format(a)
end

If you wish to make this a bit more general, then a translation subroutine would do the trick, i.e.

subroutine translate_specific_characters(string,from,to)
character*(*) string,from,to
! 'from' and 'to' must be the same length and have a one to one
! translation relationship
if(from .eq. to)return
ilen = leng(string)
do i=1,ilen
  iposition = index(from,string(i:i))
  if(iposition .gt. 0)then
   string(i:i) = to(iposition:iposition)
  endif
enddo
end

and the main program would become:

character*1 separator
character*2 translate_from, translate_to
character*20 format_string
character*2000 big_output_buffer
translate_from = '.,'
translate_to     = ',;'
big_output_buffer    = ' '
format_string='(f12.3,a)'
write(big_output_buffer,format_string)(frct(i),separator,i=1,siz)
call translate_specific_characters(big_output_buffer,translate_from,translate_to)

write(1000)trim(big_output_buffer)
1000 format(a)
end

The above program starts with the default British/american setup and translates to the European style. The translate_from and translate_to strings could be set at program start-up. If they are identical, then no translation is carried out. Note this suggestion leaves an extra separator on the right hand end of the string, which will probably not matter, but is easily removed before translation with:

ilen=leng(big_output_buffer)
big_output_buffer(ilen:) = ' '

Note if you are mixing text and numerics, then in a CSV file, if the text includes the separator, then the string is surrounded in quotes, and a slightly more complicated translation routine is required that determines whether you are within quotes or not, e.g. the translation routine becomes:

subroutine translate_specific_characters(string,from,to)
character*(*) string,from,to
! 'from' and 'to' must be the same length and have a one to one
! translation relationship
if(from .eq. to)return
ilen = leng(string)
in_quote = 0
do i=1,ilen
!toggle quotation status
  if(string(i:i) .eq. ''')in_quote = 1 - in_quote
  if(in_quote .eq. 0)then
    iposition = index(from,string(i:i))
    if(iposition .gt. 0)then
     string(i:i) = to(iposition:iposition)
    endif
  endif
enddo
end

Regards

Ian

Please login to reply.