Write CSV files in IDL

How to write CSV and tab-delimited files from IDL with WRITE_CSV or PRINTF, what changes when data becomes text, and what to check before sharing.

Writing a CSV file from IDL turns in-memory arrays or structures into plain delimited text that spreadsheets, databases and scripts in other languages can read. This review covers what the conversion keeps, what it can lose, and when another output format is a better choice. No tested measurements were supplied for this article, so the behavior below comes from IDL's documented routines and general CSV principles. Points that depend on your IDL version or destination program are marked as things to verify.

Is CSV output from IDL suitable for your use?

CSV is a good fit when your data is a flat table, meaning one value per cell and the same number of columns in every row, and the file needs to be read outside IDL. Its main benefit is reach. Almost any tool can open it, and people can inspect it in a text editor.

The main tradeoff is that CSV stores only text. Data types, array dimensions beyond two, units, missing-value conventions and metadata are not preserved unless you encode them yourself. You choose how numbers become strings, and the reading program chooses how to turn them back.

Other formats are preferable in these cases:

  • For data that will only be reloaded in IDL, an IDL SAVE file keeps variables and types intact.
  • For multi-dimensional, typed or metadata-heavy scientific data, a self-describing format such as netCDF or HDF5 avoids flattening and guessing.
  • For a human-readable report with aligned columns, formatted text written with PRINTF reads better than CSV.
  • If a destination expects tabs, or your text fields often contain commas, a tab-delimited file written with PRINTF is simpler than relying on quoting.

How to write a CSV file with WRITE_CSV

IDL's built-in WRITE_CSV procedure takes a filename and the data to write. The data can be several separate vectors of equal length, which become columns, a two-dimensional array, or a structure whose fields are vectors. Check your version's documentation for the maximum number of separate vector arguments. The HEADER keyword adds a row of column names. The TABLE_HEADER keyword adds descriptive lines above that row.

names = ['alpha', 'beta', 'gamma']
x = [1.5, 2.25, 3.0]
y = [10, 20, 30]
WRITE_CSV, 'output.csv', names, x, y, HEADER=['name', 'x', 'y']

With a structure, you can take column names from the field names. TAG_NAMES returns them in uppercase, so pass your own string array if case matters to the destination.

data = {name: names, x: x, y: y}
WRITE_CSV, 'output.csv', data, HEADER=TAG_NAMES(data)

For a two-dimensional array, IDL's index order differs from the row-and-column order many people expect. Write a small array with distinct values and open the result before relying on the orientation.

Writing tab-delimited (TSV) or custom-delimited files

WRITE_CSV writes comma-separated output. For tabs or another delimiter, open a file and write each row yourself. Join the fields with STRJOIN and use STRING(9B) for the tab character. This approach also gives you full control over number formatting.

tab = STRING(9B)
OPENW, lun, 'output.tsv', /GET_LUN
PRINTF, lun, STRJOIN(['name', 'x', 'y'], tab)
FOR i = 0, N_ELEMENTS(names) - 1 DO BEGIN
  row = [names[i], STRING(x[i], FORMAT='(F0.4)'), STRTRIM(y[i], 2)]
  PRINTF, lun, STRJOIN(row, tab)
ENDFOR
FREE_LUN, lun

Hand-written output does no quoting. If any text field can contain the delimiter, a quote character or a line break, you must escape or quote it yourself, or the columns will shift when the file is read.

Need | WRITE_CSV | OPENW with PRINTF
Comma delimiter | Built in | Manual
Tab or other delimiter | Not supported | Yes, via STRJOIN
Header row | HEADER keyword | Write it as the first line
Number formatting control | Limited; verify output | Full, via FORMAT codes
Quoting of text fields | Handled by the routine; verify rules for your version | Your responsibility

What the conversion actually changes

This is a change to stored content, not to presentation. Each numeric value is converted to a string of characters at write time, and that string is all the file keeps. Three things are affected.

  • Precision: default number-to-string conversion may not keep every significant digit, especially for double-precision values. If exact values matter, use explicit FORMAT codes and compare a read-back copy with the original.
  • Type: the file does not record whether a column was integer, float, double or string. The reading program infers types, and different programs may infer differently.
  • Special values: how NaN, infinity and empty strings appear in the output was not verified for this article. Check a sample file, because readers may treat these as text or as missing values inconsistently.

Spreadsheet display is a separate layer. A spreadsheet may show a long number in scientific notation, drop leading zeros from an ID or reinterpret a value as a date. Those changes happen at import or display. The file may be correct while the spreadsheet view is not. Check the raw file in a text editor before deciding which layer is at fault.

Checking the result by reading it back

A quick round trip catches most problems. READ_CSV returns a structure with one field per column. The HEADER output keyword returns the column names, so you can compare columns, headers and values against the source.

result = READ_CSV('output.csv', HEADER=hdr)
HELP, result, /STRUCTURE
PRINT, hdr

A clean round trip in IDL does not prove another program will read the file the same way. Test a sample in the actual destination as well.

Will the file stay readable and usable when shared?

CSV is plain text, so copying, pasting and version-control diffs generally work as they would for any text file. Reading order is simply line by line and left to right. A text editor exposes the content to assistive technology like any other text, though a long row of comma-separated values is hard to follow by ear. In spreadsheets, a clear header row is what gives each value context, so include one and use descriptive column names. No assistive-technology testing was supplied for this article.

Compatibility limits to check for your destination:

  • Character encoding: IDL writes string bytes as they are. Non-ASCII text may display incorrectly if the reading program assumes a different encoding.
  • Line endings: confirm that the destination accepts the line endings produced on your platform.
  • Regional settings: some locales use the comma as a decimal separator and expect semicolon-delimited files. A comma CSV may open as a single column there.
  • Quoting rules: confirm how your IDL version quotes text containing commas or quotes, and whether the destination reads it the same way.

Gaps in this review

No version-specific documentation excerpts or test files were supplied. The exact quoting behavior of WRITE_CSV, its representation of NaN and empty values, its default numeric precision, its line endings, and how named spreadsheet or database tools import its output remain unverified. Before you depend on any of these, test them with a small sample in your IDL version and in your intended destination.