Skip to content

Commit a1c67e6

Browse files
committed
Refactor pmin_loop into a separate function
This improves isolation for testing, and makes scoping explicit
1 parent 2cd5727 commit a1c67e6

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

src/tcpyPI/pi.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,44 @@ def pi(SSTC,MSL,P,TC,R,CKCD=0.9,ascent_flag=0,diss_flag=1,V_reduc=0.8,ptop=50,mi
585585
# if the CAPE function tripped a flag, set the output IFL to it
586586
if (IFLAG != 1):
587587
IFL=int(IFLAG)
588+
589+
CAPEM, CAPEMS, RAT, TVAV, IFL, TO, OTL = pmin_loop(SSTK,MSL,P,T,R,CAPEA,ES0,NK,CKCD,ascent_flag,diss_flag,ptop,miss_handle)
590+
#
591+
# *** If the routine does not converge, set IFL=0 and return missing PI ***
592+
#
593+
if IFL == 0:
594+
VMAX=np.nan
595+
PMIN=np.nan
596+
IFL=0
597+
TO=np.nan
598+
OTL=np.nan
599+
return(VMAX,PMIN,IFL,TO,OTL)
600+
601+
# Once converged, set potential intensity at the radius of maximum winds
602+
CATFAC=0.5*(1.+1/constants.b)
603+
CAT=(CAPEM-CAPEA)+CKCD*RAT*CATFAC*(CAPEMS-CAPEM)
604+
CAT=max([CAT,0.0])
588605

606+
# Calculate the minimum pressure at the eye of the storm
607+
# BE02 EQN. 4
608+
PMIN=MSL*np.exp(-CAT/(constants.RD*TVAV))
609+
610+
# Calculate the potential intensity at the radius of maximum winds
611+
# BE02 EQN. 3, reduced by some fraction (default 20%) to account for the reduction
612+
# of 10-m winds from gradient wind speeds (Emanuel 2000, Powell 1980)
613+
FAC=max([0.0,(CAPEMS-CAPEM)])
614+
VMAX=V_reduc*np.sqrt(CKCD*RAT*FAC)
615+
616+
# Return the calculated outputs to the above program level
617+
return(VMAX,PMIN,IFL,TO,OTL)
618+
619+
620+
@njit()
621+
def pmin_loop(SSTK,MSL,P,T,R,CAPEA,ES0,NK,CKCD=0.9,ascent_flag=0,diss_flag=1,ptop=50,miss_handle=1):
622+
"""
623+
Calculate quantities associated with the minimum pressure at the eye of the storm
624+
"""
625+
589626
#
590627
# *** Begin iteration to find mimimum pressure ***
591628
#
@@ -660,31 +697,9 @@ def pi(SSTC,MSL,P,TC,R,CKCD=0.9,ascent_flag=0,diss_flag=1,V_reduc=0.8,ptop=50,mi
660697
# increase iteration count in the loop
661698
NP += 1
662699

663-
#
664-
# *** If the routine does not converge, set IFL=0 and return missing PI ***
665-
#
700+
# if the routine does not converge, set IFL=0 and return nan
666701
if (NP > 200) or (PM < 400):
667-
VMAX=np.nan
668-
PMIN=np.nan
669702
IFL=0
670-
TO=np.nan
671-
OTL=np.nan
672-
return(VMAX,PMIN,IFL,TO,OTL)
673-
674-
# Once converged, set potential intensity at the radius of maximum winds
675-
CATFAC=0.5*(1.+1/constants.b)
676-
CAT=(CAPEM-CAPEA)+CKCD*RAT*CATFAC*(CAPEMS-CAPEM)
677-
CAT=max([CAT,0.0])
678-
679-
# Calculate the minimum pressure at the eye of the storm
680-
# BE02 EQN. 4
681-
PMIN=MSL*np.exp(-CAT/(constants.RD*TVAV))
682-
683-
# Calculate the potential intensity at the radius of maximum winds
684-
# BE02 EQN. 3, reduced by some fraction (default 20%) to account for the reduction
685-
# of 10-m winds from gradient wind speeds (Emanuel 2000, Powell 1980)
686-
FAC=max([0.0,(CAPEMS-CAPEM)])
687-
VMAX=V_reduc*np.sqrt(CKCD*RAT*FAC)
688-
689-
# Return the calculated outputs to the above program level
690-
return(VMAX,PMIN,IFL,TO,OTL)
703+
return(np.nan, np.nan, np.nan, np.nan, IFL, np.nan, np.nan)
704+
705+
return CAPEM, CAPEMS, RAT, TVAV, IFL, TO, OTL

0 commit comments

Comments
 (0)