NCL Home>
Application examples>
File IO ||
Data files for some examples
Example pages containing:
tips |
resources |
functions/procedures
NCL: Writing ASCII data
NCL has five main functions for writing data to an ASCII file:
- write_table - writes formatted, mixed-type
data with a single format statement.
- write_matrix - writes nicely-formatted 2D
arrays of integer, float, or double precision data.
- asciiwrite - an older and rather limited function
that writes one value per line. This is useful for outputting a
one-dimensional time series.
- sprinti / sprinti
- allows you to format integer and float data before writing
with write_table or asciiwrite.
For examples of reading or writing other types of ASCII files, see:
write_asc_1.ncl: This
very basic 2-line example:
data = random_uniform(-5,5,(/2,3,4/)) ; Generate a dummy 2 x 3 x 4 array
asciiwrite("file1.txt",data) ; Write it to a file
shows how asciiwrite
works if you give it an array of data to write to a file.
Each value is written on a line by itself, which is not
very useful! The first few lines of "file1.txt" will look like this:
-1.762895
-1.75608
-0.06612359
-2.112701
write_asc_2.ncl: This example
shows how to write a mix of data (string, float, and integer)
using
write_table.
Here's a sample of the first few lines of the "file2.txt" ASCII file:
string_031 -0.11 1 269.1 string_040
string_074 5.17 2 798.3 string_018
string_015 8.73 3 408.6 string_082
string_037 2.14 4 546.1 string_054
write_asc_3.ncl: This example
writes the same ASCII file as the previous example, except it
uses a combination of
sprintf and
sprinti
to format the data, and then
asciiwrite to write the
strings to a file.
This method can be *very* slow if you have a lot of data to format.
write_asc_4.ncl: This example
shows how to write a 3D array to a file, by writing 2D blocks of
data at a time. In general, we do not recommend writing large
arrays to an ASCII file as this is very inefficient.
We have to use write_table because it is the only NCL
function that allows you to append data to an existing ASCII
file. This function requires the data be in a
list object, so we push each column of
a 2D subsection of data to the list object, and then write out that 2D
list of data to the file using the "append" option.
This method gets a little kludgy because you have to use a unique
variable for every "push" onto the list object. We do this by
using unique_string to generate a unique string,
and attributes.
Here's a sample of the first few lines of the "file4.txt" ASCII file:
This ASCII file contains 200 blocks of 100 x 10 arrays
Block 1 of 200
Row 1 -1.763 -1.756 -0.066 -2.113 -1.470 -3.460 0.662 3.207 -1.745 -1.599
Row 2 3.952 -1.634 -2.150 0.034 2.735 -4.788 -4.630 -2.094 -4.139 2.476
Row 3 -1.406 -2.919 -4.202 -3.521 2.082 -1.046 2.644 1.459 -0.269 3.595
. . .
Row 98 -4.095 2.166 -4.932 -0.383 -0.702 4.747 1.909 0.623 -4.141 0.379
Row 99 -2.845 2.130 -3.881 0.845 1.950 -1.425 1.977 -1.088 -1.459 -3.042
Row 100 0.865 -1.304 -0.517 -4.102 2.249 0.197 -2.007 -1.078 3.763 -0.978
Block 2 of 200
Row 1 0.695 2.282 3.158 -4.170 -2.409 2.100 -3.492 -2.287 -1.743 3.135
Row 2 2.355 2.495 -1.418 -1.763 -3.143 -0.626 3.708 4.102 -2.925 -4.333
Row 3 2.375 -3.973 3.779 3.636 -3.529 4.529 2.923 3.823 -3.138 2.652
. . .
write_asc_5.ncl: This example
shows how to write nicely-formatted two-dimensional arrays of numeric
data to an ASCII file using
write_matrix. This
script creates three ASCII files, one with float data, one with
integer data, and one with double data.
The float data contains two missing values. Here's
what the "file5.f.txt" output ASCII file looks like:
floating point data with two missing values
4.35 4.36 9.73 4.91 1.77 -0.63 -4.29
4.39******* -5.84 4.59 3.68 -14.12 0.07
0.27 3.77 0.89 -3.09 5.08 -2.51 5.85
-3.35 -1.66 8.46******* 0.14 1.76 0.87
-6.90 4.06 10.39 4.56 -5.63 -1.43 8.65