| 1 |
<html><!-- InstanceBegin template="/Templates/MyUnidata.dwt" codeOutsideHTMLIsLocked="true" --> |
| 2 |
|
| 3 |
<head> |
| 4 |
<!-- InstanceBeginEditable name="Title" --> |
| 5 |
<TITLE>Introduction to FAN Language and Utilities </TITLE> <link rel="shortcut icon" href="https://www.unidata.ucar.edu/favicon.ico" type="image/x-icon" /> |
| 6 |
|
| 7 |
</head> |
| 8 |
|
| 9 |
<body> |
| 10 |
<!-- InstanceBeginEditable name="Content Goes Here" --> |
| 11 |
<h1 align="center">Introduction to FAN Language and Utilities <br /> |
| 12 |
FAN Version 2.0 <br /> |
| 13 |
</h1> |
| 14 |
<h3 align="center">Harvey Davies <br /> |
| 15 |
CSIRO Division of Atmospheric Research, <br /> |
| 16 |
Private Bag No. 1, Mordialloc 3195, Australia <br /> |
| 17 |
email: [email protected] <br /> |
| 18 |
<br /> |
| 19 |
Scientific Visitor from January to August 1996 at <br /> |
| 20 |
UCAR/Unidata Program Center, <br /> |
| 21 |
P.O. Box 3000, Boulder, Colorado 80307-3000, USA <br /> |
| 22 |
email: [email protected] <br /> |
| 23 |
</h3> |
| 24 |
<hr /> |
| 25 |
<h1>Introduction</h1> |
| 26 |
<p><em>FAN (File Array Notation)</em> is an array-oriented language for identifying |
| 27 |
data items in files for the purpose of extraction or modification. FAN specifications |
| 28 |
consist of</p> |
| 29 |
<ul> |
| 30 |
<li>one or more filenames</li> |
| 31 |
<li>one or more variable (array) names or ID numbers</li> |
| 32 |
<li>attribute names or ID numbers (optional)</li> |
| 33 |
<li>dimension names or ID numbers (optional)</li> |
| 34 |
<li>subscripts in various possible forms (optional)</li> |
| 35 |
</ul> |
| 36 |
<p>NetCDF is the only format currently supported. However FAN is intended to be |
| 37 |
generic and it is hoped that there will eventually also be FAN interfaces to |
| 38 |
various other formats.</p> |
| 39 |
<p>This document describes the FAN language and four utilities based on FAN. The |
| 40 |
use of these utilities can greatly decrease the need for programming in Fortran |
| 41 |
or C. They can be called from the Unix command line and shell scripts.</p> |
| 42 |
<p>The first is <tt><b>nc2text</b></tt> which prints selected data from netCDF |
| 43 |
variables. The standard utility <tt><b>ncdump</b></tt> can also print data from |
| 44 |
netCDF variables, but only entire variables and only together with metadata |
| 45 |
in CDL form.</p> |
| 46 |
<p>The second is <tt><b>ncmeta</b></tt> which prints selected metadata from one |
| 47 |
or more netCDF files. This metadata can include rank, shape, file names, variable |
| 48 |
names, dimension names and attribute names.</p> |
| 49 |
<p>The third is <tt><b>ncrob</b></tt> which reads data from one or more netCDF |
| 50 |
variables, performs some process on it and then either prints the result or |
| 51 |
writes it to a netCDF array. The letters `<tt><b>rob</b></tt>' in `<tt><b>ncrob</b></tt>' |
| 52 |
stand for <em>Reduce Or Broadcast</em>. <em>Reduce</em> means to produce an |
| 53 |
array (e.g. sum, mean, maximum) with less dimensions than the original. <em>Broadcast</em> |
| 54 |
means to copy one array to another, recycling values if necessary. An example |
| 55 |
is copying the same vector to each row of a matrix. It is possible to process |
| 56 |
large volumes of data (e.g. 100 MB) using <tt><b>ncrob</b></tt>.</p> |
| 57 |
<p>The fourth is <tt><b>text2nc</b></tt> which can be used to read small volumes |
| 58 |
(say up to a few thousand lines) of ASCII data and copy it into netCDF variables. |
| 59 |
It is also possible to use <tt><b>text2nc</b></tt> to create, modify and delete |
| 60 |
attributes.</p> |
| 61 |
<p>This document does not cover other ways of using FAN. These include some local |
| 62 |
(CSIRO DAR) utilities (e.g. contouring program <tt><b>con_cif</b></tt>), the |
| 63 |
array-oriented languages IDL and J (for which there are FAN interfaces) and |
| 64 |
direct use of the C API (application programmer interface).</p> |
| 65 |
<h2>Simple Examples</h2> |
| 66 |
<p>Let us start with a simple netCDF file <tt><b>vec.nc</b></tt> which is printed |
| 67 |
(in CDL) as follows:</p> |
| 68 |
<pre> |
| 69 |
<strong>$ ncdump vec.nc |
| 70 |
netcdf vec { |
| 71 |
dimensions: |
| 72 |
n = UNLIMITED ; // (5 currently) |
| 73 |
variables: |
| 74 |
float v(n) ; |
| 75 |
data: |
| 76 |
v = 10 , 20.3 , 30.2 , 40.9 , 50 ; |
| 77 |
} |
| 78 |
</strong> |
| 79 |
</pre> |
| 80 |
<p>Here `<tt><b>$</b></tt>' is the UNIX command-line prompt. The following |
| 81 |
uses <tt><b>nc2text</b></tt> to print the whole array <tt><b>v</b></tt>:</p> |
| 82 |
<pre> |
| 83 |
<strong>$ nc2text vec.nc v |
| 84 |
10 20.3 30.2 40.9 50 |
| 85 |
</strong> |
| 86 |
</pre> |
| 87 |
<p>Individual elements can be selected using subscripts. For example:</p> |
| 88 |
<pre> |
| 89 |
<strong>$ nc2text vec.nc 'v[0]' |
| 90 |
10 |
| 91 |
$ nc2text vec.nc 'v[3]' |
| 92 |
40.9 |
| 93 |
</strong> |
| 94 |
</pre> |
| 95 |
<p>Several can be selected using a subscript consisting of a list of indices such |
| 96 |
as:</p> |
| 97 |
<pre> |
| 98 |
<strong>$ nc2text vec.nc 'v[0 3 1 3]' |
| 99 |
10 40.9 20.3 40.9 |
| 100 |
</strong> |
| 101 |
</pre> |
| 102 |
<p>We can write to a netCDF file using <tt><b>text2nc</b></tt>. The following |
| 103 |
changes the third element from 30.2 to 30.7 and then prints <tt><b>v</b></tt>:</p> |
| 104 |
<pre> |
| 105 |
<strong>$ echo 30.7 | text2nc vec.nc 'v[2]' |
| 106 |
$ nc2text vec.nc v |
| 107 |
10 20.3 30.7 40.9 50 |
| 108 |
</strong> |
| 109 |
</pre> |
| 110 |
<p>Here <tt><b>text2nc</b></tt> reads ASCII text data from standard input, which |
| 111 |
in this case is a pipe connected to the standard output of <tt><b>echo</b></tt>. |
| 112 |
Since the dimension has <tt><b>UNLIMITED</b></tt> size, we can append values |
| 113 |
as follows:</p> |
| 114 |
<pre> |
| 115 |
<strong>$ echo 60.5 70.2 | text2nc vec.nc 'v[5 6]' |
| 116 |
$ nc2text vec.nc v |
| 117 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 118 |
</strong> |
| 119 |
</pre> |
| 120 |
<p>Next we use <tt><b>ncrob</b></tt> to calculate and print the arithmetic mean |
| 121 |
of <tt><b>v</b></tt>.</p> |
| 122 |
<pre> |
| 123 |
<strong>$ ncrob -r am vec.nc v / |
| 124 |
40.3714 |
| 125 |
</strong> |
| 126 |
</pre> |
| 127 |
<p>The option <tt><b>-r am</b></tt> specifies that an <em>arithmetic mean</em> |
| 128 |
is to be calculated. The following example stores the mean in the same file, |
| 129 |
naming the variable <tt><b>v_mean</b></tt>:</p> |
| 130 |
<pre> |
| 131 |
<strong>$ ncrob -r am vec.nc v / v_mean |
| 132 |
$ nc2text vec.nc v_mean |
| 133 |
40.3714 |
| 134 |
</strong> |
| 135 |
</pre> |
| 136 |
<p>The `<tt><b>/</b></tt>' separates the input from the output. If no output |
| 137 |
is specified then results are printed. In fact <tt><b>ncrob</b></tt> can be |
| 138 |
used in place of <tt><b>nc2text</b></tt> to print data from a netCDF file. E.g.</p> |
| 139 |
<pre> |
| 140 |
<strong>$ ncrob vec.nc v / |
| 141 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 142 |
$ ncrob vec.nc v_mean / |
| 143 |
40.3714 |
| 144 |
</strong> |
| 145 |
</pre> |
| 146 |
<p>Finally we use <tt><b>ncmeta</b></tt> to print metadata. The shape is printed |
| 147 |
by:</p> |
| 148 |
<pre> |
| 149 |
<strong>$ ncmeta v vec.nc |
| 150 |
5 |
| 151 |
</strong> |
| 152 |
</pre> |
| 153 |
<p>and the following prints the variable name, dimension name and shape:</p> |
| 154 |
<pre> |
| 155 |
<strong>$ ncmeta -w vds v vec.nc |
| 156 |
v n 5 |
| 157 |
</strong> |
| 158 |
</pre> |
| 159 |
<h2>What is New in Version 2?</h2> |
| 160 |
<p>The utility <tt><b>ncmeta</b></tt> is new.</p> |
| 161 |
<p>There are significant enhancements to the utility <tt><b>ncrob</b></tt>. It |
| 162 |
can now print results as well as write them to netCDF files. (This means that |
| 163 |
<tt><b>nc2text</b></tt> is no longer really needed.) In version 1 the output |
| 164 |
FAN specification could only be a single (final) argument. There can now be |
| 165 |
zero (implying printed output) or more output arguments following a `<tt><b>/</b></tt>' |
| 166 |
which separates input arguments from output arguments. (The old convention is |
| 167 |
deprecated but still supported.) It is now possible to create new variables |
| 168 |
without specifying the <tt><b>-c</b></tt> option or an output filename. There |
| 169 |
is a facility for merging dimensions. There are several new options related |
| 170 |
to printing and similar to those of <tt><b>nc2text</b></tt>. A number of bugs |
| 171 |
in <tt><b>ncrob</b></tt> have been fixed, including one with a serious effect |
| 172 |
on speed.</p> |
| 173 |
<h1>FAN Language</h1> |
| 174 |
<p><a id="High_level_Syntax" name="High_level_Syntax"></a></p> |
| 175 |
<h2>High-level Syntax</h2> |
| 176 |
<p>A FAN specification can be either a single command-line argument or span several |
| 177 |
arguments. Use of multiple arguments decreases the need for quoting and allows |
| 178 |
use of UNIX <em>wildcarding</em> (a.k.a. <em>globbing</em>) facilities. A FAN |
| 179 |
specification can have any of the following forms:</p> |
| 180 |
<center> |
| 181 |
<table border="1" summary="syntax meaning"> |
| 182 |
<tr> |
| 183 |
<td><b>Syntax</b> </td> |
| 184 |
<td><b>Meaning</b></td> |
| 185 |
</tr> |
| 186 |
<tr> |
| 187 |
<td><em>fanio</em> <tt><b>/</b></tt> <em>fanio</em> </td> |
| 188 |
<td>netCDF input and netCDF output</td> |
| 189 |
</tr> |
| 190 |
<tr> |
| 191 |
<td><em>fanio</em> <tt><b>/</b></tt> </td> |
| 192 |
<td>netCDF input and output to <tt><b>stdout</b></tt> (i.e. printed)</td> |
| 193 |
</tr> |
| 194 |
<tr> |
| 195 |
<td><em>fanio</em> </td> |
| 196 |
<td>Either netCDF input or netCDF output (but not both)</td> |
| 197 |
</tr> |
| 198 |
</table> |
| 199 |
</center> |
| 200 |
<p>where <em>fanio</em> is a FAN input/output specification, which has the form: |
| 201 |
<br /> |
| 202 |
<em>pair</em> <tt><b>;</b></tt> <em>pair</em> <tt><b>;</b></tt> <em>pair</em> |
| 203 |
<tt><b>;</b></tt> ... <br /> |
| 204 |
A semicolon (`<tt><b>;</b></tt>') has the same effect as commencing a new |
| 205 |
argument. Any sequence of one or more whitespace characters (space, tab, newline) |
| 206 |
is equivalent to a single space.</p> |
| 207 |
<p>A <em>pair</em> can take any of the following forms: <br /> |
| 208 |
<em>filename vas</em> <br /> |
| 209 |
<em>vas filename</em> <br /> |
| 210 |
<em>filename</em> <br /> |
| 211 |
<em>vas</em> <br /> |
| 212 |
</p> |
| 213 |
<p>A <em>filename</em> must contain at least one period (`<tt><b>.</b></tt>') |
| 214 |
to distinguish it from a variable name. This will be the case if netCDF filenames |
| 215 |
have a conventional suffix such as the recommended <tt><b>.nc</b></tt>. (In |
| 216 |
any case it is always possible to prefix a redundant `<tt><b>./</b></tt>' |
| 217 |
directory as in `<tt><b>./unconventional</b></tt>' or `<tt><b>/usr/./IdidItMyWay</b></tt>'!)</p> |
| 218 |
<p>A <em>vas</em> is a <em>variable or attribute specification</em> which can |
| 219 |
have any of the following forms: <br /> |
| 220 |
<em>var</em> <br /> |
| 221 |
<em>var</em><tt><b>[</b></tt><em>subscript</em><tt><b>,</b></tt> <em>subscript</em><tt><b>,</b></tt> |
| 222 |
<em>subscript</em><tt><b>,</b></tt> ...<tt><b>]</b></tt> <br /> |
| 223 |
<em>var</em><tt><b>[</b></tt><em>subscript</em><tt><b>,</b></tt> <em>subscript</em><tt><b>,</b></tt> |
| 224 |
<em>subscript</em><tt><b>,</b></tt> ...<tt><b>)</b></tt> <br /> |
| 225 |
<em>var</em><tt><b>(</b></tt><em>subscript</em><tt><b>,</b></tt> <em>subscript</em><tt><b>,</b></tt> |
| 226 |
<em>subscript</em><tt><b>,</b></tt> ...<tt><b>]</b></tt> <br /> |
| 227 |
<em>var</em><tt><b>(</b></tt><em>subscript</em><tt><b>,</b></tt> <em>subscript</em><tt><b>,</b></tt> |
| 228 |
<em>subscript</em><tt><b>,</b></tt> ...<tt><b>)</b></tt> <br /> |
| 229 |
<em>var</em><tt><b>:</b></tt><em>att</em> <br /> |
| 230 |
<tt><b>:</b></tt><em>att</em> <br /> |
| 231 |
where <em>var</em> is a variable name or ID number and <em>att</em> is an attribute |
| 232 |
name or ID number. It is usually more convenient to identify variables, attributes |
| 233 |
and dimensions by name rather than ID number. The use of ID numbers is discussed |
| 234 |
in Section <a |
| 235 |
href="#Using_ID_Numbers"><em>Using ID Numbers</em></a>. Attributes are discussed |
| 236 |
in Section <a |
| 237 |
href="#Attributes"><em>Attributes</em></a>.</p> |
| 238 |
<p>A pair without a <em>filename</em> or <em>vas</em> uses that of the previous |
| 239 |
pair. The first pair has no effect by itself unless it contains both a <em>filename</em> |
| 240 |
and a <em>vas</em>. Thus the following all access the same values:</p> |
| 241 |
<pre> |
| 242 |
<strong>$ nc2text 'vec.nc v[0 4]' |
| 243 |
10 50 |
| 244 |
$ nc2text 'v[0 4] vec.nc' |
| 245 |
10 50 |
| 246 |
$ nc2text vec.nc 'v[0 4]' |
| 247 |
10 50 |
| 248 |
$ nc2text 'v[0 4]' vec.nc |
| 249 |
10 50 |
| 250 |
$ nc2text ' v [ 0 4 ] vec.nc ' |
| 251 |
10 50 |
| 252 |
</strong> |
| 253 |
</pre> |
| 254 |
<p>The following are equivalent ways of concatenating variables <tt><b>v</b></tt> |
| 255 |
and <tt><b>v_mean</b></tt>:</p> |
| 256 |
<pre> |
| 257 |
<strong>$ nc2text 'vec.nc v' 'vec.nc v_mean' |
| 258 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 259 |
40.3714 |
| 260 |
$ nc2text 'vec.nc v' 'v_mean' |
| 261 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 262 |
40.3714 |
| 263 |
$ nc2text 'vec.nc v; v_mean' |
| 264 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 265 |
40.3714 |
| 266 |
$ nc2text vec.nc v v_mean |
| 267 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 268 |
40.3714 |
| 269 |
</strong> |
| 270 |
</pre> |
| 271 |
<p>Now let us copy file <tt><b>vec.nc</b></tt> to <tt><b>vec_new.nc</b></tt> and |
| 272 |
then demonstrate concatenation of data from different files:</p> |
| 273 |
<pre> |
| 274 |
<strong>$ cp vec.nc vec_new.nc |
| 275 |
$ nc2text v vec.nc vec_new.nc |
| 276 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 277 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 278 |
$ nc2text v vec*.nc |
| 279 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 280 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 281 |
</strong> |
| 282 |
</pre> |
| 283 |
<p>Note the use of UNIX <em>wildcarding</em> facilities in the latter example |
| 284 |
using the <em>metacharacter</em> `<tt><b>*</b></tt>' in <tt><b>vec*.nc</b></tt> |
| 285 |
which matches both <tt><b>vec.nc</b></tt> and <tt><b>vec_new.nc</b></tt>.</p> |
| 286 |
<h2>Subscripts</h2> |
| 287 |
<p>As mentioned in Section <a href="#High_level_Syntax"><em>High level Syntax</em></a>, |
| 288 |
subscripts are enclosed by either `<tt><b>[</b></tt>' or `<tt><b>(</b></tt>' |
| 289 |
on the left and either `<tt><b>]</b></tt>' or `<tt><b>)</b></tt>' on |
| 290 |
the right.</p> |
| 291 |
<p>A left bracket `<tt><b>[</b></tt>' implies the C convention of starting |
| 292 |
subscripts at 0; while a left parenthesis `<tt><b>(</b></tt>' implies the |
| 293 |
Fortran convention of starting at 1. This starting value of 0 or 1 is called |
| 294 |
the <em>index origin</em>. A mnemonic to associate <em>left</em> with <em>index |
| 295 |
origin</em> is an <em>x-axis with the origin on the left</em>.</p> |
| 296 |
<p>The right hand delimiter controls the relative significance of multiple dimensions. |
| 297 |
A `<tt><b>]</b></tt>' implies conventional <em>row-major</em> (or <em>lexicographic</em>) |
| 298 |
order in which the rightmost subscript varies fastest; while a `<tt><b>)</b></tt>' |
| 299 |
implies the Fortran convention of <em>column-major</em> order in which the leftmost |
| 300 |
subscript varies fastest.</p> |
| 301 |
<p>So far our examples have involved only a single dimension. Now consider a netCDF |
| 302 |
file <tt><b>mat.nc</b></tt> containing a 2-dimensional array (i.e. a matrix). |
| 303 |
We print it as follows:</p> |
| 304 |
<pre> |
| 305 |
<strong>$ ncdump mat.nc |
| 306 |
netcdf mat { |
| 307 |
dimensions: |
| 308 |
row = 2 ; |
| 309 |
col = 3 ; |
| 310 |
variables: |
| 311 |
short M(row, col) ; |
| 312 |
data: |
| 313 |
M = |
| 314 |
11, 12, 13, |
| 315 |
21, 22, 23 ; |
| 316 |
} |
| 317 |
</strong> |
| 318 |
</pre> |
| 319 |
<p>The following are equivalent ways of printing the final element:</p> |
| 320 |
<pre> |
| 321 |
<strong>$ nc2text 'mat.nc M[1,2]' |
| 322 |
23 |
| 323 |
$ nc2text 'mat.nc M(2,3]' |
| 324 |
23 |
| 325 |
$ nc2text 'mat.nc M(3,2)' |
| 326 |
23 |
| 327 |
$ nc2text 'mat.nc M[2,1)' |
| 328 |
23 |
| 329 |
</strong> |
| 330 |
</pre> |
| 331 |
<p>Subscript values can be less than the index origin and are then relative to |
| 332 |
the end. So the final element could also be accessed by:</p> |
| 333 |
<pre> |
| 334 |
<strong>$ nc2text 'mat.nc M[-1,-1]' |
| 335 |
23 |
| 336 |
$ nc2text 'mat.nc M(0,0)' |
| 337 |
23 |
| 338 |
</strong> |
| 339 |
</pre> |
| 340 |
<p>As we have seen before, a subscript can contain a list of indices. Thus one |
| 341 |
could use any of the following to select all rows, but exclude the middle column:</p> |
| 342 |
<pre> |
| 343 |
<strong>$ nc2text mat.nc 'M[0 1,0 2]' |
| 344 |
11 13 |
| 345 |
21 23 |
| 346 |
$ nc2text mat.nc 'M(1 2,1 3]' |
| 347 |
11 13 |
| 348 |
21 23 |
| 349 |
$ nc2text mat.nc 'M(1 3,1 2)' |
| 350 |
11 13 |
| 351 |
21 23 |
| 352 |
</strong> |
| 353 |
</pre> |
| 354 |
<h3>Triplet Notation</h3> |
| 355 |
<p>A sequence of indices forming an <em>arithmetic progression</em> as in</p> |
| 356 |
<pre> |
| 357 |
<strong>$ nc2text vec.nc 'v[0 2 4 6]' |
| 358 |
10 30.7 50 70.2 |
| 359 |
</strong> |
| 360 |
</pre> |
| 361 |
<p>can be specified using a generalization of Fortran 90 <em>triplet notation</em>, |
| 362 |
in this case:</p> |
| 363 |
<pre> |
| 364 |
<strong>$ nc2text vec.nc 'v[0:6:2]' |
| 365 |
10 30.7 50 70.2 |
| 366 |
</strong> |
| 367 |
</pre> |
| 368 |
<p>The triplet <tt><b>0:6:2</b></tt> means <em>0 to 6 in steps of 2</em>. A <em>triplet</em> |
| 369 |
can take two forms: <br /> |
| 370 |
<em>start</em><tt><b>:</b></tt><em>finish</em><tt><b>:</b></tt><em>stride</em> |
| 371 |
<br /> |
| 372 |
<em>start</em><tt><b>:</b></tt><em>finish</em> <br /> |
| 373 |
The second form implies a stride of 1. It is possible to omit <em>start</em> |
| 374 |
and/or <em>finish</em>. Let <var>I</var> be the index-origin (0 |
| 375 |
or 1). If the stride is positive then <em>start</em> defaults to <var>I</var> |
| 376 |
(i.e. first element) and <em>finish</em> to <var>I</var>-1 (i.e. |
| 377 |
final element). These are reversed for a negative stride; <em>start</em> defaults |
| 378 |
to <var>I</var>-1 and <em>finish</em> to <var>I</var>. E.g.</p> |
| 379 |
<pre> |
| 380 |
<strong>$ nc2text vec.nc v |
| 381 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 382 |
$ nc2text vec.nc 'v[:6:2]' |
| 383 |
10 30.7 50 70.2 |
| 384 |
$ nc2text vec.nc 'v[0::2]' |
| 385 |
10 30.7 50 70.2 |
| 386 |
$ nc2text vec.nc 'v[::2]' |
| 387 |
10 30.7 50 70.2 |
| 388 |
$ nc2text vec.nc 'v[0:2]' |
| 389 |
10 20.3 30.7 |
| 390 |
$ nc2text vec.nc 'v[:2]' |
| 391 |
10 20.3 30.7 |
| 392 |
$ nc2text vec.nc 'v[2:]' |
| 393 |
30.7 40.9 50 60.5 70.2 |
| 394 |
$ nc2text vec.nc 'v[::-1]' |
| 395 |
70.2 60.5 50 40.9 30.7 20.3 10 |
| 396 |
</strong> |
| 397 |
</pre> |
| 398 |
<p>Note how the latter example reverses the order. A triplet can wrap-around the |
| 399 |
start or end. This is useful with cyclic dimensions such as longitude. Wrap-around |
| 400 |
is shown by:</p> |
| 401 |
<pre> |
| 402 |
<strong>$ nc2text vec.nc 'v[3:1]' |
| 403 |
40.9 50 60.5 70.2 10 20.3 |
| 404 |
$ nc2text vec.nc 'v[1:3:-1]' |
| 405 |
20.3 10 70.2 60.5 50 40.9 |
| 406 |
</strong> |
| 407 |
</pre> |
| 408 |
<p>But the following does not imply wrap-around:</p> |
| 409 |
<pre> |
| 410 |
<strong>$ nc2text vec.nc 'v[0:-1:1]' |
| 411 |
10 20.3 30.7 40.9 50 60.5 70.2 |
| 412 |
</strong> |
| 413 |
</pre> |
| 414 |
<p>since <tt><b>-1</b></tt> means <em>final</em> (i.e. same as <tt><b>6</b></tt>). |
| 415 |
Each subscript can contain any number of triplets and individual values. The |
| 416 |
colon (<tt><b>:</b></tt>) operator has higher precedence than concatenation. |
| 417 |
This is shown by the following:</p> |
| 418 |
<pre> |
| 419 |
<strong>$ nc2text vec.nc 'v[2 :4]' |
| 420 |
30.2 40.9 50 |
| 421 |
</strong> |
| 422 |
</pre> |
| 423 |
<p>which is equivalent to:</p> |
| 424 |
<pre> |
| 425 |
<strong>$ nc2text vec.nc 'v[2:4]' |
| 426 |
30.2 40.9 50 |
| 427 |
</strong> |
| 428 |
</pre> |
| 429 |
<p>However parentheses can be used to override this precedence rule. E.g.</p> |
| 430 |
<pre> |
| 431 |
<strong>$ nc2text vec.nc 'v[2 (:4)]' |
| 432 |
30.2 10 20.3 30.2 40.9 50 |
| 433 |
</strong> |
| 434 |
</pre> |
| 435 |
<h3>Omitting Subscripts</h3> |
| 436 |
<p>An omitted subscript implies the whole dimension. Thus we can print the first |
| 437 |
row of <tt><b>mat</b></tt> as follows:</p> |
| 438 |
<pre> |
| 439 |
<strong>$ nc2text mat.nc 'M[0]' |
| 440 |
11 12 13 |
| 441 |
</strong> |
| 442 |
</pre> |
| 443 |
<p>and exclude the middle column by:</p> |
| 444 |
<pre> |
| 445 |
<strong>$ nc2text mat.nc 'M[,0 -1]' |
| 446 |
11 13 |
| 447 |
21 23 |
| 448 |
</strong> |
| 449 |
</pre> |
| 450 |
<h3>Dimension Names</h3> |
| 451 |
<p>Dimension names play an important role in FAN. Instead of:</p> |
| 452 |
<pre> |
| 453 |
<strong>$ nc2text mat.nc 'M(2 1,1 3]' |
| 454 |
21 23 |
| 455 |
11 13 |
| 456 |
</strong> |
| 457 |
</pre> |
| 458 |
<p>one can use:</p> |
| 459 |
<pre> |
| 460 |
<strong>$ nc2text mat.nc 'M(row=2 1,col=1 3]' |
| 461 |
21 23 |
| 462 |
11 13 |
| 463 |
</strong> |
| 464 |
</pre> |
| 465 |
<p>This is clearer for human readers. But specifying dimension names also provides |
| 466 |
the important facility of transposing dimensions. For example this allows <tt><b>ncrob</b></tt> |
| 467 |
to produce statistics (e.g. means) for rows as well as the normal columns. To |
| 468 |
transpose the above matrix, one could specify:</p> |
| 469 |
<pre> |
| 470 |
<strong>$ nc2text mat.nc 'M(col=1 3,row=2 1]' |
| 471 |
21 11 |
| 472 |
23 13 |
| 473 |
</strong> |
| 474 |
</pre> |
| 475 |
<p>since the order in which dimensions are specified controls their order in the |
| 476 |
output. To transpose a whole matrix one need only specify the dimension names |
| 477 |
as in the following:</p> |
| 478 |
<pre> |
| 479 |
<strong>$ nc2text mat.nc 'M[col,row]' |
| 480 |
11 21 |
| 481 |
12 22 |
| 482 |
13 23 |
| 483 |
</strong> |
| 484 |
</pre> |
| 485 |
<p>or using column-major order:</p> |
| 486 |
<pre> |
| 487 |
<strong>$ nc2text mat.nc 'M(row,col)' |
| 488 |
11 21 |
| 489 |
12 22 |
| 490 |
13 23 |
| 491 |
</strong> |
| 492 |
</pre> |
| 493 |
<p>In fact only one dimension name is needed, since any not mentioned are appended |
| 494 |
in their input order. E.g.</p> |
| 495 |
<pre> |
| 496 |
<strong>$ nc2text mat.nc 'M[col]' |
| 497 |
11 21 |
| 498 |
12 22 |
| 499 |
13 23 |
| 500 |
</strong> |
| 501 |
</pre> |
| 502 |
<h3>Indirect Indexing</h3> |
| 503 |
<p>So far we have located elements using direct index values. FAN also allows |
| 504 |
an indirect method using <em>coordinate variables</em> (i.e. variables with |
| 505 |
the same names as dimensions). Consider the following geographic netCDF file |
| 506 |
<tt><b>geog.nc</b></tt>:</p> |
| 507 |
<pre> |
| 508 |
<strong>$ ncdump geog.nc |
| 509 |
netcdf geog { |
| 510 |
dimensions: |
| 511 |
lat = 3 ; |
| 512 |
lon = 4 ; |
| 513 |
variables: |
| 514 |
float lat(lat) ; |
| 515 |
lat:units = "degrees_north" ; |
| 516 |
float lon(lon) ; |
| 517 |
lon:units = "degrees_east" ; |
| 518 |
double tsur(lat, lon) ; |
| 519 |
data: |
| 520 |
lat = -45 , 0 , 45 ; |
| 521 |
lon = -180 , -90 , 0 , 90 ; |
| 522 |
tsur = |
| 523 |
11, 12, 13, 14, |
| 524 |
21, 22, 23, 24, |
| 525 |
31, 32, 33, 34 ; |
| 526 |
} |
| 527 |
</strong> |
| 528 |
</pre> |
| 529 |
<p>FAN provides several <em>indirect indexing operators</em>. Perhaps the most |
| 530 |
useful of these is `<tt><b>~</b></tt>', which gives the index of the coordinate |
| 531 |
value <em>closest to</em> its argument. Thus:</p> |
| 532 |
<pre> |
| 533 |
<strong>$ nc2text geog.nc 'lat[~-40]' |
| 534 |
-45 |
| 535 |
</strong> |
| 536 |
</pre> |
| 537 |
<p>prints the latitude closest to 40�S and</p> |
| 538 |
<pre> |
| 539 |
<strong>$ nc2text geog.nc 'tsur[~-40,~10]' |
| 540 |
13 |
| 541 |
</strong> |
| 542 |
</pre> |
| 543 |
<p>prints the element of <tt><b>tsur</b></tt> closest to the point 40�S, 10�E. |
| 544 |
Note that FAN knows nothing about circular wrap-around and does not consider |
| 545 |
360� to be equal to 0�. The following shows how indirect indexing can be used |
| 546 |
within triplets:</p> |
| 547 |
<pre> |
| 548 |
<strong>$ nc2text geog.nc 'tsur[ lat = ~90:~-90:-2 , lon = ~10: ]' |
| 549 |
33 34 |
| 550 |
13 14 |
| 551 |
</strong> |
| 552 |
</pre> |
| 553 |
<p>This gives every second latitude from that closest the north pole to that closest |
| 554 |
the south pole, and all longitudes from that closest to 10�E to the final one. |
| 555 |
The other indirect indexing operators are as follows:</p> |
| 556 |
<table border="1" summary="other @ max and min operators"> |
| 557 |
<tr> |
| 558 |
<td><tt><b>@ max <</b></tt> </td> |
| 559 |
<td>index value corresponding to maximum coordinate value less than argument</td> |
| 560 |
</tr> |
| 561 |
<tr> |
| 562 |
<td><tt><b>@ max <=</b></tt> </td> |
| 563 |
<td>index value corresponding to maximum coordinate value less than or equal |
| 564 |
to argument</td> |
| 565 |
</tr> |
| 566 |
<tr> |
| 567 |
<td><tt><b>@ min ></b></tt> </td> |
| 568 |
<td>index value corresponding to minimum coordinate value greater than argument</td> |
| 569 |
</tr> |
| 570 |
<tr> |
| 571 |
<td><tt><b>@ min >=</b></tt> </td> |
| 572 |
<td>index value corresponding to minimum coordinate value greater than or |
| 573 |
equal to argument</td> |
| 574 |
</tr> |
| 575 |
</table> |
| 576 |
<p>Thus the following prints the minimum longitude greater than 10�E:</p> |
| 577 |
<pre> |
| 578 |
<strong>$ nc2text geog.nc 'lon[@ min > 10]' |
| 579 |
90 |
| 580 |
</strong> |
| 581 |
</pre> |
| 582 |
<p>and the following retrieves the rows from the <em>maximum latitude less than |
| 583 |
or equal to 30�N</em> to the <em>closest latitude to 90�N</em>, and the columns |
| 584 |
from the second (i.e 1 with respect to index origin of 0) to <em>minimum longitude |
| 585 |
greater than 0</em>.</p> |
| 586 |
<pre> |
| 587 |
<strong>$ nc2text geog.nc 'tsur[lat= @max<=30 : ~90, lon= 1 : @min > 0]' |
| 588 |
22 23 24 |
| 589 |
32 33 34 |
| 590 |
</strong> |
| 591 |
</pre> |
| 592 |
<h3>Offsets</h3> |
| 593 |
<p>It is possible to specify <em>offsets</em> using an expression of the form |
| 594 |
<br /> |
| 595 |
<em>index</em> <tt><b>+</b></tt> <em>offset</em> <br /> |
| 596 |
where <em>offset</em> is an integer constant (which can be negative). The offset |
| 597 |
must be the right hand argument of `<tt><b>+</b></tt>'. Note that this `<tt><b>+</b></tt>' |
| 598 |
operator has even higher precedence than `<tt><b>:</b></tt>'. Here are some |
| 599 |
examples of the use of offsets:</p> |
| 600 |
<pre> |
| 601 |
<strong>$ nc2text geog.nc 'lon[ ~-100 + -1 : ~-360 + 2 ]' |
| 602 |
-180 -90 0 |
| 603 |
</strong> |
| 604 |
</pre> |
| 605 |
<p>prints the longitudes from that <em>one before the closest to 100�W</em> to |
| 606 |
that <em>two beyond the closest to 360�W</em>. Note how the negative offset |
| 607 |
is specified as `<tt><b>+ -1</b></tt>', which is <em>not</em> equivalent |
| 608 |
to `<tt><b>-1</b></tt>' as in:</p> |
| 609 |
<pre> |
| 610 |
<strong>$ nc2text geog.nc 'lon[ ~-100-1 : ~-360 + 2 ]' |
| 611 |
-90 90 -180 -90 0 |
| 612 |
</strong> |
| 613 |
</pre> |
| 614 |
<p>which is equivalent to both the following (Note the wrap-around.):</p> |
| 615 |
<pre> |
| 616 |
<strong>$ nc2text geog.nc 'lon[ (~-100) (-1:~-360 + 2) ]' |
| 617 |
-90 90 -180 -90 0 |
| 618 |
$ nc2text geog.nc 'lon[ 1 3:2 ]' |
| 619 |
-90 90 -180 -90 0 |
| 620 |
</strong> |
| 621 |
</pre> |
| 622 |
<p>One use for offsets is to append along the <tt><b>UNLIMITED</b></tt> dimension |
| 623 |
without needing to know its current size. The expression `<tt><b>-1+1</b></tt>' |
| 624 |
represents the index value for appending immediately after the current final |
| 625 |
record. Thus we could append a value to variable <tt><b>v</b></tt> in file <tt><b>vec_new.nc</b></tt> |
| 626 |
(whose <tt><b>UNLIMITED</b></tt> dimension <tt><b>n</b></tt> has the current |
| 627 |
size 7) by:</p> |
| 628 |
<pre> |
| 629 |
<strong>$ echo 80 | text2nc 'vec_new.nc v[-1 + 1]' |
| 630 |
$ nc2text 'vec_new.nc v' |
| 631 |
10 20.3 30.7 40.9 50 60.5 70.2 80 |
| 632 |
</strong> |
| 633 |
</pre> |
| 634 |
<p>Then we could append two more values by:</p> |
| 635 |
<pre> |
| 636 |
<strong>$ echo 90 100.1 | text2nc 'vec_new.nc v[ -1 + 1 : -1 + 2 ]' |
| 637 |
$ nc2text 'vec_new.nc v' |
| 638 |
10 20.3 30.7 40.9 50 60.5 70.2 80 90 100.1 |
| 639 |
</strong> |
| 640 |
</pre> |
| 641 |
<p>giving a new size of 10 for the <tt><b>UNLIMITED</b></tt> dimension.</p> |
| 642 |
<h3>Coordinate Variable Unit Conversion</h3> |
| 643 |
<p>In file <tt><b>geog.nc</b></tt> the <tt><b>units</b></tt> attribute is <tt><b>degrees_north</b></tt> |
| 644 |
for <tt><b>lat</b></tt> and <tt><b>degrees_east</b></tt> for <tt><b>lon</b></tt>. |
| 645 |
One may want to specify coordinate values in some other units. The following |
| 646 |
shows how this can be done by appending the unit (enclosed in braces i.e. `<tt><b>{}</b></tt>') |
| 647 |
to the value:</p> |
| 648 |
<pre> |
| 649 |
<strong>$ nc2text geog.nc 'tsur[ lat=~0.8{radian}, lon = ~ -1.5 { radian } ]' |
| 650 |
32 |
| 651 |
</strong> |
| 652 |
</pre> |
| 653 |
<p>giving the value at the point closest to latitude 0.8 radians north and longitude |
| 654 |
1.5 radians west. This unit conversion (like that during FAN input and output) |
| 655 |
is done using the Unidata units library discussed in Appendix C of <a |
| 656 |
href="/software/netcdf/guide_toc.html"> NetCDF |
| 657 |
User's Guide</a>.</p> |
| 658 |
<p><a id="Attributes" name="Attributes"></a></p> |
| 659 |
<h2>Attributes</h2> |
| 660 |
<p>As noted in Section <a href="#High_level_Syntax"><em>High level Syntax</em></a> |
| 661 |
an attribute <em>vas</em> can take two forms: <br /> |
| 662 |
<em>var</em><tt><b>:</b></tt><em>att</em> <br /> |
| 663 |
<tt><b>:</b></tt><em>att</em> <br /> |
| 664 |
As in CDL, the latter denotes a <em>global attribute</em>. The following writes |
| 665 |
the global attribute <tt><b>title</b></tt> and then reads and prints it:</p> |
| 666 |
<pre> |
| 667 |
<strong>$ echo 'Sample geographic file' | text2nc -h 'geog.nc :title' |
| 668 |
$ nc2text 'geog.nc :title' |
| 669 |
Sample geographic file |
| 670 |
</strong> |
| 671 |
</pre> |
| 672 |
<p>(The <tt><b>-h</b></tt> flag means `<em>Do not append a line to the global |
| 673 |
attribute</em> <tt><b>history</b></tt>'.)</p> |
| 674 |
<p>Attributes cannot have subscripts, so there is no way of accessing only part |
| 675 |
of an attribute. Attributes are automatically created if they do not exist and |
| 676 |
their type and size can be changed. The following gives variable <tt><b>lat</b></tt> |
| 677 |
the new attribute <tt><b>valid_range</b></tt> (with type <tt><b>float</b></tt>) |
| 678 |
and then prints it:</p> |
| 679 |
<pre> |
| 680 |
<strong>$ echo -90 90 | text2nc -h -t float 'geog.nc lat:valid_range' |
| 681 |
$ nc2text 'geog.nc lat:valid_range' |
| 682 |
-90 90 |
| 683 |
</strong> |
| 684 |
</pre> |
| 685 |
<p>The following gives variable <tt><b>lat</b></tt> another new attribute <tt><b>foo</b></tt> |
| 686 |
(by copying variable <tt><b>v</b></tt> from file <tt><b>vec.nc</b></tt>), then |
| 687 |
modifies it, then deletes it.</p> |
| 688 |
<pre> |
| 689 |
<strong>$ nc2text 'vec.nc v[:4]' | text2nc -h -t double 'geog.nc lat:foo' |
| 690 |
$ nc2text 'geog.nc lat:foo' |
| 691 |
10 20.3 30.2 40.9 50 |
| 692 |
$ echo 'Hello' | text2nc -h 'geog.nc lat:foo' # Modify attribute 'lat:foo' |
| 693 |
$ nc2text 'geog.nc lat:foo' |
| 694 |
Hello |
| 695 |
$ text2nc -h 'geog.nc lat:foo' < /dev/null # Delete attribute 'lat:foo' |
| 696 |
</strong> |
| 697 |
</pre> |
| 698 |
<p>Note how one can delete attributes by changing their size to 0. The file <tt><b>/dev/null</b></tt> |
| 699 |
is a standard UNIX pseudo-file that is empty for input.</p> |
| 700 |
<p><a id="Using_ID_Numbers" name="Using_ID_Numbers"></a></p> |
| 701 |
<h2>Using ID Numbers for Variables, Dimensions and Attributes</h2> |
| 702 |
<p>It is possible to use ID numbers in place of names for variables, dimensions |
| 703 |
and attributes. However dimension ID numbers must be followed by <tt><b>=</b></tt> |
| 704 |
so they can be distinguished from index values. ID numbers begin at 0 regardless |
| 705 |
of the index origin. Negative values are relative to the end, which is represented |
| 706 |
by <tt><b>-1</b></tt>.</p> |
| 707 |
<p>There are some situations where ID numbers are more convenient than names. |
| 708 |
For example, one might adopt the convention that coordinate variables should |
| 709 |
be defined first, after which there should be only a single other (main) variable |
| 710 |
in each file. A shell-script to process such files can refer to the main variable |
| 711 |
as <tt><b>-1</b></tt>. The following shows the use of such a variable ID number:</p> |
| 712 |
<pre> |
| 713 |
<strong>$ nc2text geog.nc -1 |
| 714 |
11 12 13 14 |
| 715 |
21 22 23 24 |
| 716 |
31 32 33 34 |
| 717 |
</strong> |
| 718 |
</pre> |
| 719 |
<p>The following prints the first attribute of the second variable:</p> |
| 720 |
<pre> |
| 721 |
<strong>$ nc2text geog.nc '1:0' |
| 722 |
degrees_east |
| 723 |
</strong> |
| 724 |
</pre> |
| 725 |
<p>The following Korn shell script <tt><b>pratts</b></tt> prints all the non-global |
| 726 |
attributes in the files specified by its arguments.</p> |
| 727 |
<pre> |
| 728 |
<strong>$ cat pratts |
| 729 |
#!/bin/ksh |
| 730 |
for FILE |
| 731 |
do |
| 732 |
integer VARID=0 |
| 733 |
# Following true if variable VARID exists |
| 734 |
while VARNAME="$(ncmeta -s -w v $FILE $VARID)"; test -n "$VARNAME" |
| 735 |
do |
| 736 |
integer ATTID=0 |
| 737 |
# Following true if attribute ATTID exists |
| 738 |
while ATTNAME="$(ncmeta -s -w a $FILE $VARID:$ATTID)"; test -n "$ATTNAME" |
| 739 |
do |
| 740 |
print -n "$FILE $VARNAME:$ATTNAME " |
| 741 |
nc2text $FILE "$VARNAME:$ATTNAME" |
| 742 |
(( ATTID += 1 )) |
| 743 |
done |
| 744 |
(( VARID += 1 )) |
| 745 |
done |
| 746 |
done |
| 747 |
</strong> |
| 748 |
</pre> |
| 749 |
<p>We can use <tt><b>pratts</b></tt> on file <tt><b>geog.nc</b></tt> as follows:</p> |
| 750 |
<pre> |
| 751 |
<strong>$ pratts geog.nc |
| 752 |
geog.nc lat:units degrees_north |
| 753 |
geog.nc lat:valid_range -90 90 |
| 754 |
geog.nc lon:units degrees_east |
| 755 |
</strong> |
| 756 |
</pre> |
| 757 |
<h1>FAN Utilities</h1> |
| 758 |
<h2>Introduction to Utilities</h2> |
| 759 |
<p>This section provides a more detailed description of the four FAN utilities, |
| 760 |
<tt><b>nc2text</b></tt>, <tt><b>text2nc</b></tt>, <tt><b>ncmeta</b></tt> and |
| 761 |
<tt><b>ncrob</b></tt>, commencing with some features common to several utilities. |
| 762 |
The usage summaries in Sections <a href="#nc2text_Usage"><em>nc2text Usage</em></a>, |
| 763 |
<a |
| 764 |
href="#text2nc_Usage"><em>text2nc Usage</em></a>, <a |
| 765 |
href="#ncmeta_Usage"><em>ncmeta Usage</em></a> and <a |
| 766 |
href="#ncrob_Usage"><em>ncrob Usage</em></a> can be printed by entering the command |
| 767 |
name without any arguments.</p> |
| 768 |
<p>All netCDF types (<tt><b>char, byte, short, long, float</b></tt> and <tt><b>double</b></tt>) |
| 769 |
can be read and written. During input/output there is automatic conversion to |
| 770 |
or from type <tt><b>double</b></tt>, which is used for internal storage and |
| 771 |
processing.</p> |
| 772 |
<h3>Options Common to several Utilities</h3> |
| 773 |
<p>The two flags <tt><b>-h</b></tt> and <tt><b>-H</b></tt> specify what is to |
| 774 |
be written to the global attribute <tt><b>history</b></tt>. The <tt><b>-h</b></tt> |
| 775 |
flag means `<em>Do not write any history</em>'. The <tt><b>-H</b></tt> flag |
| 776 |
means `<em>Exclude time-stamp and user-name</em> (<tt><b>LOGNAME</b></tt>) <em>from |
| 777 |
history</em>'. This flag is useful in program testing, since it causes the |
| 778 |
same values to be written to <tt><b>history</b></tt> each time, thus facilitating |
| 779 |
comparison of actual output with that expected.</p> |
| 780 |
<p>Section 4.5 of <a |
| 781 |
href="/software/netcdf/guide_toc.html"> NetCDF |
| 782 |
User's Guide</a> explains the two aspects of error-handling: suppression |
| 783 |
of error messages and fatality of errors. The default mode is <em>verbose</em> |
| 784 |
and <em>fatal</em>. <em>Non-verbose (silent)</em> mode is set by flag <tt><b>-s</b></tt>. |
| 785 |
<em>Non-fatal (persevere)</em> mode is set by flag <tt><b>-p</b></tt>.</p> |
| 786 |
<p>The <tt><b>-e</b></tt> flag means `<em>Write error messages to</em> <tt><b>stdout</b></tt> |
| 787 |
<em>not</em> <tt><b>stderr</b></tt>'.</p> |
| 788 |
<p>The option `<tt><b>-t</b></tt> <em>type</em>' sets the data-type for new |
| 789 |
variables or attributes. Valid values are <tt><b>char, byte, short, long, float</b></tt> |
| 790 |
and <tt><b>double</b></tt>. These can be abbreviated to their first letter.</p> |
| 791 |
<p>The option `<tt><b>-u</b></tt> <em>unit</em>' sets the unit of measure |
| 792 |
for ASCII text data, providing conversion to or from those defined by netCDF |
| 793 |
<tt><b>units</b></tt> attributes.</p> |
| 794 |
<p><a id="Scaling" name="Scaling"></a></p> |
| 795 |
<h3>Scaling and Unit Conversion</h3> |
| 796 |
<p>All netCDF input and output values are transformed by a linear equation defined |
| 797 |
by the attributes <tt><b>add_offset</b></tt>, <tt><b>scale_factor</b></tt> and |
| 798 |
<tt><b>units</b></tt>; together with any unit defined by the <tt><b>-u</b></tt> |
| 799 |
option mentioned above. The output <tt><b>units</b></tt> attribute is defined |
| 800 |
or modified in some situations such as when it is undefined but the corresponding |
| 801 |
input attribute is defined.</p> |
| 802 |
<p>All unit conversion is done using the <em>Units Library</em> documented in |
| 803 |
Appendix C of <a |
| 804 |
href="/software/netcdf/guide_toc.html"> NetCDF |
| 805 |
User's Guide</a>. The environment variable <tt><b>UDUNITS_PATH</b></tt> |
| 806 |
can be used to specify a non-standard units file. (See <tt><b>man</b></tt> document |
| 807 |
<tt><b>udunits(3)</b></tt>.)</p> |
| 808 |
<h3>Missing Values</h3> |
| 809 |
<p>Values read from a netCDF file are considered missing if outside the <em>valid |
| 810 |
range</em> defined by the attribute <tt><b>valid_range</b></tt> or the attributes |
| 811 |
<tt><b>valid_min</b></tt>, and <tt><b>valid_max</b></tt>. If these do not define |
| 812 |
either the minimum or the maximum then an attempt is made to define it based |
| 813 |
on the principle that the <em>missing value</em> must be outside the valid range. |
| 814 |
The <em>missing value</em> is defined by the attribute <tt><b>missing_value</b></tt>, |
| 815 |
or if this is undefined then the <em>fill value</em> (defined by attribute <tt><b>_FillValue</b></tt> |
| 816 |
if defined, otherwise the default fill value for the data type).</p> |
| 817 |
<h3>Environment Variables</h3> |
| 818 |
<p>The environment variable <tt><b>UDUNITS_PATH</b></tt> was mentioned in Section |
| 819 |
<a href="#Scaling"><em>Scaling</em></a>. The environment variable <tt><b>COLUMNS</b></tt> |
| 820 |
(default: 80) defines the page width and is used to print data of type <tt><b>character</b></tt>.</p> |
| 821 |
<h2>nc2text</h2> |
| 822 |
<p>This utility prints variable and attribute values from netCDF files.</p> |
| 823 |
<p><a id="nc2text_Usage" name="nc2text_Usage"></a></p> |
| 824 |
<h3>Usage</h3> |
| 825 |
<pre> |
| 826 |
<strong>Usage: nc2text [-eps] [-f %s] [-m %s] [-n %d] [-u %s] <FANI> |
| 827 |
<FANI> netCDF FAN specification for input |
| 828 |
-e Write error messages to stdout not stderr |
| 829 |
-p Persevere after errors |
| 830 |
-s Silent mode: Suppress warning messages |
| 831 |
-f <string>: Format for output (default: C_format attribute ("%G" if none)) |
| 832 |
-m <string>: Missing value for output (default: _ ) |
| 833 |
-n <integer>: Number of fields per line of output (default: 10 if numeric) |
| 834 |
(Environment variable COLUMNS defines default for characters) |
| 835 |
-u <string>: Unit of measure for output (default: unit in file) |
| 836 |
</strong> |
| 837 |
</pre> |
| 838 |
<h3>Examples</h3> |
| 839 |
<p>The following prints the first three elements of variable <tt><b>v</b></tt> |
| 840 |
of file <tt><b>vec.nc</b></tt>:</p> |
| 841 |
<pre> |
| 842 |
<strong>$ nc2text 'vec.nc v[0 1 2]' |
| 843 |
10 20.3 30.2 |
| 844 |
</strong> |
| 845 |
</pre> |
| 846 |
<p>The following uses <tt><b>text2nc</b></tt> to</p> |
| 847 |
<ul> |
| 848 |
<li>set attribute <tt><b>v:units</b></tt> to `<tt><b>degF</b></tt>'</li> |
| 849 |
<li>set attribute <tt><b>v:valid_min</b></tt> to -460�F (just below 0�K)</li> |
| 850 |
<li>modify <tt><b>v[2]</b></tt> so it is less than this valid minimum i.e. missing.</li> |
| 851 |
</ul> |
| 852 |
<pre> |
| 853 |
<strong>$ echo degF | text2nc vec.nc 'v:units' |
| 854 |
$ echo -460 | text2nc -t float vec.nc 'v:valid_min' |
| 855 |
$ echo -999 | text2nc vec.nc 'v[2]' |
| 856 |
</strong> |
| 857 |
</pre> |
| 858 |
<p>Then we print four Celsius temperatures per line. The text `<tt><b>MISSING</b></tt>' |
| 859 |
is printed for missing values. Normal values are printed using the C format |
| 860 |
<tt><b>%8.4f</b></tt> (equivalent to the Fortran format <tt><b>F8.4</b></tt> |
| 861 |
i.e 4 decimal places with a total field width of 8 characters).</p> |
| 862 |
<pre> |
| 863 |
<strong>$ nc2text -f '%8.4f' -m ' MISSING' -n 4 -u degC vec.nc 'v[:4]' |
| 864 |
-12.2222 -6.5000 MISSING 4.9444 |
| 865 |
10.0000 |
| 866 |
</strong> |
| 867 |
</pre> |
| 868 |
<h2>ncmeta</h2> |
| 869 |
<p>This utility prints metadata from netCDF files. This metadata can include rank, |
| 870 |
shape, file names, variable names, dimension names and attribute names.</p> |
| 871 |
<p><a id="ncmeta_Usage" name="ncmeta_Usage"></a></p> |
| 872 |
<h3>Usage</h3> |
| 873 |
<pre> |
| 874 |
<strong>Usage: ncmeta [-eps] [-w <LETTERS>] <FANI> |
| 875 |
<FANI> netCDF FAN specification for input |
| 876 |
-e Write error messages to stdout not stderr |
| 877 |
-p Persevere after errors |
| 878 |
-s Silent mode: Suppress warning messages |
| 879 |
-w <LETTERS>: What to print using following (default: s) |
| 880 |
a: attribute names |
| 881 |
d: dimension names |
| 882 |
f: file names |
| 883 |
r: rank (number of dimensions) |
| 884 |
s: shape (dimension sizes) |
| 885 |
v: variable names |
| 886 |
|
| 887 |
Example: ncmeta -w fvs abc.nc var1 var2 |
| 888 |
</strong> |
| 889 |
</pre> |
| 890 |
<p><a id="ncmeta_Examples" name="ncmeta_Examples"></a></p> |
| 891 |
<h3>Examples</h3> |
| 892 |
<p>The following examples print the shape of the specified variables:</p> |
| 893 |
<pre> |
| 894 |
<strong>$ ncmeta vec.nc v |
| 895 |
5 |
| 896 |
$ ncmeta geog.nc tsur |
| 897 |
3 4 |
| 898 |
</strong> |
| 899 |
</pre> |
| 900 |
<p>The following example prints the filename, variable name, rank, dimensions |
| 901 |
and shape of the specified variables:</p> |
| 902 |
<pre> |
| 903 |
<strong>$ ncmeta -w fvrds vec.nc v 'geog.nc tsur' lat lon |
| 904 |
vec.nc v 1 n 5 |
| 905 |
geog.nc tsur 2 lat lon 3 4 |
| 906 |
geog.nc lat 1 lat 3 |
| 907 |
geog.nc lon 1 lon 4 |
| 908 |
</strong> |
| 909 |
</pre> |
| 910 |
<p>The following example prints the variable name and attribute name of the first |
| 911 |
(0) attribute of the first (0) variable:</p> |
| 912 |
<pre> |
| 913 |
<strong>$ ncmeta -w va geog.nc '0:0' |
| 914 |
lat units |
| 915 |
</strong> |
| 916 |
</pre> |
| 917 |
<h2>ncrob</h2> |
| 918 |
<p>This utility reads data from one or more netCDF variables, performs some process |
| 919 |
on it and then either prints the result or writes it to one or more netCDF variables. |
| 920 |
The type of process is defined by option `<tt><b>-r</b></tt> <em>string</em>', |
| 921 |
where <em>string</em> is one of the following:</p> |
| 922 |
<center> |
| 923 |
<table border="1" summary="option meanings"> |
| 924 |
<tr> |
| 925 |
<td><tt><b>am</b></tt> </td> |
| 926 |
<td>arithmetic mean</td> |
| 927 |
</tr> |
| 928 |
<tr> |
| 929 |
<td><tt><b>broadcast</b></tt> </td> |
| 930 |
<td>cyclic copy</td> |
| 931 |
</tr> |
| 932 |
<tr> |
| 933 |
<td><tt><b>count</b></tt> </td> |
| 934 |
<td>number of non-missing values</td> |
| 935 |
</tr> |
| 936 |
<tr> |
| 937 |
<td><tt><b>fill</b></tt> </td> |
| 938 |
<td>fill with missing values</td> |
| 939 |
</tr> |
| 940 |
<tr> |
| 941 |
<td><tt><b>gm</b></tt> </td> |
| 942 |
<td>geometric mean</td> |
| 943 |
</tr> |
| 944 |
<tr> |
| 945 |
<td><tt><b>max</b></tt> </td> |
| 946 |
<td>maximum</td> |
| 947 |
</tr> |
| 948 |
<tr> |
| 949 |
<td><tt><b>min</b></tt> </td> |
| 950 |
<td>minimum</td> |
| 951 |
</tr> |
| 952 |
<tr> |
| 953 |
<td><tt><b>prod</b></tt> </td> |
| 954 |
<td>product</td> |
| 955 |
</tr> |
| 956 |
<tr> |
| 957 |
<td><tt><b>sd</b></tt> </td> |
| 958 |
<td>unadjusted standard deviation (divisor is <var>n</var> )</td> |
| 959 |
</tr> |
| 960 |
<tr> |
| 961 |
<td><tt><b>sd1</b></tt> </td> |
| 962 |
<td>adjusted standard deviation (divisor is <var>n</var>-1 )</td> |
| 963 |
</tr> |
| 964 |
<tr> |
| 965 |
<td><tt><b>sum</b></tt> </td> |
| 966 |
<td>sum</td> |
| 967 |
</tr> |
| 968 |
<tr> |
| 969 |
<td><tt><b>sum2</b></tt> </td> |
| 970 |
<td>sum of squares of values</td> |
| 971 |
</tr> |
| 972 |
</table> |
| 973 |
</center> |
| 974 |
<p>A <tt><b>broadcast</b></tt> copies successive elements from input to output. |
| 975 |
Whenever the end of input is reached, reading begins again at the start of input. |
| 976 |
The whole process continues until reaching the end of output.</p> |
| 977 |
<p>A <tt><b>fill</b></tt> simply fills the output variable with missing values. |
| 978 |
There must be input, although it is used only to define the shape of new variables.</p> |
| 979 |
<p>The other processes are all <em>reductions</em>, in the sense that they reduce |
| 980 |
the <em>rank</em> (number of dimensions). The number of input elements ( <var>I</var> ) |
| 981 |
must be a multiple of both the number of output elements ( <var>N</var> ) |
| 982 |
and the number of weights ( <var>M</var> ) (if any, as specified by |
| 983 |
option <tt><b>-w</b></tt>).</p> |
| 984 |
<p>If the process is <tt><b>count</b></tt> and there are no weights then the result |
| 985 |
is the number of non-missing values. If there are weights then the result is |
| 986 |
the sum of the weights of the non-missing values.</p> |
| 987 |
<p>Let vector <var>X</var><sub>0</sub>, <var>X</var><sub>1</sub>, ..., <var> |
| 988 |
X</var><sub><var>i</var></sub>, ..., <var>X</var><sub><var> I</var>-1</sub> |
| 989 |
represent the selected input data elements in the specified order. Similarly, |
| 990 |
let vector <var>Y</var><sub>0</sub>, <var>Y</var><sub>1</sub>, ... <var> |
| 991 |
Y</var><sub><var>j</var></sub>, ..., <var>Y</var><sub><var> N</var>-1</sub> |
| 992 |
represent the resultant output data. Let <var>n</var> = <var>I</var> � <var>N</var>.</p> |
| 993 |
<p>If the process is <tt><b>sum</b></tt> and there are no weights then</p> |
| 994 |
<center> |
| 995 |
<var>Y</var><sub><var>j</var></sub> = <strong> Sum</strong><sub><var>i</var>=0,<var>n</var>-1</sub> <var>X</var><sub> |
| 996 |
<var>Ni</var>+<var>j</var></sub> |
| 997 |
</center> |
| 998 |
<p>If weights <var>W</var><sub>0</sub>, <var>W</var><sub>1</sub>, ..., <var> |
| 999 |
W</var><sub><var>k</var></sub>, ..., <var>W</var><sub><var> M</var>-1</sub> |
| 1000 |
are defined and <var>m</var> = <var>I</var> � <var>M</var> |
| 1001 |
then</p> |
| 1002 |
<center> |
| 1003 |
<var>Y</var><sub><var>j</var></sub> = <strong> Sum</strong><sub><var>i</var>=0,<var>n</var>-1</sub> <var>W</var><sub> |
| 1004 |
<strong>floor</strong>((<var>Ni</var>+<var>j</var>)/m)</sub><var>X</var><sub> |
| 1005 |
<var>Ni</var>+<var>j</var></sub> |
| 1006 |
</center> |
| 1007 |
<p>where <strong>floor</strong>( <var>x</var> ) represents |
| 1008 |
the floor of <var>x</var> i.e. the greatest integer <= <var>x</var>.</p> |
| 1009 |
<p>This is calculated using the following algorithm:</p> |
| 1010 |
<p> <var>n</var> <tt><b>:=</b></tt> <var>I</var>�<var>N</var> |
| 1011 |
<br /> |
| 1012 |
<var>m</var> <tt><b>:=</b></tt> <var>I</var>�<var>M</var> |
| 1013 |
<br /> |
| 1014 |
<b>for</b> <var>j</var> <b>from</b> 0 <b>to</b> <var>N</var>-1 |
| 1015 |
<br /> |
| 1016 |
<tt> </tt> <var>Y</var><sub><var>j</var></sub> |
| 1017 |
<tt><b>:=</b></tt> 0 <br /> |
| 1018 |
<b>for</b> <var>i</var> <b>from</b> 0 <b>to</b> <var>I</var>-1 |
| 1019 |
<br /> |
| 1020 |
<tt> </tt> <var>j</var> <tt><b>:=</b></tt> <var>i</var> <strong>mod</strong> <var>n</var> |
| 1021 |
<br /> |
| 1022 |
<tt> </tt> <var>k</var> <tt><b>:=</b></tt> <strong>floor</strong>( <var>i</var>/m ) |
| 1023 |
<br /> |
| 1024 |
<tt> </tt> <b>if</b> <var>Y</var><sub><var>j</var></sub> �= |
| 1025 |
<tt><b>missing_value</b></tt> <br /> |
| 1026 |
<tt> </tt> <tt> </tt> <b>if</b> <tt><b>valid_min</b></tt> |
| 1027 |
<= <var>X</var><sub><var>i</var></sub> <= <tt><b>valid_max</b></tt> |
| 1028 |
<br /> |
| 1029 |
<tt> </tt> <tt> </tt> <tt> </tt> |
| 1030 |
<var>Y</var><sub><var>j</var></sub> <tt><b>:=</b></tt> <var>Y</var><sub><var>j</var></sub> + <var>W</var><sub> |
| 1031 |
<var>k</var></sub> <var>X</var><sub><var>i</var></sub> <br /> |
| 1032 |
<tt> </tt> <tt> </tt> <b>else if</b> <tt><b>suddenDeath</b></tt> |
| 1033 |
<br /> |
| 1034 |
<tt> </tt> <tt> </tt> <tt> </tt> |
| 1035 |
<var>Y</var><sub><var>j</var></sub> <tt><b>:=</b></tt> <tt><b>missing_value</b></tt> |
| 1036 |
<br /> |
| 1037 |
</p> |
| 1038 |
<p>Note that this definition of <var>k</var> means that the first |
| 1039 |
<var>m</var> elements have the first weight <var>W</var><sub>0</sub> , |
| 1040 |
the next <var>m</var> have the second weight <var>W</var><sub>1</sub> , |
| 1041 |
and so on.</p> |
| 1042 |
<p>As an example consider an input array which is a matrix <var>A</var> |
| 1043 |
with <var>R</var> rows and <var>C</var> columns. Thus |
| 1044 |
<var>I</var>=<var>RC</var>. If we want column sums then the output vector |
| 1045 |
would be of length <var>C</var> i.e. <var>N</var>=<var>C</var>. |
| 1046 |
Now <var>n</var>= <var>I</var> � <var>N</var> = <var> |
| 1047 |
R</var>. So the unweighted sum is</p> |
| 1048 |
<center> |
| 1049 |
<var>Y</var><sub><var>j</var></sub> = <strong> Sum</strong><sub><var>i</var>=0,<var>R</var>-1</sub> <var>X</var><sub> |
| 1050 |
<var>Ci</var>+<var>j</var></sub> = <strong>Sum</strong><sub> <var>i</var>=0,<var>R</var>-1</sub> <var>A</var><sub><var>ij</var></sub> |
| 1051 |
</center> |
| 1052 |
<p>and the weighted sum is</p> |
| 1053 |
<center> |
| 1054 |
<var>Y</var><sub><var>j</var></sub> = <strong> Sum</strong><sub><var>i</var>=0,<var>R</var>-1</sub> <var>W</var><sub> |
| 1055 |
<strong>floor</strong>((<var>Ci</var>+<var>j</var>)/C)</sub> <var> X</var><sub><var>Ci</var>+<var>j</var></sub> = <strong>Sum</strong><sub> |
| 1056 |
<var>i</var>=0,<var>R</var>-1</sub> <var>W</var><sub><var>i</var></sub> <var> |
| 1057 |
A</var><sub><var>ij</var></sub> |
| 1058 |
</center> |
| 1059 |
<p>If the process is <tt><b>prod</b></tt> and there are no weights then</p> |
| 1060 |
<center> |
| 1061 |
<var>Y</var><sub><var>j</var></sub> = <strong> Product</strong><sub><var>i</var>=0,<var>n</var>-1</sub> <var>X</var><sub> |
| 1062 |
<var>Ni</var>+<var>j</var></sub> |
| 1063 |
</center> |
| 1064 |
<p>If weights are defined then</p> |
| 1065 |
<center> |
| 1066 |
<var>Y</var><sub><var>j</var></sub> = <strong> Product</strong><sub><var>i</var>=0,<var>n</var>-1</sub> <var>X</var><sub> |
| 1067 |
<var>Ni</var>+<var>j</var></sub> <sup><var>W</var><sub><strong> floor</strong>((<var>Ni</var>+<var>j</var>)/m)</sub></sup> |
| 1068 |
</center> |
| 1069 |
<p>In general the shape (dimension vector) of the destination should match the |
| 1070 |
trailing dimensions of the source. Then the reduction process operates over |
| 1071 |
those leading dimensions absent from the destination.</p> |
| 1072 |
<p>Note that FAN allows you to transpose dimensions by specifying them in an order |
| 1073 |
different from that in the file. Thus the leading source dimensions are those |
| 1074 |
specified first. The order of the remaining dimensions must match those of the |
| 1075 |
destination.</p> |
| 1076 |
<p>The other reduction processes are treated similarly. However <tt><b>min</b></tt> |
| 1077 |
and <tt><b>max</b></tt> do not allow weights.</p> |
| 1078 |
<p>If the <tt><b>-m</b></tt> flag is specified then the result is missing if any |
| 1079 |
of the values it depends on is missing (<em>sudden death mode</em>). Otherwise |
| 1080 |
missing values are omitted (<em>filter mode</em>) i.e. essentially treated as |
| 1081 |
having a weight of 0.</p> |
| 1082 |
<p>The <tt><b>-b</b></tt> option sets the size of the input buffer. This can improve |
| 1083 |
efficiency when reading very large variables.</p> |
| 1084 |
<p>The <tt><b>-c</b></tt> option creates a new destination variable with the specified |
| 1085 |
rank (number of dimensions). If the variable already exists then this option |
| 1086 |
is ignored. If the destination file does not exist then it is created. The variable |
| 1087 |
is created with the same attributes as the (first if several) source variable, |
| 1088 |
and the specified number of its trailing dimensions, together with any associated |
| 1089 |
coordinate variables. However a broadcast is slightly different in that a new |
| 1090 |
leading dimension is created from the leading source dimensions by taking the |
| 1091 |
product of their sizes (so the total number of elements is unchanged) and concatenating |
| 1092 |
their names. The data-type of the new variable is specified using option <tt><b>-t</b></tt> |
| 1093 |
and defaults to the type of the source variable.</p> |
| 1094 |
<p><a id="ncrob_Usage" name="ncrob_Usage"></a></p> |
| 1095 |
<h3>Usage</h3> |
| 1096 |
<pre> |
| 1097 |
<strong>Usage: ncrob [options] <FANI> / <FANO> |
| 1098 |
<FANI>: FAN specification for input |
| 1099 |
<FANO>: FAN specification for output (default: stdout) |
| 1100 |
-e Write error messages to stdout not stderr |
| 1101 |
-H Exclude time-stamp & LOGNAME from history |
| 1102 |
-h Do not write history |
| 1103 |
-m If any value missing then result missing |
| 1104 |
-p Persevere after errors |
| 1105 |
-s Silent mode: Suppress warning messages |
| 1106 |
-b <int>: Max. buffer size (Kbytes) (default: 512) |
| 1107 |
-c <int>: Rank (decrement if < 0) of any Created variable including stdout |
| 1108 |
(default: input rank for broadcast, else -1) |
| 1109 |
-f <string>: Format for stdout (default: C_format attribute ("%G" if none)) |
| 1110 |
-M <string>: Missing value for stdout (default: _ ) |
| 1111 |
-n <integer>: Number of fields per line for stdout (default: 10 if numeric) |
| 1112 |
(Environment variable COLUMNS defines default for characters) |
| 1113 |
-r <string>: Reduction type (am broadcast count fill gm max min prod |
| 1114 |
sd sd1 sum sum2) (default: broadcast) |
| 1115 |
-t char|byte|short|long|float|double: new variable type (default: input type) |
| 1116 |
-u <string>: Unit of measure for stdout (default: unit in file) |
| 1117 |
-w <reals>: Weight vector(e.g. -w '3 1.5 .8') |
| 1118 |
</strong> |
| 1119 |
</pre> |
| 1120 |
<p>If the `<tt><b>/</b></tt>' is omitted then the final argument is taken |
| 1121 |
as <tt><b><FANO></b></tt>. (This version 1 convention is deprecated.) |
| 1122 |
If <tt><b><FANO></b></tt> does not specify a filename or variable name |
| 1123 |
then the first one in <tt><b><FANI></b></tt> is used.</p> |
| 1124 |
<h3>Examples</h3> |
| 1125 |
<p>The following prints the variable <tt><b>M</b></tt> in file <tt><b>mat.nc</b></tt>:</p> |
| 1126 |
<pre> |
| 1127 |
<strong>$ ncrob mat.nc M / |
| 1128 |
11 12 13 |
| 1129 |
21 22 23 |
| 1130 |
</strong> |
| 1131 |
</pre> |
| 1132 |
<p>The following prints the column sums, row means and overall product:</p> |
| 1133 |
<pre> |
| 1134 |
<strong>$ ncrob -r sum mat.nc M / # sum of each column |
| 1135 |
32 34 36 |
| 1136 |
$ ncrob -r am mat.nc 'M[col]' / # arithmetic mean of each row |
| 1137 |
12 22 |
| 1138 |
$ ncrob -r prod -c 0 -f '%.0f' mat.nc M / # overall product |
| 1139 |
18234216 |
| 1140 |
</strong> |
| 1141 |
</pre> |
| 1142 |
<p>The first two commands have no <tt><b>-c</b></tt> option, so the rank of the |
| 1143 |
result is one less than that of the input. The third specifies <tt><b>-c 0</b></tt>, |
| 1144 |
so the result has rank 0, i.e. is scalar. The following attempts to put this |
| 1145 |
same data into three new variables in the same file:</p> |
| 1146 |
<pre> |
| 1147 |
<strong>$ ncrob -h -r sum mat.nc M / col_sum |
| 1148 |
$ ncrob -h -r am mat.nc 'M[col]' / row_am |
| 1149 |
$ ncrob -h -r prod -c 0 mat.nc M / prod |
| 1150 |
$ ncdump mat.nc |
| 1151 |
netcdf mat { |
| 1152 |
dimensions: |
| 1153 |
row = 2 ; |
| 1154 |
col = 3 ; |
| 1155 |
variables: |
| 1156 |
short M(row, col) ; |
| 1157 |
short col_sum(col) ; |
| 1158 |
short row_am(row) ; |
| 1159 |
short prod ; |
| 1160 |
data: |
| 1161 |
M = |
| 1162 |
11, 12, 13, |
| 1163 |
21, 22, 23 ; |
| 1164 |
col_sum = 32, 34, 36 ; |
| 1165 |
row_am = 12, 22 ; |
| 1166 |
prod = _ ; |
| 1167 |
} |
| 1168 |
</strong> |
| 1169 |
</pre> |
| 1170 |
<p>Why is <tt><b>prod</b></tt> dumped as <tt><b>_</b></tt> (i.e. fill) rather |
| 1171 |
than 18234216 as before? The problem is that <tt><b>ncrob</b></tt> has created |
| 1172 |
a new variable of the same type as the source variable, which in this case is |
| 1173 |
<tt><b>short</b></tt> and incapable of storing such a large number. The solution |
| 1174 |
is to specify the type using the <tt><b>-t</b></tt> option. Let's also create |
| 1175 |
a new file <tt><b>prod.nc</b></tt>. E.g.</p> |
| 1176 |
<pre> |
| 1177 |
<strong>$ ncrob -h -r prod -c 0 -t long mat.nc M / prod.nc prod |
| 1178 |
$ ncdump prod.nc |
| 1179 |
netcdf prod { |
| 1180 |
variables: |
| 1181 |
long prod ; |
| 1182 |
data: |
| 1183 |
prod = 18234216 ; |
| 1184 |
} |
| 1185 |
</strong> |
| 1186 |
</pre> |
| 1187 |
<p>Next let's calculate a weighted mean of each column. Let's give the |
| 1188 |
first row twice the weight of the second:</p> |
| 1189 |
<pre> |
| 1190 |
<strong>$ ncrob -r am -w '2 1' M mat.nc / |
| 1191 |
14.3333 15.3333 16.3333 |
| 1192 |
</strong> |
| 1193 |
</pre> |
| 1194 |
<p>Thus the mean of the first column is (2 * 11 + 1 * 21)/3 = 14.3333. Negative |
| 1195 |
weights can be used to obtain differences. E.g.</p> |
| 1196 |
<pre> |
| 1197 |
<strong>$ ncrob -r sum -w '1 -1' M mat.nc / # row1 - row2 |
| 1198 |
-10 -10 -10 |
| 1199 |
$ ncrob -r sum -w '1 -1' 'M(col=3 1]' mat.nc / # col3 - col1 |
| 1200 |
</strong> |
| 1201 |
</pre> |
| 1202 |
<p>Finally we demonstrate broadcasting. Let's first copy the matrix <tt><b>M</b></tt> |
| 1203 |
to two variables in a new file called <tt><b>new.nc</b></tt>. One new variable |
| 1204 |
has the same name (<tt><b>M</b></tt>) and shape. The other is named <tt><b>V</b></tt> |
| 1205 |
and is a vector with the new dimension <tt><b>row_col</b></tt> formed from dimensions |
| 1206 |
<tt><b>row</b></tt> and <tt><b>col</b></tt>.</p> |
| 1207 |
<pre> |
| 1208 |
<strong>$ ncrob -h M mat.nc / new.nc |
| 1209 |
$ ncrob -h -c 1 M mat.nc / new.nc V |
| 1210 |
$ ncdump new.nc |
| 1211 |
netcdf new { |
| 1212 |
dimensions: |
| 1213 |
row = 2 ; |
| 1214 |
col = 3 ; |
| 1215 |
row_col = 6 ; |
| 1216 |
variables: |
| 1217 |
short M(row, col) ; |
| 1218 |
short V(row_col) ; |
| 1219 |
data: |
| 1220 |
M = |
| 1221 |
11, 12, 13, |
| 1222 |
21, 22, 23 ; |
| 1223 |
V = 11, 12, 13, 21, 22, 23 ; |
| 1224 |
} |
| 1225 |
</strong> |
| 1226 |
</pre> |
| 1227 |
<p>Now let's broadcast the variable <tt><b>col_sum</b></tt> in file <tt><b>mat.nc</b></tt> |
| 1228 |
to these variables <tt><b>M</b></tt> and <tt><b>V</b></tt> in the file <tt><b>new.nc</b></tt>:</p> |
| 1229 |
<pre> |
| 1230 |
<strong>$ ncrob -h mat.nc col_sum / new.nc M V |
| 1231 |
$ ncdump new.nc |
| 1232 |
netcdf new { |
| 1233 |
dimensions: |
| 1234 |
row = 2 ; |
| 1235 |
col = 3 ; |
| 1236 |
row_col = 6 ; |
| 1237 |
variables: |
| 1238 |
short M(row, col) ; |
| 1239 |
short V(row_col) ; |
| 1240 |
data: |
| 1241 |
M = |
| 1242 |
32, 34, 36, |
| 1243 |
32, 34, 36 ; |
| 1244 |
V = 32, 34, 36, 32, 34, 36 ; |
| 1245 |
} |
| 1246 |
</strong> |
| 1247 |
</pre> |
| 1248 |
<p>Four copies of the input were needed to fill the output.</p> |
| 1249 |
<h2>text2nc</h2> |
| 1250 |
<p>This utility reads ASCII text data from standard input and writes it to a netCDF |
| 1251 |
variable or attribute. The netCDF file and variable must already exist. However, |
| 1252 |
as discussed in Section <a |
| 1253 |
href="#Attributes"><em>Attributes</em></a>, <tt><b>text2nc</b></tt> can create |
| 1254 |
attributes, delete them, and modify their type, size and value. If end-of-input |
| 1255 |
occurs before end-of-output then the input values are recycled.</p> |
| 1256 |
<p><a id="text2nc_Usage" name="text2nc_Usage"></a></p> |
| 1257 |
<h3>Usage</h3> |
| 1258 |
<pre> |
| 1259 |
<strong>Usage: text2nc [-eHhps] [-m %f] [-u %s] <FANO> |
| 1260 |
<FANO>: netCDF FAN specification for output |
| 1261 |
-e Write error messages to stdout not stderr |
| 1262 |
-H Exclude time-stamp & LOGNAME from history |
| 1263 |
-h Do not write history |
| 1264 |
-p Persevere after errors |
| 1265 |
-s Silent mode: Suppress warning messages |
| 1266 |
-m <real>: Missing value for input (default: 1.79769E+308) |
| 1267 |
-t char|byte|short|long|float|double: data-type (for attributes only) |
| 1268 |
(default: double for numeric input data, else char) |
| 1269 |
-u <string>: Unit of measure for input (default: unit in file) |
| 1270 |
</strong> |
| 1271 |
</pre> |
| 1272 |
<p><a id="text2nc_Examples" name="text2nc_Examples"></a></p> |
| 1273 |
<h3>Examples</h3> |
| 1274 |
<p>Let's start with the following file:</p> |
| 1275 |
<pre> |
| 1276 |
<strong>$ ncdump vec.nc |
| 1277 |
netcdf vec { |
| 1278 |
dimensions: |
| 1279 |
n = UNLIMITED ; // (5 currently) |
| 1280 |
variables: |
| 1281 |
float v(n) ; |
| 1282 |
data: |
| 1283 |
v = 10 , 20.3 , 30.2 , 40.9 , 50 ; |
| 1284 |
} |
| 1285 |
</strong> |
| 1286 |
</pre> |
| 1287 |
<p>Let's assume these data are Celsius temperatures, so we define a valid |
| 1288 |
minimum by:</p> |
| 1289 |
<pre> |
| 1290 |
<strong>$ echo -273.2 | text2nc -h vec.nc 'v:valid_min' |
| 1291 |
</strong> |
| 1292 |
</pre> |
| 1293 |
<p>Then we modify two existing values:</p> |
| 1294 |
<pre> |
| 1295 |
<strong>$ echo 15 17 | text2nc -h -u degC vec.nc 'v[0 3]' |
| 1296 |
$ ncdump vec.nc |
| 1297 |
netcdf vec { |
| 1298 |
dimensions: |
| 1299 |
n = UNLIMITED ; // (5 currently) |
| 1300 |
variables: |
| 1301 |
float v(n) ; |
| 1302 |
v:valid_min = -273.2 ; |
| 1303 |
v:units = "degC" ; |
| 1304 |
data: |
| 1305 |
v = 15 , 20.3 , 30.2 , 17 , 50 ; |
| 1306 |
} |
| 1307 |
</strong> |
| 1308 |
</pre> |
| 1309 |
<p>Note that the <tt><b>units</b></tt> attribute was created because we specified |
| 1310 |
<tt><b>-u degC</b></tt>, but there was no existing <tt><b>units</b></tt> |
| 1311 |
attribute. Now let's append three values and print the resulting <tt><b>v</b></tt>:</p> |
| 1312 |
<pre> |
| 1313 |
<strong>$ echo -999 32 1e9 | text2nc -h -u degF -m 1e9 vec.nc 'v[-1+1:-1+3]' |
| 1314 |
$ nc2text -f '%0.1f' vec.nc v |
| 1315 |
15.0 20.3 30.2 17.0 50.0 _ 0.0 _ |
| 1316 |
</strong> |
| 1317 |
</pre> |
| 1318 |
<p>The first value (<tt><b>-999</b></tt>) is treated as missing because (even |
| 1319 |
after conversion to Celsius) it is less than the valid minimum of <tt><b>-273.2</b></tt>. |
| 1320 |
The second value (32�F) is converted to 0�C. The third value (<tt><b>1e9</b></tt>) |
| 1321 |
is treated as missing because it matches the input missing value specified by |
| 1322 |
<tt><b>-m 1e9</b></tt>.</p> |
| 1323 |
<p>Finally, let's change every second value to 0�K:</p> |
| 1324 |
<pre> |
| 1325 |
<strong>$ echo 0 | text2nc -h -u degK vec.nc 'v[1::2]' |
| 1326 |
$ nc2text -f '%0.1f' vec.nc v |
| 1327 |
15.0 -273.1 30.2 -273.1 50.0 -273.1 0.0 -273.1 |
| 1328 |
</strong> |
| 1329 |
</pre> |
| 1330 |
<p></p> |
| 1331 |
<!-- InstanceEndEditable --> |
| 1332 |
</div> <!-- /#container --> |
| 1333 |
</div> <!-- /#wrap --> |
| 1334 |
|
| 1335 |
<!-- See footer.css for the styling of this section --> |
| 1336 |
<footer> |
| 1337 |
<div id="triptych-column-container"> |
| 1338 |
<div id="triptych-columns"> |
| 1339 |
<div class="column triptych_left"> |
| 1340 |
<h2>Follow Unidata</h2> |
| 1341 |
<ul class="tabular half follow-icons"> |
| 1342 |
<li> |
| 1343 |
<a href="https://twitter.com/unidata"><img src="/images/twitter.png" alt="Twitter" title="Twitter" /></a> |
| 1344 |
</li> |
| 1345 |
<li> |
| 1346 |
<a href="https://plus.google.com/105982992127916444326/posts"><img src="/images/googleplus.png" alt="Google+" title="Google+" /></a> |
| 1347 |
</li> |
| 1348 |
<li> |
| 1349 |
<a href="https://www.facebook.com/Unidata"><img src="/images/facebook.png" alt="Facebook" title="Facebook"/></a> |
| 1350 |
</li> |
| 1351 |
<li> |
| 1352 |
<a href="http://www.youtube.com/user/unidatanews"><img src="/images/youtube.png" alt="YouTube Channel" title="YouTube Channel"/></a> |
| 1353 |
</li> |
| 1354 |
<li> |
| 1355 |
<a href="https://www.unidata.ucar.edu/blogs/news/feed/entries/atom"><img src="/images/rss.png" alt="News@Unidata blog" title="News@Unidata blog"/></a> |
| 1356 |
</li> |
| 1357 |
<li> |
| 1358 |
<a href="https://www.unidata.ucar.edu/blogs/developer/feed/entries/atom"><img src="/images/blog.png" alt="Developers' Blog" title="Developers' Blog"/></a> |
| 1359 |
</li> |
| 1360 |
</ul> |
| 1361 |
</div> <!-- /.column column_left --> |
| 1362 |
|
| 1363 |
<div class="column triptych_center"> |
| 1364 |
<h2>Unidata |
| 1365 |
<div class="triptych_logos"> |
| 1366 |
<a class="noicon" href="http://www.nsf.gov/"> <img alt="UCP" src="/images/logos/nsf_logo_transparent.png"/></a> |
| 1367 |
</div> |
| 1368 |
<div class="triptych_logos" style="padding-right:1em;"> |
| 1369 |
<a class="noicon" href="http://www.ucp.ucar.edu/"> <img alt="UCP" id="ucp_small" src="/images/logos/ucp-logo2_transparent.png"/></a> |
| 1370 |
</div></h2> |
| 1371 |
<ul class="tabular full" style="padding-right:0.5em;"> |
| 1372 |
<li style="text-align: center;"> |
| 1373 |
Unidata is s a member of the |
| 1374 |
<a href="http://www.ucp.ucar.edu/">UCAR Community Programs</a>, |
| 1375 |
managed by the |
| 1376 |
<a href="http://www.ucar.edu">University Corporation for Atmospheric Research</a>, |
| 1377 |
and funded by the |
| 1378 |
<a href="http://www.nsf.gov">National Science Foundation</a>. |
| 1379 |
</li> |
| 1380 |
<li style="text-align: center;"> |
| 1381 |
Read Unidata's <a href="https://www.unidata.ucar.edu/legal/participation.html">Participation Policy</a>. |
| 1382 |
</li> |
| 1383 |
<li> |
| 1384 |
<table style="width:100%"> |
| 1385 |
<tr> |
| 1386 |
<td style="width:33%;text-align:left;">© |
| 1387 |
<script type="text/javascript"> |
| 1388 |
document.write(new Date().getFullYear()); |
| 1389 |
</script> UCAR</td> |
| 1390 |
<td style="width:33%;text-align:center;"><a href="http://www2.ucar.edu/privacy-policy">Privacy Policy</a></td> |
| 1391 |
<td style="width:33%;text-align:right;"><a href="http://www2.ucar.edu/terms-of-use">Terms of Use</a></td> |
| 1392 |
</tr> |
| 1393 |
</table> |
| 1394 |
</li> |
| 1395 |
</ul> |
| 1396 |
</div> <!-- /.column column_center --> |
| 1397 |
|
| 1398 |
<div class="column triptych_right"> |
| 1399 |
<h2>Contact Unidata</h2> |
| 1400 |
<ul class="tabular full"> |
| 1401 |
<li> |
| 1402 |
For support: <b><a href="mailto:[email protected]">[email protected]</a></b> |
| 1403 |
</li> |
| 1404 |
<li> |
| 1405 |
By <b><a href="/about/index.html#visit">postal mail</a></b> or <b><a href="/about/index.html#visit">in person</a></b> |
| 1406 |
</li> |
| 1407 |
<li> |
| 1408 |
About this website: <b><a href="mailto:[email protected]">[email protected]</a></b> |
| 1409 |
</li> |
| 1410 |
<li> |
| 1411 |
By telephone: <b>303.497.8643</b> |
| 1412 |
</li> |
| 1413 |
</ul> |
| 1414 |
</div> <!-- /.column column_right --> |
| 1415 |
|
| 1416 |
<a href="/about/tour/"> |
| 1417 |
<div class="column column_extra" id="who_are_we"> |
| 1418 |
<h2>Welcome to Unidata</h2> |
| 1419 |
<ul class="tabular full"> |
| 1420 |
<li> |
| 1421 |
Learn more about who we are and what we do... |
| 1422 |
</li> |
| 1423 |
</ul> |
| 1424 |
</div> <!-- /.column column_extra --> |
| 1425 |
</a> |
| 1426 |
|
| 1427 |
<span class="stretch"> </span> |
| 1428 |
</div> <!-- /#triptych-columns --> |
| 1429 |
</div> <!-- /#triptych-column-container --> |
| 1430 |
</footer> |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
</body> |
| 1435 |
|
| 1436 |
|
| 1437 |
<!-- InstanceEnd --> |