Wednesday, November 18, 2009

Angular Unit Conversion - DD, DMS, Radians

It's important to know how to convert back and forth between various units of angular measurement.

Degrees, Minutes, Seconds to Decimal Degrees (DMS to DD):

DD = Degrees + (Minutes/60) + (Seconds/3600)

If you put this in terms of a clock, where a degree equals one hour of time (and there are 360 hours in a complete cycle of a day), it becomes easier to understand how minutes and seconds work in relation to the degree. There are sixty minutes in an hour (or one degree), and also there are sixty seconds in a minute. Thus, 60 seconds times 60 minutes equals 3600 seconds in an hour (or in one degree of angular measurement). Now just add these together.



Note: When working with locations west of the prime meridian, longitude values may be represented with negative numbers (-84° in this case). The formula should use subtractions (-) instead of addition (+) (line 3), or the entire formula must be multiplied by -1 as a final step (line 4).  Note the "abs()" function which temporarily converts the negative value to a positive number so the addition signs can be used.  In short, be extra aware of your data and the formulas used to convert between these units.

Decimal Degrees to Degrees, Minutes, Seconds (DD to DMS):
This conversion is a bit more tricky because the minutes and seconds are combined and packed into the decimal.

For the DD value 30.443452º
  • Anything to the left of the decimal is the full degree value
    D = 30º
  • Multiply the decimal value by 60 to find the minutes. The value will be between zero and 59.999. The minutes value will be what is in front of the decimal again.
    M = 26' because 0.443452 * 60 = 26.60712
  • Multiply the minutes decimal value by 60 to find the decimal seconds value. Again, it will be between zero and 59.999, as there are 60 seconds in a minute. Decimal seconds are acceptable.
  • S = 36.4272" = 60 * 0.60712
To pull this off in a spreadsheet, create an intermediate "Calculate Minutes" field, which is referenced to build the Minutes and Seconds fields. Negative numbers require an additional step (see below).



Negative numbers will have a leading third character to deal with in front of the decimal. First, sort the dataset so these can be dealt with as a group. Change Degrees formula from =left(*Cell*, 2) to =left(*Cell*, 3) for all negative values. That's it.


DD to Radians to DD:

Radians are another unit of angular measurement, used most often in all but basic applications of geometry. So what is a radian?  It's the arc distance between two points on a circle where an angle intersects, equal to the circle's radius (Wikipedia). It's the unit of measure used to calculate a number of Excel formulas, so it's important to understand what it means, and at the very least how to use it.






DD to radians:



Or radians to DD:


Friday, November 13, 2009

System Shut Down Utility


This is a very light utility that I coded in just a few hours. It's purpose is to shut down the computer after a selected amount of time from the combo boxes (between 5 minutes to over 6 hours). The simple .exe illustrates a few basic but useful VB techniques:
  • Setting up combo boxes & values
  • Global variables
  • A timer
  • Time / Date / Now functions
  • Call functions from other subroutines
  • and the main feature, automatically shut down the computer
Instructions:
  • Download and unzip the package. Rename if necessary (see Final Tricks, below)
  • Open the program. The current date and time will display
  • Choose an amount of time from the Hours, and Minutes menus
  • Click "Commit" when ready. You will be prompted to verify (Yes or No) that you are ready to begin, choose Yes to continue
  • The time will be updated to report when the count began, and the remaining time (in minutes) will update every 60 seconds. When the time expires, the computer will automatically shut down. That's all there is to it!
Here are the guts of it:

Combo Boxes:
First, the contents of the pull down / combo boxes are set up in the Form_Load() subroutine, thus the contents of each menu are loaded when the app starts. Here's the code for the minutes list:

Private Sub Form_Load()
cboMins.AddItem ""
cboMins.ItemData(cboMins.NewIndex) = 0
cboMins.AddItem "5"
cboMins.ItemData(cboMins.NewIndex) = 5
cboMins.AddItem "15"
cboMins.ItemData(cboMins.NewIndex) = 15
cboMins.AddItem "30"
cboMins.ItemData(cboMins.NewIndex) = 30
cboMins.AddItem "45"
cboMins.ItemData(cboMins.NewIndex) = 45
'...
End Sub

Global Variables:
When the command button is clicked to run the program, the values from each of the combo boxes are passed to the intHours/intMinutes variables which have been dimensioned in the General Declarations (the topmost portion of the code editor). These can be called by various other functions to verify that a time has been selected (intHours + intMinutes > 0?), or to set the iterations of the timer, etc.

Option Explicit
Dim intHours As Integer
Dim intMinutes As Integer
_____________________________

Private Sub Form_Load()
'...

The VB Timer, Shutdown, & Calling Subroutines:
Timers can be a little tricky in this situation, but they're basically simple. There are two main properties: Enabled (on/off), and Interval (how long the timer runs). It would be great to set the timer to run for 4 hours and 5 minutes, but that's not how it works. The timer runs for a given number of milliseconds (1,000 milliseconds = 1 second) between 0 and 65,535 (just over 1 minute, 5 seconds). Instead we'll make a variable that will represent the total number of desired minutes, and loop through ever second (tmrShutDown.Interval = 60000) until the counter = 0; then run the desired procedure.

First, add a Timer control (Visual Basic 6) to the form named tmrShutDown. In Form_Load(), set the timer's Interval to 60000 (one minute) and make sure the timer is off as the app loads.

Private Sub Form_Load()
tmrShutDown.Enabled = False
tmrShutDown.Interval = 60000
'...
End Sub

Before the timer is enabled (via the command button), set a predefined global variable (intTime) to equal the amount of desired time in minutes. Now tell the timer to start running. Now when the timer is enabled, some code will run inside the timer's subroutine every 60 seconds (or 60,000 milliseconds); essentially as a loop. This will reiterate every 60 seconds until it is told to stop (tmrControl.Enabled = False). Insert a simple if-then statement: If the time counter (intTime) = zero, stop the timer and call the procedure to turn off the computer, otherwise, subtract one from the counter and continue.

Option Explicit
Dim intHours As Integer
Dim intMinutes As Integer
Dim intTime As Integer
___________________________________________

Private Sub cmdCommit_Click()
'...
intTime = (intHours * 60) + intMinutes
tmrShutDown.Enabled = True
'...
End Sub
___________________________________________

Private Sub tmrCommand_Timer()

If intTime = 0 Then
tmrCommand.Enabled = False 'Turn off timer
Call ShutDown
Else
intTime = intTime - 1
End If End Sub
___________________________________________

Private Sub ShutDown()
Shell "shutdown -s -f -t 00"
'...
End Sub


Final Tricks:


You might notice the odd title. Combined that with a similar inconspicuous file name alteration, and the app can go quite unnoticed on a computer - which might be useful to certain users.

Clicking the command button disables itself and both combo boxes to let the user know that it is working.

Finally, two labels are updated to include a time that the timer started, and how much time is left before shutdown. This should be updated in the timer's subroutine.

Private Sub cmdCommit_Click()
'...
cboHours.Enabled = False
cboMinutes.Enabled =
False
cmdCommit.Enabled = False

lblInitial.Caption = "Initial Time: " & Now
lblTimeLeft.Caption = "Time Remaining: " & intTime

End Sub

Dihydrogen Monoxide Alert

A bottle of 99.93% pure DHMO (Dihydrogen Monoxide)

Dihydrogen Monoxide:
  • may cause severe burns
  • is a major component of acid rain
  • has been found in excised tumors of terminal cancer patients
  • used in nuclear power plants
  • used in many forms of cruel animal research
  • may cause death by decreased effectiveness of automobile brakes
Dihydrogen monoxide is colorless, tasteless, and kills uncounted thousands of people every year. Most of these deaths are caused by accidental inhalation of DHMO but, in addition, prolonged exposure to its solid form causes severe tissue damage. Symptoms of DHMO ingestion can include excessive sweating and urination, and possibly a bloated feeling, nausea, vomiting, and body electrolyte imbalance. For those who have become dependent, DHMO withdrawal means certain death.

Environmentalists across the world all agree that DHMO contamination is reaching epidemic proportions.

Our water: Quantities of DHMO have been found in every stream, lake, and reservoir in America today and now the pollution has also been confirmed in the Antartic ice. Despite theis, spokesmen for big business & government still claim that the public has nothing to worry about. Yet companies continue to dump waste DHMO into rivers and the oceans, and nothing can be done to stop them.

Our food: DHMO is used in the distribution of pesticides. Even after washing, produce remains contaminated by this chemical.

Dihydrogen monoxide is also known as hydroxyl acid, hydrogen hydroxide, or tap water.