fp.write(MSG_BODY)
    fp.close()


if __name__ == "__main__":

    dbConnector = MSGDBConnector()
    cursor = dbConnector.conn.cursor()
    weatherUtil = MSGWeatherDataUtil()
    timeUtil = MSGTimeUtil()

    msg = "Downloading NOAA weather data (%s)." % timeUtil.conciseNow()
    print msg
    MSG_BODY = "%s\n" % msg

    msg = "Last loaded date is %s." % weatherUtil.datePart(datetime=weatherUtil.getLastDateLoaded(cursor))
    print msg
    MSG_BODY += "%s\n" % msg

    retriever = MSGWeatherDataRetriever()
    configer = MSGConfiger()
    WEATHER_DATA_PATH = configer.configOptionValue("Weather Data", "weather_data_path")

    msg = "Using URL %s." % weatherDataURL
    print msg
    MSG_BODY += "%s\n" % msg

    msg = "Using pattern %s." % weatherDataPattern
    print msg
    MSG_BODY += "%s\n" % msg
                                                      [KAHULUI_AIRPORT]),
                                                  commit=True)
            allDays += weatherDays

            fileObject.close()
            if TESTING:
                break

    else:  # Only process the latest data from the last loaded date.
        weatherUtil = MSGWeatherDataUtil()
        keepList = weatherUtil.getKeepList(weatherUtil.fileList,
                                           connector.conn.cursor())

        print "keep list = %s" % keepList

        keepDates = [weatherUtil.datePart(filename=k) for k in keepList]
        hourlyNames = [k + 'hourly.txt.gz' for k in keepDates]

        for n in hourlyNames:
            fullPath = os.path.join(root, n)
            msg = fullPath
            print msg
            msgBody += "Processing %s.\n" % msg
            fileObject = gzip.open(fullPath, "rb")
            weatherDays = inserter.insertDataDict(conn,
                                                  'WeatherNOAA',
                                                  dataParser.parseWeatherData(
                                                      fileObject,
                                                      [KAHULUI_AIRPORT]),
                                                  commit=True)
            allDays += weatherDays
                                                      [KAHULUI_AIRPORT]),
                                                  commit = True)
            allDays += weatherDays

            fileObject.close()
            if TESTING:
                break

    else: # Only process the latest data from the last loaded date.
        weatherUtil = MSGWeatherDataUtil()
        keepList = weatherUtil.getKeepList(weatherUtil.fileList,
                                           connector.conn.cursor())

        print "keep list = %s" % keepList

        keepDates = [weatherUtil.datePart(filename = k) for k in keepList]
        hourlyNames = [k + 'hourly.txt.gz' for k in keepDates]

        for n in hourlyNames:
            fullPath = os.path.join(root, n)
            msg = fullPath
            print msg
            msgBody += "Processing %s.\n" % msg
            fileObject = gzip.open(fullPath, "rb")
            weatherDays = inserter.insertDataDict(conn, 'WeatherNOAA',
                                                  dataParser.parseWeatherData(
                                                      fileObject,
                                                      [KAHULUI_AIRPORT]),
                                                  commit = True)
            allDays += weatherDays
    fp.write(MSG_BODY)
    fp.close()


if __name__ == '__main__':

    dbConnector = MSGDBConnector()
    cursor = dbConnector.conn.cursor()
    weatherUtil = MSGWeatherDataUtil()
    timeUtil = MSGTimeUtil()

    msg = "Downloading NOAA weather data (%s)." % timeUtil.conciseNow()
    print msg
    MSG_BODY = '%s\n' % msg

    msg = "Last loaded date is %s." % weatherUtil.datePart(
        datetime=weatherUtil.getLastDateLoaded(cursor))
    print msg
    MSG_BODY += '%s\n' % msg

    retriever = MSGWeatherDataRetriever()
    configer = MSGConfiger()
    WEATHER_DATA_PATH = configer.configOptionValue('Weather Data',
                                                   'weather_data_path')

    msg = "Using URL %s." % weatherDataURL
    print msg
    MSG_BODY += '%s\n' % msg

    msg = "Using pattern %s." % weatherDataPattern
    print msg
    MSG_BODY += '%s\n' % msg