String variables are defined using DEFINE VARIABLE (or its alias LET). They can be read from and written to netCDF files. Arrays of strings may be defined and a limited number of algebraic operations are defined for string variables. They can be passed to go scripts and functions. Grave accents around a scalar string return the string.
yes? LET astring = "hello everyone"
yes? LIST/NOHEAD astring
"hello everyone"
yes? message `astring`
!-> message hello everyone
hello everyone
Hit Carriage Return to continue
7.1.1 String arrays
Strings in arrays may be of variable length. The syntax {"a","b","c"} denotes an array of strings. Two commas in a row denotes a null (missing value) string. Single and double quoted strings are both allowed, but must match for an individual string, e.g. 'P" is not valid.
Examples: the following define and list valid string arrays:
yes? LET a = {"s1","s2", ,"s3"}
yes? LIST a
{"s1","s2", ,"s3"}
1 / 1:"s1"
2 / 2:"s2"
3 / 3:""
4 / 4:"s3"
yes? LET b = {, 'string1','s2',,'cccc'}
yes? LIST/i=3:5 b
{, 'string1','s2',,'cccc'}
3 / 3:"s2"
4 / 4:""
5 / 5:"cccc"
yes? LET c = {'p', 'q', 'a longer string'}
yes? LIST/NOHEAD/ORDER=x c
"p" "q" "a longer string"
String arrays may also be defined which contain information about the contents of netCDF files. The keywords varnames, attnames, and dimnames may be used to define variables with lists of these names. See Chapter 3 Section 1.8.2 for more on Attribute Keywords
yes? USE coads_climatology
yes? LET allvars = ..varnames
yes? LIST allvars
VARIABLE : ..VARNAMES
FILENAME : coads_climatology.cdf
FILEPATH : /home/porter/tmap/ferret/linux/fer_dsets/data/
SUBSET : 7 points (X)
1 / 1:"SST"
2 / 2:"AIRT"
3 / 3:"SPEH"
4 / 4:"WSPD"
5 / 5:"UWND"
6 / 6:"VWND"
7 / 7:"SLP"
yes? LET alldims = ..dimnames
yes? LIST alldims
VARIABLE : ..DIMNAMES
FILENAME : coads_climatology.cdf
FILEPATH : /home/porter/tmap/ferret/linux/fer_dsets/data/
SUBSET : 3 points (X)
1 / 1:"COADSX"
2 / 2:"COADSY"
3 / 3:"TIME"
yes? LET sst_at = sst.attnames
yes? LIST sst_at
VARIABLE : SST.ATTNAMES
FILENAME : coads_climatology.cdf
FILEPATH : /home/porter/tmap/ferret/linux/fer_dsets/data/
SUBSET : 5 points (X)
1 / 1:"missing_value"
2 / 2:"_FillValue"
3 / 3:"long_name"
4 / 4:"history"
5 / 5:"units"
! The string function IS_ELEMENT_OF_STR can check for an element in the list
! The function is case sensitive.
yes? LET has_sst = IS_ELEMENT_OF_STR(allvars,{"SST","sst"})
7.1.2 Writing strings to files
To write strings to ascii files, formats specified with /FORMAT=tab or /FORMAT=comma are often useful.
yes? LET a = {"s1","s2", ,"s3"}
yes? LET b = {, 'string1','s2',,'cccc'}
yes? LIST/format=tab/norowhead a,b
yes? LIST/format=tab/norowhead a,b
X: 0.5 to 5.5
Column 1: A is {"s1","s2", ,"s3","s4"} BAD FLAG : -1.E+34
Column 2: B is {, 'string1','s2',,'cccc'} BAD FLAG : -1.E+34
A B
"s1" ""
"s2" "string1"
"" "s2"
"s3" ""
"s4" "cccc"
When a single string variable is being listed, the Fortran format (A) may be used, but not in combination with other variables. This is useful for writing a header line to an ASCII file.
yes? let header = "Station 51 data"
yes? list/clobber/format=(A)/noheader/file=my_output_file.dat header
yes? let header = "Date T1 S1"
yes? list/append/format=(A)/noheader/file=my_output_file.dat header
yes? list/append/nohead/norowhead/format=tab/file=my_output_file.dat datestrings, t1, s1
yes? sp cat my_output_file.dat
Station 51 data
Date T1 S1
"1-jan-2010" 15.55 1.342
"2-jan-2010" 14.12 1.33
"5-jan-2010" 15.12 1.36