NCL Home> Application examples> File IO || Data files for some examples

Example pages containing: tips | resources | functions/procedures

NCL: Writing CSV (comma-separated values) files

CSV files are ASCII files whose values are separated by commas or other separators (semicolons, spaces, etc).

You can write CSV files using a combination of one or more of these functions:

  • write_table - this is the best function to use, as it allows you to write mixed data types and to append to an existing file.

  • asciiwrite - an older and rather limited function that writes one value per line. This is useful for outputting a one-dimensional time series.

  • sprintf / 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_csv_1.ncl: Shows how to create this very simple CSV file:

34,67,56
36,87,78
31,56,88
29,67,92
54,71,68
42,65,82
Each of the three columns of data are stored in their own variable in the NCL script.

A Python version of this example is available here.

write_csv_2.ncl: Shows how to read data off a NetCDF file and write to a CSV file using write_table. A header is written using the long_name and units attributes for each variable.

Seven variables are written, three of which are 4D arrays dimensioned time x lev x lat x lon, and have to be converted to 1D arrays in order to write them as single columns. The other four variables are the 1D coordinate arrays (time, lev, lat, and lon) which have to be conformed to 4D arrays using conform_dims, before writing them along with the other 4D arrays.

write_csv_3.ncl: Shows how to write data of mixed types to a CSV file using write_table. A header is also written, with each field name enclosed in double quotes.

This example uses the following format string to write a mix of floats, integers, and strings to the file:

  format = "%2d,%6.2f,%7.2f,%4d,%s,%1d"

The resultant "example3.csv" file should look like this:

"ID","LAT","LON","ELEV","SOURCE","FLAGS"
 4,-27.75, 152.45,-999,ADAM,0
 5,-27.03, 152.02,-999,MARY,0
 6,-26.76, 148.82,-999,DAVE,1
 7,-26.58, 148.77,-999,RICK,0
 8,-26.48, 148.68,1000,DENNIS,0
 9,-26.30, 148.52, 900,TIM,0
10,-26.25, 148.41,-999,SPONGEBOB,0

If you don't want any spaces in your CSV file, then don't specify any kind of field lengths in the format string:

  format = "%d,%g,%g,%g,%s,%d"

The resultant file would then look like this:

"ID","LAT","LON","ELEV","SOURCE","FLAGS"
4,-27.75,152.45,152.45,ADAM,0
5,-27.03,152.02,152.02,MARY,0
6,-26.76,148.82,148.82,DAVE,1
7,-26.58,148.77,148.77,RICK,0
8,-26.48,148.68,148.68,DENNIS,0
9,-26.3,148.52,148.52,TIM,0
10,-26.25,148.41,148.41,SPONGEBOB,0
write_csv_4.ncl: This example is similar to write_csv_3.ncl, except it shows how to append data to a CSV file, one line at a time. write_table is used to append the data.

This can be useful if you are reading data from a file or a list of files inside a do loop, and need to append data to the CSV as you go.

This example uses random_uniform to generate various forms of random data inside a do loop. As with the previous example, you can modify the "format" string to change the way the output is written to the file.

The resultant "example4.csv" file will look something like this:

"ID","NAME","LAT","LON","ELEV","SOURCE"
1,-31.7321,-63.2189,493.388,dummy_source_289
2,-26.4588,-124.574,566.211,dummy_source_820
3,-31.411,-57.5713,895.177,dummy_source_337
4,-38.6959,1.21715,773.531,dummy_source_22
5,-83.3376,-75.3851,86.0775,dummy_source_747
6,-25.2997,-105.074,79.7663,dummy_source_148
 . . .

If you use the "format" string with the formatted output, then the CSV file will look something like this:

"ID","NAME","LAT","LON","ELEV","SOURCE"
  1, -31.73,  -63.22, 493.4, dummy_source_289
  2, -26.46, -124.57, 566.2, dummy_source_820
  3, -31.41,  -57.57, 895.2, dummy_source_337
  4, -38.70,    1.22, 773.5, dummy_source_22
  5, -83.34,  -75.39,  86.1, dummy_source_747
  6, -25.30, -105.07,  79.8, dummy_source_148
 . . .
write_csv_5.ncl: This example shows how to calculate temperature at 2m from a list of WRF-ARW output files, and then write select values to a CSV file using an array of desired lat/lon values.

Here's what the output "wrf_2m_temperature.csv" file will look like:

"TIME","LAT","LON","TEMPERATURE (degC)"
2008-09-29_16:00:00, 29.87, 129.90,300.85
2008-09-29_16:00:00, 39.94, 134.95,291.25
2008-09-29_16:00:00, 49.95, 140.00,275.70
2008-09-30_00:00:00, 29.87, 129.90,300.40
2008-09-30_00:00:00, 39.94, 134.95,291.33
2008-09-30_00:00:00, 49.95, 140.00,279.48