def test_subtraction(self):
        dt1 = '2019-10-10T03:40:00Z'
        tudt1 = weather_utils.TimeUtils(dt1)
        dt2 = '2019-10-11T05:40:00Z'
        tudt2 = weather_utils.TimeUtils(dt2)

        dt12 = tudt2 - tudt1 
        self.assertEqual(dt12.days,1)
        self.assertEqual(dt12.seconds,7200)
 def test_random(self):
     in_window = True
     dt1 = '2019-10-10T03:40:00Z'
     tudt1 = weather_utils.TimeUtils(dt1)
     dt2 = '2019-10-11T05:40:00Z'
     tudt2 = weather_utils.TimeUtils(dt2)
     for i in range(20):
         rt = weather_utils.TimeUtils.randtime(dt1,dt2)
         if rt.datetime < tudt1.datetime or rt.datetime > tudt2.datetime:
             in_window = False
     self.assertTrue(in_window)       
    def test_datetime(self):

        dt1 = '2019-10-10T03:40:00Z'   # Zulu time format
        dt2 = (2012,12,12,13,3)        # Time tuple format (Should be Pacific)
        dt3 = '201910092311'           # Synoptic format
        dta = (2012,88,0,0,0)          # Nonsense time
        dtb = 'foobiar'                # Random string
        
        tudt1 = weather_utils.TimeUtils(dt1)
        tudt2 = weather_utils.TimeUtils(dt2)
        tudt3 = weather_utils.TimeUtils(dt3)

        self.assertEqual(tudt1.datetime.datetime.month,10)
        self.assertEqual(tudt2.datetime.datetime.year,2012)
        self.assertEqual(tudt3.datetime.datetime.minute,11)

        with self.assertRaises(ValueError):
            tudta = weather_utils.TimeUtils(dta)
        with self.assertRaises(ValueError):
            tudtb = weather_utils.TimeUtils(dtb)
    def test_max_gust(self):

        # I1004 test
        ttpl = (1,2)
        gtpl = (4,8)
        lat = 38.801857
        lon = -122.817551
        tm = weather_utils.TimeUtils('201609251734')
        mg = weather_utils.get_max_gust(lat,lon,tm,ttpl,gtpl,self.mydb)
        self.assertEqual(mg[0][1][0],'HWKC1')
        self.assertEqual(mg[1][1][4]/mg[0][1][4],2)  # Should be double in tm window
Beispiel #5
0
try:
    igndb = weather_utils.WeatherDB(weather_db)
except:
    igndb = weather_utils.WeatherDB.create(weather_db)

logging.info('Opening workbook ' + xl_data)
wbk = load_workbook(filename=xl_data)

sht_all = wbk[xl_input_sheet]
try:
    sht_wind = wbk[xl_output_sheet]
except KeyError:
    sht_wind = wbk.copy_worksheet(sht_all)
    sht_wind.title = xl_output_sheet

dtstart = weather_utils.TimeUtils(start_date)
dtend = weather_utils.TimeUtils(end_date)

for ievt in range(nevents):

    irow = frow + random.randrange(lrow - frow)

    # Copy row

    srow = str(irow)
    logging.info("Event #" + str(ievt) + " Processing line " + srow)
    jrow = ievt + frow + offset
    logging.debug("Writing row " + str(jrow))
    for icol in range(len(sht_all[irow])):
        sht_wind.cell(row=jrow,
                      column=icol + 1).value = sht_all[irow][icol].value
                tmxl = float(tmxlt)
            elif float(tmxlt) < 1.0:
                tmxl = float(tmxld) + float(tmxlt)
            else:
                raise TypeError("Unrecognized time value pair B:" +
                                str(tmxld) + " C:" + str(tmxlt))
            tmpydt = datetime(*xlrd.xldate_as_tuple(tmxl, 0))
    except TypeError:
        logging.warning("Exiting on error. Saving workbook " + xl_data)
        wbk.save(xl_data)
        raise

    tmlocal = pytz.timezone("America/Los_Angeles").localize(tmpydt)
    tmutc = tmlocal.astimezone(pytz.utc)

    zigtime = weather_utils.TimeUtils(tmutc)
    lat = sht_wind["D" + srow].value
    lon = sht_wind["E" + srow].value

    # Get list of max wind data based on time and space windows
    # Output is m x n list of  [station_id,time,distance,maximum gust,count]

    try:
        max_gusts = weather_utils.get_max_gust(lat, lon, zigtime, ttpl, gtpl,
                                               pgeigndb)
    except:
        logging.warning("Exiting on error. Saving workbook " + xl_data)
        wbk.save(xl_data)
        raise

    # Write output to cells
                tmxl = float(tmxlt)
            elif float(tmxlt) < 1.0:
                tmxl = float(tmxld) + float(tmxlt)
            else:
                raise TypeError("Unrecognized time value pair B:" +
                                str(tmxld) + " C:" + str(tmxlt))
            tmpydt = datetime(*xlrd.xldate_as_tuple(tmxl, 0))
    except TypeError:
        logging.warning("Exiting on error. Saving workbook " + xl_data)
        wbk.save(xl_data)
        raise

    tmlocal = pytz.timezone("America/Los_Angeles").localize(tmpydt)
    tmutc = tmlocal.astimezone(pytz.utc)
    tmmid = tmutc - timedelta(hours=ttpl[-1])
    zmidtime = weather_utils.TimeUtils(tmmid)

    lat = sht_wind[weather_config.config[program_args.utility]['XL_LAT_COLUMN']
                   + srow].value
    lon = sht_wind[
        weather_config.config[program_args.utility]['XL_LONG_COLUMN'] +
        srow].value

    # Get list of max wind data based on time and space windows
    # Output is m x n list of  [station_id,time,distance,maximum gust,count]

    try:
        max_gusts = weather_utils.get_max_gust(lat, lon, zmidtime, ttpl, gtpl,
                                               weatherdb)
    except:
        logging.warning("Exiting on error. Saving workbook " + xl_data)
    def test_synop(self):

        dt1 = '2019-10-10T03:40:00Z'
        tudt1 = weather_utils.TimeUtils(dt1)
        sydt1 = tudt1.synop()
        self.assertEqual(sydt1,'201910100340')