;
; In this example, constant data specify irregular tick mark function. 
; These data are the dry adiabatic lapse rate for constant Delet T 
; and standard atmosphere.
;
stnd_pres = (/ 1000, 939.411, 881.493, 826.175, 773.384, 723.05, 675.103 \
, 629.473, 586.092, 544.892, 505.805, 468.766, 433.707, 400.564, 369.273, \
339.769, 311.991, 285.875, 261.362, 238.389, 216.897, 196.828, 178.122, \
160.724, 144.575, 129.622, 115.808, 103.08, 91.3853, 80.6712, 70.8867, 61.9816,\
53.9064, 46.6129, 40.0537, 34.1825, 28.9539, 24.3237, 20.2486, 16.6867 /)

;
; Sort x and move coresponding values of y into
; the same locations.
; 
procedure sortit(
	x[*]:float, 	; x coordinate field that gets sorted
	y[*]:float	; corresponding y coordinate
) 
local 
	ind, 		; temporary value that holds index of value to be swapped
	k, 		; index variable
	p, 		; index variable
	tmp		; temporary value used to swap individual values
begin
	ind = 0
	do k = 0, dimsizes(x) - 2 
		ind = k
		if(.not. ismissing(x(ind))) then
			do p = k + 1, dimsizes(x) - 1
				if((.not. ismissing(x(p))).and.(x(ind) .ge. x(p))) then
					ind = p
				end if
			end do
			if(ind .ne. k) then
				tmp = x(ind)
				x(ind) = x(k)
				x(k) = tmp
				tmp = y(ind)
				y(ind) = y(k)
				y(k) = tmp
			end if
		end if
	end do
end 

;
; Take two sets of sorted (x,y) arrays and merge them into a single
; ordered set of x, y pairs. 
;
procedure merge(
	x[*]:numeric,		; data to be merged with x1
	y[*]:numeric,		; corresponding y coordinate for x
	x1[*]:numeric,		; data to be merged with x
	y1[*]:numeric,		; corresponding y coordinate for y1
	xout[*]:numeric,	; output array containing merge of x and x1
	yout[*]:numeric,	; output array of corresponding coordinates
	size_man:integer,	; size of x and y
	size_sig:integer	; size of x1 and y1
)
local 
	m,	; index variable
	l,	; index variable
	done,	; set to one when done merging
	j	; index variable
begin
	m = 0
	l = 0
	done = 0
	j = 0
	do while(done .eq. 0)
;
; Example of "lazy" conditional expression evaluation. First, the array 
; index is checked to see if it is in range; if True, then the portion
; of the conditional expression that derefences the array is used.
;

		if(((l.ge.size_man).or.(.not.ismissing(y(l)))).and.((m.ge.size_sig).or.(.not.ismissing(y1(m)))))
			if((m .lt. size_sig) .and. (l .lt. size_man)) then
				if(y(l) .lt. y1(m)) then
					xout(j) = x(l)
					yout(j) = y(l)
					l = l + 1
				else 
					if(y(l) .gt. y1(m)) then
						xout(j) = x1(m)
						yout(j) = y1(m)
						m = m+1
					else
						xout(j) = x(l)
						yout(j) = y(l)
						l = l + 1
						j = j + 1
						xout(j) = x1(m)
						yout(j) = y1(m)
						m = m + 1
					end if
				end if
			else 
				if(m .lt.  size_sig) then
					xout(j) = x1(m)
					yout(j) = y1(m)
					m = m + 1
				else 
					xout(j) = x(l)
					yout(j) = y(l)
					l = l + 1
				end if
			end if
			j = j + 1
		else
			if(l.lt.size_man)
				if(ismissing(y(l)))
					l = l +1 
				end if
			end if
			if(m.lt.size_sig)
				if(ismissing(y1(m)))
					m = m +1 
				end if
			end if
		end if
		if((l.eq.size_man).and.(m.eq.size_sig))
			done = 1
		end if
	end do
end

;
; Open data file containing upper-air radiosonde data.
;
a =  addfile("data/94082512_upa.cdf","r")

;
; Create an X workstation.
;
x = create "x" xWorkstationLayerClass noparent end create

;
; Search for Denver report.
;
do i = 0,dimsizes(a->id(:,0)) 
	if(a->id(i,:) .eq. "DEN" ) then
		break
	end if
end do

; 
; Read in samples for temperature and pressure at mandatory pressure values
; for the Denver upper-air report and sort it by pressure.
;
P_man = a->P_man(i,:)
T_man = a->T_man(i,:)
sortit(P_man,T_man)

;
; Read in samples for temperature and pressure at significant levels for
; the Denver upper-air report and sort it by pressure.
;
P_sigt = a->P_sigt(i,:)
T_sigt = a->T_sigt(i,:)
sortit(P_sigt,T_sigt)


; 
; Read in samples for dewpoint and pressure at mandatory pressure values
; for the Denver upper-air report and sort it by pressure.
;
P_man2 = a->P_man(i,:)
TD_man = a->TD_man(i,:)
sortit(P_man2,TD_man)

;
; Read in samples for dewpoint and pressure at significant levels for
; the Denver upper-air report and sort it by pressure.
;
P_sigt2 = a->P_sigt(i,:)
TD_sigt = a->TD_sigt(i,:)
sortit(P_sigt2,TD_sigt)
size_man = dimsizes(P_man)
size_sig = dimsizes(P_sigt)

;
; Create variables to contain data after merging by the merge procedure.
;
tmp_t = new(size_man + size_sig , float)
tmp_p = new(size_man + size_sig , float)
tmp_p2 = new(size_man + size_sig , float)
tmp_td = new(size_man + size_sig , float)

;
; Call the merge function to merge the two sorted lists of pressure temperature
; pressure pairs into a single list.
;
merge(T_man,P_man,T_sigt,P_sigt,tmp_t,tmp_p,size_man,size_sig)

; 
; Do same for dewpoint.
;
merge(TD_man,P_man2,TD_sigt,P_sigt2,tmp_td,tmp_p2,size_man,size_sig)

;
; Create data object with merged dewpoint profile.
;
mydatatd = create "mydatatd" coordArraysLayerClass noparent
	"caXArray" : tmp_td
	"caYArray" : tmp_p2
	"caXMissingV" : tmp_td@_FillValue
	"caYMissingV" : tmp_p2@_FillValue 
end create

;
; Create data object with just mandatory dewpoint samples.
;
mydata1td = create "mydata1td" coordArraysLayerClass noparent
	"caYArray" : P_man2
	"caXArray" : TD_man
	"caXMissingV" : P_man2@_FillValue
	"caYMissingV" : TD_man@_FillValue
end create

;
; Create data object with just significant dewpoint samples.
;
mydata2td = create "mydata2td" coordArraysLayerClass noparent
	"caYArray" : P_sigt2
	"caXArray" : TD_sigt
	"caXMissingV" : P_sigt2
	"caYMissingV" : TD_sigt
end create

;
; Create a data-dependent object for the merged dewpoint set and assign
; the data-dependent line color resource.
;
xy_deptd = create "xy_deptd" xyDataDepLayerClass noparent
	"dsDataItem" : mydatatd
	"xyColor" : 80
end create

;
; Create a data-dependent object for the mandatory dataset and configure
; it to draw the data with markers only, essentially overlaying this
; on top of the above line.
;
xy_dep1td = create "xy_dep1td" xyDataDepLayerClass noparent
	"dsDataItem" : mydata1td
	"xyMarker" : 3
	"xyMarkerMode": "MARKERSONLY"
	"xyMarkerColor": 50
	"xyMarkerSizeF": .015
end create

;
; Do the same thing with significant-level data, but change the 
; color and marker index.
;
xy_dep2td = create "xy_dep2td" xyDataDepLayerClass noparent
	"dsDataItem" : mydata2td
	"xyMarker" : 6
	"xyMarkerMode": "MARKERSONLY"
	"xyMarkerColor": 20
	"xyMarkerSizeF": .015
end create


;
; Create data objects out of temperature profiles in the same way that
; the dewpoint objects were created.
;
mydata = create "mydata" coordArraysLayerClass noparent
	"caXArray" : tmp_t
	"caYArray" : tmp_p
	"caXMissingV" : tmp_t@_FillValue
	"caYMissingV" : tmp_p@_FillValue
end create
mydata1 = create "mydata1" coordArraysLayerClass noparent
	"caYArray" : P_man
	"caXArray" : T_man
	"caXMissingV" : T_man@_FillValue
	"caYMissingV" : P_man@_FillValue
end create
mydata2 = create "mydata2" coordArraysLayerClass noparent
	"caYArray" : P_sigt
	"caXArray" : T_sigt
	"caXMissingV" : T_sigt@_FillValue
	"caYMissingV" : P_sigt@_FillValue
end create

;
; Configure main merged array to draw a line and the individual mandatory
; and significant sample to draw markers only
;
xy_dep = create "xy_dep" xyDataDepLayerClass noparent
	"dsDataItem" : mydata
	"xyColor" : 20
end create
xy_dep1 = create "xy_dep1" xyDataDepLayerClass noparent
	"dsDataItem" : mydata1
	"xyMarker" : 3
	"xyMarkerMode": "MARKERSONLY"
	"xyMarkerColor": 50
	"xyMarkerSizeF": .015
end create
xy_dep2 = create "xy_dep2" xyDataDepLayerClass noparent
	"dsDataItem" : mydata2
	"xyMarker" : 6
	"xyMarkerMode": "MARKERSONLY"
	"xyMarkerColor": 20
	"xyMarkerSizeF": .015
end create

;
; Main XyPlot object with all six data-dependent objects assigned
; to it. The data transformation is configured to be an irregular transformation
; in the y axis by assigning the stnd_pres variable to the xyIrregularPoints
; resource.
;
xy_plot = create "xy_plot" xyPlotLayerClass x
	"vpXF" : .15
	"vpYF" : .85
	"vpWidthF" : .7
	"vpHeightF" : .7
	"xyCurveData" : (/xy_dep,xy_dep1,xy_dep2,xy_deptd,xy_dep1td,xy_dep2td/)
	"tiMainOn" : "True"
	"tiXAxisOn" : "True"
	"tiYAxisOn" : "True"
	"xyYReverse" : "True"
	"xyYIrregularPoints" : stnd_pres
	"xyYStyle" : "IRREGULAR"
	"xyYMinF" : 17.00
	"xyYMaxF" : 1000.0
	"xyXMinF" : -80.0
	"xyXMaxF" : 30.0
	"tiXAxisString" : "Temperature"
	"tiYAxisString" : "Pressure"
	"tiMainString" : "Stuve Plot"
	"tiXAxisFont" : "courier"
	"tiYAxisFont" : "courier"
	"tiMainFont" : "courier"
end create

;
; Draw the whole thing.
;
draw(xy_plot)
update(x)