NCL Home > Documentation > Functions > File IO, String manipulation

write_table

Writes all elements from a list (to a file).

Available in version 6.1.0 and later.

Prototype

	procedure write_table (
		filename [1] : string,  
		option   [1] : string,  
		alist    [1] : list,    
		format   [1] : string   
	)

	return_val [*] :  string

Arguments

filename

The file name to output the list.

option

The option of writing the file. "w" for (over-)writing; "a" for appending.

alist

The input list to write.

format

The format for of each (first) element of the list.

Description

Writes/Overwrites or appends elements to a file.

See Also

print_table, write_matrix, asciiwrite, sprintf, sprinti

Examples

Example 1

 a = (/111, 222, 333, 444/)
 b = (/1.1, 2.2, 3.3/)
 c = (/"1", "22", "333", "aaaaaa", "bbbb", "cc"/)
 d = (/11h, 22h/)
 f = (/11l, 22l, 33l, 44l, 55l, 66l/)

 alist = [/a, b, c, d, f/]

 header = (/"--------------------------------", \
            "This is a file header", \
            "--------------------------------"/)
 footer = (/"--------------------------------", \
            "This is a file footer", \
            "--------------------------------"/)

 hlist = [/header/]
 flist = [/footer/]

 write_table("list.txt", "w", hlist, "%s")
 write_table("list.txt", "a", alist, "%d%16.2f%s%d%ld")
 write_table("list.txt", "a", flist, "%s")

Where list.txt contains:

--------------------------------
This is a file header
--------------------------------
111             1.10 1      11 11
222             2.20 22     22 22
333             3.30 333       33
444                  aaaaaa    44
                     bbbb      55
                     cc        66
--------------------------------
This is a file footer
--------------------------------

Example 2

 a = (/111, 222, 333, 444/)
 b = (/1.1, 2.2, 3.3/)
 c = (/"a", "b", "c"/)
 d = (/11h, 22h/)
 f = (/11l, 22l, 33l, 44l, 55l, 66l/)

 alist = [/a, b, c, d, f/]

 header = (/"--------------------------------", \
            "This is a file header", \
            "--------------------------------"/)
 footer = (/"--------------------------------", \
            "This is a file footer", \
            "--------------------------------"/)

 hlist = [/header/]
 flist = [/footer/]

 write_table("list.txt", "w", hlist, "%s ")
 write_table("list.txt", "a", alist, "%d,%16.2f,%s,%d,%ld")
 write_table("list.txt", "a", flist, "%s ")

Output will be:

-------------------------------- 
This is a file header 
-------------------------------- 
111,            1.10,a,11,11
222,            2.20,b,22,22
333,            3.30,c,  ,33
444,                , ,  ,44
   ,                , ,  ,55
   ,                , ,  ,66
-------------------------------- 
This is a file footer 
--------------------------------

Example 3

 i = ispan(101,105,1)
 j = ispan(201,205,1)

 write_table("file.txt","w",[/i,j/],"first_%05i second_%05i")

Output will be:

first_00101 second_00201
first_00102 second_00202
first_00103 second_00203
first_00104 second_00204
first_00105 second_00205

Example 4:

The variable type must be appropriate for the format. If the user does not specify the width, then the function will use deault settings.

       sta  = (/"Mum", "RNC", "CNB"/)   ; type string
       Temp = (/30, 20, 25/)            ; type integer
       RH   = (/80, 900, 95/)
       WS   = (/2, 3, 4/)
       WD   = (/80, 150, 95/)
       Time = 201411050800>b>l             ; the appended 'l' makes this a 'long' int

; Temp, RH, WS, WD are all integers

       fil_I = "foo_I.txt"
       system("/bin/rm -f "+fil_I)   ; remove any pre-existing file
       write_table(fil_I, "w", [/Time/], "%li")
       write_table(fil_I, "a", [/"Station","Temp","RH","WS","WD"/], "%s %s %s %s %s ")
       write_table(fil_I, "a", [/sta,Temp,RH,WS,WD/],"%s %i %i %i %i")

; change to float; use  :=  syntax

        Temp := (/30, 20, 25/)*1.0
        RH   := (/80, 900, 95/)*1.0
        WS   := (/2, 3, 4/)*1.0
        WD   := (/80, 150, 95/)*1.0

        fil_F = "foo_F.txt"
        system("/bin/rm -f "+fil_F)   ; remove any pre-existing file
        write_table(fil_F, "w", [/Time/], "%li")
        write_table(fil_F, "a", [/"Station","Temp","RH","WS","WD"/], "%s %s %s %s %s ")
        write_table(fil_F, "a", [/sta,Temp,RH,WS,WD/],"%s %f %f %f %f")

With default settings for %i the 'fil_I.txt' file looks like

201411050800
Station Temp RH WS WD 
Mum 30 80 2 80
RNC 20 900 3 150
CNB 25 95 4 95
With default settings for %f the 'fil_F.txt' file looks like

201411050800
Station Temp RH WS WD 
Mum 30.000000 80.000000 2.000000 80.000000
RNC 20.000000 900.000000 3.000000 150.000000
CNB 25.000000 95.000000 4.000000 95.000000