Repository: ARM-DOE/ACT

Issue:
Add option to include required global attributes when writing to netCDF
Some people are planning to use ACT to generate official datastreams and need to have required global attributes set. We should help them add the correct attributes with correct values.


Discussion:
These are the attributes in question

:command_line = "run Jupyter Notebook workbench" ;
:process_version = "" ;
:history = "created on Jupyter workbench" ;
@kenkehoe  I was thinking about this and I'm not sure we need to force users to set this.  I think an example showing users how they can add these attributes would suffice.  Thoughts?
I agree we would not force users to set these global attributes. But we could add some code to help set the variables. Maybe a few method calls to set the values before writing?
I can write up this example. Do we normally set this attributes with system commands or manually input them? And is there an example on what these usually state?


Resolving patch:
diff --git a/act/utils/inst_utils.py b/act/utils/inst_utils.py
--- a/act/utils/inst_utils.py
+++ b/act/utils/inst_utils.py
@@ -134,9 +134,14 @@ def decode_present_weather(ds, variable=None, decoded_name=None):
 
     # Massage the data array to set back in the dataset
     data.values = wx_type
-    data.attrs['long_name'] = data.attrs['long_name'] + ' Decoded'
-    del data.attrs['valid_min']
-    del data.attrs['valid_max']
+    if 'long_name' in data.attrs:
+        data.attrs['long_name'] = data.attrs['long_name'] + ' Decoded'
+    else:
+        data.attrs['long_name'] = 'Decoded present weather values'
+    if 'valid_min' in data.attrs:
+        del data.attrs['valid_min']
+    if 'valid_max' in data.attrs:
+        del data.attrs['valid_max']
 
     ds[decoded_name] = data
 
diff --git a/examples/io/plot_create_arm_ds.py b/examples/io/plot_create_arm_ds.py
new file mode 100644
--- /dev/null
+++ b/examples/io/plot_create_arm_ds.py
@@ -0,0 +1,55 @@
+"""
+Create a dataset to mimic ARM file formats
+------------------------------------------
+Example shows how to create a dataset from an ARM DOD.
+This will enable users to create files that mimic ARM
+files, making for easier use across the community.
+
+Author: Adam Theisen
+
+"""
+
+import act
+
+# Create an empty dataset using an ARM DOD
+ds = act.io.armfiles.create_ds_from_arm_dod('vdis.b1', {'time': 1440}, scalar_fill_dim='time')
+
+# Print out the xarray dataset to see that it's empty
+print(ds)
+
+# The user could populate this through a number of ways
+# and that's best left up to the user on how to do it.
+# If one has an existing dataset, a mapping of variable
+# names is sometimes the easiest way
+
+# Let's look at some variable attributes
+# These can be updated and it would be up to the
+# user to ensure these tests are being applied
+# and are appropriately set in the cooresponding QC variable
+print(ds['num_drops'].attrs)
+
+# Next, let's print out the global attribuets
+print(ds.attrs)
+
+# Add additional attributes or append to existing
+# if they are needed using a dictionary
+atts = {
+    'command_line': 'python  plot_create_arm_ds.py',
+    'process_version': '1.2.3',
+    'history': 'Processed with Jupyter Workbench',
+    'random': '1234253sdgfadf'
+}
+for a in atts:
+    if a in ds.attrs:
+        ds.attrs[a] += atts[a]
+    else:
+        ds.attrs[a] = atts[a]
+    # Print out the attribute
+    print(a, ds.attrs[a])
+
+# Write data out to netcdf
+ds.to_netcdf('./sgpvdisX1.b1.20230101.000000.nc')
+
+# If one wants to clean up the dataset to better match CF standards
+# the following can be done as well
+ds.write.write_netcdf(cf_compliant=True, path='./sgpvdisX1.b1.20230101.000000.cf')


Test patch: