C Copyright(c) 1999, 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: smpunitcvt.f,v 1.0 1999/09/28 14:38:32 russd Tst $ *** C ? SMPUNITCVT - McIDAS unit conversion sample program C ? SMPUNITCVT value unit C ? Parameters: C ? value | value to convert C ? unit | units of value C ? Keywords: C ? none C ? Remarks: C ? McIDAS programing sample. C ? C ? This sample converts a specified value with given units C ? for all supported conversion of the unit type. C ? ---------- subroutine main0 implicit none c --- external functions c --- internal functions integer unit_utils ! test unit conversion utils c --- internal variables integer status ! function status c **************** Program Body ************** c --- set initial program status to FAILED call MCCODESET( -1 ) c --- UNIt conversion utility status = UNIT_UTILS( ) if( status.lt.0 ) return c --- set the program status to SUCCESS call MCCODESET( 0 ) c --- signal finished call EDEST('Done ...',0) return end ** Name: ** unit_utils - code samples for unit conversion utilities ** ** Interface: ** integer function ** unit_utils( ) ** ** Input: ** none ** ** Input and Output: ** none ** ** Output: ** none ** ** Return values: ** 0 - Success ** -1 - Failed ** ** Remarks: ** Used to illustrate basic command line retrieval functions ** ** Categories: ** unit ** conversion integer function unit_utils( ) implicit none c --- parameters integer NUM_LENGTH parameter (NUM_LENGTH = 11) integer NUM_SPEED parameter (NUM_SPEED = 6) integer NUM_TEMPERATURE parameter (NUM_TEMPERATURE = 3) integer NUM_PRESSURE parameter (NUM_PRESSURE = 4) integer NUM_TIME parameter (NUM_TIME = 5) integer NUM_WEIGHT parameter (NUM_WEIGHT = 5) c --- internal constants c --- external functions character*12 cfe character*12 cff integer len_trim integer mccmdstr integer mccmddbl integer mcucvtd c --- internal variables character*4 LENGTH_UNITS(NUM_LENGTH) character*4 SPEED_UNITS(NUM_SPEED) character*4 TEMPERATURE_UNITS(NUM_TEMPERATURE) character*4 PRESSURE_UNITS(NUM_PRESSURE) character*4 TIME_UNITS(NUM_TIME) character*4 WEIGHT_UNITS(NUM_WEIGHT) character*12 cvalue character*12 unit character*12 cnew character*256 input_string double precision dvalue double precision new_dvalue integer ok integer i integer j integer len_cvalue integer len_unit integer len_cnew integer len_units integer num_val c --- list of valid LENTGH units data LENGTH_UNITS/ & 'M', ! meters & 'KM', ! kilometers & 'DM', ! decameters & 'CM', ! centimeters & 'MM', ! millimeters & 'MI', ! miles & 'NMI', ! nautical miles & 'YD', ! yards & 'FT', ! feet & 'IN', ! inches & 'DEGL' ! degrees of Latitude/Longitude & / c --- list of valid SPEED units data SPEED_UNITS/ & 'MPH', ! miles per hour & 'KT', ! nautical miles per hour & 'KTS', ! nautical miles per hour & 'MPS', ! meters per second & 'FPS', ! feet per second & 'KPH' ! kilometers per hour & / c --- list of valid TEMPERATURE units data TEMPERATURE_UNITS/ & 'K', ! kelvin & 'F', ! fahrenheit & 'C' ! centigrade & / c --- list of valid PRESSURE units data PRESSURE_UNITS/ & 'MB', ! millibars & 'INHG', ! inchs of mercury & 'PA', ! pascals & 'HPA' ! hectopascals & / c --- list of valid TIME units data TIME_UNITS/ & 'HR', ! hours & 'MIN', ! minutes & 'SEC', ! seconds & 'DAY', ! days & 'YR' ! years & / c --- list of valid WEIGHT units data WEIGHT_UNITS/ & 'G', ! grams & 'KG', ! kilograms & 'LB', ! pounds & 'OZ', ! ounces & 'TON' ! tons & / c ************ Function Body ************* c --- initialize the function status unit_utils = -1 c --- read the input value from the command line ok= MCCMDDBL( ' ', 1, 'VALUE',0.0D0, 0.0D0, -1.0D0, dvalue) if( ok.lt.0 ) return c --- read the units ok= MCCMDSTR( ' ', 2, ' ', unit) if( ok.lt.0 ) return c --- make sure that user entered something if( unit.eq.' ' ) then call EDEST('User must specify a value and unit to convert',0) return else num_val = 1 cvalue = cff( dvalue, 2 ) len_cvalue = len_trim( cvalue ) len_unit = len_trim( unit ) endif c --- test for unit of type = LENGTH do i = 1, NUM_LENGTH if( unit.eq.LENGTH_UNITS(i) ) then c ------ value is a length, loop through the list of c valid length types and print the value do j = 1, NUM_LENGTH c ------ convert input VALUE[UNIT] to new UNIT ok= MCUCVTD(num_val,unit,dvalue,LENGTH_UNITS(j),new_dvalue,0) if( ok.ne.0 ) then call EDEST('MCUCVTD: failed --> status=',ok) call DDEST(' input units='//unit// & ' output units='//LENGTH_UNITS(j),0) return c ------ conversion was successful, print result else cnew = CFF( new_dvalue, 4 ) if( cnew(1:1).eq.'*' ) then cnew = CFE( real(new_dvalue), 6 ) endif len_cnew = LEN_TRIM( cnew ) len_units = LEN_TRIM( LENGTH_UNITS(j) ) call SDEST(cvalue(1:len_cvalue)// & '['//unit(1:len_unit)//'] = '// & cnew(1:len_cnew)// & '['//LENGTH_UNITS(j)(1:len_units)//']',0) end if end do c ------ set return status to success unit_utils = 0 return end if end do c --- test for unit of type = SPEED do i = 1, NUM_SPEED if( unit.eq.SPEED_UNITS(i) ) then c ------ value is a length, loop through the list of c valid length types and print the value do j = 1, NUM_SPEED c ------ convert input VALUE[UNIT] to new UNIT ok= MCUCVTD(num_val,unit,dvalue,SPEED_UNITS(j),new_dvalue,0) if( ok.ne.0 ) then call EDEST('MCUCVTD: failed --> status=',ok) call DDEST(' input units='//unit// & ' output units='//SPEED_UNITS(j),0) return c ------ conversion was successful, print result else cnew = CFF( new_dvalue, 4 ) if( cnew(1:1).eq.'*' ) then cnew = CFE( real(new_dvalue), 6 ) endif len_cnew = LEN_TRIM( cnew ) len_units = LEN_TRIM( SPEED_UNITS(j) ) call SDEST(cvalue(1:len_cvalue)// & '['//unit(1:len_unit)//'] = '// & cnew(1:len_cnew)// & '['//SPEED_UNITS(j)(1:len_units)//']',0) end if end do c ------ set return status to success unit_utils = 0 return end if end do c --- test for unit of type = TEMPERATURE do i = 1, NUM_TEMPERATURE if( unit.eq.TEMPERATURE_UNITS(i) ) then c ------ value is a length, loop through the list of c valid length types and print the value do j = 1, NUM_TEMPERATURE c ------ convert input VALUE[UNIT] to new UNIT ok= MCUCVTD(num_val,unit,dvalue,TEMPERATURE_UNITS(j), & new_dvalue,0) if( ok.ne.0 ) then call EDEST('MCUCVTD: failed --> status=',ok) call DDEST(' input units='//unit// & ' output units='//TEMPERATURE_UNITS(j),0) return c ------ conversion was successful, print result else cnew = CFF( new_dvalue, 4 ) if( cnew(1:1).eq.'*' ) then cnew = CFE( real(new_dvalue), 6 ) endif len_cnew = LEN_TRIM( cnew ) len_units = LEN_TRIM( TEMPERATURE_UNITS(j) ) call SDEST(cvalue(1:len_cvalue)// & '['//unit(1:len_unit)//'] = '// & cnew(1:len_cnew)// & '['//TEMPERATURE_UNITS(j)(1:len_units)// & ']',0) end if end do c ------ set return status to success unit_utils = 0 return end if end do c --- test for unit of type = PRESSURE do i = 1, NUM_PRESSURE if( unit.eq.PRESSURE_UNITS(i) ) then c ------ value is a length, loop through the list of c valid length types and print the value do j = 1, NUM_PRESSURE c ------ convert input VALUE[UNIT] to new UNIT ok= MCUCVTD(num_val,unit,dvalue,PRESSURE_UNITS(j), & new_dvalue,0) if( ok.ne.0 ) then call EDEST('MCUCVTD: failed --> status=',ok) call DDEST(' input units='//unit// & ' output units='//PRESSURE_UNITS(j),0) return c ------ conversion was successful, print result else cnew = CFF( new_dvalue, 4 ) if( cnew(1:1).eq.'*' ) then cnew = CFE( real(new_dvalue), 6 ) endif len_cnew = LEN_TRIM( cnew ) len_units = LEN_TRIM( PRESSURE_UNITS(j) ) call SDEST(cvalue(1:len_cvalue)// & '['//unit(1:len_unit)//'] = '// & cnew(1:len_cnew)// & '['//PRESSURE_UNITS(j)(1:len_units)// & ']',0) end if end do c ------ set return status to success unit_utils = 0 return end if end do c --- test for unit of type = TIME do i = 1, NUM_TIME if( unit.eq.TIME_UNITS(i) ) then c ------ value is a length, loop through the list of c valid length types and print the value do j = 1, NUM_TIME c ------ convert input VALUE[UNIT] to new UNIT ok= MCUCVTD(num_val,unit,dvalue,TIME_UNITS(j),new_dvalue,0) if( ok.ne.0 ) then call EDEST('MCUCVTD: failed --> status=',ok) call DDEST(' input units='//unit// & ' output units='//TIME_UNITS(j),0) return c ------ conversion was successful, print result else cnew = CFF( new_dvalue, 4 ) if( cnew(1:1).eq.'*' ) then cnew = CFE( real(new_dvalue), 6 ) endif len_cnew = LEN_TRIM( cnew ) len_units = LEN_TRIM( TIME_UNITS(j) ) call SDEST(cvalue(1:len_cvalue)// & '['//unit(1:len_unit)//'] = '// & cnew(1:len_cnew)// & '['//TIME_UNITS(j)(1:len_units)//']',0) end if end do c ------ set return status to success unit_utils = 0 return end if end do c --- test for unit of type = WEIGHT do i = 1, NUM_WEIGHT if( unit.eq.WEIGHT_UNITS(i) ) then c ------ value is a length, loop through the list of c valid length types and print the value do j = 1, NUM_WEIGHT c ------ convert input VALUE[UNIT] to new UNIT ok= MCUCVTD(num_val,unit,dvalue,WEIGHT_UNITS(j),new_dvalue,0) if( ok.ne.0 ) then call EDEST('MCUCVTD: failed --> status=',ok) call DDEST(' input units='//unit// & ' output units='//WEIGHT_UNITS(j),0) return c ------ conversion was successful, print result else cnew = CFF( new_dvalue, 4 ) if( cnew(1:1).eq.'*' ) then cnew = CFE( real(new_dvalue), 6 ) endif len_cnew = LEN_TRIM( cnew ) len_units = LEN_TRIM( WEIGHT_UNITS(j) ) call SDEST(cvalue(1:len_cvalue)// & '['//unit(1:len_unit)//'] = '// & cnew(1:len_cnew)// & '['//WEIGHT_UNITS(j)(1:len_units)//']',0) end if end do c ------ set return status to success unit_utils = 0 return end if end do c --- Invalid unit conversion call EDEST('Invalid conversion specified ='//input_string,0) return end