File size: 32,045 Bytes
f4a39ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Modules that computes relevant state features to be used by ML components."""

from typing import Any, Callable, Mapping, Optional, Protocol, Sequence
from dinosaur import coordinate_systems
from dinosaur import primitive_equations
from dinosaur import pytree_utils
from dinosaur import radiation
from dinosaur import scales
from dinosaur import spherical_harmonic
from dinosaur import typing
from dinosaur import xarray_utils
import gin
import haiku as hk
import jax
import jax.numpy as jnp
from model.legacy import transforms
import numpy as np


Array = typing.Array
Pytree = typing.Pytree
TransformModule = typing.TransformModule
KeyWithCosLatFactor = typing.KeyWithCosLatFactor


class FeaturesFn(Protocol):

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    ...


FeaturesModule = Callable[..., FeaturesFn]


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class PrimitiveEquationsDiagnosticState(hk.Module):
  """Features modules that returns processed DiagnosticState for PE."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )
    self.coords = coords

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> primitive_equations.DiagnosticState:
    del memory, diagnostics, randomness, forcing  # unused
    if not isinstance(inputs, primitive_equations.State):
      inputs = primitive_equations.State(**inputs)
    d_state = primitive_equations.compute_diagnostic_state(inputs, self.coords)
    return self.features_transform_fn(d_state.asdict())


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class VelocityAndPrognostics(hk.Module):
  """Features module that returns prognostics + u,v and optionally gradients."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      fields_to_include: Optional[Sequence[str]] = None,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      compute_gradients_module: TransformModule = transforms.EmptyTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )
    self.coords = coords
    self.fields_to_include = fields_to_include
    self.compute_gradients_fn = compute_gradients_module(
        coords, dt, physics_specs, aux_features
    )

  def _extract_features(
      self,
      inputs: typing.Pytree,
      prefix: str = '',
  ) -> typing.Pytree:
    """Returns a nodal velocity and prognostic features."""
    # Note: all intermediate features have an explicit cos-lat factors in key.
    # These factors are removed in the `__call__` method before returning.

    # compute `u, v` if div/curl is available and `u, v` not in prognosics.
    if set(['vorticity', 'divergence']).issubset(inputs.keys()) and not set(
        ['u', 'v']
    ).intersection(inputs.keys()):
      cos_lat_u, cos_lat_v = spherical_harmonic.get_cos_lat_vector(
          inputs['vorticity'], inputs['divergence'], self.coords.horizontal
      )
      modal_features = {
          KeyWithCosLatFactor(prefix + 'u', 1): cos_lat_u,
          KeyWithCosLatFactor(prefix + 'v', 1): cos_lat_v,
      }
    else:
      modal_features = {}
    prognostics_keys = list(inputs.keys())
    prognostics_keys.remove('tracers')
    prognostics_keys.remove('sim_time')
    for k in prognostics_keys:
      if self.fields_to_include is None or k in self.fields_to_include:
        modal_features[KeyWithCosLatFactor(prefix + k, 0)] = inputs[k]

    for k, v in inputs['tracers'].items():
      if self.fields_to_include is None or k in self.fields_to_include:
        modal_features[KeyWithCosLatFactor(prefix + k, 0)] = v
    # Computing gradient features and adjusting cos_lat factors.
    modal_features = self.coords.with_dycore_sharding(modal_features)
    diff_operator_features = self.compute_gradients_fn(modal_features)
    sec_lat = 1 / self.coords.horizontal.cos_lat
    sec2_lat = self.coords.horizontal.sec2_lat
    sec_lat_scales = {0: 1, 1: sec_lat, 2: sec2_lat}
    # Computing all features in nodal space.
    features = {}
    for k, v in (diff_operator_features | modal_features).items():
      sec_lat_scale = sec_lat_scales[k.factor_order]
      features[k.name] = self.coords.horizontal.to_nodal(v) * sec_lat_scale
    features = self.coords.with_dycore_sharding(features)
    return features

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del memory, diagnostics, randomness, forcing  # unused.
    nodal_features = self._extract_features(inputs)
    return self.features_transform_fn(nodal_features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class MemoryVelocityAndValues(VelocityAndPrognostics):
  """Similar to `VelocityAndPrognostics`, but operates on memory."""

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del inputs, diagnostics, randomness, forcing  # unused.
    nodal_features = self._extract_features(memory, 'memory_')
    return self.features_transform_fn(nodal_features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class NodalInputVelocityAndPrognostics(VelocityAndPrognostics):
  """Features modules that returns velocities, temperature, and optionally gradients."""

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    to_modal_fn = self.coords.horizontal.to_modal
    inputs = to_modal_fn(inputs)
    memory = to_modal_fn(memory)
    return super().__call__(inputs, memory, randomness, forcing)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class RadiationFeatures(hk.Module):
  """Feature module that computes incident radiation flux."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )
    ref_datetime_str = str(aux_features[xarray_utils.REFERENCE_DATETIME_KEY])
    self.solar_radiation = radiation.SolarRadiation.normalized(
        coords=coords,
        physics_specs=physics_specs,
        reference_datetime=np.datetime64(ref_datetime_str),
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del memory, diagnostics, randomness, forcing  # unused.
    features = {}
    features['radiation'] = self.solar_radiation.radiation_flux(
        inputs['sim_time']
    )
    # TODO(janniyuval) add a flag that allow to get radiation of next time step
    # insert a feature axis.
    features = jax.tree_util.tree_map(lambda x: jnp.expand_dims(x, 0), features)
    return self.features_transform_fn(features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class OrbitalTimeFeatures(hk.Module):
  """Feature module that computes orbital time features."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )
    ref_datetime_str = str(aux_features[xarray_utils.REFERENCE_DATETIME_KEY])
    self.solar_radiation = radiation.SolarRadiation.normalized(
        coords=coords,
        physics_specs=physics_specs,
        reference_datetime=np.datetime64(ref_datetime_str),
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del memory, diagnostics, randomness, forcing  # unused.
    features = {}
    # Cosine and sine of Earth's orbital phase around the Sun
    orbital_time = self.solar_radiation.time_to_orbital_time(inputs['sim_time'])
    # Convert from orbital_phase=0 on January 1st UTC to orbital_phase=0 at the
    # approximate perihelion (when earth is closest to the sun).
    orbital_phase = orbital_time.orbital_phase - radiation.PERIHELION
    # All longitude, latitude locations share the same orbital phase
    ones = jnp.ones(self.solar_radiation.coords.surface_nodal_shape)
    features['cos_orbital_phase'] = jnp.cos(orbital_phase) * ones
    features['sin_orbital_phase'] = jnp.sin(orbital_phase) * ones
    # Cosine and sine of local hour angle (angle from solar noon)
    solar_hour_angle = self.solar_radiation.solar_hour_angle(inputs['sim_time'])
    solar_hour_angle = jnp.expand_dims(solar_hour_angle, 0)
    features['cos_solar_hour'] = jnp.cos(solar_hour_angle)
    features['sin_solar_hour'] = jnp.sin(solar_hour_angle)
    # TODO(janniyuval) add a flag that allow to get radiation of next time step
    return self.features_transform_fn(features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class ForcingFeatures(hk.Module):
  """Feature module that provides forcing values as features."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      forcing_to_include: Sequence[str] = tuple(),
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.forcing_to_include = forcing_to_include
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )

  def __call__(
      self,
      inputs: Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Forcing] = None,
  ) -> Pytree:
    del inputs, memory, diagnostics, randomness
    features = {}
    for key in self.forcing_to_include:
      value = forcing[key]  # pyrefly: ignore[unsupported-operation]
      # Expect singleton "level" dimension for surface forcings
      if value.ndim > 3:
        raise ValueError(
            f'Expected forcing "{key}" to have ndim <= 3, got {value.ndim}'
        )
      if value.ndim == 2:
        value = jnp.expand_dims(value, axis=0)
      if value.shape[0] != 1:
        raise ValueError(
            f'Expected forcing "{key}" to have leading dimension 1'
            f'for level, got {value.shape}'
        )
      features[key] = value

    return self.features_transform_fn(features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class LatitudeFeatures(hk.Module):
  """Feature module that creates cos and sin of latitude as features."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )
    self.coords = coords

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del inputs, memory, diagnostics, randomness, forcing  # unused.
    _, sin_lat = self.coords.horizontal.nodal_mesh
    sin_features = sin_lat[np.newaxis, ...]
    cos_features = jnp.cos(jnp.arcsin(sin_features))
    features = {
        'cos_latitude': cos_features,
        'sin_latitude': sin_features,
    }
    return self.features_transform_fn(features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class RandomnessFeatures(hk.Module):
  """Feature module that returns fields from `randomness` as features."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del inputs, memory, diagnostics, forcing  # unused.
    if randomness is None:
      random_features = {}
    elif isinstance(randomness, dict):
      random_features, _ = pytree_utils.flatten_dict(randomness)
    elif isinstance(randomness, jax.Array):
      random_features = {'randomness': randomness}
    else:
      raise ValueError(f'randomness has unsupported {type(randomness)=}.')
    # random fields are 2D by construction, adding a feature/level dimension.
    if randomness is not None:
      ndims = set(x.ndim for x in jax.tree_util.tree_leaves(random_features))
      if not ndims.issubset({2, 3}):
        raise ValueError(
            f'Random fields expected to be 2D and/or 3D. Found {ndims=}'
        )

      def make_3d(x):
        if x.ndim == 3:
          return x
        if x.ndim == 2:
          return x[np.newaxis, ...]

      random_features = jax.tree_util.tree_map(make_3d, random_features)
    return self.features_transform_fn(random_features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class OrographyFeatures(hk.Module):
  """Feature module that computes orographic features."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    if xarray_utils.OROGRAPHY not in aux_features:
      raise ValueError('OrographyFeatures requires orography in aux_features.')
    self.nodal_orography = aux_features[xarray_utils.OROGRAPHY]
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del inputs, memory, diagnostics, randomness, forcing  # unused.
    features = {
        xarray_utils.OROGRAPHY: jnp.expand_dims(self.nodal_orography, 0),
    }
    return self.features_transform_fn(features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class OneHotAuxFeatures(hk.Module):
  """Feature module that produces one-hot encodings from binary covariates."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      covariate_keys: Sequence[str] = gin.REQUIRED,  # pyrefly: ignore[bad-function-definition]
      convert_float_to_int: bool = False,
      name: Optional[str] = None,
  ):
    del coords, dt, physics_specs  # unused.
    super().__init__(name=name)
    covariates = {}
    num_classes = {}
    for key in covariate_keys:
      if key not in aux_features:
        raise ValueError(f'Covariate {key} not found in aux_features.')
      if not np.issubdtype(aux_features[key].dtype, np.integer):
        if convert_float_to_int:
          aux_features[key] = np.round(aux_features[key]).astype(int)
        else:
          raise ValueError(
              f'Covariate {key} is expected to be integer dtype, '
              f'but is: {aux_features[key].dtype}'
          )
      covariates[key] = aux_features[key]
      num_classes[key] = np.unique(aux_features[key]).size
    self.covariates = covariates
    self.num_classes = num_classes

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> dict[str, jnp.ndarray]:
    del inputs, memory, diagnostics, randomness, forcing  # unused.
    features = {
        k: jax.nn.one_hot(v, self.num_classes[k], axis=0)
        for k, v in self.covariates.items()
    }
    return features


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class LearnedPositionalFeatures(hk.Module):
  """Feature module with learned params at surface nodal locations."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      latent_size: int,
      scale: float = 1.0,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.scale = scale
    self.padding = coords.horizontal.nodal_padding
    unpadded_nodal_shape = tuple(
        x - y for x, y in zip(coords.horizontal.nodal_shape, self.padding)
    )
    self.positional_features = hk.get_parameter(
        'learned_positional_features',
        (latent_size,) + unpadded_nodal_shape,
        jnp.float32,
        init=hk.initializers.Constant(0.0),
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> dict[str, jnp.ndarray]:
    """Returns scaled parameter values at surface nodal locations."""
    del inputs, memory, diagnostics, randomness, forcing  # unused.
    pad_x, pad_y = self.padding
    positional_features = self.scale * jnp.pad(
        self.positional_features, [(0, 0), (0, pad_x), (0, pad_y)]
    )
    return {'learned_positional_features': positional_features}


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class EmbeddingSurfaceFeatures(hk.Module):
  """Feature module that specifies embedding surface outputs as features.

  Returns {feature_name: nn_output}
    where nn_output.shape = (output_size, lon, lat).
  """

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      feature_name: str,
      output_size: int,
      embedding_module: typing.EmbeddingModule,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    # output shapes are arrays to be pytree leaves for tree_map
    output_shapes = {
        feature_name: np.asarray((output_size,) + coords.horizontal.nodal_shape)
    }
    self.embedding_fn = embedding_module(
        coords, dt, physics_specs, aux_features, output_shapes=output_shapes
    )
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    features = self.embedding_fn(
        inputs, memory, diagnostics, randomness, forcing  # pyrefly: ignore[bad-argument-type]
    )
    return self.features_transform_fn(features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class EmbeddingVolumeFeatures(hk.Module):
  """Feature module that specifies embedding volume outputs as features.

  Returns {feature_name_0: nn_output_0,
           feature_name_1: nn_output_1,
           ...
          }
    where the NN output array has shape (output_size, level, lon, lat), which is
    unpacked over output_size such that nn_output_{i}.shape = (level, lon, lat)
    for each i in range(output_size).
  """

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      feature_name: str,
      output_size: int,
      embedding_module: typing.EmbeddingModule,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    # output shapes are arrays to be pytree leaves for tree_map
    output_shapes = {
        f'{feature_name}_{i}': np.asarray(coords.nodal_shape)
        for i in range(output_size)
    }
    self.embedding_fn = embedding_module(
        coords, dt, physics_specs, aux_features, output_shapes=output_shapes
    )
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    features = self.embedding_fn(
        inputs, memory, diagnostics, randomness, forcing  # pyrefly: ignore[bad-argument-type]
    )
    return self.features_transform_fn(features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class FloatDataFeatures(hk.Module):
  """Feature module that supplies floating point covariates from data."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      covariate_data_path: str = gin.REQUIRED,  # pyrefly: ignore[bad-function-definition]
      covariate_keys: Sequence[str] = gin.REQUIRED,  # pyrefly: ignore[bad-function-definition]
      renaming_dict: Optional[Mapping[str, str]] = None,
      compute_gradients_module: TransformModule = transforms.EmptyTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.covariates = {}
    self.compute_gradients_fn = compute_gradients_module(
        coords, dt, physics_specs, aux_features
    )
    self.coords = coords
    ds = xarray_utils.ds_from_path_or_aux(covariate_data_path, aux_features)
    if renaming_dict is not None:
      ds = ds.rename(renaming_dict)
    lon, lat = (ds[xarray_utils.XR_LON_NAME], ds[xarray_utils.XR_LAT_NAME])
    xarray_utils.verify_grid_consistency(lon, lat, coords.horizontal)
    lon_lat_order = (xarray_utils.XR_LON_NAME, xarray_utils.XR_LAT_NAME)
    for key in covariate_keys:
      data = ds[key].transpose(*lon_lat_order)
      data_units = scales.parse_units(data.attrs['units'])
      data = physics_specs.nondimensionalize(data.values * data_units)
      if data.ndim != 3:
        data = data[np.newaxis, ...]
      self.covariates[key] = data

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> dict[str, jnp.ndarray]:
    del inputs, memory, diagnostics, forcing, randomness  # unused.
    features = {k: v for k, v in self.covariates.items()}
    modal_features = self.coords.horizontal.to_modal(features)
    modal_features = {  # jit should eliminate to_modal if it is not used.
        KeyWithCosLatFactor(k, 0): v for k, v in modal_features.items()
    }
    modal_gradient_features = self.compute_gradients_fn(modal_features)
    sec_lat = 1 / self.coords.horizontal.cos_lat
    sec2_lat = self.coords.horizontal.sec2_lat
    sec_lat_scales = {0: 1, 1: sec_lat, 2: sec2_lat}
    for k, v in modal_gradient_features.items():
      sec_lat_scale = sec_lat_scales[k.factor_order]
      features[k.name] = self.coords.horizontal.to_nodal(v) * sec_lat_scale
    return features


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class CombinedFeatures(hk.Module):
  """Feature module that combines multiple feature modules together."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      feature_modules: Sequence[FeaturesModule] = gin.REQUIRED,  # pyrefly: ignore[bad-function-definition]
      feature_module_names_to_exclude: Sequence[str] = tuple(),
      features_to_exclude: Sequence[str] = tuple(),
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.feature_fns = [
        module(coords, dt, physics_specs, aux_features)
        for module in feature_modules
    ]
    self.feature_module_names_to_exclude = feature_module_names_to_exclude
    self.features_to_exclude = features_to_exclude
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Forcing] = None,
  ) -> dict[str, jnp.ndarray]:
    all_features = {}
    for feature_fn in self.feature_fns:
      if type(feature_fn).__name__ not in self.feature_module_names_to_exclude:
        features = feature_fn(inputs, memory, diagnostics, randomness, forcing)
        for k, v in features.items():
          if k in all_features:
            raise ValueError(f'Encountered duplicate feature {k}')
          all_features[k] = v
    all_features = self.features_transform_fn(all_features)
    for k in self.features_to_exclude:
      all_features.pop(k, None)
    return all_features


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class NullFeatures(hk.Module):
  """Placeholder features module that returns an empty dict."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      name: Optional[str] = None,
  ):
    del coords, dt, physics_specs, aux_features  # unused
    super().__init__(name=name)

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> dict[str, jnp.ndarray]:
    del inputs, memory, diagnostics, randomness, forcing  # unused
    return {}


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class PressureFeatures(hk.Module):
  """Feature module that computes pressure."""

  def __init__(
      self,
      coords: coordinate_systems.CoordinateSystem,
      dt: float,
      physics_specs: Any,
      aux_features: typing.AuxFeatures,
      features_transform_module: TransformModule = transforms.IdentityTransform,
      name: Optional[str] = None,
  ):
    super().__init__(name=name)
    self.coords = coords
    self.features_transform_fn = features_transform_module(
        coords, dt, physics_specs, aux_features
    )

  def _nodal_pressure(
      self,
      inputs: typing.Pytree,
      prefix: str = '',
  ) -> Mapping[str, Array]:
    """Computes nodal pressure from model inputs."""
    # Compute nodal, dimensionalized quantities
    to_nodal_fn = self.coords.horizontal.to_nodal
    sigma = self.coords.vertical.centers
    surface_pressure = jnp.exp(to_nodal_fn(inputs['log_surface_pressure']))
    pressure = surface_pressure * sigma[:, jnp.newaxis, jnp.newaxis]
    nodal_features = {prefix + 'pressure': pressure}
    return nodal_features

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del memory, diagnostics, randomness, forcing  # unused.
    nodal_features = self._nodal_pressure(inputs)
    return self.features_transform_fn(nodal_features)


@gin.register(denylist=['coords', 'dt', 'physics_specs', 'aux_features'])
class MemoryPressureFeatures(PressureFeatures):
  """Feature module that computes pressure from memory values."""

  def __call__(
      self,
      inputs: typing.Pytree,
      memory: Optional[typing.PyTreeState] = None,
      diagnostics: Optional[typing.Pytree] = None,
      randomness: Optional[typing.PyTreeState] = None,
      forcing: Optional[typing.Pytree] = None,
  ) -> typing.Pytree:
    del inputs, diagnostics, randomness, forcing  # unused.
    nodal_features = self._nodal_pressure(memory, 'memory_')
    return self.features_transform_fn(nodal_features)