| 506 | self.decode_timedelta = decode_timedelta |
| 507 | |
| 508 | def encode(self, variable: Variable, name: T_Name = None) -> Variable: |
| 509 | dims, data, attrs, encoding = unpack_for_encoding(variable) |
| 510 | |
| 511 | if "scale_factor" in encoding or "add_offset" in encoding: |
| 512 | # if we have a _FillValue/masked_value we do not want to cast now |
| 513 | # but leave that to CFMaskCoder |
| 514 | dtype = data.dtype |
| 515 | if "_FillValue" not in encoding and "missing_value" not in encoding: |
| 516 | dtype = _choose_float_dtype(data.dtype, encoding) |
| 517 | # but still we need a copy prevent changing original data |
| 518 | data = duck_array_ops.astype(data, dtype=dtype, copy=True) |
| 519 | if "add_offset" in encoding: |
| 520 | data -= pop_to(encoding, attrs, "add_offset", name=name) |
| 521 | if "scale_factor" in encoding: |
| 522 | data /= pop_to(encoding, attrs, "scale_factor", name=name) |
| 523 | |
| 524 | return Variable(dims, data, attrs, encoding, fastpath=True) |
| 525 | |
| 526 | def decode(self, variable: Variable, name: T_Name = None) -> Variable: |
| 527 | _attrs = variable.attrs |