-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IMPRO-511 implement metadata cube saving #538
Changes from 1 commit
acf8cb4
ceeb6be
f016ea3
37d4383
c837506
2281ecc
784f7f9
e8959e1
17c1e07
752a220
258fc3f
a6b743f
b8daf7d
221a0b3
4e25fae
ab9f53d
b90d42d
688f102
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,8 +43,35 @@ def append_metadata_cube(cubelist): | |
Returns: | ||
iris.cube.Cubelist with appended metadata cube | ||
""" | ||
|
||
keys_for_global_attr = {} | ||
|
||
# Set up a basic prefix cube | ||
prefix_cube = iris.cube.Cube(0, long_name='prefixes', | ||
var_name='prefix_list') | ||
|
||
# Iterate through the cubelist cubes attributes (using dictionary | ||
# comprehension), collecting attributes that we want to be global | ||
# attributes in a resulting netCDF file. | ||
for cube in cubelist: | ||
keys = cube.attributes | ||
keys_for_global_attr = {k: v for k, v in keys.items() | ||
if k is 'um_version' | ||
or k is 'institution' | ||
or k is 'source' | ||
or k is 'grid_id' | ||
or k is 'history' | ||
or k is 'Conventions' | ||
or k is 'title'} | ||
|
||
# Attributes have to appear on all cubes in a cubelist for Iris 2 to save | ||
# these attributes as global in a resulting netCDF file, so add all of the | ||
# global attributes to the prefix cube (otherwise they will be made | ||
# variables in the netCDF file). | ||
for key, value in keys_for_global_attr.iteritems(): | ||
prefix_cube.attributes[key] = value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah actually, I think what this will do (when you create a set above) is add attributes to the prefix cube which have no value (as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And maybe a unit test to check the right global attributes are added? |
||
|
||
# Add metadata prefix attributes to the prefix cube | ||
prefix_cube.attributes['spp__'] = \ | ||
'http://reference.metoffice.gov.uk/statistical-process/properties/' | ||
prefix_cube.attributes['spv__'] = \ | ||
|
@@ -57,6 +84,7 @@ def append_metadata_cube(cubelist): | |
|
||
cubelist = list(cubelist) | ||
cubelist.append(prefix_cube) | ||
# bald__isPrefixedBy should be an attribute on all the cubes | ||
for cube in cubelist: | ||
cube.attributes['bald__isPrefixedBy'] = 'prefix_list' | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid multiple definitions, could we pass in the list of global keys as an argument to
append_metadata_cube
? This would then simplify this code block to be:keys_for_global_attr = {k for k in cube.attributes.keys() if k in global_keys}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK so I've just tried this out using a list of global keys in append_metadata_cube (which could or course be passed in as argument ultimately).
Your code seems to be producing a set rather than dictionary which caused problems when adding the keys to the prefix cube, so I added a line to produce a dictionary from the set which also set's the global key's value to ' '. I'm not sure if this is what you meant?
For the attributes to kept as global I'm not sure if the keys and the values of the attributes have to be the same or just the keys? (Its not clear to me from the Iris2 docs, but guess I'll find out when I generate some new data)
I'll push up this commit so you can see what you think.......
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could have it as I put it (returning a set), but then further down (at line 65) you need the following:
That's slightly simpler, but probably doesn't matter if your code works.
However I do think it would be best if we didn't define the list of global keys twice. I'd recommend changing
append_metadata_cube(cubelist)
toappend_metadata_cube(cubelist, global_keys)
, and passing in the list of global keys as defined insave_netcdf
. An alternative would be to defineGLOBAL_KEYS
at the top of the file as a global variable, rather than locally within the functions, but this is not the preferred convention (see https://google.github.io/styleguide/pyguide.html#Global_variables).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that would be the best way, passing in the global_keys list as an argument to append_metadata_cube.