C Program to take weather grids and group weather types together for basic 
C weather products. 
C First, The grids must be decoded useing the local tables for each grid time.
C Group together certain types of weather to put into grid
C
C log:
C	c. Bailey/HPC	8/28/09		Modify to assign a constant number
C					to a Wx string
C       C. Bailey/WPC   8/2014          Modify to 2.5km
C
C Ugly String:
C	Areal Coverage:WX Type:Intensity:Visibility:Attributes
C**********************************************************************
C Coverage Key
C       #       Ugly string ID          Name			Prob
C	0	<NoCov>			No coverage
C	1	Iso			Isolated		Low
C	2	Sct			Scattered		Low
C	3	Num			Numerous		High
C	4	Wide			Widespread		High
C	5	Ocnl			Occasional		High
C	6	SChc			Slight Chance of	Low
C	7	Chc			Chance of		Low
C	8	Lkly			Likely			High
C	9	Def			Definite		High
C	10	Patchy			Patchy			Low
C	11	Areas			Areas of		High
C	12	Pds			Periods of
C	13	Frq			Frequent
C	14	Inter			Intermittent
C	15	Brf			Brief
C**********************************************************************
C WX TYPE Key
C	#	Ugly string ID		Name
C	0	<NoWx>			no Weather
C	1	T			Thunderstorm
C	2	A			Hail
C	3	R			Rain
C	4	RW			Rain Showers
C	5	L			Drizzle
C	6	ZR			Frezzing Rain
C	7	ZL			Freezing Drizzle
C	8	S			Snow
C	9	SW			Snow Showers
C	10	IP			Ice Pellets
C	11	F			Fog
C	12	H			haze
C	13	BS			Blowing Snow
C	14	K			Snoke
C	15	BD			Blowing Dust
C	16	FR			Frost
C**********************************************************************
C Intensity Key
C	#	Ugly string ID          Name
C	0	<NoInten>		No Intensity
C	1	--			Very Light
C	2	-			Light
C	3	m			Moderate
C	4	+			Heavy
C**********************************************************************
C Visibility key
C       #       Ugly string ID          Name
C	0	<NoVis>			No Visibility
C	1	0SM			0 Miles
C	2	1/4SM			1/4 Miles
C	3	1/2SM			1/2 Miles
C	4	3/4SM			3/4 Miles
C	5	1SM			1 Mile
C	6	11/2SM			1 1/2 Miles
C	7	2SM			2 Miles
C	8	21/2SM			2 1/2 Miles
C	9	3SM			3 Miles
C	10	4SM			4 Miles
C	11	5SM			5 Miles
C	12	6SM			6 Miles
C	13	P6SM			Greater than 6 Miles
C**********************************************************************
C Simplified Number coding (2 Probabilitys)
C	#	Wx	Prob	Wx Category
C	0			No WX
C	1	L	Low	Rain
C	2	R-	Low	Rain
C	3	R	Low	Rain
C	4	R+	Low	Rain
C	5	R/T	Low	Rain 
C	6	RW	Low	Rain
C       7       RW/T    Low     Rain
C       8	T       Low     Rain
C       9       L       High    Rain
C       10      R-      High    Rain 
C       11      R       High    Rain
C       12      R+      High    Rain
C       13      R/T     High    Rain 
C       14      RW      High    Rain
C       15      RW/T    High    Rain
C       16      T       High	Rain
C	17	T+		Severe Storms
C	18	R/S	Low	Mix
C	19	RW/SW	Low	Mix
C	20	R/IP	Low	Mix
C	21	S/IP	Low	Mix
C	22      R/S     High    Mix
C       23      RW/SW   High    Mix
C       24      R/IP    High    Mix
C       25      S/IP    High    Mix
C	26	IP-	Low	Ice
C	27	IP	Low	Ice
C	28	IP+	Low	Ice
C	29	ZL	Low	Ice
C	30	ZL/R	Low	Ice
C	31	ZR-	Low	Ice
C	32	ZR	Low     Ice
C	33	ZR+	Low	Ice
C	34	ZR/R	Low	Ice
C	35	ZR/IP	Low	Ice
C       36      IP-     High    Ice
C       37      IP      High    Ice
C       38      IP+     High    Ice
C       39      ZL      High    Ice
C       40      ZL/R    High    Ice
C       41      ZR-     High    Ice
C       42      ZR      High    Ice 
C       43      ZR+     High    Ice
C       44      ZR/R    High    Ice
C	45	ZR/IP   High    Ice
C	46	SW	Low	Snow
C	47	S-	Low	Snow
C	48	S	Low	Snow
C	49	S+	Low	Snow
C       50      SW      High    Snow
C       51      S-      High    Snow
C       52      S       High    Snow
C       53      S+      High    Snow
C	54	F		Fog
C	55	H		Haze
C	56	K		Smoke
C	57	BS		Blowing Snow
C	58	BD		Blowing Dust
C**********************************************************************

	INCLUDE         'GEMPRM.PRM'

       parameter(ix = 2145, iy = 1377)
              
       character dattim*6, hour*2, fhour*3, gridfile*50, parm*5
       character ugly_strings(250)*100, filename*24, gdattim(2)*15
       
       character*10 wxtype(250), coverage(250), intensity(250)
       character*10 wxtype2(250), coverage2(250), intensity2(250)

	real iwxtype(250), icov(250), iintes(250)
	real iwxtype2(250), icov2(250), iintes2(250)

       integer num_strings, e, level(2), ivcord, igx, igy, MAXGRD
       integer iret, igdfln, IANLSZ, NAVSZ, IHDRSZ, pass, old_j, i, j
	integer num

       real RNVBLK(256), ANLBLK(256), IGHDR(256)
       
       real wx(ix,iy)
       real pop(ix,iy)
       
       real wx2(ix,iy)
       real rainlow(ix,iy)
       real rainhi(ix,iy)
       real snowlow(ix,iy)
       real snowhi(ix,iy)
       real thunder(ix,iy)
       real sleethi(ix,iy)
       real sleetlow(ix,iy)
       real mixlow(ix,iy)
       real mixhi(ix,iy)
       real svrtsm(ix,iy)

       logical rain(250), REWRIT /.true./, wrtflg /.true./
       logical rain_bkn(250), rainsct(250), snow(250) 
       
	character ustring(2)*40, ustr1(5)*10, ustr2(5)*10

       do 45 i = 1, 250,1 
           rain(i) = .false.
           snow(i) = .false.
           rain_bkn(i) = .false.
           rainsct(i) = .false.
 45    continue
  
c read in date/time, hour and forecast variables for use in program
      
       call getarg(1,dattim)
       call getarg(2,hour)
       call getarg(3,fhour)
       
c Open Local Use Table
       
       gdattim(1) = dattim//"/"//hour//"00F"//fhour
       gdattim(2) = ''
       
       write(*,*)  gdattim(1)
       
       filename = "local_"//dattim//hour//"00F"//fhour//".txt"
       write(*,*) filename
       open(1, file=filename)

c The ugly string corresponds to i-1
       
       num_strings = 0
       
       do 100 i=1, 250, 1
          num_strings = num_strings + 1
          read(1,30,iostat=e) ugly_strings(i)
 30       format(6x, a100)
          if(e .ne. 0) then
              go to 110
          end if 
 100   continue
 
c Group types together
 110   do 120 i = 1, num_strings, 1
C	   Sperate Ugly String by "^"
	   CALL ST_CLST (ugly_strings(i), '^', " ", 2, ustring, num, 
     +								iret)

C!	   Sperate wx string into components

	   CALL ST_CLST (ustring(1), ':', " ", 5, ustr1, num, iret)
	   CALL ST_CLST (ustring(2), ':', " ", 5, ustr2, num, iret)
C
C!	Assign Numeric Value to Wx Type key from Ugly String
C
	   if (ustr1(2) .eq. "<noWx>") then
		iwxtype(i) = 0
	   else if (ustr1(2) .eq. "T") then
		iwxtype(i) = 1
           else if (ustr1(2) .eq. "A") then 
                iwxtype(i) = 2
           else if (ustr1(2) .eq. "R") then 
                iwxtype(i) = 3
           else if (ustr1(2) .eq. "RW") then 
                iwxtype(i) = 4
           else if (ustr1(2) .eq. "L") then 
                iwxtype(i) = 5
           else if (ustr1(2) .eq. "ZR") then 
                iwxtype(i) = 6
           else if (ustr1(2) .eq. "ZL") then 
                iwxtype(i) = 7
           else if (ustr1(2) .eq. "S") then 
                iwxtype(i) = 8
           else if (ustr1(2) .eq. "SW") then 
                iwxtype(i) = 9
           else if (ustr1(2) .eq. "IP") then 
                iwxtype(i) = 10
           else if (ustr1(2) .eq. "F") then 
                iwxtype(i) = 11
           else if (ustr1(2) .eq. "H") then
                iwxtype(i) = 12 
           else if (ustr1(2) .eq. "BS") then
                iwxtype(i) = 13 
           else if (ustr1(2) .eq. "K") then
                iwxtype(i) = 14 
           else if (ustr1(2) .eq. "BD") then
                iwxtype(i) = 15 
           else if (ustr1(2) .eq. "FR") then
                iwxtype(i) = 16 
	   else
		iwxtype(i) = 0
	   endif
           if (ustr2(2) .eq. "<noWx>") then
                iwxtype2(i) = 0
           else if (ustr2(2) .eq. "T") then
                iwxtype2(i) = 1
           else if (ustr2(2) .eq. "A") then
                iwxtype2(i) = 2
           else if (ustr2(2) .eq. "R") then
                iwxtype2(i) = 3
           else if (ustr2(2) .eq. "RW") then
                iwxtype2(i) = 4
           else if (ustr2(2) .eq. "L") then
                iwxtype2(i) = 5
           else if (ustr2(2) .eq. "ZR") then
                iwxtype2(i) = 6
           else if (ustr2(2) .eq. "ZL") then
                iwxtype2(i) = 7
           else if (ustr2(2) .eq. "S") then
                iwxtype2(i) = 8 
           else if (ustr2(2) .eq. "SW") then
                iwxtype2(i) = 9
           else if (ustr2(2) .eq. "IP") then
                iwxtype2(i) = 10
           else if (ustr2(2) .eq. "F") then
                iwxtype2(i) = 11
           else if (ustr2(2) .eq. "H") then
                iwxtype2(i) = 12
           else if (ustr2(2) .eq. "BS") then
                iwxtype2(i) = 13
           else if (ustr2(2) .eq. "K") then
                iwxtype2(i) = 14
           else if (ustr2(2) .eq. "BD") then
                iwxtype2(i) = 15
           else if (ustr2(2) .eq. "FR") then
                iwxtype2(i) = 16
           else
                iwxtype2(i) = 0
           endif

	   if (ustr1(1) .eq. "<NoCov>") then
		icov(i) = 0
	   else if (ustr1(1) .eq. "Chc") then
		icov(i) = 7
           else if (ustr1(1) .eq. "SChc") then 
                icov(i) = 6
           else if (ustr1(1) .eq. "Sct") then 
                icov(i) = 2
           else if (ustr1(1) .eq. "Lkly") then 
                icov(i) = 8
           else if (ustr1(1) .eq. "Num") then 
                icov(i) = 3
           else if (ustr1(1) .eq. "Def") then 
                icov(i) = 9
           else if (ustr1(1) .eq. "Iso") then 
                icov(i) = 1
           else if (ustr1(1) .eq. "Pds") then 
                icov(i) = 12
           else if (ustr1(1) .eq. "Ocnl") then
                icov(i) = 5
           else if (ustr1(1) .eq. "Patchy") then   
                icov(i) = 10
           else if (ustr1(1) .eq. "Areas") then
                icov(i) = 11 
           else if (ustr1(1) .eq. "Wide") then
                icov(i) = 4
           else if (ustr1(1) .eq. "Frq") then
                icov(i) = 13
           else if (ustr1(1) .eq. "Inter") then
                icov(i) = 14 
           else if (ustr1(1) .eq. "Brf") then
                icov(i) = 15
	   else
		icov(i) = 0
	   endif
           if (ustr2(1) .eq. "<NoCov>") then
                icov2(i) = 0
           else if (ustr2(1) .eq. "Chc") then
                icov2(i) = 7
           else if (ustr2(1) .eq. "SChc") then
                icov2(i) = 6
           else if (ustr2(1) .eq. "Sct") then
                icov2(i) = 2
           else if (ustr2(1) .eq. "Lkly") then
                icov2(i) = 8
           else if (ustr2(1) .eq. "Num") then
                icov2(i) = 3
           else if (ustr2(1) .eq. "Def") then
                icov2(i) = 9
           else if (ustr2(1) .eq. "Iso") then
                icov2(i) = 1
           else if (ustr2(1) .eq. "Pds") then
                icov2(i) = 12
           else if (ustr2(1) .eq. "Ocnl") then
                icov2(i) = 5
           else if (ustr2(1) .eq. "Patchy") then
                icov2(i) = 10
           else if (ustr2(1) .eq. "Areas") then
                icov2(i) = 11
           else if (ustr2(1) .eq. "Wide") then
                icov2(i) = 4
           else if (ustr2(1) .eq. "Frq") then
                icov2(i) = 13
           else if (ustr2(1) .eq. "Inter") then
                icov2(i) = 14
           else if (ustr2(1) .eq. "Brf") then
                icov2(i) = 15
           else
                icov2(i) = 0
           endif
C
C!	INTENSITY
C
	   if (ustr1(3) .eq. "<NoInten>") then
                iintes(i) = 0
	   else if (ustr1(3) .eq. "--" ) then
                iintes(i) = 1
	   else if (ustr1(3) .eq. "-" ) then
                iintes(i) = 2
	   else if (ustr1(3) .eq. "m" ) then
                iintes(i) = 3
	   else if (ustr1(3) .eq. "+" ) then
                iintes(i) = 4
	   else
		iintes(i) = 0
	   endif 

           if (ustr2(3) .eq. "<NoInten>") then
                iintes2(i) = 0
           else if (ustr2(3) .eq. "--" ) then
                iintes2(i) = 1
           else if (ustr2(3) .eq. "-" ) then
                iintes2(i) = 2
           else if (ustr2(3) .eq. "m" ) then
                iintes2(i) = 3
           else if (ustr2(3) .eq. "+" ) then
                iintes2(i) = 4
           else 
                iintes2(i) = 0
           endif

 120   continue                    
           
c Open grid file
  
       call IN_BDTA(iret)
	call GD_INIT(iret)

       gridfile = "ndfd_2p5.grd"
       
       call GD_OPNF(gridfile, WRTFLG, IGDFLN, NAVSZ, RNVBLK,    
     +         ANLSZ, ANLBLK, IHDRSZ, MAXGRD, IRET) 
      
       if(iret .ne. 0) then
          write(*,*) filename,' did not open', iret
       endif

c read in WX GRIDS
    
       ivcord = 0
       level(1) = 0
       level(2) = -1
       parm='WXNM'
       call GD_RDAT ( IGDFLN, GDATTIM, level, ivcord, PARM, wx, IGX,     
     +           IGY, IGHDR,  IRET )   
      
       if(iret .ne. 0) then
          write(*,*) 'Grid did not get retreved', iret
       endif 

c Read in 12-Hour POP
       
       ivcord = 0
       level(1) = 0
       level(2) = -1
       parm='PP12'
     
       call GD_RDAT ( IGDFLN, GDATTIM, level, ivcord, PARM, pop, IGX,     
     +           IGY, IGHDR,  IRET )   
      
       if(iret .ne. 0) then
          write(*,*) 'Grid did not get retreved', iret
       endif 
       
       do i = 1, ix, 1
           do j = 1, iy, 1
	       wx2(i,j) = -9999
	   enddo
       enddo          
  
c LOOP THROUGH wx(i,j) grid AND PLACE VALUE FOR EACH GROUP     
  
       do 500 i = 1, ix, 1
           do 510 j = 1, iy, 1
c Check 1st WX Key for rain (R)       
    	        if (wx(i,j) .eq. -9999) goto 510
	        if (icov(wx(i,j)+1) .eq. 6 .or. 
     +              icov(wx(i,j)+1) .eq. 1 ) then
		    wx2(i,j) = 0
		    goto 510
		endif
		if( iwxtype(wx(i,j)+1) .eq. 3 ) then
		    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
			if (iwxtype2(wx(i,j)+1) .eq. 8 .or.
     +                      iwxtype2(wx(i,j)+1) .eq. 9 ) then
			    wx2(i,j) = 18
			else if (iwxtype2(wx(i,j)+1) .eq. 6) then
			    wx2(i,j) = 34
			else if (iwxtype2(wx(i,j)+1) .eq. 10) then
                            wx2(i,j) = 20
			else if (iwxtype2(wx(i,j)+1) .eq. 7) then
                            wx2(i,j) = 30
			else if (iwxtype2(wx(i,j)+1) .eq. 1) then
                            wx2(i,j) = 5
			else
			    if (iintes(wx(i,j)+1) .eq. 1 ) then
				wx2(i,j) = 2
			    else if (iintes(wx(i,j)+1) .eq. 2 ) then
                                wx2(i,j) = 2
			    else if (iintes(wx(i,j)+1) .eq. 4 ) then
                                wx2(i,j) = 4
			    else
				wx2(i,j) = 3
			    endif
			endif
		    else
			if (iwxtype2(wx(i,j)+1) .eq. 8 .or.
     +                      iwxtype2(wx(i,j)+1) .eq. 9 ) then
                            wx2(i,j) = 22
                        else if (iwxtype2(wx(i,j)+1) .eq. 6) then
                            wx2(i,j) = 44
                        else if (iwxtype2(wx(i,j)+1) .eq. 10) then
                            wx2(i,j) = 24
                        else if (iwxtype2(wx(i,j)+1) .eq. 7) then
                            wx2(i,j) = 40
                        else if (iwxtype2(wx(i,j)+1) .eq. 1) then
                            wx2(i,j) = 13
                        else
			    if (iintes(wx(i,j)+1) .eq. 1 ) then
                                wx2(i,j) = 10
                            else if (iintes(wx(i,j)+1) .eq. 2 ) then
                                wx2(i,j) = 10
                            else if (iintes(wx(i,j)+1) .eq. 4 ) then
                                wx2(i,j) = 12
                            else
                                wx2(i,j) = 11
                            endif
                        endif
		    endif
		else if (iwxtype(wx(i,j)+1) .eq. 4 ) then
		    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 1 ) then
                            wx2(i,j) = 7
                        else if (iwxtype2(wx(i,j)+1) .eq. 9) then
                            wx2(i,j) = 19
                        else 
			    wx2(i,j) = 6
			endif
		    else
			if (iwxtype2(wx(i,j)+1) .eq. 1 ) then
                            wx2(i,j) = 15
                        else if (iwxtype2(wx(i,j)+1) .eq. 9) then
                            wx2(i,j) = 23
                        else 
                            wx2(i,j) = 14
                        endif
		    endif
		else if (iwxtype(wx(i,j)+1) .eq. 5 ) then 
                    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 7 ) then
                            wx2(i,j) = 29
                        else 
                            wx2(i,j) = 1
                        endif
                    else
                        if (iwxtype2(wx(i,j)+1) .eq. 7 ) then
                            wx2(i,j) = 40
                        else  
                            wx2(i,j) = 9
                        endif
                    endif
		else if (iwxtype(wx(i,j)+1) .eq. 7 ) then
                    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 30
                        else
                            wx2(i,j) = 39
                        endif
                    else
                        if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 40
                        else
                            wx2(i,j) = 39
                        endif
                    endif
		else if (iwxtype(wx(i,j)+1) .eq. 6 ) then
                    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 34
			else if (iwxtype2(wx(i,j)+1) .eq. 10 ) then
                            wx2(i,j) = 35
                        else
			    if (iintes(wx(i,j)+1) .eq. 1) then
				wx2(i,j) = 31
			    else if (iintes(wx(i,j)+1) .eq. 2) then
				wx2(i,j) = 31
			    else if (iintes(wx(i,j)+1) .eq. 4) then
                                wx2(i,j) = 33
			    else
                                wx2(i,j) = 32
			    endif
                        endif
                    else
			if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 44
                        else if (iwxtype2(wx(i,j)+1) .eq. 10 ) then
                            wx2(i,j) = 45
                        else
                            if (iintes(wx(i,j)+1) .eq. 1) then
                                wx2(i,j) = 41
                            else if (iintes(wx(i,j)+1) .eq. 2) then
                                wx2(i,j) = 41
                            else if (iintes(wx(i,j)+1) .eq. 4) then
                                wx2(i,j) = 43
                            else 
                                wx2(i,j) = 42 
                            endif
                        endif
		    endif
		else if (iwxtype(wx(i,j)+1) .eq. 10 ) then
                    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 20
                        else if (iwxtype2(wx(i,j)+1) .eq. 8 ) then
                            wx2(i,j) = 21
			else if (iwxtype2(wx(i,j)+1) .eq. 6 ) then
                            wx2(i,j) = 35
                        else
                            if (iintes(wx(i,j)+1) .eq. 1) then
                                wx2(i,j) = 26
                            else if (iintes(wx(i,j)+1) .eq. 2) then
                                wx2(i,j) = 26
                            else if (iintes(wx(i,j)+1) .eq. 4) then
                                wx2(i,j) = 28
                            else
                                wx2(i,j) = 27
                            endif
                        endif
                    else
			if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 24
                        else if (iwxtype2(wx(i,j)+1) .eq. 8 ) then
                            wx2(i,j) = 25
                        else if (iwxtype2(wx(i,j)+1) .eq. 6 ) then
                            wx2(i,j) = 45
                        else
                            if (iintes(wx(i,j)+1) .eq. 1) then
                                wx2(i,j) = 36
                            else if (iintes(wx(i,j)+1) .eq. 2) then
                                wx2(i,j) = 36
                            else if (iintes(wx(i,j)+1) .eq. 4) then
                                wx2(i,j) = 38
                            else
                                wx2(i,j) = 37
                            endif
                        endif
		    endif
		else if (iwxtype(wx(i,j)+1) .eq. 9 ) then
                    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 18
                        else if (iwxtype2(wx(i,j)+1) .eq. 4 ) then
                            wx2(i,j) = 19
                        else
                            wx2(i,j) = 46
                        endif
		    else
			if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 22
                        else if (iwxtype2(wx(i,j)+1) .eq. 4 ) then
                            wx2(i,j) = 23
                        else
                            wx2(i,j) = 50
                        endif
		    endif
		else if (iwxtype(wx(i,j)+1) .eq. 8 ) then
                    if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 18
                        else if (iwxtype2(wx(i,j)+1) .eq. 4 ) then
                            wx2(i,j) = 18
			else if (iwxtype2(wx(i,j)+1) .eq. 10 ) then
                            wx2(i,j) = 21
                        else
                            if (iintes(wx(i,j)+1) .eq. 1) then
                                wx2(i,j) = 47 
                            else if (iintes(wx(i,j)+1) .eq. 2) then
                                wx2(i,j) = 47 
                            else if (iintes(wx(i,j)+1) .eq. 4) then
                                wx2(i,j) = 49 
                            else 
                                wx2(i,j) = 48
                            endif 
                        endif
		    else
			if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 22
                        else if (iwxtype2(wx(i,j)+1) .eq. 4 ) then
                            wx2(i,j) = 22
                        else if (iwxtype2(wx(i,j)+1) .eq. 10 ) then
                            wx2(i,j) = 25
                        else
                            if (iintes(wx(i,j)+1) .eq. 1) then
                                wx2(i,j) = 51 
                            else if (iintes(wx(i,j)+1) .eq. 2) then
                                wx2(i,j) = 51 
                            else if (iintes(wx(i,j)+1) .eq. 4) then
                                wx2(i,j) = 53
                            else
                                wx2(i,j) = 52
                            endif  
                        endif 
		    endif
		else if (iwxtype(wx(i,j)+1) .eq. 1 ) then
		    if (iintes(wx(i,j)+1) .eq. 4) then
                                wx2(i,j) = 17
		    else if (icov(wx(i,j)+1) .eq. 2 .or.
     +                  icov(wx(i,j)+1) .eq. 6 .or.
     +                  icov(wx(i,j)+1) .eq. 7 .or.
     +                  icov(wx(i,j)+1) .eq. 10 .or.
     +                  icov(wx(i,j)+1) .eq. 1 ) then
                        if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 5
                        else if (iwxtype2(wx(i,j)+1) .eq. 4 ) then
                            wx2(i,j) = 7
                        else
                            wx2(i,j) = 8
                        endif
		    else
			if (iwxtype2(wx(i,j)+1) .eq. 3 ) then
                            wx2(i,j) = 13
                        else if (iwxtype2(wx(i,j)+1) .eq. 4 ) then
                            wx2(i,j) = 15
                        else 
                            wx2(i,j) = 16
                        endif
		    endif
		else if (iwxtype(wx(i,j)+1) .eq. 11 ) then
		    wx2(i,j) = 54
		else if (iwxtype(wx(i,j)+1) .eq. 12 ) then
                    wx2(i,j) = 55
		else if (iwxtype(wx(i,j)+1) .eq. 13 ) then
                    wx2(i,j) = 57
		else if (iwxtype(wx(i,j)+1) .eq. 15 ) then
                    wx2(i,j) = 58
		else
		    wx2(i,j) = 0
		endif

		rainlow(i,j) = 0
		rainhi(i,j) = 0
		snowlow(i,j) = 0
		snowhi(i,j) = 0
		mixlow(i,j) = 0
		mixhi(i,j) = 0
		sleethi(i,j) = 0
		sleetlow(i,j) = 0
		thunder(i,j) = 0
		svrtsm(i,j) = 0
C
		if( wx2(i,j) .eq. -9999 ) then
		    rainlow(i,j) = -9999
                    rainhi(i,j) = -9999
                    snowlow(i,j) = -9999
                    snowhi(i,j) = -9999
                    mixlow(i,j) = -9999
                    mixhi(i,j) = -9999
                    sleethi(i,j) = -9999
                    sleetlow(i,j) = -9999
                    thunder(i,j) = -9999
		    svrtsm(i,j) = -9999
		else if( wx2(i,j) .gt. 1 .and. wx2(i,j) .le. 7 ) then
		    rainlow(i,j) = 1
		else if ( wx2(i,j) .eq. 8) then
		    thunder(i,j) = 1
		    svrtsm(i,j) = 1
		else if ( wx2(i,j) .gt. 9 .and. wx2(i,j) .le. 15) then
		    rainhi(i,j) = 1
		else if ( wx2(i,j) .eq. 16) then
		    thunder(i,j) = 1
		    svrtsm(i,j) = 1
		else if ( wx2(i,j) .eq. 17) then
		    thunder(i,j) = 1
		    svrtsm(i,j) = 1
		else if ( wx2(i,j) .gt. 17 .and. wx2(i,j) .le. 21) then
		    mixlow(i,j) = 1
		else if ( wx2(i,j) .gt. 21 .and. wx2(i,j) .le. 25) then
		    mixhi(i,j) = 1
		else if ( wx2(i,j) .gt. 25 .and. wx2(i,j) .le. 35) then
		    sleetlow(i,j) = 1
		else if ( wx2(i,j) .gt. 35 .and. wx2(i,j) .le. 45) then
		    sleethi(i,j) = 1
		else if ( wx2(i,j) .gt. 45 .and. wx2(i,j) .le. 49) then
		    snowlow(i,j) = 1
		else if ( wx2(i,j) .gt. 49 .and. wx2(i,j) .le. 53) then
		    snowhi(i,j) = 1
		endif
		if( wx2(i,j) .eq. 5 .or. wx2(i,j) .eq. 7 .or.
     +              wx2(i,j) .eq. 13 .or. wx2(i,j) .eq. 15 ) then
		    thunder(i,j) = 1
		endif

 510       continue
 500    continue
                       
C Insert Grids into output grid file
       
       igx = ix
       igy = iy 
      
       ivcord = 0
       level(1) = 0
       level(2) = -1
       parm = 'WX'
	call GD_WDAT(IGDFLN, wx2, IGX, IGY, IGHDR, GDATTIM, LEVEL, 
     +         IVCORD, PARM, REWRIT, IRET )  
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif 

	parm = 'RAINL'
	call GD_WDAT(IGDFLN, rainlow, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )   
       if(iret .ne. 0) then 
          write(*,*) 'Grid Not Written', iret 
       endif  

	parm = 'RAINH'
        call GD_WDAT(IGDFLN, rainhi, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif
	parm = 'SNOWH'
        call GD_WDAT(IGDFLN, snowhi, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif

	parm = 'SNOWL'
        call GD_WDAT(IGDFLN, snowlow, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif

	parm = 'MIXL'
        call GD_WDAT(IGDFLN, mixlow, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then 
          write(*,*) 'Grid Not Written', iret
       endif 

	parm = 'MIXH'
        call GD_WDAT(IGDFLN, mixhi, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif

	parm = 'ICEL'
        call GD_WDAT(IGDFLN, sleetlow, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif

	parm = 'ICEH'
        call GD_WDAT(IGDFLN, sleethi, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif

	parm = 'TSTM'
        call GD_WDAT(IGDFLN, thunder, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif

	parm = 'STSM'
        call GD_WDAT(IGDFLN, svrtsm, IGX, IGY, IGHDR, GDATTIM, LEVEL,
     +         IVCORD, PARM, REWRIT, IRET )
       if(iret .ne. 0) then
          write(*,*) 'Grid Not Written', iret
       endif

      call GD_CLOS  ( IGDFLN, IRET )  
      
      end
