forked from Unidata/MetPy
/
kinematics.py
1046 lines (877 loc) · 41.3 KB
/
kinematics.py
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2009,2017,2018,2019 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Contains calculation of kinematic parameters (e.g. divergence or vorticity)."""
import numpy as np
from . import coriolis_parameter
from .tools import first_derivative, get_layer_heights, gradient
from .. import constants as mpconsts
from ..package_tools import Exporter
from ..units import check_units, units
from ..xarray import add_grid_arguments_from_xarray, preprocess_and_wrap
exporter = Exporter(globals())
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='u')
@check_units('[speed]', '[speed]', dx='[length]', dy='[length]')
def vorticity(u, v, *, dx=None, dy=None, x_dim=-1, y_dim=-2):
r"""Calculate the vertical vorticity of the horizontal wind.
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input. Keyword-only argument.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input. Keyword-only argument.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`. Keyword-only argument.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`. Keyword-only argument.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
vertical vorticity
.. versionchanged:: 1.0
Changed signature from ``(u, v, dx, dy)``
See Also
--------
divergence
"""
dudy = first_derivative(u, delta=dy, axis=y_dim)
dvdx = first_derivative(v, delta=dx, axis=x_dim)
return dvdx - dudy
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='u')
@check_units(dx='[length]', dy='[length]')
def divergence(u, v, *, dx=None, dy=None, x_dim=-1, y_dim=-2):
r"""Calculate the horizontal divergence of a vector.
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the vector
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the vector
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input. Keyword-only argument.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input. Keyword-only argument.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`. Keyword-only argument.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`. Keyword-only argument.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
The horizontal divergence
.. versionchanged:: 1.0
Changed signature from ``(u, v, dx, dy)``
See Also
--------
vorticity
"""
dudx = first_derivative(u, delta=dx, axis=x_dim)
dvdy = first_derivative(v, delta=dy, axis=y_dim)
return dudx + dvdy
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='u')
@check_units('[speed]', '[speed]', '[length]', '[length]')
def shearing_deformation(u, v, dx=None, dy=None, x_dim=-1, y_dim=-2):
r"""Calculate the shearing deformation of the horizontal wind.
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
Shearing Deformation
.. versionchanged:: 1.0
Changed signature from ``(u, v, dx, dy)``
See Also
--------
stretching_deformation, total_deformation
"""
dudy = first_derivative(u, delta=dy, axis=y_dim)
dvdx = first_derivative(v, delta=dx, axis=x_dim)
return dvdx + dudy
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='u')
@check_units('[speed]', '[speed]', '[length]', '[length]')
def stretching_deformation(u, v, dx=None, dy=None, x_dim=-1, y_dim=-2):
r"""Calculate the stretching deformation of the horizontal wind.
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
Stretching Deformation
.. versionchanged:: 1.0
Changed signature from ``(u, v, dx, dy)``
See Also
--------
shearing_deformation, total_deformation
"""
dudx = first_derivative(u, delta=dx, axis=x_dim)
dvdy = first_derivative(v, delta=dy, axis=y_dim)
return dudx - dvdy
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='u')
@check_units('[speed]', '[speed]', '[length]', '[length]')
def total_deformation(u, v, dx=None, dy=None, x_dim=-1, y_dim=-2):
r"""Calculate the horizontal total deformation of the horizontal wind.
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
Total Deformation
Notes
-----
If inputs have more than two dimensions, they are assumed to have either leading dimensions
of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.
.. versionchanged:: 1.0
Changed signature from ``(u, v, dx, dy)``
See Also
--------
shearing_deformation, stretching_deformation
"""
dudy, dudx = gradient(u, deltas=(dy, dx), axes=(y_dim, x_dim))
dvdy, dvdx = gradient(v, deltas=(dy, dx), axes=(y_dim, x_dim))
return np.sqrt((dvdx + dudy)**2 + (dudx - dvdy)**2)
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='scalar', broadcast=('scalar', 'u', 'v', 'w'))
def advection(
scalar,
u=None,
v=None,
w=None,
*,
dx=None,
dy=None,
dz=None,
x_dim=-1,
y_dim=-2,
vertical_dim=-3
):
r"""Calculate the advection of a scalar field by the wind.
Parameters
----------
scalar : `pint.Quantity` or `xarray.DataArray`
Array (with N-dimensions) with the quantity to be advected. Use `xarray.DataArray` to
have dimension ordering automatically determined, otherwise, use default
[..., Z, Y, X] ordering or specify \*_dim keyword arguments.
u, v, w : `pint.Quantity` or `xarray.DataArray` or None
N-dimensional arrays with units of velocity representing the flow, with a component of
the wind in each dimension. For 1D advection, use 1 positional argument (with `dx` for
grid spacing and `x_dim` to specify axis if not the default of -1) or use 1 applicable
keyword argument (u, v, or w) for proper physical dimension (with corresponding `d\*`
for grid spacing and `\*_dim` to specify axis). For 2D/horizontal advection, use 2
positional arguments in order for u and v winds respectively (with `dx` and `dy` for
grid spacings and `x_dim` and `y_dim` keyword arguments to specify axes), or specify u
and v as keyword arguments (grid spacings and axes likewise). For 3D advection,
likewise use 3 positional arguments in order for u, v, and w winds respectively or
specify u, v, and w as keyword arguments (either way, with `dx`, `dy`, `dz` for grid
spacings and `x_dim`, `y_dim`, and `vertical_dim` for axes).
dx, dy, dz: `pint.Quantity` or None, optional
Grid spacing in applicable dimension(s). If using arrays, each array should have one
item less than the size of `scalar` along the applicable axis. If `scalar` is an
`xarray.DataArray`, these are automatically determined from its coordinates, and are
therefore optional. Required if `scalar` is a `pint.Quantity`. These are keyword-only
arguments.
x_dim, y_dim, vertical_dim: int or None, optional
Axis number in applicable dimension(s). Defaults to -1, -2, and -3 respectively for
(..., Z, Y, X) dimension ordering. If `scalar` is an `xarray.DataArray`, these are
automatically determined from its coordinates. These are keyword-only arguments.
Returns
-------
`pint.Quantity` or `xarray.DataArray`
An N-dimensional array containing the advection at all grid points.
.. versionchanged:: 1.0
Changed signature from ``(scalar, wind, deltas)``
"""
return -sum(
wind * first_derivative(scalar, axis=axis, delta=delta)
for wind, delta, axis in (
(u, dx, x_dim),
(v, dy, y_dim),
(w, dz, vertical_dim)
)
if wind is not None
)
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(
wrap_like='potential_temperature',
broadcast=('potential_temperature', 'u', 'v')
)
@check_units('[temperature]', '[speed]', '[speed]', '[length]', '[length]')
def frontogenesis(potential_temperature, u, v, dx=None, dy=None, x_dim=-1, y_dim=-2):
r"""Calculate the 2D kinematic frontogenesis of a temperature field.
The implementation is a form of the Petterssen Frontogenesis and uses the formula
outlined in [Bluestein1993]_ pg.248-253
.. math:: F=\frac{1}{2}\left|\nabla \theta\right|[D cos(2\beta)-\delta]
* :math:`F` is 2D kinematic frontogenesis
* :math:`\theta` is potential temperature
* :math:`D` is the total deformation
* :math:`\beta` is the angle between the axis of dilitation and the isentropes
* :math:`\delta` is the divergence
Parameters
----------
potential_temperature : (..., M, N) `xarray.DataArray` or `pint.Quantity`
Potential temperature
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
2D Frontogenesis in [temperature units]/m/s
Notes
-----
Conversion factor to go from [temperature units]/m/s to [temperature units/100km/3h]
:math:`1.08e4*1.e5`
.. versionchanged:: 1.0
Changed signature from ``(thta, u, v, dx, dy, dim_order='yx')``
"""
# Get gradients of potential temperature in both x and y
ddy_thta = first_derivative(potential_temperature, delta=dy, axis=y_dim)
ddx_thta = first_derivative(potential_temperature, delta=dx, axis=x_dim)
# Compute the magnitude of the potential temperature gradient
mag_thta = np.sqrt(ddx_thta**2 + ddy_thta**2)
# Get the shearing, stretching, and total deformation of the wind field
shrd = shearing_deformation(u, v, dx, dy, x_dim=x_dim, y_dim=y_dim)
strd = stretching_deformation(u, v, dx, dy, x_dim=x_dim, y_dim=y_dim)
tdef = total_deformation(u, v, dx, dy, x_dim=x_dim, y_dim=y_dim)
# Get the divergence of the wind field
div = divergence(u, v, dx=dx, dy=dy, x_dim=x_dim, y_dim=y_dim)
# Compute the angle (beta) between the wind field and the gradient of potential temperature
psi = 0.5 * np.arctan2(shrd, strd)
beta = np.arcsin((-ddx_thta * np.cos(psi) - ddy_thta * np.sin(psi)) / mag_thta)
return 0.5 * mag_thta * (tdef * np.cos(2 * beta) - div)
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like=('height', 'height'), broadcast=('height', 'latitude'))
@check_units(dx='[length]', dy='[length]', latitude='[dimensionless]')
def geostrophic_wind(height, dx=None, dy=None, latitude=None, x_dim=-1, y_dim=-2):
r"""Calculate the geostrophic wind given from the height or geopotential.
Parameters
----------
height : (..., M, N) `xarray.DataArray` or `pint.Quantity`
The height or geopotential field.
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
latitude : `xarray.DataArray` or `pint.Quantity`
The latitude, which is used to calculate the Coriolis parameter. Its dimensions must
be broadcastable with those of height. Optional if `xarray.DataArray` with latitude
coordinate used as input. Note that an argument without units is treated as
dimensionless, which is equivalent to radians.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
A 2-item tuple of arrays
A tuple of the u-component and v-component of the geostrophic wind.
.. versionchanged:: 1.0
Changed signature from ``(heights, f, dx, dy)``
"""
f = coriolis_parameter(latitude)
if height.dimensionality['[length]'] == 2.0:
norm_factor = 1. / f
else:
norm_factor = mpconsts.g / f
dhdy = first_derivative(height, delta=dy, axis=y_dim)
dhdx = first_derivative(height, delta=dx, axis=x_dim)
return -norm_factor * dhdy, norm_factor * dhdx
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(
wrap_like=('height', 'height'),
broadcast=('height', 'u', 'v', 'latitude')
)
@check_units(
u='[speed]',
v='[speed]',
dx='[length]',
dy='[length]',
latitude='[dimensionless]'
)
def ageostrophic_wind(height, u, v, dx=None, dy=None, latitude=None, x_dim=-1, y_dim=-2):
r"""Calculate the ageostrophic wind given from the height or geopotential.
Parameters
----------
height : (M, N) ndarray
The height or geopotential field.
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
The u wind field.
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
The u wind field.
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
latitude : `xarray.DataArray` or `pint.Quantity`
The latitude, which is used to calculate the Coriolis parameter. Its dimensions must
be broadcastable with those of height. Optional if `xarray.DataArray` with latitude
coordinate used as input. Note that an argument without units is treated as
dimensionless, which is equivalent to radians.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
A 2-item tuple of arrays
A tuple of the u-component and v-component of the ageostrophic wind
.. versionchanged:: 1.0
Changed signature from ``(heights, f, dx, dy, u, v, dim_order='yx')``
"""
u_geostrophic, v_geostrophic = geostrophic_wind(
height,
dx,
dy,
latitude,
x_dim=x_dim,
y_dim=y_dim
)
return u - u_geostrophic, v - v_geostrophic
@exporter.export
def montgomery_streamfunction(height, temperature):
r"""Compute the Montgomery Streamfunction on isentropic surfaces.
The Montgomery Streamfunction is the streamfunction of the geostrophic wind on an
isentropic surface. This quantity is proportional to the geostrophic wind in isentropic
coordinates, and its gradient can be interpreted similarly to the pressure gradient in
isobaric coordinates.
Parameters
----------
height : `pint.Quantity` or `xarray.DataArray`
Array of geopotential height of isentropic surfaces
temperature : `pint.Quantity` or `xarray.DataArray`
Array of temperature on isentropic surfaces
Returns
-------
stream_func : `pint.Quantity` or `xarray.DataArray`
Notes
-----
The formula used is that from [Lackmann2011]_ p. 69.
.. math:: \Psi = gZ + C_pT
* :math:`\Psi` is Montgomery Streamfunction
* :math:`g` is avg. gravitational acceleration on Earth
* :math:`Z` is geopotential height of the isentropic surface
* :math:`C_p` is specific heat at constant pressure for dry air
* :math:`T` is temperature of the isentropic surface
See Also
--------
get_isentropic_pressure, dry_static_energy
"""
from . import dry_static_energy
return dry_static_energy(height, temperature)
@exporter.export
@preprocess_and_wrap()
@check_units('[length]', '[speed]', '[speed]', '[length]',
bottom='[length]', storm_u='[speed]', storm_v='[speed]')
def storm_relative_helicity(height, u, v, depth, *, bottom=None, storm_u=None, storm_v=None):
# Partially adapted from similar SharpPy code
r"""Calculate storm relative helicity.
Calculates storm relatively helicity following [Markowski2010]_ pg.230-231
.. math:: \int\limits_0^d (\bar v - c) \cdot \bar\omega_{h} \,dz
This is applied to the data from a hodograph with the following summation:
.. math:: \sum_{n = 1}^{N-1} [(u_{n+1} - c_{x})(v_{n} - c_{y}) -
(u_{n} - c_{x})(v_{n+1} - c_{y})]
Parameters
----------
u : array-like
U component winds
v : array-like
V component winds
height : array-like
Atmospheric height, will be converted to AGL
depth : number
Depth of the layer
bottom : number
Height of layer bottom AGL (default is surface)
storm_u : number
U component of storm motion (default is 0 m/s)
storm_v : number
V component of storm motion (default is 0 m/s)
Returns
-------
`pint.Quantity`
Positive storm-relative helicity
`pint.Quantity`
Negative storm-relative helicity
`pint.Quantity`
Total storm-relative helicity
Notes
-----
Only functions on 1D profiles (not higher-dimension vertical cross sections or grids).
Since this function returns scalar values when given a profile, this will return Pint
Quantities even when given xarray DataArray profiles.
.. versionchanged:: 1.0
Renamed ``heights`` parameter to ``height`` and converted ``bottom``, ``storm_u``, and
``storm_v`` parameters to keyword-only arguments
"""
if bottom is None:
bottom = units.Quantity(0, 'm')
if storm_u is None:
storm_u = units.Quantity(0, 'm/s')
if storm_v is None:
storm_v = units.Quantity(0, 'm/s')
_, u, v = get_layer_heights(height, depth, u, v, with_agl=True, bottom=bottom)
storm_relative_u = u - storm_u
storm_relative_v = v - storm_v
int_layers = (storm_relative_u[1:] * storm_relative_v[:-1]
- storm_relative_u[:-1] * storm_relative_v[1:])
# Need to manually check for masked value because sum() on masked array with non-default
# mask will return a masked value rather than 0. See numpy/numpy#11736
positive_srh = int_layers[int_layers.magnitude > 0.].sum()
if np.ma.is_masked(positive_srh):
positive_srh = units.Quantity(0.0, 'meter**2 / second**2')
negative_srh = int_layers[int_layers.magnitude < 0.].sum()
if np.ma.is_masked(negative_srh):
negative_srh = units.Quantity(0.0, 'meter**2 / second**2')
return (positive_srh.to('meter ** 2 / second ** 2'),
negative_srh.to('meter ** 2 / second ** 2'),
(positive_srh + negative_srh).to('meter ** 2 / second ** 2'))
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='u', broadcast=('u', 'v', 'latitude'))
@check_units('[speed]', '[speed]', '[length]', '[length]')
def absolute_vorticity(u, v, dx=None, dy=None, latitude=None, x_dim=-1, y_dim=-2):
"""Calculate the absolute vorticity of the horizontal wind.
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
latitude : `pint.Quantity`, optional
Latitude of the wind data. Optional if `xarray.DataArray` with latitude/longitude
coordinates used as input. Note that an argument without units is treated as
dimensionless, which translates to radians.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
absolute vorticity
.. versionchanged:: 1.0
Changed signature from ``(u, v, dx, dy, lats, dim_order='yx')``
"""
f = coriolis_parameter(latitude)
relative_vorticity = vorticity(u, v, dx=dx, dy=dy, x_dim=x_dim, y_dim=y_dim)
return relative_vorticity + f
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(
wrap_like='potential_temperature',
broadcast=('potential_temperature', 'pressure', 'u', 'v', 'latitude')
)
@check_units('[temperature]', '[pressure]', '[speed]', '[speed]',
'[length]', '[length]', '[dimensionless]')
def potential_vorticity_baroclinic(
potential_temperature,
pressure,
u,
v,
dx=None,
dy=None,
latitude=None,
x_dim=-1,
y_dim=-2,
vertical_dim=-3
):
r"""Calculate the baroclinic potential vorticity.
.. math:: PV = -g \left(\frac{\partial u}{\partial p}\frac{\partial \theta}{\partial y}
- \frac{\partial v}{\partial p}\frac{\partial \theta}{\partial x}
+ \frac{\partial \theta}{\partial p}(\zeta + f) \right)
This formula is based on equation 4.5.93 [Bluestein1993]_
Parameters
----------
potential_temperature : (..., P, M, N) `xarray.DataArray` or `pint.Quantity`
potential temperature
pressure : (..., P, M, N) `xarray.DataArray` or `pint.Quantity`
vertical pressures
u : (..., P, M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., P, M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
latitude : `pint.Quantity`, optional
Latitude of the wind data. Optional if `xarray.DataArray` with latitude/longitude
coordinates used as input. Note that an argument without units is treated as
dimensionless, which translates to radians.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Z, Y, X] order).
Automatically parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Z, Y, X] order).
Automatically parsed from input if using `xarray.DataArray`.
vertical_dim : int, optional
Axis number of vertical dimension. Defaults to -3 (implying [..., Z, Y, X] order).
Automatically parsed from input if using `xarray.DataArray`.
Returns
-------
(..., P, M, N) `xarray.DataArray` or `pint.Quantity`
baroclinic potential vorticity
Notes
-----
The same function can be used for isobaric and isentropic PV analysis. Provide winds
for vorticity calculations on the desired isobaric or isentropic surface. At least three
layers of pressure/potential temperature are required in order to calculate the vertical
derivative (one above and below the desired surface). The first two terms will be zero if
isentropic level data is used due to the gradient of theta in both the x and y-directions
will be zero since you are on an isentropic surface.
This function expects pressure/isentropic level to increase with increasing array element
(e.g., from higher in the atmosphere to closer to the surface. If the pressure array is
one-dimensional, and not given as `xarray.DataArray`, p[:, None, None] can be used to make
it appear multi-dimensional.)
.. versionchanged:: 1.0
Changed signature from ``(potential_temperature, pressure, u, v, dx, dy, lats)``
"""
if (
np.shape(potential_temperature)[vertical_dim] < 3
or np.shape(pressure)[vertical_dim] < 3
or np.shape(potential_temperature)[vertical_dim] != np.shape(pressure)[vertical_dim]
):
raise ValueError('Length of potential temperature along the vertical axis '
'{} must be at least 3.'.format(vertical_dim))
avor = absolute_vorticity(u, v, dx, dy, latitude, x_dim=x_dim, y_dim=y_dim)
dthtadp = first_derivative(potential_temperature, x=pressure, axis=vertical_dim)
if (
(np.shape(potential_temperature)[y_dim] == 1)
and (np.shape(potential_temperature)[x_dim] == 1)
):
dthtady = units.Quantity(0, 'K/m') # axis=y_dim only has one dimension
dthtadx = units.Quantity(0, 'K/m') # axis=x_dim only has one dimension
else:
dthtady = first_derivative(potential_temperature, delta=dy, axis=y_dim)
dthtadx = first_derivative(potential_temperature, delta=dx, axis=x_dim)
dudp = first_derivative(u, x=pressure, axis=vertical_dim)
dvdp = first_derivative(v, x=pressure, axis=vertical_dim)
return (-mpconsts.g * (dudp * dthtady - dvdp * dthtadx
+ avor * dthtadp)).to('K * m**2 / (s * kg)')
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(wrap_like='height', broadcast=('height', 'u', 'v', 'latitude'))
@check_units('[length]', '[speed]', '[speed]', '[length]', '[length]', '[dimensionless]')
def potential_vorticity_barotropic(
height,
u,
v,
dx=None,
dy=None,
latitude=None,
x_dim=-1,
y_dim=-2
):
r"""Calculate the barotropic (Rossby) potential vorticity.
.. math:: PV = \frac{f + \zeta}{H}
This formula is based on equation 7.27 [Hobbs2006]_.
Parameters
----------
height : (..., M, N) `xarray.DataArray` or `pint.Quantity`
atmospheric height
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
latitude : `pint.Quantity`, optional
Latitude of the wind data. Optional if `xarray.DataArray` with latitude/longitude
coordinates used as input. Note that an argument without units is treated as
dimensionless, which translates to radians.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
barotropic potential vorticity
.. versionchanged:: 1.0
Changed signature from ``(heights, u, v, dx, dy, lats, dim_order='yx')``
"""
avor = absolute_vorticity(u, v, dx, dy, latitude, x_dim=x_dim, y_dim=y_dim)
return (avor / height).to('meter**-1 * second**-1')
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(
wrap_like=('u', 'u'),
broadcast=('u', 'v', 'u_geostrophic', 'v_geostrophic', 'latitude')
)
@check_units('[speed]', '[speed]', '[speed]', '[speed]', '[length]', '[length]',
'[dimensionless]')
def inertial_advective_wind(
u,
v,
u_geostrophic,
v_geostrophic,
dx=None,
dy=None,
latitude=None,
x_dim=-1,
y_dim=-2
):
r"""Calculate the inertial advective wind.
.. math:: \frac{\hat k}{f} \times (\vec V \cdot \nabla)\hat V_g
.. math:: \frac{\hat k}{f} \times \left[ \left( u \frac{\partial u_g}{\partial x} + v
\frac{\partial u_g}{\partial y} \right) \hat i + \left( u \frac{\partial v_g}
{\partial x} + v \frac{\partial v_g}{\partial y} \right) \hat j \right]
.. math:: \left[ -\frac{1}{f}\left(u \frac{\partial v_g}{\partial x} + v
\frac{\partial v_g}{\partial y} \right) \right] \hat i + \left[ \frac{1}{f}
\left( u \frac{\partial u_g}{\partial x} + v \frac{\partial u_g}{\partial y}
\right) \right] \hat j
This formula is based on equation 27 of [Rochette2006]_.
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the advecting wind
v : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the advecting wind
u_geostrophic : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the geostrophic (advected) wind
v_geostrophic : (..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of the geostrophic (advected) wind
dx : `pint.Quantity`, optional
The grid spacing(s) in the x-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
dy : `pint.Quantity`, optional
The grid spacing(s) in the y-direction. If an array, there should be one item less than
the size of `u` along the applicable axis. Optional if `xarray.DataArray` with
latitude/longitude coordinates used as input.
latitude : `pint.Quantity`, optional
Latitude of the wind data. Optional if `xarray.DataArray` with latitude/longitude
coordinates used as input. Note that an argument without units is treated as
dimensionless, which translates to radians.
x_dim : int, optional
Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
y_dim : int, optional
Axis number of y dimension. Defaults to -2 (implying [..., Y, X] order). Automatically
parsed from input if using `xarray.DataArray`.
Returns
-------
(..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of inertial advective wind
(..., M, N) `xarray.DataArray` or `pint.Quantity`
y component of inertial advective wind
Notes
-----
Many forms of the inertial advective wind assume the advecting and advected
wind to both be the geostrophic wind. To do so, pass the x and y components
of the geostrophic wind for u and u_geostrophic/v and v_geostrophic.
.. versionchanged:: 1.0
Changed signature from ``(u, v, u_geostrophic, v_geostrophic, dx, dy, lats)``
"""
f = coriolis_parameter(latitude)
dugdy, dugdx = gradient(u_geostrophic, deltas=(dy, dx), axes=(y_dim, x_dim))
dvgdy, dvgdx = gradient(v_geostrophic, deltas=(dy, dx), axes=(y_dim, x_dim))
u_component = -(u * dvgdx + v * dvgdy) / f
v_component = (u * dugdx + v * dugdy) / f
return u_component, v_component
@exporter.export
@add_grid_arguments_from_xarray
@preprocess_and_wrap(
wrap_like=('u', 'u'),
broadcast=('u', 'v', 'temperature', 'pressure', 'static_stability')
)
@check_units('[speed]', '[speed]', '[temperature]', '[pressure]', '[length]', '[length]')
def q_vector(
u,
v,
temperature,
pressure,
dx=None,
dy=None,
static_stability=1,
x_dim=-1,
y_dim=-2
):
r"""Calculate Q-vector at a given pressure level using the u, v winds and temperature.
.. math:: \vec{Q} = (Q_1, Q_2)
= - \frac{R}{\sigma p}\left(
\frac{\partial \vec{v}_g}{\partial x} \cdot \nabla_p T,
\frac{\partial \vec{v}_g}{\partial y} \cdot \nabla_p T
\right)
This formula follows equation 5.7.55 from [Bluestein1992]_, and can be used with the
the below form of the quasigeostrophic omega equation to assess vertical motion
([Bluestein1992]_ equation 5.7.54):
.. math:: \left( \nabla_p^2 + \frac{f_0^2}{\sigma} \frac{\partial^2}{\partial p^2}
\right) \omega =
- 2 \nabla_p \cdot \vec{Q} -
\frac{R}{\sigma p} \beta \frac{\partial T}{\partial x}
Parameters
----------
u : (..., M, N) `xarray.DataArray` or `pint.Quantity`
x component of the wind (geostrophic in QG-theory)