! bn_save_asglobal
! 11/2017
!  6/18 add tests for writing scalar numeric variables
! Simple command to save a variable as a global attribute.
! Works on an existing or a new file.

! Write to a new file.  Gets the default global attributes.

can mode upcase
let my_extra_attribute = "Extra attribute: This file is just attributes to start with"
save/asglobal/clobber/file=a.nc my_extra_attribute
sp ncdump -h a.nc

! Or write new attributes to a file that has data

use coads_climatology
save/clobber/file=a.nc sst[x=150:170,y=0:10]
let my_extra_attribute = "Extra attribute: Adding to a file with existing data and attributes"
save/asglobal/append/file=a.nc my_extra_attribute

let my_num_att = 12.56
save/asglobal/append/file=a.nc my_num_att
sp ncdump -h a.nc

! Create an attribute from a file variable

use maize_1
save/append/file=a.nc/j=4/outtype=float/asglobal maize_yield
sp ncdump -h a.nc

! A second write of the same attribute overwrites the attribute values 
! (append here is saying dont start a new file)

save/append/file=a.nc/j=5/outtype=float/asglobal maize_yield
sp ncdump -h a.nc

! Allow for various /OUTTYPE as in any SAVE command
let another = {1, 2, 3.1}
save/append/file=a.nc/outtype=INT/asglobal another

let negnum = -123.4
save/append/file=a.nc/outtype=INT/asglobal negnum
sp ncdump -h a.nc


! Intentional errors 
set mode ignore
set data 1

! String attributes may only be single-valued
let multistring = {"a", "b", "c", "", "e"} 
save/asglobal/append/file=a.nc multistring

! Refuse to write multi-dimensioned attributes y-t
save/append/asglobal/x=150/outtype=float/file=a.nc sst

! Also arbitrarily refuse to write more than a list of 100 values.
set mode ignore
save/append/asglobal/y=10/L=2/outtype=float/file=a.nc sst

set mode /last upcase
