C Copyright(c) 1997, Space Science and Engineering Center, UW-Madison
C Refer to "McIDAS Software Acquisition and Distribution Policies"
C in the file  mcidas/data/license.txt

C *** $Id: pmcnvt.f,v 1.10 1997/10/10 20:18:38 dglo Exp $ ***

C $ CALL PMCNVT(NKEYS, UNITS, NUNITS, OVAL, SCALES, MISS)  (GAD)
C $ PERFORM UNIT CONVERSION FOR MDO
C $ INPUT:
C $   NKEYS = (I) NUMBER OF KEY VALUES IN OVAL
C $   UNITS = (I) ARRAY OF UNITS FROM MD FILE
C $   NUNITS = (I) ARRAY OF NEW UNITS
C $   OVAL = (I) ARRAY OF VALUES FROM MD FILE
C $   SCALES = (I) ARRAY OF SCALE FACTORS FROM MD FILE
C $   MISS   = (I) MISSING DATA VALUE
C $ OUTPUT:
C $   OVAL = (I) VALUES AFTER UNIT CONVERSION
C $$ PMCNVT = CONVERT, MD

      SUBROUTINE PMCNVT(NKEYS,UNITS,NUNITS,OVAL,SCALES,MISS)
      IMPLICIT INTEGER (A-Y)
      REAL FTIME,FVAL
      DIMENSION UNITS(400),NUNITS(400),OVAL(400),IOUT(20),SCALES(400)
      DIMENSION KEYW(14)
      CHARACTER*80 COUT
      EQUIVALENCE (IOUT,COUT)
      DATA NUMK/14/
C-----
      BLANK=LIT('    ')
      CALL MOVCW('KM  MI  DEG HMS H   K   SYD MPS MB  ',KEYW)
      CALL MOVCW('CM  IN  M   FT  KM2 ',KEYW(10))
C-----
C---LOOP BY KEYS
C-----
      DO 500 I=1,NKEYS
      IF (OVAL(I).EQ.MISS) GO TO 500
      IF (NUNITS(I).EQ.BLANK.OR.UNITS(I).EQ.NUNITS(I)) GO TO 500
      DO 5 J=1,NUMK
      IF (UNITS(I).EQ.KEYW(J)) GO TO 9
5     CONTINUE
      GO TO 500
9     GO TO (10,20,30,40,50,60,70,80,90,100,110,120,130,140),J
C--- KM TO MI
10    IF (NUNITS(I).NE.LIT('MI  ')) GO TO 99
      OVAL(I)=(OVAL(I)*10000/161+50)/100
      GO TO 500
C--- MI TO KM
20    IF (NUNITS(I).NE.LIT('KM  ')) GO TO 99
      OVAL(I)=(OVAL(I)*161+50)/100
      GO TO 500
C--- SCALED DEGREES TO DEGREES/MINUTES/SECONDS
30    IF (NUNITS(I).NE.LIT('DMS ')) GO TO 99
      IS=SCALES(I)
      ZVAL=FLOAT(OVAL(I))/10**IS
      OVAL(I)=ILALO(ZVAL)
      GO TO 500
C--- HOURS/MINUTES/SECONDS TO SCALED HOURS
40    IF (NUNITS(I).NE.LIT('H   ')) GO TO 99
      ZVAL=FTIME(OVAL(I))
      OVAL(I)=IFIX(ZVAL*100.)
      GO TO 500
C--- SCALED HOURS TO HOURS/MINUTES/SECONDS
50    IF (NUNITS(I).NE.LIT('HMS ')) GO TO 99
      IS=SCALES(I)
      ZVAL=FLOAT(OVAL(I))/10**IS
      OVAL(I)=M0ITIME(ZVAL)
      GO TO 500
C--- KELVIN TO CELSIUS OR FAHRENHEIT DEGREES
60    IF (NUNITS(I).NE.LIT('C   ').AND.NUNITS(I).NE.LIT('F   '))
     &  GO TO 99
      if (nunits(i).eq.lit('C   ')) then
         oval(i)=nint( (oval(i)-27316)/10.0)
      else
         oval(i)=nint( (oval(i)-27316)*.01*1.8 + 32.0)
      endif
      GO TO 500
C--- SATELLITE/YEAR/JULIAN DAY TO DAY/MONTH/YEAR
70    IF (NUNITS(I).NE.LIT('DMY ')) GO TO 99
      OVAL(I)=MOD(OVAL(I),100000)
      CALL YDDMY(OVAL(I),DD,MM,YY)
      OVAL(I)=DD*10000+MM*100+YY
      GO TO 500
C--- METERS/SEC TO MILES/HR OR KNOTS
80    IF (NUNITS(I).NE.LIT('MPH ').AND.NUNITS(I).NE.LIT('KT  ').AND.
     &    NUNITS(I).NE.LIT('FPS ')) GO TO 99
      IF (NUNITS(I).EQ.LIT('MPH ')) OVAL(I)=(OVAL(I)*2236+500)/1000
      IF (NUNITS(I).EQ.LIT('KT  ')) OVAL(I)=(OVAL(I)*1944+500)/1000
      IF (NUNITS(I).EQ.LIT('FPS ')) THEN
         FVAL=OVAL(I)/.3048
         OVAL(I)=IROUND(FVAL)
      ENDIF
      GO TO 500
C--- MB TO INCHES OF HG
90    IF (NUNITS(I).NE.LIT('INHG')) GO TO 99
      OVAL(I)=NINT(OVAL(I)/33.864)
      GO TO 500
C--- CM TO MM OR INCHES
100   IF (NUNITS(I).NE.LIT('MM  ').AND.NUNITS(I).NE.LIT('IN  '))
     &  GO TO 99
      IF (NUNITS(I).EQ.LIT('MM  ')) OVAL(I)=OVAL(I)*10
      IF (NUNITS(I).EQ.LIT('IN  ')) OVAL(I)=(OVAL(I)*10000/254+50)/100
      GO TO 500
C--- INCHES TO MM OR CM
110   IF (NUNITS(I).NE.LIT('MM  ').AND.NUNITS(I).NE.LIT('CM  '))
     &  GO TO 99
      OVAL(I)=(OVAL(I)*254+5)/10
      IF (NUNITS(I).EQ.LIT('CM  ')) OVAL(I)=(OVAL(I)+5)/10
      GO TO 500
C--- METERS TO FEET
120   IF (NUNITS(I).NE.LIT('FT  ')) GO TO 99
      FVAL=OVAL(I)/.3048
      OVAL(I)=IROUND(FVAL)
      GO TO 500
C--- FEET TO METERS
130   IF (NUNITS(I).NE.LIT('M   ')) GO TO 99
      OVAL(I)=(OVAL(I)*10000/328+50)/100
      GO TO 500
C--- SQUARE KM TO SQUARE MILES
140   IF (NUNITS(I).NE.LIT('MI2 ')) GO TO 99
      OVAL(I)=(OVAL(I)*10000/259+50)/100
      GO TO 500
99    WRITE(COUT,'(''UNABLE TO CONVERT '',A4,'' TO '',A4)')
     &     UNITS(I),NUNITS(I)
      CALL LTQ(IOUT)
500   CONTINUE
C-----
      RETURN
      END
