def extract_phase(zdrdir, datadir, date, outdir):

    filelist = glob.glob(datadir + date + '/*.nc')
    if len(filelist) > 0:
        ml_file = os.path.join(zdrdir, date, 'day_ml_zdr.csv')
        ml_data = pd.read_csv(ml_file, index_col=0, parse_dates=True)
        rain_index = np.where(ml_data['ML'].notnull())[0]
        all_phase = []
        time = []
        for file in rain_index[0:10]:
            print(filelist[file])
            rad = pyart.io.read(filelist[file], delay_field_loading=True)
            try:
                IP = pyart.correct.phase_proc.det_sys_phase(
                    rad,
                    ncp_lev=0.4,
                    rhohv_lev=0.6,
                    ncp_field='SQI',
                    rhv_field='RhoHV',
                    phidp_field='uPhiDP')
                all_phase.append(IP)
                time_of_file = filelist[file][120:135]
                time.append(time_of_file)
            except:
                print(date, 'no uphidp')
                return False

    pdtime = pd.to_datetime(time, format='%Y%m%d-%H%M%S')
    data = pd.DataFrame({'Initial Phase': all_phase}, index=pdtime)
    phase_file = os.path.join(outdir, 'initial_phase_' + date + '.csv')
    data.to_csv(phase_file)
    return True
Example #2
0
def get_plot():
    img = io.BytesIO()
    time = []
    consumed = []
    produced = []
    with open('test1.csv') as data:
        next(data)
        for line in csv.reader(data):
            datetime_obj = datetime.strptime(line[0], '%d-%m-%y %H:%M')
            time.append(datetime_obj)
            consumed.append(line[1])
            produced.append(line[2].strip('-'))
    cons = go.Scatter(x=time,
                      y=consumed,
                      name="Consumed Electricity",
                      line=dict(color='#17BECF'),
                      opacity=0.8)

    prod = go.Scatter(x=time,
                      y=produced,
                      name="Produced Electricity",
                      line=dict(color='#7F7F7F'),
                      opacity=0.8)

    data = [cons, prod]
    layout = go.Layout(title='Electricity consumption and production',
                       xaxis=dict(title='Time', ),
                       yaxis=dict(title='kWh', ))
    fig = dict(data=data, layout=layout)
    return plot(fig, output_type='div')
Example #3
0
def cast():
    if request.method == "GET":
        return render_template("forecast.html")
    else:
        city = request.form.get("location")
        result = weatherapp(city, None)
        if result["cod"] == "404" or result["name"] == "None":
            return apology("No data found for your search request", 403)
        data = forecast(result["coord"])
        timezoneoff = data["timezone_offset"]
        sunrise = []
        sunset = []
        time = []
        for info in data["daily"]:
            sunrise.append(
                datetime.fromtimestamp(info["sunrise"] + timezoneoff).time())
            sunset.append(
                datetime.fromtimestamp(info["sunset"] + timezoneoff).time())
        for i in range(8):
            time.append((dt.datetime.today() + dt.timedelta(days=i)).date())

        return render_template("forecastResults.html",
                               data=data,
                               city=city,
                               sunrise=sunrise,
                               sunset=sunset,
                               time=time)
Example #4
0
    def get_full_datetime(self):
        time = []

        if self.time_start:
            time.append(self.time_start.strftime('%H:%M'))

        if self.time_end:
            time.append(self.time_end.strftime('%H:%M'))

        return '%s %s' % (self.date.strftime('%d.%m.%Y'), '-'.join(time))
Example #5
0
    def flight_duration_list(self, data):
        """ This method retrieve a list which contains flight duration """

        time = []
        try:

            for path, _, node in jxmlease.parse(
                    data,
                    generator=
                    "tir38:TravelItinerary/tir38:ItineraryInfo/tir38:ReservationItems/tir38:Item/tir38:FlightSegment"
            ):
                time1 = node.get_xml_attr('ElapsedTime')
                time.append(str(time1))
        except:
            time = ['N/A']
        return time
Example #6
0
region = df['Region'].value_counts()
city = df['City'].value_counts()

df.describe()
df.dtypes

from datetime import datetime, time

date = []
time = []
for i in range(0, 127826):
    a = i
    aa = df.timestamp[i][0:10]
    date.append(aa)
    ab = df.timestamp[i][11:-1]
    time.append(ab)

date.append(df.timestamp[125825][0:10])
time.append(df.timestamp[125825][11:-1])

# Creating a Date column to store the actual Date format for the given Month column
df["Date"] = date
df["time"] = time

df["Date"] = pd.to_datetime(df.Date)
df["time"] = pd.to_datetime(df.time)

df["Date1"] = pd.to_datetime(df.Date, format="%b-%y")

# Extracting Day, weekday name, month name, year from the Date column using
# Date functions from pandas
Example #7
0
def calculate_activity_level(cow):
    # imports
    import pandas as pd
    from datetime import date
    from datetime import time
    from datetime import datetime
    from datetime import timedelta

    # convert time column from str to datetime type
    # sort in ascending order (chronologically)
    cow['time'] = pd.to_datetime(cow['time'])
    cow = cow.sort_values(by=['time'])

    # find the initial time value
    starting_time = cow.loc[0, 'time']

    # initialize the lists that are to be appended in the loop
    time = []
    activity_level = []

    # create one hour time slices
    end_time = starting_time
    while (end_time <= cow.iloc[len(cow) - 1, 0]):
        # create the 1-hour time range for the data to be filtered in
        new_time = end_time
        end_time = new_time + timedelta(hours=1)  # one hour slice

        # create date filter mask
        # greater than the start date and smaller than the end date
        # hold the data from the 1-hour timeslice in a placeholder dataframe
        mask = (cow['time'] > new_time) & (cow['time'] <= end_time)
        placeholder = cow[mask]

        # summarise the value counts for the labels in the placeholder dataframe
        summary = pd.DataFrame(placeholder['labels'].value_counts())
        summary['cluster'] = summary.index

        # rename columns
        summary.rename(columns={summary.columns[0]: "count"}, inplace=True)

        # add weights column
        # tabulate the values in the weight column
        # IMPORTANT: assumption is cluster 0 is low activity, cluster 1 is medium activity, cluster 2 is high activity
        summary['weight'] = None
        for i in range(len(summary)):
            if (summary.iloc[i, 1] == 0):
                summary.loc[i, 'weight'] = 0
            elif (summary.iloc[i, 1] == 1):
                summary.loc[i, 'weight'] = 0.1
            else:
                summary.loc[i, 'weight'] = 0.9

        # initialise the hourly activity level as an empty list
        hourly_activity_level = []

        # keep appending the product of weight and value count for each cluster label to the hourly_activity_level list
        for i in range(len(summary)):
            activity_level.append(summary.iloc[i, 0] * summary.iloc[i, 2])

        # calculate the sum of the elements in the hourly_activity_level list
        hourly_activity_level = sum(hourly_activity_level)

        # append the time and sum of hourly activity level to the time and activity_level lists
        time.append(new_time)
        activity_level.append(hourly_activity_level)

        # create a dataframe using the time and activity level lists
        activity_df = pd.DataFrame(list(zip(time, activity_level)),
                                   columns=['time', 'activity_level'])

        # convert the time column to datetime object
        activity_df['time'] = pd.to_datetime(activity_df['time'])

        # initialise the activity level columns for the previous 1, 24, 48, and 72 hours
        activity_df['activity_level_1'] = None
        activity_df['activity_level_24'] = None
        activity_df['activity_level_48'] = None
        activity_df['activity_level_72'] = None

        for i in range(len(activity_df)):

            timevalue_1 = activity_df.loc[i, 'time'] - timedelta(hours=1)
            timevalue_24 = activity_df.loc[i, 'time'] - timedelta(hours=24)
            timevalue_48 = activity_df.loc[i, 'time'] - timedelta(hours=48)
            timevalue_72 = activity_df.loc[i, 'time'] - timedelta(hours=72)

            # some errors arise due to duplicate time values being present in the data
            # trying to cheat my way through with the use of try except block
            try:
                activity_df.loc[i, 'activity_level_1'] = activity_df.loc[
                    activity_df['time'] ==
                    timevalue_1]['activity_level'].values[0]
                activity_df.loc[i, 'activity_level_24'] = activity_df.loc[
                    activity_df['time'] ==
                    timevalue_24]['activity_level'].values[0]
                activity_df.loc[i, 'activity_level_48'] = activity_df.loc[
                    activity_df['time'] ==
                    timevalue_48]['activity_level'].values[0]
                activity_df.loc[i, 'activity_level_72'] = activity_df.loc[
                    activity_df['time'] ==
                    timevalue_72]['activity_level'].values[0]
            except:
                pass

    # return the activity level dataframe
    return activity_df