爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 1234|回复: 2

[图形美化] 你能帮我解决一下这个问题吗?

[复制链接]

新浪微博达人勋

发表于 2016-6-23 21:13:23 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
这种图,用ncep再分析资料可以画出来吗?小白求大神解救
QQ截图20160623211344.jpg
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-6-24 08:30:42 | 显示全部楼层
可以啊
  1. 'reinit'
  2. 'open e:\nc\fnl_20160415_00_00.ctl'
  3. say 'enter location options:'
  4. say ' 1  default 121.15E 38.48N'
  5. say ' 2  special your own location'
  6. pull opt
  7. if(opt=1)
  8. 'set lon 121.5'
  9. 'set lat 38.48'
  10. else
  11. say 'input lon'
  12. pull clon
  13. say 'intput lat'
  14. pull clat
  15. 'set lon 'clon
  16. 'set lat 'clat
  17. endif
  18. say 'input time:  03Z10JUN2008 to 18Z10JUN2008 intervl 3hour'
  19. pull tm
  20. 'set time 00z09oct2015'
  21. 'set lev 1000 100'
  22. 'define td=tmpprs-273.15-35*log10(RHprs/100)'
  23. 'define tc=tmpprs-273.15'
  24. 'd tmpprs-273.15-td'
  25. 'define uspd=mag(ugrdprs,vgrdprs)*2'
  26. 'define udir=270-atan2(ugrdprs,vgrdprs)*180/3.1415926'
  27. rc=plotskew(tc,td,uspd,udir)


  28. *=======================================================================================
  29. *    below scripts from grads function
  30. *---------------------------------------------------------------------------------------
  31. function plotskew(sndtemp,snddewp,sndspd,snddir)
  32. *************************************************************************

  33. * added by lang fengwang
  34. 'set xlopts 1 4 0.2'
  35. 'set ylopts 1 4 0.2'
  36. *
  37. * GrADS Script to Plot a SkewT/LogP Diagram      
  38. *
  39. * Bob Hart  (rhart@fsu.edu)
  40. * Florida State University / Dept of Earth, Ocean, and Atmospheric Sciences
  41. * Last Minor Update:  March 27, 2011
  42. * Last Major Update:  August 29, 2009  ** IMPORTANT UPDATE **
  43. * http://moe.met.fsu.edu/~rhart/software.php
  44. *
  45. * Recent Changes:
  46. *
  47. * 03/27/11 - The storm motion vector was always plotted in red.  It is now the same color
  48. *            as the hodograph trace, given by the user through the variable SVectCol.  
  49. *            Thanks to H. Winterbottom for this.
  50. *
  51. * 08/29/09 - Fixed a rare bug related to plotting the wind profile
  52. *
  53. * 08/08/09 - Changed defines such that no adjustment is needed for projected data (e.g. lcc).
  54. *
  55. * 08/08/09 - Fixed typo in CAPE/CIN calculation.
  56. *
  57. * 08/07/09 - Changed math_int to math_nint in a few places to correct the
  58. *            rounding for isotherm drawing.   Also fixed DrawPrcl=0 with DrawPmax=1
  59. *            interaction problem.   Also fixed mb to Pa conversion for MAscentI.
  60. *
  61. * 08/07/09 - Considerably improved/enhanced CAPE/CIN calculation by converting
  62. *            G. Bryan's (NCAR) Fortran90 CAPE/CIN calculation to GrADS.
  63. *            Sincere gratitude to G. Bryan for permission.   ** Users
  64. *            are strongly encouraged to update to this version of plotskew
  65. *            based upon the improvement in this routine.   Note additional
  66. *            options for user regarding CAPE/CIN calculation:  MAscentT and MAscentI.
  67. *
  68. * 08/05/09 - Finally converted all hack-coded math functions (abs/int/pow)
  69. *            to the intrinsic math_* equivalents.  Results in improved speed, but
  70. *            now requires GrADS v1.8 or higher.  Users should not be using anything
  71. *            less than 1.9 anyway by now...      
  72. *
  73. * 08/28/02 - Small bug fixed that caused the SkewT frame to be missing
  74. *            on consecutive plots when ClrScrn=1
  75. *
  76. * 01/23/01 - Fixed a small bug in the theta-e calculation.  
  77. *            Errors averaged 0.5-3K.  Thank you George Bryan.
  78. *
  79. * 11/10/99 - Change in calculation method for CAPE/CIN.  Trapezoid
  80. *            integration method is now used.  Speeds up execution
  81. *            by 25%, and increases accuracy by 5-10%.
  82. *
  83. * 10/18/99 - Minor glitch fixed that occasionally caused crash.
  84. *
  85. *  8/26/99 - Datasets with missing data can now be used.
  86. *
  87. * Features:
  88. *   - All features of standard skewt/logp plot
  89. *   - RH sounding
  90. *   - LCL location
  91. *   - Parcel trajectory for both sfc based convection and elevated from
  92. *     most unstable level (highest theta-e level reported)
  93. *   - Stability indices and precipitable water calculations
  94. *   - CAPE & CIN Calculations
  95. *   - Wind Profile
  96. *   - Hodograph / Hodograph scaling
  97. *   - Helicity and SR Helicity Calculations and Display
  98. *   - Color aspects of output
  99. *   - Line Thickness, style aspects of output
  100. *   - Can be run in either PORTRAIT or LANDSCAPE mode.
  101. *
  102. * There are numerous tunable parameters below to change the structure
  103. * and output for the diagram.
  104. *
  105. * Function Arguments:
  106. *    sndtemp - temperature data (Celsius) as a function of pressure
  107. *    snddewp - dewpoint data (Celsius) as a function of pressure      
  108. *    sndspd  - wind speed data (knots) as a function of pressure
  109. *    snddir  - wind direction data as a function of pressure
  110. *
  111. * Use '-1' for any of the above 4 arguments to indicate that you
  112. * are not passing that variable.  The appropriate options will
  113. * be ignored based on your specifying '-1' for that variable.
  114. *
  115. * NOTE:  Make sure to set the vertical range of the plot before running.
  116. *        I.e., "SET LEV 1050 150", for example.   This does not have to
  117. *        be limited to the pressure range of your data.
  118. *
  119. * Labelling:  Pressure/Height is labelled along left side.  Temperature is
  120. *             labelled along bottom.  Mixing ratio is labelled along right
  121. *             side/top.
  122. *
  123. * PROBLEMS:  First check out the web page for the script (which also
  124. *            has a link to a FAQ with answers to many common questions
  125. *            about using the script):
  126. *            http://moe.met.fsu.edu/~rhart/software/skew.html
  127. *
  128. * Please send any further problems, comments, or suggestions to
  129. * <rhart@fsu.edu>
  130. *
  131. * ACKNOWLEDGMENTS:  Thanks go to the innumerable users who have helped
  132. * fine tune the script from the horrible mess from which it began back in 1996.
  133. * In particular, thanks go out to George Bryan (NCAR), Paul Markowski (PSU),
  134. * Steve Lord (NCEP), Mike Fiorino (NOAA/ESRL), Davide Sacchetti (CMIRL), and
  135. * Enrico Minguzzi (CMIRL).
  136. *
  137. * PLEASE NOTE THAT THIS CODE COMES WITH NO WARRANTY.
  138. *
  139. **************************************************************************
  140. *           !!!!!   BEGINNING OF USER-SPECIFIED OPTIONS  !!!!!!
  141. **************************************************************************
  142. *
  143. * --------------------- Initialization options  ----------------------
  144. *
  145. * ClrScrn = Whether to clear the screen before drawing diagram
  146. *           [1 = yes, 0 = no]

  147. ClrScrn = 1

  148. *
  149. * ------------------- Define Skew-T Diagram Shape/Slope-----------------
  150. *
  151. * (P1,T1) = Pres, Temp of some point on left-most side
  152. * (P2,T2) = Pres, Temp of some point on right-most side
  153. * (P3,T3) = Pres, Temp of some point in diagram which is mid-point
  154. *           in the horizontal between 1 and 2.
  155. *
  156. * P1, P2, P3 are in mb ; T1, T2, T3 are in Celsius
  157. *
  158. * These define the SLOPE and WIDTH of the diagram as you see it but DO NOT
  159. * DEFINE THE HEIGHT of the diagram as you see it.  In other words,
  160. * 1 and 2 do NOT necessarily need to be at the bottom of the diagram and
  161. * 3 does NOT necessarily need to be at the top.  THE VERTICAL PRESSURE
  162. * RANGE OF THE SKEWT AS YOU SEE IT IS DETERMINED BY YOUR 'SET Z ...'  
  163. * COMMAND OR THE 'SET LEV ...' COMMAND BEFORE RUNNING THIS SCRIPT.
  164. *
  165. *    _______________________
  166. *   |                       |
  167. *   |                       |
  168. *   |           3           |
  169. *   |                       |
  170. *   |                       |
  171. *   |                       |
  172. *   |                       |
  173. *   |                       |
  174. *   |                       |
  175. *   |                       |
  176. *   |                       |
  177. *   |1                     2|
  178. *   |                       |
  179. *   |_______________________|
  180. *   
  181. *
  182. * A good set of defining points are given below.   Feel free
  183. * to experiment with variations.


  184. P1 = 1000
  185. *T1 = -40
  186. T1=10

  187. P2 = 1000
  188. T2 = 40

  189. P3 = 200
  190. T3 = -50

  191. * Another good set of defining points suggested by George Bryan (NCAR)
  192. * are:
  193. *
  194. * P1 = 1000
  195. * T1 = -30
  196. *
  197. * P2 = 1000
  198. * T2 = 40
  199. *
  200. * P3 = 500
  201. * T3 = -18

  202. * ------------------- Contour Intervals / Levels --------------------------
  203. *
  204. * All variables below are contour intervals/levels for diagram
  205. *
  206. * Thetaint = interval for potential temperature lines
  207. * Thetwint = interval for moist pseudo adiabats
  208. * tempint  = interval for temperature lines
  209. * wsclevs  = contour LEVELS for mixing ratio lines
  210. *
  211. *
  212. thetaint= 5
  213. thetwint= 5
  214. tempint = 5
  215. wsclevs = "1 2 3 4 6 8 10 15 20 25 30 35 40"
  216. *
  217. *
  218. * ------------------------ Output Options --------------------------------
  219. *
  220. * All variables below are logical .. 1=yes, 0=no, unless otherwise
  221. * specified.
  222. *
  223. * DrawBarb = Draw wind barbs along right side of plot
  224. * DrawThet = Draw dry adiabats
  225. * DrawThtw = Draw moist pseudo-adiabats
  226. * DrawTemp = Draw temperature lines
  227. * DrawMix  = Draw mixing ratio lines
  228. * DrawTSnd = Draw temperature sounding
  229. * DrawDSnd = Draw dewpoint sounding
  230. * DrawRH   = Draw relative humidity sounding
  231. * DrawPrcl = Draw parcel path from surface upward
  232. * DrawPMax = Draw parcel path from most unstable level upward
  233. * DrawIndx = Display stability indices & CAPE
  234. * MAscentT = Type of moist adiabatic ascent for CAPE calculation
  235. *            1 = pseudoadiabatic, liquid only [default]
  236. *            2 = reversible, liquid only
  237. *            3 = pseudoadiabatic, with ice
  238. *            4 = reversible, with ice
  239. * MAscentI = Value of pressure increment (in mb) for CAPE calculation
  240. *            Smaller=more accurate, but slower.  Default=10 for speed.
  241. * DrawHeli = Calculate and display absolute and storm-relative helicity
  242. * DrawHodo = Draw hodograph
  243. * DrawPLev = Draw Pressure Levels
  244. * DrawZLev = Draw height levels and lines
  245. *            0 = no lines
  246. *            1 = above ground level (AGL)
  247. *            2 = above sea level (ASL)
  248. * DrawZSTD = Draw Height levels using standard atm lapse rate           
  249. * LblAxes  = Label the x,y axes (temperature, pressure,mixing ratio)
  250. *
  251. * ThtwStop = Pressure level at which to stop drawing Theta-w lines
  252. * MixStop  = Pressure level at which to stop drawing Mixratio lines

  253. DrawBarb= 1
  254. DrawThet= 1
  255. DrawThtw= 1
  256. DrawTemp= 1
  257. DrawMix = 1
  258. DrawTSnd= 1
  259. DrawDSnd= 1
  260. DrawRH  = 0
  261. DrawPrcl= 1
  262. DrawPMax= 1
  263. DrawIndx= 1
  264. MAscentT= 1
  265. MAscentI= 10
  266. DrawHeli= 1
  267. DrawHodo= 1
  268. DrawPLev= 1
  269. DrawZLev= 0
  270. DrawZSTD= 0
  271. LblAxes = 1

  272. ThtwStop = 400
  273. MixStop  = 600


  274. *
  275. * -----------------  Sounding Geography options ------------------------
  276. *
  277. * SfcElev = Elevation above sea-level (meters) of lowest level reported
  278. *           in sounding.  Used only if DrawZLev = 2

  279. SfcElev = 0


  280. *
  281. * ------------------ Thermodynamic Index Options --------------------
  282. *
  283. * All variables here are in inches.  Use -1 for the default values.
  284. *
  285. *  Text1XC = X-location of midpoint of K,TT,PW output box
  286. *  Text1YC = Y-location of midpoint of K,TT,PW output box
  287. *  Text2XC = X-Location of midpoint of surface indices output box
  288. *  Text2YC = Y-location of midpoint of surface indices output box
  289. *  Text3XC = X-Location of midpoint of most unstable level-based indices
  290. *            output box
  291. *  Text3YC = Y-location of midpoint of most unstable level-based indices
  292. *            output box

  293. Text1XC = -1
  294. Text1YC = -1
  295. Text2XC = -1
  296. Text2YC = -1
  297. Text3XC = -1
  298. Text3YC = -1

  299. *
  300. * ----------------- Wind Barb Profile Options ----------------------------
  301. *
  302. * All variables here are in units of inches, unless otherwise specified
  303. *
  304. *  barbint = Interval for plotting barbs (in units of levels)
  305. *  poleloc = X-Location of profile.  Choose -1 for the default.
  306. *  polelen = Length of wind-barb pole
  307. *  Len05   = Length of each 5-knot barb
  308. *  Len10   = Length of each 10-knot barb
  309. *  Len50   = Length of each 50-knot flag
  310. *  Wid50   = Width of base of 50-knot flag
  311. *  Spac50  = Spacing between 50-knot flag and next barb/flag
  312. *  Spac10  = Spacing between 10-knot flag and next flag
  313. *  Spac05  = Spacing between 5-knot flag and next flag
  314. *  Flagbase= Draw flagbase (filled circle) for each windbarb [1=yes, 0 =no]
  315. *  Fill50  = Solid-fill 50-knot flag [1=yes, 0=no]
  316. *  barbline= Draw a vertical line connecting all the wind barbs [1=yes, 0=no]
  317. *
  318. barbint = 1
  319. poleloc = -1
  320. polelen = 0.35
  321. len05   = 0.07
  322. len10   = 0.15
  323. len50   = 0.15
  324. wid50   = 0.06
  325. spac50  = 0.07
  326. spac10  = 0.05
  327. spac05  = 0.05
  328. Fill50  = 1
  329. flagbase= 1
  330. barbline= 1

  331. *
  332. *
  333. *---------------- Hodograph Options -------------------------------------
  334. *
  335. * All variables here are in units of inches, unless otherwise specified
  336. *
  337. * HodXcent= x-location of hodograph center.  Use -1 for default location.
  338. * HodYcent= y-location of hodograph center.  Use -1 for default location.
  339. * HodSize = Size of hodograph in inches
  340. * NumRing = Number of rings to place in hodograph (must be at least 1)
  341. * HodRing = Wind speed increment of each hodograph ring
  342. * HodoDep = Depth (above lowest level in mb) of end of hodograph trace
  343. * TickInt = Interval (in kts) at which tick marks are drawn along the axes
  344. *           Use 0 for no tick marks.
  345. * TickSize= Size of tick mark in inches
  346. * Text4XC = X-location of midpoint of hodograph text output. Use -1 for default.
  347. * Text4YC = Y-location of midpoint of hodograph text output. Use -1 for default.

  348. HodXcent= -1
  349. HodYcent= -1
  350. HodSize = 2
  351. NumRing = 3
  352. HodRing = 15
  353. HodoDep = 300
  354. TickInt = 5
  355. TickSize= 0.05
  356. Text4XC = -1
  357. Text4YC = -1

  358. *--------------- Helicity Options ---------------------------------------
  359. *
  360. * MeanVTop = Top pressure level (mb) of mean-wind calculation
  361. * MeanVBot = Bottom pressure level (mb) of mean-wind calculation
  362. * HelicDep = Depth in mb (above ground) of helicity integration
  363. * StormMot = Type of storm motion estimation scheme.  Use following:  
  364. *            0 = No departure from mean wind.
  365. *            1 = Davies-Jones (1990) approach
  366. * FillArrw = Whether to fill the arrowhead of the storm motion vector
  367. *            [1 = yes, 0 = no]

  368. MeanVTop= 300
  369. MeanVBot= 850
  370. HelicDep= 300
  371. StormMot= 1
  372. FillArrw= 1

  373. *
  374. *---------------- Color Options ------------------------------------------
  375. *
  376. * ThetCol = Color of dry adiabats
  377. * TempCol = Color of temperature lines
  378. * MixCol  = Color of mixing ratio lines
  379. * ThtwCol = Color of moist adiabats
  380. * TSndCol = Color of Temperature Sounding
  381. * DSndCol = Color of Dewpoint Sounding
  382. * RHCol   = Color of RH Sounding
  383. * PrclCol = Color of parcel trace
  384. * BarbCol = Color of wind barbs (choose -1 for color according to speed)
  385. * HodoCol = Color of hodograph trace
  386. * SVectCol= Color of storm motion vector arrow in hodograph

  387. ThetCol = 2
  388. TempCol = 4
  389. MixCol  = 7
  390. ThtwCol = 3
  391. TSndCol = 1
  392. DSndCol = 1
  393. RHCol   = 3
  394. PrclCol = 5
  395. BarbCol = -1
  396. HodoCol = 3
  397. SVectCol= 2

  398. *
  399. *-------------------- Line Style Options ------------------------------------
  400. *
  401. * GrADS Styles: 1=solid;2=long dash;3=short dash;4=long,short dashed;
  402. *               5=dotted;6=dot dash;7=dot dot dash
  403. *
  404. * ThetLine = Line Style of dry adiabats
  405. * TempLine = Line Style of temperature lines
  406. * MixLine  = Line Style of mixing ratio lines
  407. * ThtwLine = Line Style of moist adiabats
  408. * TSndLine = Line Style of Temperature Sounding
  409. * DSndLine = Line Style of Dewpoint Sounding
  410. * RHLine   = Line Style of RH sounding
  411. * PrclLine = Line Style of parcel trace
  412. * HodoLine = Line Style of hodograph trace
  413. *

  414. ThetLine = 1
  415. TempLine = 1
  416. MixLine  = 5
  417. ThtwLine = 3
  418. TSndLine = 1
  419. DSndLine = 2
  420. RHLine   = 1
  421. PrclLine = 5
  422. HodoLine = 1

  423. *
  424. *------------------- Line Thickness Options---------------------------------
  425. * GrADS Line Thickness: increases with increasing number. Influences
  426. *                       hardcopy output more strongly than screen output.
  427. *
  428. *
  429. * ThetThk = Line Thickness of dry adiabats
  430. * TempThk = Line Thickness of temperature lines
  431. * MixThk  = Line Thickness of mixing ratio lines
  432. * ThtwThk = Line Thickness of moist adiabats
  433. * TSndThk = Line Thickness of temperature sounding
  434. * DSndThk = Line thickness of dewpoint sounding
  435. * RHThk   = Line thickness of RH sounding
  436. * PrclThk = Line thickness of parcel trace
  437. * HodoThk = Line thickness of hodograph trace
  438. * BarbThk = Line thickness of wind barbs

  439. ThetThk = 3
  440. TempThk = 3
  441. MixThk  = 3
  442. ThtwThk = 3
  443. TSndThk = 8
  444. DSndThk = 8
  445. RHThk   = 8
  446. PrclThk = 6
  447. HodoThk = 3
  448. BarbThk = 2

  449. *
  450. *------------------- Data Point Marker Options -----------------------------
  451. * GrADS Marker Types: 0 = none ; 1 = cross ; 2 = open circle ;
  452. *                     3 = closed circle ; 4 = open square ; 5 = closed square
  453. *                     6 = X ; 7 = diamond ; 8 = triangle ; 9 = none
  454. *                    10 = open circle with vertical line ; 11 = open oval
  455. *
  456. * TSndMrk = Mark type of data point marker for temperature sounding
  457. * DSndMrk = Mark type of data point marker for dewpoint sounding
  458. * RHMrk   = Mark type of data point marker for relative humidity sounding
  459. * MrkSize = Mark size (inches) of each data marker

  460. TSndMrk = 0
  461. DSndMrk = 0
  462. RHMrk   = 0
  463. MrkSize = 0.1


  464. * !!!!! YOU SHOULD NOT NEED TO CHANGE ANYTHING BELOW HERE !!!!!
  465. ****************************************************************************

  466. *-------------------------------------------
  467. * grab user-specified environment dimensions
  468. *-------------------------------------------

  469. "q dims"
  470. rec=sublin(result,2)
  471. _xtype=subwrd(rec,3)
  472. _xval=subwrd(rec,9)
  473. rec=sublin(result,3)
  474. _yval=subwrd(rec,9)
  475. _ytype=subwrd(rec,3)
  476. rec=sublin(result,4)
  477. _ptype=subwrd(rec,3)
  478. _pmax=subwrd(rec,6)
  479. _pmin=subwrd(rec,8)
  480. _zmin=subwrd(rec,11)
  481. _zmax=subwrd(rec,13)
  482. rec=sublin(result,5)
  483. _ttype=subwrd(rec,3)
  484. _tval=subwrd(rec,9)

  485. "q file"
  486. rec=sublin(result,5)
  487. _zmaxfile=subwrd(rec,9)

  488. *-------------------------------------------------------------
  489. * Check to ensure that dimensions are valid.  Warn & exit if not.
  490. *--------------------------------------------------------------

  491. dimrc=0
  492. If (_xtype != "fixed")
  493.   say "X-Dims Error:  Not fixed.  Use 'set lon' or 'set x' to specify a value."
  494.   dimrc=-1
  495. Endif

  496. If (_ytype != "fixed")
  497.   say "Y-Dims Error:  Not fixed.  Use 'set lat' or 'set y' to specify a value"
  498.   dimrc=-1
  499. Endif

  500. If (_ptype != "varying")
  501.    say "Z-Dims Error:  Not varying.  Use 'set lev' or 'set z' to specify a range."
  502.    dimrc=-1
  503. Endif

  504. If (_ttype != "fixed")
  505.   say "Time Error:     Not fixed.  Use 'set time' or 'set t' to specify a value"
  506.   dimrc=-1
  507. Endif


  508. If (dimrc < 0)
  509.   Return(-1)
  510. Endif


  511. *
  512. * A few global variables used in units conversion
  513. *

  514. _pi=3.14159265
  515. _dtr=_pi/180
  516. _rtd=1/_dtr
  517. _ktm=0.514444
  518. _mtk=1/_ktm

  519. * A few global constants used in thermo calcs

  520. _C0=0.99999683
  521. _C1=-0.90826951/100
  522. _C2= 0.78736169/10000
  523. _C3=-0.61117958/1000000
  524. _C4= 0.43884187/math_pow(10,8)
  525. _C5=-0.29883885/math_pow(10,10)
  526. _C6= 0.21874425/math_pow(10,12)
  527. _C7=-0.17892321/math_pow(10,14)
  528. _C8= 0.11112018/math_pow(10,16)         
  529. _C9=-0.30994571/math_pow(10,19)

  530. * A pressure array of power calculations which should be performed
  531. * only once to reduce execution time.

  532. zz=1100
  533. while (zz > 10)
  534.     subscr=zz/10
  535.     _powpres.subscr=math_pow(zz,0.286)
  536.     zz=zz-10
  537. endwhile

  538. *
  539. * Turn off options not available due to user data limitations
  540. *

  541. If (ClrScrn = 1)
  542.   "clear"
  543. Endif

  544. If (sndspd = -1 | snddir = -1)
  545.   DrawBarb = 0
  546.   DrawHodo = 0
  547.   DrawHeli = 0
  548. Endif

  549. If (snddewp = -1)
  550.   DrawDSnd = 0
  551.   DrawRH   = 0
  552.   DrawPrcl = 0
  553.   DrawPMax = 0
  554.   DrawIndx = 0
  555. Endif

  556. If (sndtemp = -1)
  557.   DrawTSnd = 0
  558.   DrawRH   = 0
  559.   DrawPrcl = 0
  560.   DrawPMax = 0
  561.   DrawIndx = 0
  562.   DrawZLev = 0
  563. Endif

  564. If (NumRing < 1)
  565.   DrawHodo = 0
  566. Endif
  567.   
  568. "q gxinfo"
  569. rec=sublin(result,2)
  570. xsize=subwrd(rec,4)

  571. If (xsize = 11)
  572.    PageType = "Landscape"
  573. Else
  574.    PageType = "Portrait"
  575. Endif
  576.   
  577. *------------------------------------------------------
  578. * calculate constants determining slope/shape of diagram
  579. * based on temp/pressure values given by user
  580. *-------------------------------------------------------

  581. "set x 1"
  582. "set y 1"
  583. "set z 1"
  584. "set t 1"
  585. _m1=(T1+T2-2*T3)/(2*math_log10(P2/P3))
  586. _m2=(T2-T3-_m1*math_log10(P2/P3))/50
  587. _m3=(T1-_m1*math_log10(P1))

  588. "set z "_zmin" "_zmax           
  589. "set zlog on"
  590. "set xlab off"

  591. *-------------------------------------------------
  592. * perform coordinate transformation to Skew-T/LogP
  593. *-------------------------------------------------

  594. "set gxout stat"
  595. "set x "_xval
  596. "set y "_yval
  597. "set t "_tval
  598. "define tempx=("sndtemp"-"_m1"*log10(lev)-"_m3")/"_m2
  599. "define dewpx=("snddewp"-"_m1"*log10(lev)-"_m3")/"_m2

  600. If (PageType = "Portrait")
  601.    "set parea 0.8 7 0.75 10.5"
  602. Else
  603.    "set parea 0.8 6.5 0.5 8"
  604. Endif
  605.   
  606. "set axlim 0 100"
  607. "set lon 0 100"
  608. "set grid on 1 1"

  609. "set z "_zmin " " _zmax
  610. "set lon 0 100"
  611. "set clevs -900"
  612. "set gxout contour"

  613. *-------------------------------------
  614. * Draw pressure lines
  615. *-------------------------------------

  616. If (DrawPLev = 0)
  617.    "set ylab off"
  618. Else
  619.    "set ylab on"
  620.    "set ylopts 1 2 0.2"
  621.    "set xlopts 1 2 0.2"
  622. Endif

  623. "set frame on"
  624. "set grads off"
  625. "d lon"

  626. *--------------------------------------
  627. * Determine corners of skewt/logp frame
  628. *--------------------------------------

  629. "q w2xy 100 "_pmin
  630. rxloc=subwrd(result,3)
  631. tyloc=subwrd(result,6)
  632. "q w2xy 0 "_pmax
  633. lxloc=subwrd(result,3)
  634. byloc=subwrd(result,6)

  635. If (DrawPLev = 1 & LblAxes = 1)
  636.    "set strsiz 0.20"
  637.    "set string 1 c 3 0"
  638.    If (PageType = "Portrait")
  639.       "draw string 0.5 10.85 mb"
  640.    Else
  641.       "draw string 0.5 8.35 mb"
  642.    Endif
  643. Endif

  644. *---------------------------------------------------
  645. * Calculate & draw actual height lines using temp data
  646. *---------------------------------------------------

  647. If (DrawZLev > 0)
  648.    say "Calculating observed height levels from temp/pressure data."
  649.    zz=1
  650.    "set gxout stat"
  651.    "set x "_xval
  652.    "set y "_yval
  653.    "set t "_tval
  654.    count=0
  655.    while (zz < _zmax)
  656.       "set z "zz
  657.       pp.zz=subwrd(result,4)
  658.       lpp.zz=math_log(pp.zz)
  659.       "define dd="sndtemp
  660.       "d dd"
  661.       rec=sublin(result,8)
  662.       tt=subwrd(rec,4)
  663.       if (tt > -900)
  664.          tk=tt+273.15
  665.          count=count+1
  666.          zzm=zz-1
  667.          If (count = 1)
  668.             If (DrawZLev = 2)
  669.                htlb="ASL"
  670.                height.zz=SfcElev
  671.             Else
  672.                htlb="AGL"
  673.                height.zz=0
  674.             Endif
  675.             sfcz=height.zz
  676.          Else
  677.             DZ=29.2857*(lpp.zzm-lpp.zz)*(lpp.zz*tk+lpp.zzm*tkold)/(lpp.zz+lpp.zzm)
  678.             height.zz=height.zzm+DZ
  679.             highz=height.zz
  680.          Endif
  681.       else
  682.          height.zz = -9999
  683.       endif
  684.       tkold=tk
  685.       zz=zz+1
  686.    endwhile

  687.    maxht=math_int(highz/1000)
  688.    if (math_int(sfcz/1000) = sfcz/1000)
  689.       minht=math_int(sfcz/1000)
  690.    else
  691.       minht=1+math_int(sfcz/1000)
  692.    endif

  693.    ht=minht
  694.    "set line 1 3 1"
  695.    "set strsiz 0.15"
  696.    "set string 1 l 3 0"
  697.    while (ht <= maxht)
  698.        zz=1
  699.        while (height.zz/1000 <= ht)
  700.           zz=zz+1
  701.        endwhile
  702.        zzm=zz-1
  703.        PBelow=pp.zzm
  704.        PAbove=pp.zz
  705.        HBelow=height.zzm
  706.        HAbove=height.zz
  707.        DZ=HAbove-HBelow
  708.        DP=PAbove-PBelow
  709.        Del=ht*1000-HBelow
  710.        Est=PBelow+Del*DP/DZ
  711.        If (Est >= _pmin & Est <= _pmax)
  712.           "q w2xy 1 " Est
  713.           yloc=subwrd(result,6)
  714.           "draw line " lxloc " " yloc " " rxloc " " yloc
  715.           "draw string 0.22 "yloc-0.05" "ht
  716.        Endif
  717.        ht=ht+1
  718.    endwhile
  719.    "set strsiz 0.10"
  720.    "set string 1"
  721.    If (LblAxes = 1)
  722.       If (PageType = "Portrait")
  723.          "draw string 0.25 10.85 km"
  724.          "draw string 0.25 10.75 "htlb
  725.          "draw string 0.25 10.65 OBS"
  726.       Else
  727.          "draw string 0.25 8.35 km"
  728.          "draw string 0.25 8.25 "htlb
  729.          "draw string 0.25 8.15 OBS"
  730.       Endif
  731.    Endif
  732. Endif


  733. *---------------------------------------------------
  734. * Draw height levels (height above MSL using Std Atm)
  735. *---------------------------------------------------

  736. If (DrawZSTD = 1)
  737.    "set strsiz 0.10"
  738.    minht=30.735*(1-math_pow(_pmax/1013.26,0.287))
  739.    minht=math_int(minht+0.5)
  740.    maxht=30.735*(1-math_pow(_pmin/1013.26,0.287))
  741.    maxht=math_int(maxht)
  742.    "set gxout stat"
  743.    zcount=minht        
  744.    while (zcount <= maxht)
  745.       plev=1013.26*math_pow((1-zcount/30.735),3.4843)
  746.       "q w2xy 0 "plev
  747.       yloc=subwrd(result,6)
  748.       "draw string 0 "yloc-0.05" "zcount
  749.       zcount=zcount+1
  750.    endwhile
  751.    "set strsiz 0.10"
  752.    If (LblAxes = 1)
  753.       If (PageType = "Portrait")
  754.          "draw string 0 10.85 km"
  755.          "draw string 0 10.75 ASL"
  756.          "draw string 0 10.65 STD"
  757.       Else
  758.          "draw string 0 8.35 km"
  759.          "draw string 0 8.25 ASL"
  760.          "draw string 0 8.15 STD"
  761.       Endif
  762.   Endif
  763. Endif


  764. *-----------------------
  765. * Plot temperature lines
  766. *-----------------------

  767. If (DrawTemp = 1)
  768.    "set strsiz 0.15"
  769.    "set z "_zmin " " _zmax
  770.    "set line "TempCol " " TempLine " "TempThk
  771.    "set string 1 c 3 0"
  772.    "set gxout stat"
  773.    maxtline=GetTemp(100,_pmax)
  774.    mintline=GetTemp(0,_pmin)

  775.    maxtline=tempint*math_nint(maxtline/tempint)
  776.    mintline=tempint*math_nint(mintline/tempint)

  777.    tloop=mintline
  778.    While (tloop <= maxtline)
  779.        Botxtemp=GetXLoc(tloop,_pmax)
  780.        "q w2xy "Botxtemp " " _pmax
  781.        Botxloc=subwrd(result,3)
  782.        Botyloc=byloc           
  783.        Topxtemp=GetXLoc(tloop,_pmin)
  784.         "q w2xy "Topxtemp " " _pmin
  785.        Topxloc=subwrd(result,3)
  786.        Topyloc=tyloc            
  787.        If (Botxtemp <= 100 | Topxtemp <= 100)
  788.           If (Topxtemp > 100)
  789.              Slope=(Topyloc-Botyloc)/(Topxtemp-Botxtemp)
  790.              b=Topyloc-Slope*Topxtemp
  791.              Topyloc=Slope*100+b
  792.              Topxloc=rxloc         
  793.           Endif
  794.           If (Botxtemp < 0)
  795.              Slope=(Topyloc-Botyloc)/(Topxtemp-Botxtemp)
  796.              b=Botyloc-Slope*Botxtemp
  797.              Botyloc=b
  798.              Botxloc=lxloc
  799.           Else
  800.              "draw string " Botxloc-0.05 " " Botyloc-0.15 " " tloop
  801.           Endif
  802.           "draw line "Botxloc " " Botyloc " " Topxloc " " Topyloc
  803.        Endif
  804.        tloop=tloop+tempint
  805.    EndWhile
  806.    If (LblAxes = 1)
  807.       "set strsiz 0.15"
  808.       "set string 1 c"
  809.       If (PageType = "Portrait")
  810.          "draw string 4.0 0.30 Temperature (`3.`0C)"
  811.       Else
  812.          "draw string 3.5 0.15 Temperature (`3.`0C)"
  813.       Endif
  814.    Endif
  815. Endif


  816. *------------------
  817. * Plot dry adiabats
  818. *------------------

  819. If (DrawThet = 1)
  820.    temp=GetTemp(100,_pmin)
  821.    maxtheta=GetThet2(temp,-100,_pmin)
  822.    maxtheta=thetaint*math_int(maxtheta/thetaint)
  823.    temp=GetTemp(0,_pmax)
  824.    mintheta=GetThet2(temp,-100,_pmax)
  825.    mintheta=thetaint*math_int(mintheta/thetaint)
  826.    
  827.    "set lon 0 100"
  828.    "set y 1"
  829.    "set z 1"
  830.    tloop=mintheta
  831.    "set line "ThetCol" "ThetLine " "ThetThk
  832.    While (tloop <= maxtheta)
  833.      PTemp=LiftDry(tloop,1000,_pmin,1,_pmin,_pmax)     
  834.      tloop=tloop+thetaint
  835.    Endwhile
  836. Endif

  837. *------------------------
  838. * Plot mixing ratio lines
  839. *------------------------

  840. If (DrawMix = 1)
  841.    If (MixStop < _pmin)
  842.       MixStop = _pmin
  843.    Endif
  844.    "set string 1 l"
  845.    "set z "_zmin " " _zmax
  846.    "set cint 1"
  847.    "set line "MixCol" " MixLine " "MixThk
  848.    cont = 1
  849.    mloop=subwrd(wsclevs,1)
  850.    count = 1
  851.    While (cont = 1)
  852.        BotCoef=math_log(mloop*_pmax/3801.66)
  853.        BotTval=-245.5*BotCoef/(BotCoef-17.67)
  854.        Botxtemp=GetXLoc(BotTval,_pmax)
  855.        "q w2xy "Botxtemp " " _pmax
  856.        Botxloc=subwrd(result,3)
  857.        Botyloc=byloc            
  858.        TopCoef=math_log(mloop*MixStop/3801.66)
  859.        TopTval=-245.5*TopCoef/(TopCoef-17.67)
  860.        Topxtemp=GetXLoc(TopTval,MixStop)
  861.        "q w2xy "Topxtemp " " MixStop
  862.        Topxloc=subwrd(result,3)
  863.        Topyloc=subwrd(result,6)
  864.        "set string "MixCol" l 3"
  865.        "set strsiz 0.09"
  866.        If (Botxtemp <= 100 | Topxtemp <= 100)
  867.           If (Topxtemp > 100)
  868.              Slope=(Topyloc-Botyloc)/(Topxtemp-Botxtemp)
  869.              b=Topyloc-Slope*Topxtemp
  870.              Topyloc=Slope*100+b
  871.              Topxloc=rxloc
  872.              "draw string " Topxloc+0.05 " " Topyloc  " " mloop
  873.           Else
  874.              "draw string " Topxloc " " Topyloc+0.1 " " mloop
  875.           Endif
  876.           If (Botxtemp < 0)
  877.              Slope=(Topyloc-Botyloc)/(Topxtemp-Botxtemp)
  878.              b=Botyloc-Slope*Botxtemp
  879.              Botyloc=b
  880.              Botxloc=lxloc        
  881.           Endif
  882.           "draw line "Botxloc " " Botyloc " " Topxloc " " Topyloc
  883.        Endif
  884.        count=count+1
  885.        mloop=subwrd(wsclevs,count)
  886.        If (mloop = "" | count > 50)
  887.           cont = 0
  888.        Endif
  889.    EndWhile
  890.    If (LblAxes = 1)
  891.       "set strsiz 0.15"
  892.       "set string 1 c 3 90"
  893.       If (PageType = "Portrait")
  894.          "draw string 7.40 4.75 Mixing Ratio (g/kg)"
  895.       Else
  896.          "draw string 6.90 4.25 Mixing Ratio (g/kg)"
  897.       Endif
  898.       "set string 1 c 3 0"
  899.    Endif
  900. Endif

  901. *-----------------------------
  902. * Plot moist (pseudo) adiabats
  903. *-----------------------------

  904. If (DrawThtw = 1)
  905.    "set lon 0 100"
  906.    "set y 1"
  907.    "set z 1"
  908.    "set gxout stat"
  909.    tloop=80
  910.    "set line "ThtwCol" "ThtwLine " "ThtwThk
  911.    While (tloop > -80)
  912.      PTemp=LiftWet(tloop,1000,ThtwStop,1,_pmin,_pmax)     
  913.      tloop=tloop-thetwint
  914.    Endwhile
  915. Endif


  916. *-----------------------------------------------------
  917. * Plot transformed user-specified temperature sounding
  918. *-----------------------------------------------------

  919. If (DrawTSnd = 1)
  920.    say "Drawing temperature sounding."
  921.    "set gxout line"
  922.    "set x "_xval
  923.    "set y "_yval
  924.    "set z "_zmin" "_zmax     
  925.    "set ccolor "TSndCol
  926.    "set cstyle "TSndLine
  927.    "set cmark "TSndMrk
  928.    "set digsiz "MrkSize
  929.    "set cthick "TSndThk
  930.    "set missconn on"
  931.    "d tempx"
  932. Endif

  933. *---------------------------------------------------
  934. * Plot transformed user-specified dewpoint sounding
  935. *---------------------------------------------------

  936. If (DrawDSnd = 1)
  937.    say "Drawing dewpoint sounding."
  938.    "set gxout line"
  939.    "set x "_xval
  940.    "set y "_yval
  941.    "set z "_zmin" "_zmax
  942.    "set cmark "DSndMrk
  943.    "set digsiz "MrkSize
  944.    "set ccolor "DSndCol
  945.    "set cstyle "DSndLine
  946.    "set cthick "DSndThk
  947.    "set missconn on"
  948.    "d dewpx"
  949. Endif

  950. *----------------------------------------
  951. * Determine lowest level of reported  data
  952. *----------------------------------------

  953. If (DrawTSnd = 1)
  954.    field=sndtemp
  955. Else
  956.    field=sndspd
  957. Endif

  958. "set gxout stat"
  959. "set x "_xval
  960. "set y "_yval
  961. "set t "_tval
  962. "set lev " _pmax " " _pmin
  963. "d maskout(lev,"field"+300)"
  964. rec=sublin(result,1)
  965. check=substr(rec,1,6)
  966. If (check = "Notice")
  967.     rec=sublin(result,9)
  968. Else
  969.     rec=sublin(result,8)
  970. Endif
  971. SfcPlev=subwrd(rec,5)

  972. If (DrawTSnd = 1 & DrawDSnd = 1)
  973.    "set lev "SfcPlev
  974.    "define dd="sndtemp
  975.    "d dd"
  976.    rec=sublin(result,8)
  977.    Sfctemp=subwrd(rec,4)
  978.    "define dd="snddewp
  979.    "d dd"
  980.    rec=sublin(result,8)
  981.    Sfcdewp=subwrd(rec,4)
  982.    SfcThee=Thetae(Sfctemp,Sfcdewp,SfcPlev)

  983. *------------------------------------------
  984. * Calculate temperature and pressure of LCL
  985. *------------------------------------------

  986.    TLcl=Templcl(Sfctemp,Sfcdewp)
  987.    PLcl=Preslcl(Sfctemp,Sfcdewp,SfcPlev)
  988. Endif

  989. *----------------------------------------------------------
  990. * Plot parcel path from surface to LCL and up moist adiabat
  991. *----------------------------------------------------------

  992. If (DrawPrcl = 1)
  993.    say "Drawing parcel path from surface upward."
  994.    If (PageType = "Portrait")
  995.       xloc=7.15
  996.    Else
  997.       xloc=6.65
  998.    Endif
  999.    "q w2xy 1 "PLcl
  1000.    rec=sublin(result,1)
  1001.    yloc=subwrd(rec,6)
  1002.    "set strsiz 0.1"
  1003.    If (PLcl < _pmax)
  1004.       "set string 1 l"
  1005.       "draw string "xloc" "yloc" LCL"
  1006.       "set line 1 1 1"
  1007.       "draw line "xloc-0.15" "yloc" "xloc-0.05" "yloc
  1008.    Endif
  1009.    "set lon 0 100"
  1010.    "set gxout stat"
  1011.    "set line "PrclCol" "PrclLine " " PrclThk
  1012.    PTemp=LiftDry(Sfctemp,SfcPlev,PLcl,1,_pmin,_pmax)
  1013.    Ptemp=LiftWet(TLcl,PLcl,_pmin,1,_pmin,_pmax)
  1014. Endif

  1015. *-------------------------------------------------------
  1016. * Determine level within lowest 250mb having highest
  1017. * theta-e value
  1018. *-------------------------------------------------------

  1019. If (DrawTSnd = 1 & DrawDSnd = 1)
  1020.   "set x "_xval
  1021.   "set y "_yval
  1022.   "set t "_tval
  1023.    zz=1
  1024.    MaxThee=-999
  1025.    "set gxout stat"
  1026.    while (zz <= _zmax & pp > _pmax-250)
  1027.        "set z "zz
  1028.        pp=subwrd(result,4)
  1029.        "define dd="sndtemp
  1030.        "d dd"
  1031.        rec=sublin(result,8)
  1032.        tt=subwrd(rec,4)
  1033.        "define dd="snddewp
  1034.        "d dd"
  1035.        rec=sublin(result,8)
  1036.        dd=subwrd(rec,4)
  1037.        If (math_abs(tt) < 130 & math_abs(dd) < 130)
  1038.           Thee=Thetae(tt,dd,pp)
  1039.           If (Thee > MaxThee)
  1040.              MaxThee=Thee
  1041.              TMaxThee=tt
  1042.              DMaxThee=dd
  1043.              PMaxThee=pp
  1044.           Endif
  1045.        endif
  1046.        zz=zz+1
  1047.    Endwhile
  1048.    If (PMaxThee = SfcPlev-250)
  1049.       PMaxThee = SfcPlev
  1050.    Endif
  1051. *------------------------------------------------------
  1052. * Calculate temperature and pressure of LCL from highest   
  1053. * theta-e level
  1054. *------------------------------------------------------
  1055.    TLclMax=Templcl(TMaxThee,DMaxThee)
  1056.    PLclMax=Preslcl(TMaxThee,DMaxThee,PMaxThee)
  1057. Endif


  1058. *----------------------------------------------------------
  1059. * Plot parcel path from highest theta-e level to LCL and up
  1060. * moist adiabat
  1061. *----------------------------------------------------------

  1062. If (DrawPMax = 1)
  1063.    say "Drawing parcel path from most unstable level upward."
  1064.    If (PageType = "Portrait")
  1065.       xloc=7.15
  1066.    Else
  1067.       xloc=6.65
  1068.    Endif
  1069.    "q w2xy 1 "PLclMax
  1070.    rec=sublin(result,1)
  1071.    yloc=subwrd(rec,6)
  1072.    "set strsiz 0.1"
  1073.    If (PLclMax < _pmax)
  1074.       "set string 1 l"
  1075.       "draw string "xloc" "yloc" LCL"
  1076.       "set line 1 1 1"
  1077.       "draw line "xloc-0.15" "yloc" "xloc-0.05" "yloc
  1078.    Endif
  1079.    "set lon 0 100"
  1080.    "set gxout stat"
  1081.    "set line "PrclCol" "PrclLine " " PrclThk
  1082.    PTemp=LiftDry(TMaxThee,PMaxThee,PLclMax,1,_pmin,_pmax)
  1083.    Ptemp=LiftWet(TLclMax,PLclMax,_pmin,1,_pmin,_pmax)
  1084. Endif

  1085. *--------------------------------
  1086. * Draw thermodynamic indices
  1087. *--------------------------------

  1088. If (DrawIndx = 1)
  1089.    "set string 1 l"
  1090.    "set strsiz 0.10"
  1091.    "set x "_xval
  1092.    "set y "_yval
  1093.    "set t "_tval
  1094.    say "Calculating precipitable water."
  1095.    pw=precipw(sndtemp,snddewp,_pmax,_pmin)
  1096.    say "Calculating thermodynamic indices."
  1097.    Temp850=interp(sndtemp,850)
  1098.    Temp700=interp(sndtemp,700)
  1099.    Temp500=interp(sndtemp,500)
  1100.    Dewp850=interp(snddewp,850)
  1101.    Dewp700=interp(snddewp,700)
  1102.    Dewp500=interp(snddewp,500)
  1103.    If (Temp850>-900 & Dewp850>-900 & Dewp700>-900 & Temp700>-900 & Temp500>-900)
  1104.       K=Temp850+Dewp850+Dewp700-Temp700-Temp500
  1105.    Else
  1106.       K=-999
  1107.    Endif
  1108.    If (Temp850 > -900 & Dewp850 > -900 & Temp500 > -900)
  1109.       tt=Temp850+Dewp850-2*Temp500
  1110.    Else
  1111.       tt=-999
  1112.    Endif
  1113.    Temp500V=virtual2(Temp500+273.15,Dewp500+273.15,500)-273.15
  1114.    PclTemp=LiftWet(TLcl,PLcl,500,0)
  1115.    PclTempV=virtual2(PclTemp+273.15,PclTemp+273.15,500)-273.15
  1116.    SLI=Temp500V-PclTempV
  1117.    rec=GHBCAPE(sndtemp,snddewp,MAscentI*100,1,200,MAscentT)
  1118.    Pos=subwrd(rec,1)
  1119.    CIN=subwrd(rec,2)

  1120.    If (SfcPlev != PMaxThee)
  1121.       PclTemp=LiftWet(TLclMax,PLclMax,500,0)
  1122.       PclTempV=virtual2(PclTemp+273.15,PclTemp+273.15,500)-273.15
  1123.       LIMax=Temp500V-PclTempV
  1124.       rec=GHBCAPE(sndtemp,snddewp,MAscentI*100,2,200,MAscentT)
  1125.       PosMax=subwrd(rec,1)
  1126.       CINMax=subwrd(rec,2)
  1127.    Else
  1128.       LIMax=SLI
  1129.       PosMax=Pos
  1130.       CINMax=CIN
  1131.       MaxThee=SfcThee
  1132.    Endif

  1133.    If (PageType = "Portrait")
  1134.       If (Text1XC = -1)
  1135.          Text1XC=rxloc-0.75
  1136.       Endif
  1137.       If (Text1YC = -1)
  1138.          Text1YC=tyloc-2.25
  1139.       Endif
  1140.       If (Text2XC = -1)
  1141.          Text2XC=rxloc-0.75
  1142.       Endif
  1143.       If (Text2YC = -1)
  1144.          Text2YC=tyloc-3.45
  1145.       Endif
  1146.       If (Text3XC = -1)
  1147.           Text3XC=rxloc-0.75
  1148.       Endif
  1149.       If (Text3YC = -1)
  1150.          Text3YC=tyloc-4.60
  1151.       Endif
  1152.    Else
  1153.       If (Text1XC = -1)
  1154.          Text1XC=rxloc+2.50
  1155.       Endif
  1156.       If (Text1YC = -1)
  1157.          Text1YC=tyloc-2.75
  1158.       Endif
  1159.       If (Text2XC = -1)
  1160.          Text2XC=rxloc+2.50
  1161.       Endif
  1162.       If (Text2YC = -1)
  1163.          Text2YC=tyloc-4.00
  1164.       Endif
  1165.       If (Text3XC = -1)
  1166.          Text3XC=rxloc+2.50
  1167.       Endif
  1168.       If (Text3YC = -1)
  1169.          Text3YC=tyloc-5.10
  1170.       Endif
  1171.    Endif
  1172.    "set string 1 l 3"
  1173.    'set strsiz 0.10'
  1174.    "set line 0 1 3"
  1175.    "draw recf  "Text1XC-0.75 " " Text1YC-0.40 " " Text1XC+0.75 " " Text1YC+0.25
  1176.    "set line 1 1 3"
  1177.    "draw rec  "Text1XC-0.75 " " Text1YC-0.40 " " Text1XC+0.75 " " Text1YC+0.25
  1178.    "draw string "Text1XC-0.70 " " Text1YC+0.10"  K"
  1179.    "draw string "Text1XC+0.25 " " Text1YC+0.10" " math_int(K)      
  1180.    "draw string "Text1XC-0.70 " " Text1YC-0.10 "  TT"
  1181.    "draw string "Text1XC+0.25 " " Text1YC-0.10 " " math_int(tt)
  1182.    "draw string "Text1XC-0.70 " " Text1YC-0.25 "  PW(cm)"
  1183.    "draw string "Text1XC+0.25 " " Text1YC-0.25 " " math_int(pw*100)/100
  1184.    "set line 0 1 3"
  1185.    "draw recf  "Text2XC-0.75 " " Text2YC-0.60 " " Text2XC+0.75 " " Text2YC+0.85
  1186.    "set line 1 1 3"
  1187.    "draw rec  "Text2XC-0.75 " " Text2YC-0.60 " " Text2XC+0.75 " " Text2YC+0.85
  1188.    "draw string "Text2XC-0.45 " " Text2YC+0.70 " Lowest level"
  1189.    "draw string "Text2XC-0.70 " " Text2YC+0.45 "  Press(mb)"
  1190.    "draw string "Text2XC+0.25 " " Text2YC+0.45 " " math_int(SfcPlev)
  1191.    "draw string "Text2XC-0.70 " " Text2YC+0.30 "  Temp(`3.`0C)"
  1192.    "draw string "Text2XC+0.25 " " Text2YC+0.30 " " math_int(Sfctemp*10)/10
  1193.    "draw string "Text2XC-0.70 " " Text2YC+0.15 "  Dewp(`3.`0C)"
  1194.    "draw string "Text2XC+0.25 " " Text2YC+0.15 " " math_int(Sfcdewp*10)/10
  1195.    "draw string "Text2XC-0.70 " " Text2YC "   `3z`0`bE`n(K)"
  1196.    "draw string "Text2XC+0.25 " " Text2YC " " math_int(SfcThee)
  1197.    "draw string "Text2XC-0.70 " " Text2YC-0.15 "  LI (`3.`0C)"
  1198.    "draw string "Text2XC+0.25 " " Text2YC-0.15 " " math_nint(SLI)
  1199.    "draw string "Text2XC-0.70 " " Text2YC-0.30 "  CAPE(Jkg`a-1`n)"
  1200.    "draw string "Text2XC+0.25 " " Text2YC-0.30 " " math_int(Pos)   
  1201.    "draw string "Text2XC-0.70 " " Text2YC-0.45 "  CIN(Jkg`a-1`n)"
  1202.    "draw string "Text2XC+0.25 " " Text2YC-0.45 " " math_int(CIN)      
  1203.    "set line 0 1 3"
  1204.    "draw recf  "Text3XC-0.75 " " Text3YC-0.55 " "  Text3XC+0.75 " " Text3YC+0.55
  1205.    "set line 1 1 3"
  1206.    "draw rec  "Text3XC-0.75 " " Text3YC-0.55 " "  Text3XC+0.75 " " Text3YC+0.55
  1207.    "draw string "Text3XC-0.60 " " Text3YC+0.45 " Most Unstable"
  1208.    "draw string "Text3XC-0.70 " " Text3YC+0.20 "  Press(mb)"
  1209.    "draw string "Text3XC+0.25 " " Text3YC+0.20 " " math_int(PMaxThee)
  1210.    "draw string "Text3XC-0.70 " " Text3YC+0.05 " `3z`0`bE`n(K)"
  1211.    "draw string "Text3XC+0.25 " " Text3YC+0.05 " " math_int(MaxThee)
  1212.    "draw string "Text3XC-0.70 " " Text3YC-0.10 " LI (`3.`0C)"
  1213.    "draw string "Text3XC+0.25 " " Text3YC-0.10 " "math_nint(LIMax)
  1214.    "draw string "Text3XC-0.70 " " Text3YC-0.25 " CAPE(Jkg`a-1`n)"
  1215.    "draw string "Text3XC+0.25 " " Text3YC-0.25 " "math_int(PosMax)
  1216.    "draw string "Text3XC-0.70 " " Text3YC-0.40 " CIN(Jkg`a-1`n)"
  1217.    "draw string "Text3XC+0.25 " " Text3YC-0.40 " " math_int(CINMax)
  1218. Endif

  1219. *-----------------------------
  1220. * Draw wind profile along side
  1221. *-----------------------------

  1222. If (DrawBarb = 1)
  1223.    say "Drawing Wind Profile."
  1224.    If (poleloc = -1)
  1225.       If (PageType = "Portrait")
  1226.          poleloc = 8.0
  1227.       Else
  1228.          poleloc = 7.5
  1229.       Endif
  1230.    Endif
  1231.    If (barbline = 1)
  1232.       "set line 1 1 3"
  1233.       "draw line "poleloc " " byloc " " poleloc " " tyloc
  1234.    Endif
  1235.    If (BarbCol = -1)
  1236.       'set rgb 41 255 0 132'
  1237.       'set rgb 42 255 0 168'
  1238.       'set rgb 43 255 0 204'
  1239.       'set rgb 44 255 0 240'
  1240.       'set rgb 45 255 0 255'
  1241.       'set rgb 46 204 0 255'
  1242.       'set rgb 47 174 0 255'
  1243.       'set rgb 48 138 0 255'
  1244.       'set rgb 49 108 0 255'
  1245.       'set rgb 50 84 0 255'
  1246.       'set rgb 51 40 0 255'
  1247.       'set rgb 52 0 0  255'
  1248.       'set rgb 53 0 42 255'
  1249.       'set rgb 54 0 84 255'
  1250.       'set rgb 55 0 120 255'
  1251.       'set rgb 56 0 150 255'
  1252.       'set rgb 57 0 192 255'
  1253.       'set rgb 58 0 240 255'
  1254.       'set rgb 59 0 255 210'
  1255.       'set rgb 60 0 255 160'
  1256.       'set rgb 61 0 255 126'
  1257.       'set rgb 62 0 255 78'
  1258.       'set rgb 63 84 255 0'
  1259.       'set rgb 64 138 255 0'
  1260.       'set rgb 65 188 255 0'
  1261.       'set rgb 66 236 255 0'
  1262.       'set rgb 67 255 255 0'
  1263.       'set rgb 68 255 222 0'
  1264.       'set rgb 69 255 192 0'
  1265.       'set rgb 70 255 162 0'
  1266.       'set rgb 71 255 138 0'
  1267.       'set rgb 72 255 108 0'
  1268.       'set rgb 73 255 84 0'
  1269.       'set rgb 74 255 54 0'
  1270.       'set rgb 75 255 12 0'
  1271.       'set rgb 76 255 0 34'
  1272.       'set rgb 77 255 0 70'
  1273.       'set rgb 78 255 0 105'
  1274.       'set rgb 79 255 0 140'
  1275.       'set rgb 80 255 0 175'
  1276.       'set rgb 81 255 0 215'
  1277.       'set rgb 82 255 0 255'
  1278.       'set rgb 83 255 255 255'

  1279.       col1='83 83 83 83 83 83 83 83 83 83 82 81 80 79 78'
  1280.       col2='77 76 75 74 73 72 71 70 69 68 67 66 65 64 63'
  1281.       col3='62 61 60 59 58 57 56 55 54 53 52 51 50 49 48'
  1282.       'set rbcols 'col1' 'col2' 'col3
  1283.    Endif
  1284.    "set z "_zmin" "_zmax
  1285.    "set gxout stat"
  1286.    zz=1
  1287.    wspd=-999
  1288.    cont=1
  1289.    While (cont = 1 & zz < _zmax)
  1290.       "set z "zz
  1291.       pres=subwrd(result,4)
  1292.       "define dd="sndspd
  1293.       "d dd"
  1294.       rec=sublin(result,8)
  1295.       wspd=subwrd(rec,4)
  1296.       if (math_abs(wspd) > 500 | pres > _pmax)
  1297.           zz=zz+1
  1298.       else
  1299.           cont=0
  1300.       Endif
  1301.    Endwhile
  1302.    While (zz <= _zmax)
  1303.       "set z "zz
  1304.       "define dd="sndspd
  1305.       "d dd"
  1306.       rec=sublin(result,8)
  1307.       wspd=subwrd(rec,4)
  1308.       If (BarbCol >= 0)
  1309.          "set line "BarbCol " 1 "BarbThk
  1310.       Else
  1311.          tempbcol=55+wspd/5     
  1312.          If (tempbcol > 83)
  1313.             tempbcol = 83
  1314.          Endif
  1315.          "set line "tempbcol " 1 "BarbThk
  1316.       Endif
  1317.       "define dd="snddir
  1318.       "d dd"
  1319.       rec=sublin(result,8)
  1320.       wdir=subwrd(rec,4)
  1321.       xwind=GetUWnd(wspd,wdir)
  1322.       ywind=GetVWnd(wspd,wdir)
  1323.       "query gr2xy 5 "zz
  1324.       y1=subwrd(result,6)
  1325.       if (math_abs(wspd) < 500)
  1326.          cc=polelen/wspd
  1327.          xendpole=poleloc-xwind*cc
  1328.          yendpole=y1-ywind*cc
  1329.       endif
  1330.       if (xendpole>0 & wspd >= 0.5)
  1331.         if (flagbase = 1)
  1332.            "draw mark 3 "poleloc " " y1 " 0.05"
  1333.         endif
  1334.         "draw line " poleloc " " y1 "  " xendpole " " yendpole
  1335.         flagloop=wspd/10
  1336.         windcount=wspd
  1337.         flagcount=0
  1338.         xflagstart=xendpole
  1339.         yflagstart=yendpole
  1340.         dx=math_cos((180-wdir)*_dtr)
  1341.         dy=math_sin((180-wdir)*_dtr)
  1342.         while (windcount > 47.5)
  1343.            flagcount=flagcount+1
  1344.            dxflag=-len50*dx
  1345.            dyflag=-len50*dy
  1346.            xflagend=xflagstart+dxflag
  1347.            yflagend=yflagstart+dyflag
  1348.            windcount=windcount-50
  1349.            x1=xflagstart+0.5*wid50*xwind/wspd
  1350.            y1=yflagstart+0.5*wid50*ywind/wspd
  1351.            x2=xflagstart-0.5*wid50*xwind/wspd
  1352.            y2=yflagstart-0.5*wid50*ywind/wspd
  1353.            If (Fill50 = 1)
  1354.               "draw polyf "x1" "y1" "x2" "y2" "xflagend" "yflagend" "x1" "y1
  1355.            Else
  1356.               "draw line "x1 " "y1 " " xflagend " " yflagend " "  
  1357.               "draw line "x2 " "y2 " " xflagend " " yflagend
  1358.               "draw line "x1 " "y1 " " x2 " " y2
  1359.            Endif
  1360.            xflagstart=xflagstart+spac50*xwind/wspd
  1361.            yflagstart=yflagstart+spac50*ywind/wspd
  1362.         endwhile
  1363.         while (windcount > 7.5 )
  1364.            flagcount=flagcount+1
  1365.            dxflag=-len10*dx
  1366.            dyflag=-len10*dy
  1367.            xflagend=xflagstart+dxflag
  1368.            yflagend=yflagstart+dyflag
  1369.            windcount=windcount-10
  1370.            "draw line " xflagstart " " yflagstart " " xflagend " " yflagend
  1371.            xflagstart=xflagstart+spac10*xwind/wspd
  1372.            yflagstart=yflagstart+spac10*ywind/wspd
  1373.         endwhile
  1374.         if (windcount > 2.5)
  1375.            flagcount=flagcount+1
  1376.            if (flagcount = 1)
  1377.               xflagstart=xflagstart+spac05*xwind/wspd
  1378.               yflagstart=yflagstart+spac05*ywind/wspd
  1379.            endif
  1380.            dxflag=-len05*dx
  1381.            dyflag=-len05*dy
  1382.            xflagend=xflagstart+dxflag
  1383.            yflagend=yflagstart+dyflag
  1384.            windcount=windcount-5
  1385.            "draw line " xflagstart " " yflagstart " " xflagend " " yflagend
  1386.         endif
  1387.       else
  1388.         if (wspd < 0.5 & wspd >= 0)
  1389.            "draw mark 2 " poleloc " " y1 " 0.08"
  1390.         endif
  1391.       endif
  1392.       zz=zz+barbint
  1393.    endwhile
  1394. Endif

  1395. *----------------
  1396. * Draw Hodograph
  1397. *----------------

  1398. If (DrawHodo = 1)
  1399.    say "Drawing Hodograph."

  1400.    If (HodXcent = -1 | HodYcent = -1)
  1401.       If (PageType = "Portrait")
  1402.          HodXcent=6
  1403.          HodYcent=9.5
  1404.       Else
  1405.          HodXcent=9
  1406.          HodYcent=7.0
  1407.       Endif
  1408.    Endif
  1409.    HodL=HodXcent-HodSize/2.0
  1410.    HodR=HodXcent+HodSize/2.0
  1411.    HodT=HodYcent+HodSize/2.0
  1412.    HodB=HodYcent-HodSize/2.0
  1413.    RingSpac=HodSize/(NumRing*2)
  1414.    "set line 0"
  1415.    "draw recf "HodL" "HodB" "HodR" "HodT
  1416.    "set line 1 1 6"
  1417.    "draw rec "HodL" "HodB" "HodR" "HodT
  1418.    "set line 1 1 3"
  1419.    "set string 1 c"
  1420.    "draw mark 1 "HodXcent " "HodYcent " " HodSize
  1421.    i=1
  1422.    While (i <= NumRing)
  1423.      "set strsiz 0.10"
  1424.      "set string 1 c 3 45"
  1425.      uwnd=-i*HodRing*math_cos(45*_dtr)
  1426.      xloc=HodXcent+uwnd*RingSpac/HodRing
  1427.      yloc=HodYcent+uwnd*RingSpac/HodRing
  1428.   
  1429.      "draw mark 2 "HodXcent " " HodYcent " " i*HodSize/NumRing
  1430.      "draw string "xloc " " yloc " " HodRing*i
  1431.      i=i+1
  1432.    Endwhile
  1433.    "set string 1 l 3 0"
  1434.    If (TickInt > 0)
  1435.       i=0
  1436.       while (i < HodRing*NumRing)
  1437.          dist=i*RingSpac/HodRing
  1438.          hrxloc=HodXcent+dist               
  1439.          hlxloc=HodXcent-dist                     
  1440.          htyloc=HodYcent+dist
  1441.          hbyloc=HodYcent-dist
  1442.          "set line 1 1 3"
  1443.          "draw line "hrxloc " " HodYcent-TickSize/2 " " hrxloc " " HodYcent+TickSize/2
  1444.          "draw line "hlxloc " " HodYcent-TickSize/2 " " hlxloc " " HodYcent+TickSize/2
  1445.          "draw line "HodXcent+TickSize/2 " " htyloc " " HodXcent-TickSize/2 " " htyloc
  1446.          "draw line "HodXcent+TickSize/2 " " hbyloc " " HodXcent-TickSize/2 " " hbyloc
  1447.          i=i+TickInt
  1448.       endwhile
  1449.    Endif
  1450.    "set line "HodoCol " " HodoLine " "HodoThk
  1451.    "draw string "HodL+0.05 " " HodT-0.1 " knots"
  1452.    zloop=_zmin
  1453.    xold=-999
  1454.    yold=-999
  1455.    count=0
  1456.    Depth=0
  1457.    While (zloop < _zmax & Depth < HodoDep)
  1458.       "set z "zloop
  1459.       pres=subwrd(result,4)
  1460.       "define dd="sndspd
  1461.       "d dd"
  1462.       rec=sublin(result,8)
  1463.       wspd=subwrd(rec,4)
  1464.       "define dd="snddir
  1465.       "d dd"
  1466.       rec=sublin(result,8)
  1467.       wdir=subwrd(rec,4)
  1468.       uwnd=GetUWnd(wspd,wdir)
  1469.       vwnd=GetVWnd(wspd,wdir)
  1470.       If (wspd >= 0)
  1471.          xloc=HodXcent+uwnd*RingSpac/HodRing
  1472.          yloc=HodYcent+vwnd*RingSpac/HodRing
  1473.          If (xloc > 0 & yloc > 0 & xold > 0 & yold > 0)
  1474.             Depth=Depth+pold-pres
  1475.             count=count+1
  1476.             If (count = 1)
  1477.                "draw mark 3 "xold " " yold " 0.05"
  1478.             Endif
  1479.             "draw line "xold" "yold" "xloc" "yloc
  1480.          Endif
  1481.          xold=xloc
  1482.          yold=yloc
  1483.       Endif
  1484.       zloop=zloop+1
  1485.       pold=pres
  1486.    EndWhile

  1487.    If (count > 0)
  1488.       "draw mark 3 "xold " " yold " 0.05"
  1489.    Endif
  1490. Endif

  1491. *----------------------------------------------
  1492. * Calculate and Display Absolute & S-R Helicity
  1493. *----------------------------------------------

  1494. If (DrawHeli = 1)
  1495.    say "Calculating Helicity & SR Helicity."
  1496.    delp=10
  1497.    UTotal=0
  1498.    VTotal=0

  1499. * First, calculate mass-weighted mean wind
  1500. * Since delp is a constant, and mass is proportional to
  1501. * delp, this is a simple sum.

  1502.    "set lev "_pmax " " _pmin
  1503.    "define uwndarr="sndspd"*cos((270-"snddir")*"_dtr")"
  1504.    "define vwndarr="sndspd"*sin((270-"snddir")*"_dtr")"

  1505.    pres=MeanVBot
  1506.    While (pres >= MeanVTop)
  1507.       uwnd=interp(uwndarr,pres)*_ktm
  1508.       vwnd=interp(vwndarr,pres)*_ktm
  1509.       If (uwnd > -900 & vwnd > -900)
  1510.          UTotal=UTotal+uwnd
  1511.          VTotal=VTotal+vwnd
  1512.       Endif
  1513.       pres=pres-delp
  1514.    EndWhile
  1515.    vcount=1+(MeanVBot-MeanVTop)/delp
  1516.    Umean=UTotal/vcount
  1517.    Vmean=VTotal/vcount
  1518.    Spdmean=GetWSpd(Umean,Vmean)
  1519.    MeanDir=GetWDir(Umean,Vmean)

  1520. * Now, rotate and reduce mean wind to get storm motion

  1521.    If (StormMot = 1)
  1522.       If (Spdmean < 15)
  1523.          Reduct=0.25
  1524.          Rotate=30
  1525.       Else
  1526.          Reduct=0.20
  1527.          Rotate=20
  1528.       Endif
  1529.    Else
  1530.       Reduct=0.0
  1531.       Rotate=0.0
  1532.    Endif

  1533.    UReduce=(1-Reduct)*Umean
  1534.    VReduce=(1-Reduct)*Vmean
  1535.    StormSpd=GetWSpd(UReduce,VReduce)

  1536.    StormDir=GetWDir(UReduce,VReduce)+Rotate
  1537.    If (StormDir >= 360)
  1538.       StormDir=StormDir-360
  1539.    Endif

  1540.    StormU=GetUWnd(StormSpd,StormDir)
  1541.    StormV=GetVWnd(StormSpd,StormDir)

  1542. * Draw Storm Motion Vector

  1543.    xloc=HodXcent+_mtk*StormU*RingSpac/HodRing
  1544.    yloc=HodYcent+_mtk*StormV*RingSpac/HodRing

  1545.    "set line "SVectCol" 1 4"
  1546.    "draw line "HodXcent " " HodYcent " " xloc " " yloc
  1547.    Arr1U=GetUWnd(HodRing/10,StormDir+30)
  1548.    Arr1V=GetVWnd(HodRing/10,StormDir+30)
  1549.    Arr2U=GetUWnd(HodRing/10,StormDir-30)
  1550.    Arr2V=GetVWnd(HodRing/10,StormDir-30)

  1551.    xloc2=xloc-Arr1U/HodRing
  1552.    xloc3=xloc-Arr2U/HodRing
  1553.    yloc2=yloc-Arr1V/HodRing
  1554.    yloc3=yloc-Arr2V/HodRing

  1555.    "set line "SVectCol" 1 3"

  1556.    If (FillArrw = 0)
  1557.       "draw line "xloc" "yloc" "xloc2" "yloc2
  1558.       "draw line "xloc" "yloc" "xloc3" "yloc3
  1559.    Else
  1560.       "draw polyf "xloc" "yloc" "xloc2" "yloc2" "xloc3" "yloc3" "xloc" "yloc
  1561.    Endif


  1562. * Now, calculate SR and Environmental Helicity

  1563.    helic=0
  1564.    SRhelic=0
  1565.    MinP=SfcPlev-HelicDep
  1566.    pres=SfcPlev
  1567.    uwndold=-999
  1568.    vwndold=-999
  1569.    While (pres >= MinP)
  1570.       uwnd=interp(uwndarr,pres)*_ktm
  1571.       vwnd=interp(vwndarr,pres)*_ktm
  1572.       If (uwnd > -900 & uwndold > -900)
  1573.           du=uwnd-uwndold
  1574.           dv=vwnd-vwndold
  1575.           ubar=0.5*(uwnd+uwndold)
  1576.           vbar=0.5*(vwnd+vwndold)
  1577.           uhelic=-dv*ubar                  
  1578.           vhelic=du*vbar                  
  1579.           SRuhelic=-dv*(ubar-StormU)
  1580.           SRvhelic=du*(vbar-StormV)
  1581.           SRhelic=SRhelic+SRuhelic+SRvhelic
  1582.           helic=helic+uhelic+vhelic
  1583.       Endif
  1584.       uwndold=uwnd
  1585.       vwndold=vwnd
  1586.       pres=pres-delp
  1587.    EndWhile

  1588.    "set strsiz 0.1"
  1589.    "set string 1 l 3"
  1590.    If (PageType = "Portrait")
  1591.       If (Text4XC = -1)
  1592.          Text4XC=rxloc-0.75
  1593.       Endif
  1594.       If (Text4YC = -1)
  1595.          Text4YC=tyloc-5.65
  1596.       Endif
  1597.    Else
  1598.       If (Text4XC = -1)
  1599.          Text4XC=rxloc+2.50
  1600.       Endif
  1601.       If (Text4YC = -1)
  1602.          Text4YC=tyloc-6.10
  1603.       Endif
  1604.    Endif
  1605.    "set line 0 1 3"
  1606.    "draw recf  "Text4XC-0.75 " "Text4YC-0.5 " " Text4XC+0.75 " " Text4YC+0.5
  1607.    "set line 1 1 3"
  1608.    "draw rec  "Text4XC-0.75 " "Text4YC-0.5 " " Text4XC+0.75 " " Text4YC+0.5
  1609.    "draw string "Text4XC-0.45 " " Text4YC+0.40 " Hodograph"
  1610.    "draw string "Text4XC-0.70 " " Text4YC+0.20 " EH"
  1611.    "draw string "Text4XC+0.25 " " Text4YC+0.20 " "math_int(helic)
  1612.    "draw string "Text4XC-0.70 " " Text4YC+0.05 " SREH"
  1613.    "draw string "Text4XC+0.25 " " Text4YC+0.05 " " math_int(SRhelic)
  1614.    "draw string "Text4XC-0.70 " " Text4YC-0.20 " StmDir"
  1615.    "draw string "Text4XC+0.25 " " Text4YC-0.20 " " math_int(StormDir)"`3.`0"
  1616.    "draw string "Text4XC-0.70 " " Text4YC-0.35 " StmSpd(kt)"
  1617.    "draw string "Text4XC+0.25 " " Text4YC-0.35 " " math_int(_mtk*StormSpd)
  1618. Endif

  1619. *---------------------------------------------------
  1620. * Plot RH profile.
  1621. *---------------------------------------------------

  1622. If (DrawRH = 1)
  1623.   "set z "_zmin" "_zmax
  1624.   "set x "_xval
  1625.   "set y "_yval
  1626.   "set t "_tval
  1627.   "set zlog on"
  1628.   "set gxout line"
  1629.   "set ccolor "RHCol
  1630.   "set cstyle "RHLine
  1631.   "set cmark "RHMrk
  1632.   "set digsiz "MrkSize
  1633.   "set missconn on"
  1634.   "set xlab on"
  1635.   "set frame off"
  1636.   "set vrange 0 350"
  1637.   "set xlpos 0 t"
  1638.   "set xlevs 25 50 75 100"
  1639.   "set grid vertical 5"
  1640.   "define rh=100*exp((17.2694*"snddewp")/("snddewp"+237.3)-(17.2694*"sndtemp")/("sndtemp"+237.3))"
  1641.   "d rh"
  1642.    If (LblAxes = 1)
  1643.      "set string 1 c 3 0"
  1644.      "set strsiz 0.125"
  1645.      If (PageType = "Portrait")
  1646.        "draw string 1.5 10.85 RH (%)"
  1647.      Else
  1648.        "draw string 1.75 8.35 RH (%)"
  1649.      Endif
  1650.    Endif
  1651. Endif

  1652. *------------------------------------------
  1653. * Reset environment to original dimensions
  1654. *------------------------------------------

  1655. "set t "_tval
  1656. "set x "_xval
  1657. "set y "_yval
  1658. "set z "_zmin " "_zmax

  1659. say "Done."

  1660. Return(0)

  1661. *************************************************************************
  1662. function Templcl(temp,dewp)

  1663. *------------------------------------------------------
  1664. * Calculate the temp at the LCL given temp & dewp in C
  1665. *------------------------------------------------------

  1666. tempk=temp+273.15
  1667. dewpk=dewp+273.15
  1668. Parta=1/(dewpk-56)
  1669. Partb=math_log(tempk/dewpk)/800
  1670. Tlcl=1/(Parta+Partb)+56
  1671. return(Tlcl-273.15)

  1672. **************************************************************************

  1673. function Preslcl(temp,dewp,pres)

  1674. *-------------------------------------------------------
  1675. * Calculate press of LCL given temp & dewp in C and pressure
  1676. *-------------------------------------------------------

  1677. Tlclk=Templcl(temp,dewp)+273.15
  1678. tempk=temp+273.15
  1679. theta=tempk*math_pow(1000/pres,0.286)
  1680. plcl=1000*math_pow(Tlclk/theta,3.48)
  1681. return(plcl)

  1682. **************************************************************************
  1683. function LiftWet(startt,startp,endp,display,Pmin,Pmax)

  1684. *--------------------------------------------------------------------
  1685. * Lift a parcel moist adiabatically from startp to endp.
  1686. * Init temp is startt in C.  If you wish to see the parcel's
  1687. * path plotted, display should be 1.  Returns temp of parcel at endp.
  1688. *--------------------------------------------------------------------

  1689. temp=startt
  1690. pres=startp
  1691. cont = 1
  1692. delp=10
  1693. While (pres >= endp & cont = 1)
  1694.     If (display = 1)
  1695.        xtemp=GetXLoc(temp,pres)
  1696.        "q w2xy "xtemp" "pres
  1697.        xloc=subwrd(result,3)
  1698.        yloc=subwrd(result,6)
  1699.        If (xtemp < 0 | xtemp > 100)
  1700.           cont=0
  1701.        Else
  1702.           If (pres >= Pmin & pres < Pmax & pres < startp)  
  1703.              "draw line "xold" "yold" "xloc" "yloc
  1704.           Endif
  1705.        Endif
  1706.     Endif
  1707.     xold=xloc
  1708.     yold=yloc
  1709.     temp=temp-100*delp*gammaw(temp,pres-delp/2,100)
  1710.     pres=pres-delp
  1711. EndWhile
  1712. return(temp)


  1713. **************************************************************************
  1714. function LiftDry(startt,startp,endp,display,Pmin,Pmax)

  1715. *--------------------------------------------------------------------
  1716. * Lift a parcel dry adiabatically from startp to endp.
  1717. * Init temp is startt in C.  If you wish to see the parcel's
  1718. * path plotted, display should be 1.  Returns temp of parcel at endp.
  1719. *--------------------------------------------------------------------

  1720. starttk=startt+273.15
  1721. cont = 1
  1722. delp=10
  1723. round=math_int(startp/10)*10
  1724. subscr=0.1*round
  1725. powstart=math_pow(startp,-0.286)
  1726. temp=starttk*_powpres.subscr*powstart-273.15
  1727. pres=round-10
  1728. While (pres >= endp & cont = 1)
  1729.     subscr=0.1*pres
  1730.     temp=starttk*_powpres.subscr*powstart-273.15
  1731.     If (display = 1)
  1732.        xtemp=GetXLoc(temp,pres)
  1733.        "q w2xy "xtemp" "pres
  1734.        xloc=subwrd(result,3)
  1735.        yloc=subwrd(result,6)
  1736.        If (xtempold > 0 & xtempold < 100 & xtemp > 0 & xtemp < 100)
  1737.           If (pres >= Pmin & pres < Pmax & pres < startp)  
  1738.              "draw line "xold" "yold" "xloc" "yloc
  1739.           Endif
  1740.        Endif
  1741.     Endif
  1742.     xold=xloc
  1743.     xtempold=xtemp
  1744.     yold=yloc
  1745.     pres=pres-delp
  1746. EndWhile
  1747. return(temp)

  1748. ***************************************************************************
  1749. function gammaw(tempc,pres,rh)

  1750. *-----------------------------------------------------------------------
  1751. * Function to calculate the moist adiabatic lapse rate (deg C/Pa) based
  1752. * on the temperature, pressure, and rh of the environment.
  1753. *----------------------------------------------------------------------

  1754. tempk=tempc+273.15
  1755. es=satvap2(tempc)
  1756. ws=mixratio(es,pres)
  1757. w=rh*ws/100
  1758. tempv=virtual(tempk,w)
  1759. latent=latentc(tempc)

  1760. A=1.0+latent*ws/(287*tempk)
  1761. B=1.0+0.622*latent*latent*ws/(1005*287*tempk*tempk)
  1762. Density=100*pres/(287*tempv)
  1763. lapse=(A/B)/(1005*Density)
  1764. return(lapse)

  1765. *************************************************************************
  1766. function latentc(tempc)

  1767. *-----------------------------------------------------------------------
  1768. * Function to return the latent heat of condensation in J/kg given
  1769. * temperature in degrees Celsius.
  1770. *-----------------------------------------------------------------------

  1771. val=2502.2-2.43089*tempc

  1772. return(val*1000)

  1773. *************************************************************************
  1774. function precipw(sndtemp,snddewp,startp,endp)

  1775. *-----------------------------------------------------------------------
  1776. * Function to calculate the precipitable water (cm) in a sounding
  1777. * starting at pressure level startp and ending at pressure level endp.
  1778. *-----------------------------------------------------------------------

  1779. ppold=-999
  1780. ttold=-999
  1781. ddold=-999
  1782. delp=10
  1783. Int=0
  1784. mix=0
  1785. pres=startp
  1786. logpp=math_log(pres)
  1787. logppm=math_log(pres-delp)
  1788. while (pres >= endp)
  1789.    tt=interp(sndtemp,pres)
  1790.    dd=interp(snddewp,pres)
  1791.    if (tt>-900 & ttold>-900 & dd>-900 & ddold>-900)
  1792.       e=satvap2(dd)
  1793.       mix=mixratio(e,pres)
  1794.       mixavg=(logpp*mix+logppm*mixold)/(logpp+logppm)
  1795.       Int=Int+1.020408*mixavg*delp
  1796.    endif
  1797.    ttold=tt
  1798.    ddold=dd
  1799.    ppold=pp
  1800.    mixold=mix
  1801.    pres=pres-delp
  1802.    logpp=logppm
  1803.    logppm=math_log(pres-delp)
  1804. endwhile

  1805. return(Int)

  1806. *************************************************************************

  1807. function virtual(temp,mix)

  1808. *------------------------------------------------------------
  1809. * Function to return virtual temperature given temperature in
  1810. * kelvin and mixing ratio in g/g.
  1811. *-------------------------------------------------------------

  1812. tempv=temp*(1.0+0.6*mix)

  1813. return (tempv)

  1814. ************************************************************************

  1815. function virtual2(temp,dewp,pres)
  1816.   
  1817. *------------------------------------------------------------
  1818. * Function to return virtual temperature in kelvin given temperature in
  1819. * kelvin and dewpoint in kelvin and pressure in mb
  1820. *-------------------------------------------------------------

  1821. if (temp > 0 & dewp > 0)
  1822.   vap=satvap2(dewp-273.15)
  1823.   mix=mixratio(vap,pres)
  1824.   tempv=virtual(temp,mix)
  1825. else
  1826.   tempv=-9999
  1827. endif

  1828. return (tempv)
  1829.   
  1830. ************************************************************************

  1831. function satvapor(temp)

  1832. *---------------------------------------------------------------
  1833. * Given temp in Celsius, returns saturation vapor pressure in mb
  1834. *---------------------------------------------------------------

  1835. pol=_C0+temp*(_C1+temp*(_C2+temp*(_C3+temp*(_C4+temp*(_C5+temp*(_C6+temp*(_C7+temp*(_C8+temp*(_C9)))))))))

  1836. return(6.1078/math_pow(pol,8))

  1837. ************************************************************************

  1838. function satvap2(temp)

  1839. *---------------------------------------------------------------
  1840. * Given temp in Celsius, returns saturation vapor pressure in mb
  1841. *---------------------------------------------------------------

  1842. es=6.112*math_exp(17.67*temp/(temp+243.5))

  1843. return(es)

  1844. *************************************************************************

  1845. function mixratio(e,p)

  1846. *------------------------------------------------------
  1847. * Given vapor pressure and pressure, return mixing ratio
  1848. *-------------------------------------------------------

  1849. mix=0.622*e/(p-e)

  1850. return(mix)

  1851. ************************************************************************

  1852. function getrh(temp,dewp,pres)

  1853. tempk=temp+273.15
  1854. dewpk=dewp+273.15

  1855. es=satvap2(temp)

  1856. If (temp > 0)
  1857.    A=2.53*math_pow(10,9)
  1858.    B=5420
  1859. Else
  1860.    A=3.41*math_pow(10,10)
  1861.    B=6130
  1862. Endif

  1863. w=A*0.622*math_exp(-B/dewpk)/pres
  1864. ws=mixratio(es,pres)

  1865. return(100*w/ws)

  1866. ************************************************************************
  1867. function interp(array,pres)

  1868. *------------------------------------------------------------------------
  1869. * Interpolate inside array for pressure level pres.
  1870. * Returns estimated value of array at pressure pres.
  1871. *------------------------------------------------------------------------

  1872. "set gxout stat"
  1873. "set lev "pres
  1874. altpres=subwrd(result,4)
  1875. "q dims"
  1876. rec=sublin(result,4)
  1877. zlev=subwrd(rec,9)

  1878. If (zlev < 2 | zlev > _zmaxfile)
  1879.   Vest = -9999.0
  1880. Else
  1881.   If (altpres > pres)
  1882.     zlev=zlev+1
  1883.   Endif
  1884.   "set z "zlev
  1885.   PAbove=subwrd(result,4)
  1886.   "define dd="array"(lev="PAbove")"
  1887.   "d dd"
  1888.   rec=sublin(result,8)
  1889.   VAbove=subwrd(rec,4)
  1890.   "set z "zlev-1
  1891.   PBelow=subwrd(result,4)
  1892.   "define dd="array"(lev="PBelow")"
  1893.   "d dd"
  1894.   rec=sublin(result,8)
  1895.   VBelow=subwrd(rec,4)

  1896. * Now if we are in a region of missing data, find next good level.

  1897.   If (math_abs(VAbove) > 130 & zlev > 1 & zlev < _zmaxfile)
  1898.      zz=zlev
  1899.      While (math_abs(VAbove) > 130 & zz < _zmaxfile)
  1900.        zz=zz+1
  1901.        "set z "zz
  1902.        PAbove=subwrd(result,4)
  1903.        "define dd="array"(lev="PAbove")"
  1904.        "d dd"
  1905.        rec=sublin(result,8)
  1906.        VAbove=subwrd(rec,4)
  1907.      EndWhile
  1908.   Endif

  1909.   If (math_abs(VBelow) > 130 & zlev > 1 & zlev < _zmaxfile)
  1910.       zz=zlev-1
  1911.       While (math_abs(VBelow) > 130 & zz > 1)
  1912.         zz=zz-1
  1913.         "set z "zz
  1914.         PBelow=subwrd(result,4)
  1915.         "define dd="array"(lev="PBelow")"
  1916.         "d dd"
  1917.         rec=sublin(result,8)
  1918.         VBelow=subwrd(rec,4)
  1919.       EndWhile
  1920.   Endif

  1921.   If (math_abs(VAbove) < 130 & math_abs(VBelow) < 130)
  1922.      Vest=VBelow+math_log(PBelow/pres)*(VAbove-VBelow)/math_log(PBelow/PAbove)
  1923.   Else
  1924.      Vest=-9999.0
  1925.   Endif

  1926. Endif

  1927. Return(Vest)

  1928. ****************************************************************************

  1929. function GetUWnd(wspd,wdir)

  1930. *------------------------
  1931. * Get x-component of wind.
  1932. *------------------------


  1933. If (wspd >= 0)
  1934.    xwind=wspd*math_cos((270-wdir)*_dtr)
  1935. Else
  1936.    xwind = -9999.0
  1937. Endif
  1938. return(xwind)

  1939. **************************************************************************

  1940. function GetVWnd(wspd,wdir)

  1941. *-----------------------
  1942. * Get y-component of wind
  1943. *------------------------

  1944. If (wspd >= 0)
  1945.    ywind=wspd*math_sin((270-wdir)*_dtr)
  1946. Else
  1947.    ywind = -9999.0
  1948. Endif
  1949. return(ywind)


  1950. *************************************************************************

  1951. function GetWSpd(xwind,ywind)


  1952. "set gxout stat"
  1953. "d mag("xwind","ywind")"
  1954. rec=sublin(result,8)
  1955. val=subwrd(rec,4)

  1956. return (val)

  1957. *************************************************************************

  1958. function GetWDir(xwind,ywind)

  1959. * Return wind direction given x and y components

  1960. "set gxout stat"
  1961. "define theta=270-"_rtd"*atan2("ywind","xwind")"
  1962. "d theta"
  1963. rec=sublin(result,8)
  1964. Dir=subwrd(rec,4)

  1965. If (Dir > 360)
  1966.    Dir=Dir-360
  1967. Endif

  1968. If (Dir < 0)
  1969.    Dir=360+Dir
  1970. Endif

  1971. return(Dir)

  1972. *************************************************************************

  1973. function GetXLoc(temp,pres)

  1974. *-------------------------------------------------
  1975. * Get x-location on skew-t based on temp, pressure
  1976. *-------------------------------------------------

  1977. xloc=(temp-_m1*math_log10(pres)-_m3)/_m2
  1978. return(xloc)

  1979. *************************************************************************

  1980. function GetTemp(xloc,pres)

  1981. *-------------------------------------------------
  1982. * Return temperature at location given by xloc,pres
  1983. *-------------------------------------------------

  1984. tempval=_m1*math_log10(pres)+_m2*xloc+_m3
  1985. return(tempval)

  1986. **************************************************************************

  1987. function GetTheta(temp,pres)         

  1988. *---------------------------------------------------
  1989. * Calculate theta for a given temperature and pressure
  1990. *---------------------------------------------------

  1991. theta=(temp+273.15)*math_pow(1000/pres,0.286)-273.15
  1992. return(theta)


  1993. *************************************************************************

  1994. function GetThet2(temp,dewp,pres)         

  1995. *---------------------------------------------------
  1996. * Calculate theta for a given temperature,dewp, and pressure
  1997. *---------------------------------------------------

  1998. tempk=273.15+temp
  1999. dewpk=273.15+dewp

  2000. es=satvap2(temp)
  2001. ws=mixratio(es,pres)

  2002. mix=10*getrh(temp,dewp,pres)*ws

  2003. exponent=0.2854*(1.0-0.00028*mix)
  2004. theta=(temp+273.15)*math_pow(1000/pres,exponent)-273.15
  2005. return(theta)

  2006. *************************************************************************

  2007. function Thetae(temp,dewp,pres)

  2008. *--------------------------------------------------------------
  2009. * Return equiv. pot. temp in Kelvin given temp, dewp in celsius
  2010. * From Bolton (1980) Mon Wea Rev
  2011. *--------------------------------------------------------------

  2012. es=satvap2(temp)
  2013. ws=mixratio(es,pres)
  2014. mix=10*getrh(temp,dewp,pres)*ws
  2015. theta=GetThet2(temp,dewp,pres)+273.15
  2016. TLcl=Templcl(temp,dewp)+273.15
  2017. thetae=theta*math_exp((3.376/TLcl-0.00254)*mix*(1.0+0.00081*mix))

  2018. return(thetae)

  2019. *************************************************************************

  2020. function GHBCAPE(sndtemp,snddewp,pinc,source,ml_depth,adiabat)

  2021. *-----------------------------------------------------------------------
  2022. *
  2023. *  GHBcape - A GrADS routine converted from a Fortran90 subroutine to
  2024. *            calculate Convective Available Potential Energy (CAPE) and
  2025. *            Convective Inhibition (CIN) from a sounding.
  2026. *
  2027. *  Version 1.02                           Fortran Last modified:  10 October 2008
  2028. *                                         GrADS version Last modified:  7 August 2009
  2029. *
  2030. *  Fortran Author:  George H. Bryan
  2031. *           Mesoscale and Microscale Meteorology Division
  2032. *           National Center for Atmospheric Research
  2033. *           Boulder, Colorado, USA
  2034. *           gbryan@ucar.edu
  2035. *
  2036. *  GrADS conversion:  Robert E. Hart
  2037. *           Florida State University
  2038. *           Department of Meteorology
  2039. *           rhart@fsu.edu
  2040. *
  2041. *  For original Fortran, please see http://www.mmm.ucar.edu/people/bryan/Code/getcape.F
  2042. *
  2043. *  Disclaimer:  This code is made available WITHOUT WARRANTY.
  2044. *
  2045. *  References:  Bolton (1980, MWR, p. 1046) (constants and definitions)
  2046. *               Bryan and Fritsch (2004, MWR, p. 2421) (ice processes)
  2047. *
  2048. *-----------------------------------------------------------------------
  2049. *
  2050. *  Options passed into function:
  2051. *
  2052. *   sndtemp            ! Array of sounding temp values in C
  2053. *
  2054. *   snddewp            ! Array of sounding dewp values in C
  2055. *
  2056. *   pinc               ! Pressure increment (Pa)
  2057. *                      ! (smaller number yields more accurate
  2058. *                      !  results,larger number makes code
  2059. *                      !  go faster)
  2060. *
  2061. *   source             ! Source parcel:
  2062. *                      ! 1 = surface
  2063. *                      ! 2 = most unstable (max theta-e)
  2064. *                      ! 3 = mixed-layer (specify ml_depth)
  2065. *
  2066. *   ml_depth           ! depth (m) of mixed layer
  2067. *                      ! for source=3
  2068. *
  2069. *   adiabat            ! Formulation of moist adiabat:
  2070. *                      ! 1 = pseudoadiabatic, liquid only
  2071. *                      ! 2 = reversible, liquid only
  2072. *                      ! 3 = pseudoadiabatic, with ice
  2073. *                      ! 4 = reversible, with ice
  2074. *
  2075. *-----------------------------------------------------------------------
  2076. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2077. *-----------------------------------------------------------------------
  2078. *            No need to modify anything below here:
  2079. *-----------------------------------------------------------------------
  2080. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2081. *-----------------------------------------------------------------------

  2082.    say "Start calculating CAPE & CIN."

  2083. * Get max k with legit temp data

  2084.    "q file"
  2085.    rec=sublin(result,5)
  2086.    nk1=subwrd(rec,9)

  2087.    "set gxout value"
  2088.    "set grads off"

  2089.    found=0
  2090.    m=0
  2091.    k=nk1
  2092.    while (k >= 1 & found = 0)
  2093.       "set z "k
  2094.       "define dd="sndtemp
  2095.       "d dd"
  2096.       temp=subwrd(result,4)
  2097.       if (math_abs(temp) < 130)
  2098.            found=1
  2099.            nk=k
  2100.       endif
  2101.       k=k-1
  2102.    endwhile


  2103. * Create script arrays of variables matching expectations of original Fortran
  2104. * Allow for missing data by:
  2105. * Data is only included at a level if
  2106. *     - Temperature and dewpoint exist with a pressure at or above 500mb
  2107. *     - Temperature data exists with a pressure below 500mb
  2108. *
  2109. * If only temperature data exists at pressures below 500mb, insert Td=T-50C

  2110.     k=1
  2111.     m=0
  2112.     while (k <= nk)
  2113.       "set z "k
  2114.       pres=subwrd(result,4)
  2115.       "define dd="sndtemp
  2116.       "d dd"
  2117.       temp=subwrd(result,4)
  2118.       "define dd="snddewp
  2119.       "d dd"
  2120.       dewp=subwrd(result,4)
  2121.       if (math_abs(temp) < 130 )
  2122.          if (math_abs(dewp) < 130)
  2123.              m=m+1
  2124.              pin.m=pres
  2125.              tin.m=temp
  2126.              tdin.m=dewp
  2127.          endif
  2128.          if (math_abs(dewp) > 130 & pres <= 500)
  2129.              m=m+1
  2130.              pin.m=pres
  2131.              tin.m=temp
  2132.              tdin.m=tin.m-50
  2133.          endif
  2134.       endif
  2135.       k=k+1
  2136.     endwhile

  2137.     nk=m

  2138. * Conversion of George Bryan's Fortran begins here
  2139. *-----------------------------------------------------------------------

  2140.     g     = 9.81
  2141.     p00   = 100000
  2142.     cp    = 1005.7
  2143.     rd    = 287.04
  2144.     rv    = 461.5
  2145.     xlv   = 2501000
  2146.     xls   = 2836017
  2147.     t0    = 273.15
  2148. *-----------------------------------------------------------------------

  2149.     g     = 9.81
  2150.     p00   = 100000
  2151.     cp    = 1005.7
  2152.     rd    = 287.04
  2153.     rv    = 461.5
  2154.     xlv   = 2501000
  2155.     xls   = 2836017
  2156.     t0    = 273.15
  2157.     cpv   = 1875
  2158.     cpl   = 4190
  2159.     cpi   = 2118.636
  2160.     lv1   = xlv+(cpl-cpv)*t0
  2161.     lv2   = cpl-cpv
  2162.     ls1   = xls+(cpi-cpv)*t0
  2163.     ls2   = cpi-cpv

  2164.     rp00  = 1/p00
  2165.     eps   = rd/rv
  2166.     reps  = rv/rd
  2167.     rddcp = rd/cp
  2168.     cpdrd = cp/rd
  2169.     cpdg  = cp/g

  2170.     converge = 0.0002

  2171.     debug_level =  0

  2172. *-----------------------------------------------------------------------

  2173. *---- convert p,t,td to mks units; get pi,q,th,thv ----!

  2174.     k=1
  2175.     while (k <= nk)
  2176.         p.k = 100.0*pin.k
  2177.         t.k = 273.15+tin.k
  2178.        td.k = 273.15+tdin.k
  2179.        pi.k = math_pow(p.k*rp00,rddcp)
  2180.         q.k = GHBgetqvs(p.k,td.k)
  2181.        th.k = t.k/pi.k
  2182.       thv.k = th.k*(1+reps*q.k)/(1.0+q.k)
  2183.       k=k+1
  2184.     endwhile

  2185. *---- get height using the hydrostatic equation ----!
  2186.     say "   Calculating height of levels"

  2187.     z.1 = 0
  2188.     k=2
  2189.     while (k <= nk)
  2190.       km=k-1
  2191.       dz = -cpdg*0.5*(thv.k+thv.km)*(pi.k-pi.km)
  2192.       z.k = z.km + dz
  2193.       k=k+1
  2194.     endwhile

  2195. *---- find source parcel ----!

  2196.   say "   Finding/calculating source parcel"

  2197.   if (source = 1)
  2198. *     use surface parcel
  2199.     kmax = 1
  2200.   endif

  2201.   if (source = 2)
  2202. *     use most unstable parcel (max theta-e)
  2203.     if (p.1 < 50000)
  2204. *     first report is above 500mb... just use the first level reported
  2205.       kmax = 1
  2206.       maxthe = GHBgetthe(p.1,t.1,td.1,q.1)
  2207.     else
  2208. *     find max theta-e below 500mb
  2209.       maxthe = 0.0
  2210.       k=1
  2211.       while (k<=nk)
  2212.         if(p.k >= 50000)
  2213.           the = GHBgetthe(p.k,t.k,td.k,q.k)
  2214.           if (the > maxthe )
  2215.             maxthe = the
  2216.             kmax = k
  2217.           endif
  2218.         endif
  2219.         k=k+1
  2220.       endwhile
  2221.     endif
  2222.     if(debug_level >= 100)
  2223.           say '  kmax,maxthe = ' kmax' 'maxthe
  2224.     endif
  2225.    endif

  2226.    if  (source = 3)
  2227. *     use mixed layer
  2228.     if ( z.2-z.1 > ml_depth )
  2229. *     the second level is above the mixed-layer depth:  just use the
  2230. *     lowest level

  2231.       avgth = th.1
  2232.       avgqv = q.1
  2233.       kmax = 1
  2234.     else
  2235.        if( z.nk < ml_depth )
  2236. *     the top-most level is within the mixed layer:  just use the
  2237. *     upper-most level
  2238.           avgth = th.nk
  2239.           avgqv = q.nk
  2240.           kmax = nk
  2241.        else
  2242. *     calculate the mixed-layer properties:
  2243.           avgth = 0.0
  2244.           avgqv = 0.0
  2245.           k = 2
  2246.           if(debug_level >= 100)
  2247.               say '  ml_depth = 'ml_depth
  2248.               say '  k,z,th,q:'
  2249.               say 1" "z.1" "th.1" "q.1
  2250.           endif

  2251.           while (z.k <= ml_depth & k<=nk)
  2252.                if(debug_level >= 100)  
  2253.                   say k" "z.k" "th.k" "q.k
  2254.                endif
  2255.                km=k-1
  2256.                avgth = avgth + 0.5*(z.k-z.km)*(th.k+th.km)
  2257.                avgqv = avgqv + 0.5*(z.k-z.km)*(q.k+q.km)
  2258.                k = k + 1
  2259.           endwhile

  2260.           km=k-1
  2261.           th2 = th.km+(th.k-th.km)*(ml_depth-z.km)/(z.k-z.km)
  2262.           qv2 = q.km+(q.k - q.km)*(ml_depth-z.km)/(z.k-z.km)

  2263.           if(debug_level >= 100)
  2264.               say 999" "ml_depth" "th2" "qv2
  2265.           endif

  2266.           avgth = avgth + 0.5*(ml_depth-z.km)*(th2+th.km)
  2267.           avgqv = avgqv + 0.5*(ml_depth-z.km)*(qv2+q.km)

  2268.           if(debug_level >= 100)
  2269.             say k" "z.k" "th.k" "q.k
  2270.           endif
  2271.           avgth = avgth/ml_depth
  2272.           avgqv = avgqv/ml_depth

  2273.           kmax = 1
  2274.         endif
  2275.       endif
  2276.      else
  2277.          if (source != 1 & source != 2 & source != 3)
  2278.             say
  2279.             say '  Unknown value for source'
  2280.             say
  2281.             say ' source = ' source
  2282.             say
  2283.             "quit"
  2284.          endif
  2285.      endif

  2286. *---- define parcel properties at initial location ----!
  2287.     narea = 0.0

  2288.   if( source = 1 | source = 2)
  2289.     k    = kmax
  2290.     th2  = th.kmax
  2291.     pi2  = pi.kmax
  2292.     p2   = p.kmax
  2293.     t2   = t.kmax
  2294.     thv2 = thv.kmax
  2295.     qv2  = q.kmax
  2296.     b2   = 0.0
  2297.   endif
  2298.   if( source = 3 )
  2299.     k    = kmax
  2300.     th2  = avgth
  2301.     qv2  = avgqv
  2302.     thv2 = th2*(1.0+reps*qv2)/(1.0+qv2)
  2303.     pi2  = pi.kmax
  2304.     p2   = p.kmax
  2305.     t2   = th2*pi2
  2306.     b2   = g*( thv2-thv.kmax )/thv.kmax
  2307.   endif

  2308.     ql2 = 0.0
  2309.     qi2 = 0.0
  2310.     qt  = qv2

  2311.     cape = 0.0
  2312.     cin  = 0.0
  2313.     lfc  = 0.0

  2314.     doit = 1     
  2315.     cloud = 0      
  2316.     if(adiabat = 1 | adiabat = 2)
  2317.       ice = 0
  2318.     else
  2319.       ice = 1
  2320.     endif


  2321.      the = GHBgetthe(p2,t2,t2,qv2)
  2322.      if(debug_level >= 100)
  2323.         say '  the = 'the
  2324.      endif

  2325. *---- begin ascent of parcel ----!
  2326.     say "   Begin ascent of parcel"

  2327.       if(debug_level >= 100)
  2328.         say '  Start loop:'
  2329.         say '  p2,th2,qv2 = 'p2" "th2" "qv2
  2330.       endif

  2331.     while( doit = 1  &  k < nk )
  2332.         k = k+1
  2333.         km=k-1
  2334.        b1 =  b2
  2335.        dp = p.km-p.k
  2336.        if( dp < pinc )
  2337.         nloop = 1
  2338.       else
  2339.         nloop = 1 + math_int( dp/pinc )
  2340.         dp = dp/nloop
  2341.       endif
  2342.       n=1
  2343.       while (n<=nloop)
  2344.          p1 =  p2
  2345.          t1 =  t2
  2346.         pi1 = pi2
  2347.         th1 = th2
  2348.         qv1 = qv2
  2349.         ql1 = ql2
  2350.         qi1 = qi2
  2351.         thv1 = thv2

  2352.         p2 = p2 - dp
  2353.         pi2 = math_pow(p2*rp00,rddcp)

  2354.         thlast = th1
  2355.         i = 0
  2356.         not_converged = 1

  2357.        while( not_converged = 1)
  2358.           i = i + 1
  2359.           t2 = thlast*pi2
  2360.           if (ice = 1)
  2361.             val1=GHBmin((t2-233.15)/(273.15-233.15),1.0)
  2362.             fliq = GHBmax(val1,0.0)
  2363.             fice = 1.0-fliq
  2364.           else
  2365.             fliq = 1.0
  2366.             fice = 0.0
  2367.           endif
  2368.           val1=GHBgetqvs(p2,t2)
  2369.           val2=GHBgetqvi(p2,t2)
  2370.           val3=fliq*val1+fice*val2
  2371.           qv2 = GHBmin(qt,val3)
  2372.           val4=fice*(qt-qv2)
  2373.           qi2 = GHBmax( val4 , 0 )
  2374.           val5=qt-qv2-qi2
  2375.           ql2 = GHBmax(val5 , 0 )

  2376.           tbar  = 0.5*(t1+t2)
  2377.           qvbar = 0.5*(qv1+qv2)
  2378.           qlbar = 0.5*(ql1+ql2)
  2379.           qibar = 0.5*(qi1+qi2)

  2380.           lhv = lv1-lv2*tbar
  2381.           lhs = ls1-ls2*tbar
  2382.           lhf = lhs-lhv


  2383.           rm=rd+rv*qvbar
  2384.           cpm=cp+cpv*qvbar+cpl*qlbar+cpi*qibar
  2385.           th2=th1*math_exp(lhv*(ql2-ql1)/(cpm*tbar)+lhs*(qi2-qi1)/(cpm*tbar)+(rm/cpm-rd/cp)*math_log(p2/p1) )
  2386.           if(i > 90)
  2387.              say i" "th2" "thlast" "th2-thlast
  2388.           endif
  2389.           if(i >  100)
  2390.             say
  2391.             say '  Error:  lack of convergence'
  2392.             say
  2393.             say '  ... stopping iteration '
  2394.             say
  2395.             "quit"
  2396.           endif
  2397.           if( math_abs(th2-thlast) > converge )
  2398.             thlast=thlast+0.3*(th2-thlast)
  2399.           else
  2400.             not_converged = 0
  2401.           endif
  2402.         endwhile

  2403. *       Latest pressure increment is complete.  Calculate some
  2404. *       important stuff:

  2405.         if (ql2 >= 1.0e-10)
  2406.             cloud=1
  2407.         endif
  2408.         
  2409.         if (adiabat = 1 | adiabat = 3)
  2410.              qt=qv2
  2411.              ql2=0
  2412.              qi2=0
  2413.         endif
  2414.         if (adiabat <= 0 | adiabat >= 5)
  2415.              say
  2416.              say "Undefined adiabat."
  2417.              say
  2418.              "quit"
  2419.         endif
  2420.         n=n+1

  2421.         endwhile

  2422.         km=k-1
  2423.         thv2 = th2*(1.0+reps*qv2)/(1.0+qv2+ql2+qi2)
  2424.         b2 = g*( thv2-thv.k )/thv.k
  2425.         dz = -cpdg*0.5*(thv.k+thv.km)*(pi.k-pi.km)

  2426.         the = GHBgetthe(p2,t2,t2,qv2)

  2427. *     Get contributions to CAPE and CIN:


  2428.       chk=0
  2429.       if( b2 >= 0 & b1 < 0)
  2430.         chk=1
  2431.         ps = p.km+(p.k-p.km)*(0-b1)/(b2-b1)
  2432.         frac = b2/(b2-b1)
  2433.         parea =  0.5*b2*dz*frac
  2434.         narea = narea-0.5*b1*dz*(1.0-frac)
  2435.         if(debug_level >= 200)
  2436.           say '      b1,b2 = 'b1" "b2
  2437.           say '      p1,ps,p2 = 'p.km" "ps" "p.k
  2438.           say '      frac = 'frac
  2439.           say '      parea = 'parea
  2440.           say '      narea = 'narea
  2441.         endif
  2442.         cin  = cin  + narea
  2443.         narea = 0.0
  2444.       endif
  2445.       if (chk = 0 & b2 < 0 &  b1 > 0)
  2446.           chk=2
  2447.           km=k-1
  2448.           ps = p.km+(p.k-p.km)*(0.0-b1)/(b2-b1)
  2449.           frac = b1/(b1-b2)
  2450.           parea =  0.5*b1*dz*frac
  2451.           narea = -0.5*b2*dz*(1.0-frac)
  2452.           if(debug_level >= 200)
  2453.               say '      b1,b2 = 'b1' 'b2
  2454.               say '      p1,ps,p2 = 'p.km' 'ps' 'p.k
  2455.               say '      frac = 'frac
  2456.               say '      parea = 'parea
  2457.               say '      narea = 'narea
  2458.            endif
  2459.        endif
  2460.       if (chk = 0 &  b2 < 0)
  2461.         chk=3
  2462.         parea =  0.0
  2463.         narea = narea-0.5*dz*(b1+b2)
  2464.       endif
  2465.       if (chk = 0)
  2466.         parea =  0.5*dz*(b1+b2)
  2467.         narea =  0.0
  2468.       endif
  2469.       cape = cape + GHBmax(0,parea)

  2470.       if( p.k <= 10000 & b2 < 0)
  2471.         doit = 0
  2472.       endif
  2473. endwhile

  2474. *---- All done ----!

  2475. say "Done calculating CAPE & CIN."

  2476. return(cape" "cin)

  2477. *-----------------------------------------------------------------------
  2478. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2479. *-----------------------------------------------------------------------

  2480. function GHBmax(val1,val2)

  2481. if (val1 > val2)
  2482.     maxval=val1
  2483. else
  2484.     maxval=val2
  2485. endif
  2486. return(maxval)

  2487. *-----------------------------------------------------------------------
  2488. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2489. *-----------------------------------------------------------------------

  2490. function GHBmin(val1,val2)

  2491. if (val1 < val2)
  2492.     minval=val1
  2493. else
  2494.     minval=val2
  2495. endif
  2496. return(minval)

  2497. *-----------------------------------------------------------------------
  2498. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2499. *-----------------------------------------------------------------------

  2500. function GHBgetqvs(p,t)

  2501. eps=287.04/461.5
  2502. es = 611.2*math_exp(17.67*(t-273.15)/(t-29.65))
  2503. val = eps*es/(p-es)
  2504. return(val)

  2505. *-----------------------------------------------------------------------
  2506. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2507. *-----------------------------------------------------------------------

  2508. function GHBgetqvi(p,t)

  2509. eps=287.04/461.5
  2510. es = 611.2*math_exp(21.8745584*(t-273.15)/(t-7.66))
  2511. val = eps*es/(p-es)
  2512. return(val)

  2513. *-----------------------------------------------------------------------
  2514. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2515. *-----------------------------------------------------------------------

  2516. function GHBgetthe(p,t,td,q)

  2517. if( td-t >= -0.1 )
  2518.       tlcl = t
  2519. else
  2520.       tlcl = 56.0 + 1/(1/(td-56.0) + 0.00125*math_log(t/td))
  2521. endif

  2522. tthe=t*math_pow(100000.0/p,(0.2854*(1.0-0.28*q)))*math_exp(((3376.0/tlcl)-2.54)*q*(1.0+0.81*q))

  2523. return(tthe)


  2524. *-----------------------------------------------------------------------
  2525. *ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
  2526. *-----------------------------------------------------------------------
复制代码
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-6-24 11:09:58 | 显示全部楼层

是用你给的代码吗?大神
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表