## sdfwrite

`sdfwrite varname`

This`` command will write out a defined variable `varname` into a NetCDF formatted data file.

### Usage Notes

The `sdfwrite` command works with GrADS version 2.0.a3 (or higher). Changes to the command in version 2.0.a5 included the option to force the output to have 4 or 5 dimensions. Changes to include compression and floating-point output were added with version 2.0.a8.

The name of the output file will be `grads.sdfwrite.nc` unless specified otherwise with the `set sdfwrite` command. If the file exists, it will be replaced. The `q sdfwrite` command returns the status of the `sdfwrite` options.

The dimensions of the variable written to file correspond to the dimension environment that is set when the variable is defined. The dimension environment that is set when the 'sdfwrite' command is invoked is ignored. Note this behavior is different from the fwrite command.

By default, the output file will have a coordinate variable only for varying dimensions in the defined variable; non-varying dimensions will not appear as a coordinate variable with a size of 1. However, as of version 2.0.a5, options have been added to the `set sdfwrite` command to force the variable in the output file to have at least 4 or all 5 dimensions. When either of these options to `set sdfwrite` are used, the output file will retain information about the values of the dimensions that were fixed when the variable was defined; non-varying dimensions will appear as a coordinate variable of size 1.

The coordinate variables will be type 'double' and have two default attributes ("units" and "long_name") with values that are based on the GrADS 5-dimensional gridded data model. The data variable will also be type 'double' by default; beginning with version 2.0.a8, you can also write out data of type 'float' if you use the -flt option with `set sdfwrite` command. Data variables will have a 'missing value' attribute.

For example, if `varname` is called "testvar" and varies in X, Y, Z, T, and E, then the output file might have a NetCDF header that looks like this:

`netcdf foo {
dimensions:
longitude = 9 ;
latitude = 9 ;
level = 9 ;
time = 9 ;
ensemble = 9 ;
variables:
double longitude(longitude) ;
longitude:units = "degrees_east" ;
longitude:long_name = "Longitude" ;
double latitude(latitude) ;
latitude:units = "degrees_north" ;
latitude:long_name = "Latitude" ;
double level(level) ;
level:units = "millibar" ;
level:long_name = "Level" ;
double time(time) ;
time:units = "minutes since 01-01-0001 00:00" ;
time:long_name = "Time" ;
double ensemble(ensemble) ;
ensemble:grads_dim = "e" ;
ensemble:long_name = "Ensemble member" ;
double testvar(ensemble, time, level, latitude, longitude) ;
testvar:missing_value = -888. ;
}`

The time axis units will always be "minutes since ..." and the date of the time axis origin will correspond to the initial time of the defined variable.

If the variable has an ensemble dimension, the attribute "grads_dim" with the value "e" will always be present so that the resulting output file can be opened with GrADS using the 'sdfopen' command.

To supplement or override the default attributes of the output file, use the `set sdfattr` command.

Beginning with version 2.0.a8, the output file may also be a compressed netCDF file. Use the `-zip` option with the `set sdfwrite` command to enable compression. Please see the documentation on compression for more details.

### Examples

The following commands produce a regional subset of a global precip forecast :

`open global_forecast.ctl
set lon -111.3 -103.8
set lat 40.9 45.0
set lev 1000 10
set t 1 last
set e 1 last
define precip = ptot``
set sdfwrite wyoming_precip.nc
sdfwrite precip
`

Suppose you want to write out a long time series in one file, but memory limits do not allow you to define the entire time series in a single variable. You can write out the variable in time chunks, and then concatenate them together sequentially using the NetCDF Operators.

    open long_run.ctl