Sunday, February 28, 2010

How the Richter Scale Works

I saw this headline:

Chile’s earthquake 500 times stronger yet less destructive than Haiti’s

and started wondering: how Chile’s quake could be 500x as powerful as Haiti’s when their respective Richter scale magnitudes are 8.8 vs. 7.0? The “less destructive” part is interesting, too, but everyone’s already talking about that so I’ll focus on the scale.

The first clue is that the scale is logarithmic. That means that each increment of 1 is a 10x increase in magnitude. If you were to plot x vs. log10(x), it might look like this:

 image 

Well, that’s not very revealing until you realize that the x-axis is increasing logarithmically. Consider the same data but with the x-axis presented linearly:

 image

Now we’re talking. Each increment on the y-axis is a 10x increment on the y-axis. i.e. 8 is 10x greater in magnitude than a 7:

108/107 = 10

So it follows then that an 8.8 (Chile) vs. 7.0 (Haiti) would be:

108.8/107 = 63

Thus an 8.8 is 63 times greater in magnitude than a 7. Wow. OK so how do we get to 500x more destructive? It would seem that the news is using a different factor: energy. I didn’t dig into all the math behind it but the gist of it seems to be that while the Richter scale measures raw amplitude of the little pencil on the paper wheel/strip triggered by the earthquake's waves, the actual energy factor is much higher than 10:

Because of the logarithmic basis of the scale, each whole number increase in magnitude represents a tenfold increase in measured amplitude; in terms of energy, each whole number increase corresponds to an increase of about 31.6 times the amount of energy released.

So I guess the news people are applying this to the log scale with a base of 31.6 instead of 10. Let’s check:

31.68.8/31.77 = 501

Hooray! I honestly didn’t know that would work until I got to this point and calculated it (phew!). So this makes me wonder how effective a scale is when it’s not…intuitive at all. Certainly a simple, scientific number is helpful, but I’m not sure this is the right number to use in casual conversation.

Instead, numbers related to life lost, people displaced, or damage are probably more relevant and appropriate to compare. For example, the current mortality estimates are: Chile 700, Haiti 220,000:

220000/700 = 314

The quake in Haiti has killed (to date) 314 times more people than the quake in Chile. (That article also explains the 500 figure pretty well—I should have read it before writing this post :P)

On a very related note, there’s a super cool Radio Lab podcast about numbers which explores the idea that we count logarithmically when born but learn to count absolutely as children. Neat.

Also of note: decibels—a measurement of sound intensity is in logarithmic units, too, as is pH in chemistry.

Finally, for tons of info on Earthquakes, checkout the USGS.

Saturday, February 27, 2010

Why Twitter is Awesome

I don’t understand Twitter—how it works, how it makes money, or anything. As a computer engineer, I marvel at how impressive it is in terms of scale. For example, they are now crunching 50,000,000 tweets per day. That’s incredible.

Volume doesn’t make it awesome, though. Most of those tweets are about what uninteresting people are eating, watching, etc. Here’s why it’s awesome: I am empowered to connect with people in otherwise unreachable places. This calls for some examples:

Example 1: Microsoft

I recently ranted a bit about Visual Studio 2010 RC crashes:

If you crash on me one more damn time, VS2010, I will drop kick you onto the roof of my building. I'm not even kidding. 9:58 PM Feb 24th

It's official, VS2010 is getting taped to a ball and kicked onto the roof of my office tomorrow. Hope you're happy...(I warned you). 10:59 PM Feb 24th

This brought a very nice reply from someone I’ve never heard of (@Pilchie):

@mharen: Are you using a touch screen? Seen http://bit.ly/dlT7ez ? 11:09 PM Feb 24th 

It turns out that @Pilchie is “Dev Lead for C# IntelliSense/Refactoring/etc at Microsoft”. Well how about that? In a mere 10 minutes, I was getting help from not just Microsoft, but the guy who works on the product I was complaining about. And his link worked!

Example 2: Stackoverflow/Codinghorror

I was working on a project which is using a lot of the same libraries that Stackoverflow uses. I ran into trouble with one of the components: I couldn’t figure out which version to use. So I pinged the lead developer/co-creator of SO:

@codinghorror where do i get the best wmd editor for use with markdownsharp? your version on google code? 7:30 PM Feb 8th

And his very prompt response:

r @mharen our version is on Google Code, or you can dig through the fork-splosion on github and try to guess which version is "best" 7:33 PM Feb 8th

In three minutes I had my answer from the top guy of the a product that’s used by bajillions (if not gajillions) of people. Even if he hadn’t answered, I was hoping that someone else might (this wasn’t a private query).

Example 3: Red Gate’s Neil Davidson

Via Twitter, I discovered a neat little book on software pricing. I tweeted about it:

40% in and still interesting... RT @spolsky: A short ebook about software product pricing http://retwt.me/1bmXM (from @neildavidson) 9:10 AM Nov 3rd, 2009

A half-hour later, I see this in my feed:

@mharen Glad you're liking the eBook! If you have time, could you post a review on Amazon once you've finished? ( http://bit.ly/18xFJx) 9:37 AM Nov 3rd, 2009

It’s at this point that I realize who wrote the book: Neil Davidson, co-founder/joint CEO of Red Gate Software, and founder  of the Business of Software conferences among other things. A cool guy to get to know, no doubt. I finished the book and left a review as he asked and took the opportunity to ask about one of his products:

@NeilDavidson you bet; we use a lot of your products btw--thanks! What's up with Log Rescue not supporting SQL >2000? Anything planned? 9:40 AM Nov 3rd, 2009

To which I got a very simple and candid reply (the beauty of the 140 character limit!):

@mharen We're not going to do any more work on SQL Log Rescue. Lots of people used it, but nobody bought, and SQL 2000 work would be huge 9:42 AM Nov 3rd, 2009


It’s so cool that this simple medium enables and promotes these types of quick conversations. In 2/3 of these cases, I wasn’t even trying to initiate communication—others were listening for certain things and they found me.

When I first starting toying with Twitter it seemed that its signal-to-noise ratio was cripplingly low. Now I see that by following keywords—not users—the signal is strong.

It also has a unique ability to cut through the BS by forcing you to choose your words carefully. I like that.

Fun with Google’s Public Data

I was going to do a post about our world’s exploding population growth and stumbled upon Google’s awesome public data service with my first research query:

image

I haven’t been able to uncover an index for what types of things you can show here but a little searching will get you other things.

Anyway, so this is what I was looking for initially (data source):

image 

And since the last 200 years:

image

Clearly my source data could be more complete. Even with the gaps, though, the exploding trend is obvious. It comes with this neat, mind-blowing factoid, too:

The total number of humans that have ever lived has been estimated at 110 billion.
Approximately 6% of all those people are alive today.

Wow. With the modern improvements to life expectancy and high rate of population growth (10-15 years per billion), it might be a while before this 6% figure stabilizes (i.e. stops growing).

I’m not suggesting that this is a bad thing—not at all. I just find it interesting.

Word Document Automation with .NET 4: Update All Fields

I’ve been told that these programming posts are not interesting or funny. For those that have no interest in programming, I offer the following joke:

I hope the next time I move I get a real easy phone number, something that's real easy to remember. Something like two two two two two two two two. I would say "Sweet." And then people would say, "Mitch, how do I get a hold of you?" I'd say, "Just press two for a while. And when I answer, you will know you have pressed two enough." (more classics from the late and invariably hilarious Mitch Hedberg)

Now would be a good time for you to stop reading.


On with the show! This handy method will attempt to update all the fields in the document passed to it:

/// <summary>
/// Update damn near every field in the document
/// </summary>
/// <param name="document">The document in dire need of updating</param>
private static void UpdateAllFields(Document document)
{
    // there must be a better way than searching everything I can thing of
    // please let me know if you find one!

    foreach (Range R in document.StoryRanges)
    {
        R.Fields.Update();
    }

    foreach (TableOfContents T in document.TablesOfContents)
    {
        T.Update();
    }

    foreach (Section S in document.Sections)
    {
        foreach (HeaderFooter F in S.Footers)
        {
            F.Range.Select();
            F.Range.Fields.Update();
        }
        foreach (HeaderFooter H in S.Headers)
        {
            H.Range.Select();
            H.Range.Fields.Update();
        }
    }

    foreach (Shape S in document.Shapes)
    {
        if (S.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox && S.TextFrame.HasText > 0)
        {
            S.TextFrame.TextRange.Fields.Update();
        }
    }

    foreach (Range S in document.Sentences)
    {
        if (S.ShapeRange.Count > 0)
        {
            foreach (Shape Shape in S.ShapeRange)
            {
                if (Shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
                {
                    Shape.TextFrame.TextRange.Fields.Update();
                }
            }
        }
    }

    document.Application.ActiveWindow.View.Type = WdViewType.wdMasterView;
    document.Application.ActiveWindow.View.Type = WdViewType.wdPrintPreview;
}

To use this, add the following references to your project:

  • Microsoft.Office.Interop.Word, v12
  • Office, v12

It makes me cry a little to brute force every container I can think of this way. Surely there’s a better way. Maybe I could just traverse the documents DOM an update anything that looks like a field castable to a Field…I’ll think about that. Until then, this seems to work…

Word Document Automation with .NET 4

I’ve been toying around with some document generation lately and thought I’d share a bit of what I’ve learned. Here’s a method for extracting a list of custom properties in use in the document:

/// <summary>
/// Retrieves custom properties from a given Word document
/// </summary>
/// <param name="file">Full path to the Word document</param>
/// <returns>A dictionary representation of the document's custom properties</returns>
public static Dictionary<string, string> GetDocProperties(string file)
{
    Application WordApp = null;
    var DocProperties = new Dictionary<string, string>();

    try
    {
        // spin up a new WinWord.exe
        WordApp = new Application();

        // open the specified document
        WordApp.Documents.Open(file);

        // grab the custom properties container
        dynamic CustomProps = WordApp.ActiveDocument.CustomDocumentProperties;

        // extract each property
        foreach (var Prop in CustomProps)
        {
            DocProperties.Add(Prop.Name, Prop.Value);
        }
    }
    finally
    {
        // close doc and shutdown word
        if (WordApp != null)
        {
            if (WordApp.ActiveDocument != null)
            {
                WordApp.ActiveDocument.Close();
            }
            WordApp.Quit();
        }
    }

    // return properties
    return DocProperties;
}

This is so much easier and cleaner with .NET4’s new dynamic capabilities and how nice it plays with COM. To use this, add the following references to your project:

  • Microsoft.Office.Interop.Word, v12
  • Office, v12