Encode a set of CF encoded variables and attributes. Takes a dicts of variables and attributes and encodes them to conform to CF conventions as much as possible. This includes masking, scaling, character array handling, and CF-time encoding. Parameters ---------- va
(variables: T_Variables, attributes: T_Attrs)
| 764 | |
| 765 | |
| 766 | def cf_encoder(variables: T_Variables, attributes: T_Attrs): |
| 767 | """ |
| 768 | Encode a set of CF encoded variables and attributes. |
| 769 | Takes a dicts of variables and attributes and encodes them |
| 770 | to conform to CF conventions as much as possible. |
| 771 | This includes masking, scaling, character array handling, |
| 772 | and CF-time encoding. |
| 773 | |
| 774 | Parameters |
| 775 | ---------- |
| 776 | variables : dict |
| 777 | A dictionary mapping from variable name to xarray.Variable |
| 778 | attributes : dict |
| 779 | A dictionary mapping from attribute name to value |
| 780 | |
| 781 | Returns |
| 782 | ------- |
| 783 | encoded_variables : dict |
| 784 | A dictionary mapping from variable name to xarray.Variable, |
| 785 | encoded_attributes : dict |
| 786 | A dictionary mapping from attribute name to value |
| 787 | |
| 788 | See Also |
| 789 | -------- |
| 790 | decode_cf_variable, encode_cf_variable |
| 791 | """ |
| 792 | |
| 793 | # add encoding for time bounds variables if present. |
| 794 | _update_bounds_encoding(variables) |
| 795 | |
| 796 | new_vars = {} |
| 797 | for k, v in variables.items(): |
| 798 | try: |
| 799 | new_vars[k] = encode_cf_variable(v, name=k) |
| 800 | except Exception as e: |
| 801 | e.add_note(f"Raised while encoding variable {k!r} with value {v!r}") |
| 802 | raise |
| 803 | |
| 804 | # Remove attrs from bounds variables (issue #2921) |
| 805 | for var in new_vars.values(): |
| 806 | bounds = var.attrs.get("bounds") |
| 807 | if bounds and bounds in new_vars: |
| 808 | # see http://cfconventions.org/cf-conventions/cf-conventions.html#cell-boundaries |
| 809 | for attr in [ |
| 810 | "units", |
| 811 | "standard_name", |
| 812 | "axis", |
| 813 | "positive", |
| 814 | "calendar", |
| 815 | "long_name", |
| 816 | "leap_month", |
| 817 | "leap_year", |
| 818 | "month_lengths", |
| 819 | ]: |
| 820 | if ( |
| 821 | attr in new_vars[bounds].attrs |
| 822 | and attr in var.attrs |
| 823 | and new_vars[bounds].attrs[attr] == var.attrs[attr] |
searching dependent graphs…