コード例 #1
0
    def create_current_obs_graphs(self):

        location_objects = []

        for i in range(0, len(self.loc_names)):

            root_folder = "C:\Users\Nathan\Documents\Storm Chasing\Chases\\"
            date_path = arrow_now().format('YYYY-MM-DD')

            for j in range(0, len(self.obs_list)):
                if self.loc_names[i] == self.obs_list[j].code:

                    code = self.obs_list[j].code
                    loc_name = self.obs_list[j].loc_name
                    lat = self.obs_list[j].lat
                    lon = self.obs_list[j].lon
                    height = self.obs_list[j].height
                    break
            with open(root_folder + date_path + "\Observations\\" +
                      self.loc_names[i] + ".csv") as f:
                for line in f:
                    line = line.split(',')
                    location_objects.append(
                        ObservationLocation(code, loc_name, lat, lon, height))
                    location_objects[len(location_objects) - 1].time = line[0]
                    location_objects[len(location_objects) - 1].temp = line[1]
                    location_objects[len(location_objects) - 1].dew = line[2]
                    location_objects[len(location_objects) - 1].rain = line[3]
                    location_objects[len(location_objects) -
                                     1].pressure = line[4]
                    location_objects[len(location_objects) - 1].lcl = line[5]
                    location_objects[len(location_objects) -
                                     1].rel_hum = line[6]
                    location_objects[len(location_objects) -
                                     1].wind_vel = line[7]
                    location_objects[len(location_objects) - 1].wind_dir = str(
                        line[8]).replace('\n', '')

        print
        t = 0

        dpi_int = 40
        fig = plt.figure(figsize=(16, 9))

        for k in self.loc_names:

            loc_obs_temp = []
            loc_obs_dew = []
            loc_obs_rain = []
            loc_obs_pressure = []
            loc_obs_lcl = []
            loc_obs_rel_hum = []
            loc_obs_wind_vel = []
            loc_obs_wind_dir = []

            for j in location_objects:

                if k == j.code:
                    loc_obs_temp.append([j.time, j.temp])
                    loc_obs_dew.append([j.time, j.dew])
                    loc_obs_rain.append([j.time, j.rain])
                    loc_obs_pressure.append([j.time, j.pressure])
                    loc_obs_lcl.append([j.time, j.lcl])
                    loc_obs_rel_hum.append([j.time, j.rel_hum])
                    loc_obs_wind_vel.append([j.time, j.wind_vel])
                    loc_obs_wind_dir.append([j.time, j.wind_dir])

            if True:
                print
                print str(
                    int(round(100 * float(t) / float(len(self.loc_names)),
                              0))) + "% Done"

                t += 1
                print "Beginning to create graphs for", k
                if len(loc_obs_temp) > 1:

                    # Code to create graphs go here.

                    # Create temperature graph first

                    graph_type = "temperature"

                    file_short_list = []
                    for f in listdir(root_folder + date_path +
                                     "\Observations\\"):
                        if re_match(k + "-" + graph_type, f):
                            file_short_list.append(f)
                    if len(file_short_list) > 1:
                        file_short_list.sort()
                        file_short_list.remove(file_short_list[-1])
                        for g in file_short_list:
                            os_remove(root_folder + date_path +
                                      "\Observations\\" + g)

                    print "Creating", graph_type, "graph"

                    x = []
                    y = []

                    for m in loc_obs_temp:
                        try:
                            y.append(float(m[1]))
                            x.append(datetime.fromtimestamp(float(m[0])))
                        except ValueError:
                            continue
                    try:
                        time_now = arrow_get(x[len(x) - 1]).format('HH-mm')
                        with open(root_folder + date_path + "\Observations\\" +
                                  k + "-" + graph_type + "-" + time_now +
                                  ".png"):
                            print "Graph Already Exists"
                    except IOError:

                        ax = fig.add_subplot(111)
                        ax.plot(x, y)

                        for xy in zip(x, y):
                            ax.annotate(xy[1], xy=xy, textcoords='data')

                        buf = (max(y) - min(y)) * .1
                        ax.get_yaxis().get_major_formatter().set_useOffset(
                            False)
                        ax.axis(
                            [x[0], x[len(x) - 1],
                             min(y) - buf,
                             max(y) + buf])

                        fig.tight_layout(pad=3)
                        print "Saving"
                        fig.savefig(
                            root_folder + date_path + "\Observations\\" + k +
                            "-" + graph_type + "-" + time_now + ".png",
                            dpi=dpi_int,
                        )
                        fig.clf()
                    except IndexError:
                        pass

                    # Create dew points graph

                    graph_type = "dew-point"

                    file_short_list = []
                    for f in listdir(root_folder + date_path +
                                     "\Observations\\"):
                        if re_match(k + "-" + graph_type, f):
                            file_short_list.append(f)
                    if len(file_short_list) > 1:
                        file_short_list.sort()
                        file_short_list.remove(file_short_list[-1])
                        for g in file_short_list:
                            os_remove(root_folder + date_path +
                                      "\Observations\\" + g)

                    print "Creating", graph_type, "graph"

                    x = []
                    y = []

                    for m in loc_obs_dew:
                        try:
                            y.append(float(m[1]))
                            x.append(datetime.fromtimestamp(float(m[0])))
                        except ValueError:
                            continue
                    try:
                        time_now = arrow_get(x[len(x) - 1]).format('HH-mm')
                        with open(root_folder + date_path + "\Observations\\" +
                                  k + "-" + graph_type + "-" + time_now +
                                  ".png"):
                            print "Graph Already Exists"
                    except IOError:

                        ax = fig.add_subplot(111)
                        ax.plot(x, y)

                        for xy in zip(x, y):
                            ax.annotate(xy[1], xy=xy, textcoords='data')

                        buf = (max(y) - min(y)) * .1
                        ax.get_yaxis().get_major_formatter().set_useOffset(
                            False)
                        ax.axis(
                            [x[0], x[len(x) - 1],
                             min(y) - buf,
                             max(y) + buf])

                        fig.tight_layout(pad=3)
                        print "Saving"
                        fig.savefig(root_folder + date_path +
                                    "\Observations\\" + k + "-" + graph_type +
                                    "-" + time_now + ".png",
                                    dpi=dpi_int)
                        fig.clf()
                    except IndexError:
                        pass

                    # Create rain points graph

                    graph_type = "rain"

                    file_short_list = []
                    for f in listdir(root_folder + date_path +
                                     "\Observations\\"):
                        if re_match(k + "-" + graph_type, f):
                            file_short_list.append(f)
                    if len(file_short_list) > 1:
                        file_short_list.sort()
                        file_short_list.remove(file_short_list[-1])
                        for g in file_short_list:
                            os_remove(root_folder + date_path +
                                      "\Observations\\" + g)

                    print "Creating", graph_type, "graph"

                    x = []
                    y = []

                    for m in loc_obs_rain:
                        try:
                            y.append(float(m[1]))
                            x.append(datetime.fromtimestamp(float(m[0])))
                        except ValueError:
                            continue
                    try:
                        time_now = arrow_get(x[len(x) - 1]).format('HH-mm')
                        with open(root_folder + date_path + "\Observations\\" +
                                  k + "-" + graph_type + "-" + time_now +
                                  ".png"):
                            print "Graph Already Exists"
                    except IOError:

                        ax = fig.add_subplot(111)
                        ax.plot(x, y)

                        for xy in zip(x, y):
                            ax.annotate(xy[1], xy=xy, textcoords='data')

                        buf = (max(y) - min(y)) * .1
                        ax.get_yaxis().get_major_formatter().set_useOffset(
                            False)
                        ax.axis(
                            [x[0], x[len(x) - 1],
                             min(y) - buf,
                             max(y) + buf])

                        fig.tight_layout(pad=3)
                        print "Saving"
                        fig.savefig(root_folder + date_path +
                                    "\Observations\\" + k + "-" + graph_type +
                                    "-" + time_now + ".png",
                                    dpi=dpi_int)
                        fig.clf()
                    except IndexError:
                        pass

                    # Create Pressure Graph

                    graph_type = "pressure"

                    file_short_list = []
                    for f in listdir(root_folder + date_path +
                                     "\Observations\\"):
                        if re_match(k + "-" + graph_type, f):
                            file_short_list.append(f)
                    if len(file_short_list) > 1:
                        file_short_list.sort()
                        file_short_list.remove(file_short_list[-1])
                        for g in file_short_list:
                            os_remove(root_folder + date_path +
                                      "\Observations\\" + g)

                    print "Creating", graph_type, "graph"

                    x = []
                    y = []

                    for m in loc_obs_pressure:
                        try:
                            y.append(float(m[1]))
                            x.append(datetime.fromtimestamp(float(m[0])))
                        except ValueError:
                            continue
                    try:
                        time_now = arrow_get(x[len(x) - 1]).format('HH-mm')
                        with open(root_folder + date_path + "\Observations\\" +
                                  k + "-" + graph_type + "-" + time_now +
                                  ".png"):
                            print "Graph Already Exists"
                    except IOError:

                        ax = fig.add_subplot(111)
                        ax.plot(x, y)

                        for xy in zip(x, y):
                            ax.annotate(xy[1], xy=xy, textcoords='data')

                        buf = (max(y) - min(y)) * .1
                        ax.get_yaxis().get_major_formatter().set_useOffset(
                            False)
                        ax.axis(
                            [x[0], x[len(x) - 1],
                             min(y) - buf,
                             max(y) + buf])

                        fig.tight_layout(pad=3)
                        print "Saving"
                        fig.savefig(root_folder + date_path +
                                    "\Observations\\" + k + "-" + graph_type +
                                    "-" + time_now + ".png",
                                    dpi=dpi_int)
                        fig.clf()
                    except IndexError:
                        pass

                    # Create LCL Graph

                    graph_type = "lcl"

                    file_short_list = []
                    for f in listdir(root_folder + date_path +
                                     "\Observations\\"):
                        if re_match(k + "-" + graph_type, f):
                            file_short_list.append(f)
                    if len(file_short_list) > 1:
                        file_short_list.sort()
                        file_short_list.remove(file_short_list[-1])
                        for g in file_short_list:
                            os_remove(root_folder + date_path +
                                      "\Observations\\" + g)

                    print "Creating", graph_type, "graph"

                    x = []
                    y = []

                    for m in loc_obs_lcl:
                        try:
                            y.append(float(m[1]))
                            x.append(datetime.fromtimestamp(float(m[0])))
                        except ValueError:
                            continue
                    try:
                        time_now = arrow_get(x[len(x) - 1]).format('HH-mm')
                        with open(root_folder + date_path + "\Observations\\" +
                                  k + "-" + graph_type + "-" + time_now +
                                  ".png"):
                            print "Graph Already Exists"
                    except IOError:

                        ax = fig.add_subplot(111)
                        ax.plot(x, y)

                        for xy in zip(x, y):
                            ax.annotate(xy[1], xy=xy, textcoords='data')

                        buf = (max(y) - min(y)) * .1
                        ax.get_yaxis().get_major_formatter().set_useOffset(
                            False)
                        ax.axis(
                            [x[0], x[len(x) - 1],
                             min(y) - buf,
                             max(y) + buf])

                        fig.tight_layout(pad=3)
                        print "Saving"
                        fig.savefig(root_folder + date_path +
                                    "\Observations\\" + k + "-" + graph_type +
                                    "-" + time_now + ".png",
                                    dpi=dpi_int)
                        fig.clf()
                    except IndexError:
                        pass

                    # Create relative humidity Graph

                    graph_type = "rel_hum"

                    file_short_list = []
                    for f in listdir(root_folder + date_path +
                                     "\Observations\\"):
                        if re_match(k + "-" + graph_type, f):
                            file_short_list.append(f)
                    if len(file_short_list) > 1:
                        file_short_list.sort()
                        file_short_list.remove(file_short_list[-1])
                        for g in file_short_list:
                            os_remove(root_folder + date_path +
                                      "\Observations\\" + g)

                    print "Creating", graph_type, "graph"

                    x = []
                    y = []

                    for m in loc_obs_rel_hum:
                        try:
                            y.append(float(m[1]))
                            x.append(datetime.fromtimestamp(float(m[0])))
                        except ValueError:
                            continue
                    try:
                        time_now = arrow_get(x[len(x) - 1]).format('HH-mm')
                        with open(root_folder + date_path + "\Observations\\" +
                                  k + "-" + graph_type + "-" + time_now +
                                  ".png"):
                            print "Graph Already Exists"
                    except IOError:

                        ax = fig.add_subplot(111)
                        ax.plot(x, y)

                        for xy in zip(x, y):
                            ax.annotate(xy[1], xy=xy, textcoords='data')

                        buf = (max(y) - min(y)) * .1
                        ax.get_yaxis().get_major_formatter().set_useOffset(
                            False)
                        ax.axis(
                            [x[0], x[len(x) - 1],
                             min(y) - buf,
                             max(y) + buf])

                        fig.tight_layout(pad=3)
                        print "Saving"
                        fig.savefig(root_folder + date_path +
                                    "\Observations\\" + k + "-" + graph_type +
                                    "-" + time_now + ".png",
                                    dpi=dpi_int)
                        fig.clf()
                    except IndexError:
                        pass

                    # Create wind speed graph

                    graph_type = "wind"

                    file_short_list = []
                    for f in listdir(root_folder + date_path +
                                     "\Observations\\"):
                        if re_match(k + "-" + graph_type, f):
                            file_short_list.append(f)
                    if len(file_short_list) > 1:
                        file_short_list.sort()
                        file_short_list.remove(file_short_list[-1])
                        for g in file_short_list:
                            os_remove(root_folder + date_path +
                                      "\Observations\\" + g)

                    print "Creating", graph_type, "graph"

                    x = []
                    y = []

                    for m in loc_obs_wind_vel:
                        try:
                            y.append(float(m[1]))
                            x.append(datetime.fromtimestamp(float(m[0])))
                        except ValueError:
                            continue
                    try:
                        time_now = arrow_get(x[len(x) - 1]).format('HH-mm')
                        with open(root_folder + date_path + "\Observations\\" +
                                  k + "-" + graph_type + "-" + time_now +
                                  ".png"):
                            print "Graph Already Exists"
                    except IOError:

                        ax = fig.add_subplot(111)
                        ax.plot(x, y)

                        for xy in zip(x, y):
                            ax.annotate(xy[1], xy=xy, textcoords='data')

                        buf = (max(y) - min(y)) * .1
                        ax.get_yaxis().get_major_formatter().set_useOffset(
                            False)
                        ax.axis(
                            [x[0], x[len(x) - 1],
                             min(y) - buf,
                             max(y) + buf])

                        fig.tight_layout(pad=3)
                        print "Saving"
                        fig.savefig(root_folder + date_path +
                                    "\Observations\\" + k + "-" + graph_type +
                                    "-" + time_now + ".png",
                                    dpi=dpi_int)
                        fig.clf()
                    except IndexError:
                        pass
コード例 #2
0
    def create_current_overlays(self):

        root_folder = "C:\Users\Nathan\Documents\Storm Chasing\Chases\\"
        date_path = arrow_now().format('YYYY-MM-DD')

        config_filename = root_folder + date_path + "\Observations\\" + "range.cfg"

        min_temp = "-"
        max_temp = "-"
        min_dew = "-"
        max_dew = "-"
        min_pressure = "-"
        max_pressure = "-"
        min_rain = "-"
        max_rain = "-"
        min_rel_hum = "-"
        max_rel_hum = "-"
        min_lcl = "-"
        max_lcl = "-"
        min_wind_vel = "-"
        max_wind_vel = "-"

        with open(config_filename, 'r') as f:
            line = f.readline()
            line_list = line.split(',')
            min_temp = float(line_list[0])
            max_temp = float(line_list[1].replace('\n', ''))

            line = f.readline()
            line_list = line.split(',')
            min_dew = float(line_list[0])
            max_dew = float(line_list[1].replace('\n', ''))

            line = f.readline()
            line_list = line.split(',')
            min_rain = float(line_list[0])
            max_rain = float(line_list[1].replace('\n', ''))

            line = f.readline()
            line_list = line.split(',')
            min_pressure = float(line_list[0])
            max_pressure = float(line_list[1].replace('\n', ''))

            line = f.readline()
            line_list = line.split(',')
            min_rel_hum = float(line_list[0])
            max_rel_hum = float(line_list[1].replace('\n', ''))

            line = f.readline()
            line_list = line.split(',')
            min_lcl = float(line_list[0])
            max_lcl = float(line_list[1].replace('\n', ''))

            line = f.readline()
            line_list = line.split(',')
            min_wind_vel = float(line_list[0])
            max_wind_vel = float(line_list[1].replace('\n', ''))
        print

        # Temperature Graph
        if True:
            graph_type = "temp"

            time_now = arrow_get(self.obs_list[36].time,
                                 'HH:mmA').format('HH-mm')
            graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

            try:
                with open(graph_filename, 'r') as f:
                    print "Graph already created"
            except IOError:
                x = []
                y = []
                z = []
                for i in range(0, len(self.obs_list)):
                    if self.obs_list[i].temp == "-":
                        continue
                    y.append(self.obs_list[i].lat)
                    x.append(self.obs_list[i].lon)
                    z.append(self.obs_list[i].temp)
                resolution = 100
                xi = linspace(141, 150, resolution)
                yi = linspace(-39, -34, resolution)
                zi = griddata((x, y),
                              z, (xi[None, :], yi[:, None]),
                              method='cubic')
                print min(z), max(z)
                w = 15
                h = 12.5
                fig = plt.figure(figsize=(w, h),
                                 facecolor='w',
                                 frameon=False,
                                 edgecolor="Yellow")
                ax = fig.add_subplot(111)
                ax.contourf(xi,
                            yi,
                            zi,
                            120,
                            cmap=plt.cm.jet,
                            vmin=min_temp,
                            vmax=max_temp)
                C = ax.contour(xi, yi, zi, 20, linewidths=0.5, colors='k')
                ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                ax.scatter(x, y, marker='o', c='b', s=.1)
                ax.axis([141, 150, -39, -34])
                ax.set_axis_off()
                fig.subplots_adjust(left=0,
                                    right=1,
                                    bottom=0,
                                    top=1,
                                    hspace=0,
                                    wspace=0)
                fig.tight_layout(pad=0)

                print "Start Save for", graph_type
                print graph_filename
                self.SaveFigureAsImage(graph_filename, fig=fig)
                print "End Save"

        # Dew Point Graph
        if True:
            graph_type = "dew"

            time_now = arrow_get(self.obs_list[36].time,
                                 'HH:mmA').format('HH-mm')
            graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

            try:
                with open(graph_filename, 'r') as f:
                    print "Graph already created"
            except IOError:
                x = []
                y = []
                z = []
                for i in range(0, len(self.obs_list)):
                    if self.obs_list[i].dew == "-":
                        continue
                    y.append(self.obs_list[i].lat)
                    x.append(self.obs_list[i].lon)
                    z.append(self.obs_list[i].dew)
                resolution = 100
                xi = linspace(141, 150, resolution)
                yi = linspace(-39, -34, resolution)
                zi = griddata((x, y),
                              z, (xi[None, :], yi[:, None]),
                              method='cubic')
                w = 15
                h = 12.5
                fig = plt.figure(figsize=(w, h),
                                 facecolor='w',
                                 frameon=False,
                                 edgecolor="Yellow")
                ax = fig.add_subplot(111)
                ax.contourf(xi,
                            yi,
                            zi,
                            15,
                            cmap=plt.cm.jet,
                            vmin=min_dew,
                            vmax=max_dew)
                C = ax.contour(xi, yi, zi, 20, linewidths=0.5, colors='k')
                ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                ax.scatter(x, y, marker='o', c='b', s=.1)
                ax.axis([141, 150, -39, -34])
                ax.set_axis_off()
                fig.subplots_adjust(left=0,
                                    right=1,
                                    bottom=0,
                                    top=1,
                                    hspace=0,
                                    wspace=0)
                fig.tight_layout(pad=0)

                print "Start Save for", graph_type
                print graph_filename
                self.SaveFigureAsImage(graph_filename, fig=fig)
                print "End Save"

        # Rain Graph
        if True:
            graph_type = "rain"

            time_now = arrow_get(self.obs_list[36].time,
                                 'HH:mmA').format('HH-mm')
            graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

            try:
                with open(graph_filename, 'r') as f:
                    print "Graph already created"
            except IOError:
                x = []
                y = []
                z = []
                for i in range(0, len(self.obs_list)):
                    if self.obs_list[i].rain == "-":
                        continue
                    y.append(self.obs_list[i].lat)
                    x.append(self.obs_list[i].lon)
                    z.append(self.obs_list[i].rain)
                resolution = 100
                xi = linspace(141, 150, resolution)
                yi = linspace(-39, -34, resolution)
                zi = griddata((x, y),
                              z, (xi[None, :], yi[:, None]),
                              method='cubic')
                w = 15
                h = 12.5
                fig = plt.figure(figsize=(w, h),
                                 facecolor='w',
                                 frameon=False,
                                 edgecolor="Yellow")
                ax = fig.add_subplot(111)
                ax.contourf(xi,
                            yi,
                            zi,
                            15,
                            cmap=plt.cm.jet,
                            vmin=min_rain,
                            vmax=max_rain)
                C = ax.contour(xi, yi, zi, 20, linewidths=0.5, colors='k')
                ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                ax.scatter(x, y, marker='o', c='b', s=.1)
                ax.axis([141, 150, -39, -34])
                ax.set_axis_off()
                fig.subplots_adjust(left=0,
                                    right=1,
                                    bottom=0,
                                    top=1,
                                    hspace=0,
                                    wspace=0)
                fig.tight_layout(pad=0)

                print "Start Save for", graph_type
                print graph_filename
                self.SaveFigureAsImage(graph_filename, fig=fig)
                print "End Save"

        # Pressure Point Graph
        if True:
            graph_type = "pressure"

            time_now = arrow_get(self.obs_list[36].time,
                                 'HH:mmA').format('HH-mm')
            graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

            try:
                with open(graph_filename, 'r') as f:
                    print "Graph already created"
            except IOError:
                x = []
                y = []
                z = []
                for i in range(0, len(self.obs_list)):
                    if self.obs_list[i].pressure == "-":
                        continue
                    y.append(self.obs_list[i].lat)
                    x.append(self.obs_list[i].lon)
                    z.append(self.obs_list[i].pressure)
                resolution = 100
                xi = linspace(141, 150, resolution)
                yi = linspace(-39, -34, resolution)
                zi = griddata((x, y),
                              z, (xi[None, :], yi[:, None]),
                              method='cubic')
                w = 15
                h = 12.5
                fig = plt.figure(figsize=(w, h),
                                 facecolor='w',
                                 frameon=False,
                                 edgecolor="Yellow")
                ax = fig.add_subplot(111)
                ax.contourf(xi,
                            yi,
                            zi,
                            15,
                            cmap=plt.cm.jet,
                            vmin=min_pressure,
                            vmax=max_pressure)
                C = ax.contour(xi, yi, zi, 20, linewidths=0.5, colors='k')
                ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                ax.scatter(x, y, marker='o', c='b', s=.1)
                ax.axis([141, 150, -39, -34])
                ax.set_axis_off()
                fig.subplots_adjust(left=0,
                                    right=1,
                                    bottom=0,
                                    top=1,
                                    hspace=0,
                                    wspace=0)
                fig.tight_layout(pad=0)

                print "Start Save for", graph_type
                print graph_filename
                self.SaveFigureAsImage(graph_filename, fig=fig)
                print "End Save"

        # Relative Humidity Graph
        if True:
            graph_type = "rel_hum"

            time_now = arrow_get(self.obs_list[36].time,
                                 'HH:mmA').format('HH-mm')
            graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

            try:
                with open(graph_filename, 'r') as f:
                    print "Graph already created"
            except IOError:
                x = []
                y = []
                z = []
                for i in range(0, len(self.obs_list)):
                    if self.obs_list[i].rel_hum == "-":
                        continue
                    y.append(self.obs_list[i].lat)
                    x.append(self.obs_list[i].lon)
                    z.append(self.obs_list[i].rel_hum)
                resolution = 100
                xi = linspace(141, 150, resolution)
                yi = linspace(-39, -34, resolution)
                zi = griddata((x, y),
                              z, (xi[None, :], yi[:, None]),
                              method='cubic')
                w = 15
                h = 12.5
                fig = plt.figure(figsize=(w, h),
                                 facecolor='w',
                                 frameon=False,
                                 edgecolor="Yellow")
                ax = fig.add_subplot(111)
                ax.contourf(xi,
                            yi,
                            zi,
                            15,
                            cmap=plt.cm.jet,
                            vmin=min_rel_hum,
                            vmax=max_rel_hum)
                C = ax.contour(xi, yi, zi, 20, linewidths=0.5, colors='k')
                ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                ax.scatter(x, y, marker='o', c='b', s=.1)
                ax.axis([141, 150, -39, -34])
                ax.set_axis_off()
                fig.subplots_adjust(left=0,
                                    right=1,
                                    bottom=0,
                                    top=1,
                                    hspace=0,
                                    wspace=0)
                fig.tight_layout(pad=0)

                print "Start Save for", graph_type
                print graph_filename
                self.SaveFigureAsImage(graph_filename, fig=fig)
                print "End Save"

        # LCL Graph
        if True:
            graph_type = "lcl"

            time_now = arrow_get(self.obs_list[36].time,
                                 'HH:mmA').format('HH-mm')
            graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

            try:
                with open(graph_filename, 'r') as f:
                    print "Graph already created"
            except IOError:
                x = []
                y = []
                z = []
                for i in range(0, len(self.obs_list)):
                    if self.obs_list[i].lcl == "-":
                        continue
                    y.append(self.obs_list[i].lat)
                    x.append(self.obs_list[i].lon)
                    z.append(self.obs_list[i].lcl)
                resolution = 100
                xi = linspace(141, 150, resolution)
                yi = linspace(-39, -34, resolution)
                zi = griddata((x, y),
                              z, (xi[None, :], yi[:, None]),
                              method='cubic')
                w = 15
                h = 12.5
                fig = plt.figure(figsize=(w, h),
                                 facecolor='w',
                                 frameon=False,
                                 edgecolor="Yellow")
                ax = fig.add_subplot(111)
                ax.contourf(xi,
                            yi,
                            zi,
                            15,
                            cmap=plt.cm.jet,
                            vmin=min_lcl,
                            vmax=max_lcl)
                C = ax.contour(xi, yi, zi, 20, linewidths=0.5, colors='k')
                ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                ax.scatter(x, y, marker='o', c='b', s=.1)
                ax.axis([141, 150, -39, -34])
                ax.set_axis_off()
                fig.subplots_adjust(left=0,
                                    right=1,
                                    bottom=0,
                                    top=1,
                                    hspace=0,
                                    wspace=0)
                fig.tight_layout(pad=0)

                print "Start Save for", graph_type
                print graph_filename
                self.SaveFigureAsImage(graph_filename, fig=fig)
                print "End Save"

        # Wind Graph
        if True:
            graph_type = "wind_vel"

            time_now = arrow_get(self.obs_list[36].time,
                                 'HH:mmA').format('HH-mm')
            graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

            try:
                with open(graph_filename, 'r') as f:
                    print "Graph already created"
            except IOError:
                x = []
                y = []
                z = []
                for i in range(0, len(self.obs_list)):
                    if self.obs_list[i].wind_vel == "-":
                        continue
                    y.append(self.obs_list[i].lat)
                    x.append(self.obs_list[i].lon)
                    z.append(self.obs_list[i].wind_vel)
                resolution = 100
                xi = linspace(141, 150, resolution)
                yi = linspace(-39, -34, resolution)
                zi = griddata((x, y),
                              z, (xi[None, :], yi[:, None]),
                              method='cubic')
                w = 15
                h = 12.5
                fig = plt.figure(figsize=(w, h),
                                 facecolor='w',
                                 frameon=False,
                                 edgecolor="Yellow")
                ax = fig.add_subplot(111)
                ax.contourf(xi,
                            yi,
                            zi,
                            15,
                            cmap=plt.cm.jet,
                            vmin=min_wind_vel,
                            vmax=max_wind_vel)
                C = ax.contour(xi, yi, zi, 20, linewidths=0.5, colors='k')
                ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                ax.scatter(x, y, marker='o', c='b', s=.1)
                ax.axis([141, 150, -39, -34])
                ax.set_axis_off()
                fig.subplots_adjust(left=0,
                                    right=1,
                                    bottom=0,
                                    top=1,
                                    hspace=0,
                                    wspace=0)
                fig.tight_layout(pad=0)

                print "Start Save for", graph_type
                print graph_filename
                self.SaveFigureAsImage(graph_filename, fig=fig)
                print "End Save"
コード例 #3
0
    def create_all_overlays(self):

        give_me_full_obs()

        root_folder = "C:\Users\Nathan\Documents\Storm Chasing\Chases\\"
        date_path = arrow_now().format('YYYY-MM-DD')

        config_filename = root_folder + date_path + "\Observations\\" + "range.cfg"

        dir_list = listdir(root_folder + date_path + "\Observations")

        csv_list = []
        for fn in dir_list:
            if fn[-3:] == "csv":
                csv_list.append(fn)

        len_loc = len(csv_list)
        for fn in csv_list:

            with open(root_folder + date_path + "\Observations\\" + fn,
                      'r') as f:
                i = 0
                for line in f:
                    i += 1
                    line_list = line.split(',')
                    len_details = len(line_list)
                    len_time = i

        len_details += 1

        all_obs = [[["" for k in xrange(len_details)]
                    for j in xrange(len_time)] for i in xrange(len_loc)]

        for i in range(0, len(csv_list)):
            fn = csv_list[i]
            with open(root_folder + date_path + "\Observations\\" + fn,
                      'r') as f:
                j = 0
                last_time = 0
                for line in f:
                    line_list = line.split(',')
                    this_time = int(line_list[0])
                    if j == 0:
                        for k in range(0, len(line_list)):
                            all_obs[i][j][k] = line_list[k]
                        all_obs[i][j][-1] = str(fn).split('.')[0]
                        last_time = this_time
                    elif this_time - last_time != 1800:
                        continue
                    else:
                        for k in range(0, len(line_list)):
                            all_obs[i][j][k] = line_list[k]
                        all_obs[i][j][-1] = str(fn).split('.')[0]
                        last_time = this_time

                    j += 1

        all_obs_sorted = [[["" for k in xrange(len_loc)]
                           for j in xrange(len_time)]
                          for i in xrange(len_details)]

        for i in range(0, len_loc):
            for j in range(0, len_time):
                for k in range(0, len_details):
                    all_obs_sorted[k][j][i] = all_obs[i][j][k]

        for gt in range(1, 6):

            if gt == 1:
                graph_type = "temp"
                min_graph = 0
                max_graph = 40
            elif gt == 2:
                graph_type = "dew"
                min_graph = 0
                max_graph = 25
            elif gt == 3:
                graph_type = "rain"
                min_graph = 0
                max_graph = 80
            elif gt == 4:
                graph_type = "pressure"
                min_graph = 990
                max_graph = 1050
            elif gt == 5:
                graph_type = "lcl"
                min_graph = 0
                max_graph = 3000
            elif gt == 6:
                graph_type = "rel_hum"
                min_graph = 0
                max_graph = 100
            elif gt == 7:
                graph_type = "wind_vel"
                min_graph = 0
                max_graph = 120

            w = 15
            h = 12.5
            fig = plt.figure(figsize=(w, h),
                             facecolor='w',
                             frameon=False,
                             edgecolor="Yellow")

            for i in range(0, len(all_obs_sorted[9])):
                x = []
                y = []
                z = []

                for obs in self.obs_list:
                    for j in range(0, len(all_obs_sorted[9][i])):
                        if obs.code == all_obs_sorted[9][i][j]:
                            try:
                                z.append(float(all_obs_sorted[gt][i][j]))
                                x.append(float(obs.lon))
                                y.append(float(obs.lat))
                            except ValueError:
                                continue
                            break

                if True:
                    try:
                        time_now = arrow_get(all_obs_sorted[0][i][36]).to(
                            'Australia/Melbourne').format('HH-mm')
                    except parser.ParserError:
                        continue
                    graph_filename = root_folder + date_path + "\Observations\\" + "vic-" + graph_type + "-" + time_now + ".png"

                    try:
                        with open(graph_filename, 'r'):
                            print "Graph already exists:"
                            print graph_filename
                            continue
                    except IOError:
                        pass

                    resolution = 100
                    xi = linspace(141, 150, resolution)
                    yi = linspace(-39, -34, resolution)
                    zi = griddata((x, y),
                                  z, (xi[None, :], yi[:, None]),
                                  method='cubic')
                    fig.clf()
                    ax = fig.add_subplot(111)
                    ax.contourf(xi,
                                yi,
                                zi,
                                120,
                                cmap=plt.cm.jet,
                                vmin=min_graph,
                                vmax=max_graph)
                    try:
                        C = ax.contour(xi,
                                       yi,
                                       zi,
                                       20,
                                       linewidths=0.5,
                                       colors='k')
                        ax.clabel(C, inline=1, fontsize=6, fmt='%1.0f')
                    except ValueError:
                        pass
                    ax.scatter(x, y, marker='o', c='b', s=.1)
                    ax.axis([141, 150, -39, -34])
                    ax.set_axis_off()
                    fig.subplots_adjust(left=0,
                                        right=1,
                                        bottom=0,
                                        top=1,
                                        hspace=0,
                                        wspace=0)
                    fig.tight_layout(pad=0)

                    print "Start Save for", graph_type, time_now
                    print graph_filename
                    self.SaveFigureAsImage(graph_filename, fig=fig)
                    print "End Save"
コード例 #4
0
print "Begin Imports"
from arrow import get as arrow_get
from arrow import now as arrow_now
from arrow import parser
first_time = arrow_now().timestamp
from Download_Observations import give_me_vic_obs
from Download_Observations import give_me_full_obs
from Download_Observations import ObservationLocation
from Create_KML_Observations import create_vic_obs_points
from Create_KML_Observations import create_vic_overlays
from datetime import datetime
from matplotlib import pyplot as plt
from os import remove as os_remove
from os import listdir
from re import match as re_match
from scipy.interpolate import griddata
from numpy import linspace
from pprint import pprint
print "End Imports"


class ProcessObs:

    obs_list = []
    loc_names = []

    def __init__(self):
        self.obs_list, self.loc_names = give_me_vic_obs()

    def update_observations(self):
コード例 #5
0
def give_me_full_obs():
    string_of_observation_locations = """aireys-inlet,Aireys Inlet,-38.4583,144.0883,105
avalon,Avalon Airport,-38.0287,144.4783,10.6
bairnsdale,Bairnsdale Airport,-37.8817,147.5669,49.4
ballarat,Ballarat Aerodrome,-37.5127,143.7911,435.2
bendigo,Bendigo Airport,-36.7395,144.3266,208
geelong-racecourse,Breakwater (Geelong Racecourse),-38.1737,144.3765,12.9
cape-nelson,Cape Nelson Lighthouse,-38.4306,141.5437,45.4
cape-otway,Cape Otway Lighthouse,-38.8556,143.5128,82
casterton,Casterton,-37.583,141.3339,130.6
cerberus,Cerberus,-38.3646,145.1785,12.69
charlton,Charlton,-36.2847,143.3341,131.7
mount-gellibrand,Colac (Mount Gellibrand),-38.2333,143.7925,261
coldstream,Coldstream,-37.7239,145.4092,83
combienbar,Combienbar AWS,-37.3417,149.0228,640
yanakie,Corner Inlet (Yanakie),-38.8051,146.1939,13.3
dartmoor,Dartmoor,-37.9222,141.2614,51
mount-hotham-airport,Dinner Plain (Mount Hotham Airport),-37.0491,147.3347,1295.4
east-sale,East Sale Airport,-38.1156,147.1322,4.6
edenhope,Edenhope Airport,-37.0222,141.2657,155
eildon-fire-tower,Eildon Fire Tower,-37.2091,145.8423,637
essendon-airport,Essendon Airport,-37.7276,144.9066,78.4
falls-creek,Falls Creek,-36.8708,147.2755,1765
ferny-creek,Ferny Creek,-37.8748,145.3496,512.9
frankston,Frankston AWS,-38.1481,145.1156,6
gabo-island,Gabo Island Lighthouse,-37.5679,149.9158,15.2
gelantipy,Gelantipy,-37.22,148.2625,755
mount-william,Grampians (Mount William),-37.295,142.6039,1150
hamilton,Hamilton Airport,-37.6486,142.0636,241.1
hopetoun-airport,Hopetoun Airport,-35.7151,142.3569,77.3
hunters-hill,Hunters Hill,-36.2136,147.5394,981
kanagulk,Kanagulk,-37.1169,141.8031,188.8
warracknabeal-airport,Kellalac (Warracknabeal Airport),-36.3204,142.4161,118.3
kyabram,Kyabram,-36.335,145.0638,105
laverton,Laverton RAAF,-37.8565,144.7566,20.1
longerenong,Longerenong,-36.6722,142.2991,133
mallacoota,Mallacoota,-37.5976,149.7289,22
mangalore,Mangalore Airport,-36.8886,145.1859,140.8
melbourne-olympic-park,Melbourne (Olympic Park),-37.8255,144.9816,7.53
melbourne-airport,Melbourne Airport,-37.6655,144.8321,113.4
mildura,Mildura Airport,-34.2358,142.0867,50
moorabbin-airport,Moorabbin Airport,-37.98,145.0962,12.1
mortlake,Mortlake Racecourse,-38.0737,142.7744,130
latrobe-valley,Morwell (Latrobe Valley Airport),-38.2094,146.4747,55.7
mount-baw-baw,Mount Baw Baw,-37.8383,146.2747,1561
mount-buller,Mount Buller,-37.145,146.4394,1707
mount-moornapa,Mount Moornapa,-37.7481,147.1428,480
mount-nowa-nowa,Mount Nowa Nowa,-37.6924,148.0908,350
nhill-aerodrome,Nhill Aerodrome,-36.3092,141.6486,138.9
omeo,Omeo,-37.1017,147.6008,689.8
orbost,Orbost,-37.6922,148.4667,62.65
port-fairy,Port Fairy AWS,-38.3906,142.2347,10
portland-airport,Portland (Cashmore Airport),-38.3148,141.4705,80.9
portland-harbour,Portland NTC AWS,-38.3439,141.6136,0
pound-creek,Pound Creek,-38.6297,145.8107,3
redesdale,Redesdale,-37.0194,144.5203,290
rhyll,Rhyll,-38.4612,145.3101,13.4
rutherglen,Rutherglen Research,-36.1047,146.5094,175
scoresby,Scoresby Research Institute,-37.871,145.2561,80
sheoaks,Sheoaks,-37.9075,144.1303,236.7
shepparton,Shepparton Airport,-36.4289,145.3947,113.9
stawell,Stawell Aerodrome,-37.072,142.7402,235.36
swan-hill,Swan Hill Aerodrome,-35.3766,143.5416,71
tatura,Tatura Inst Sustainable Ag,-36.4378,145.2672,114
viewbank,Viewbank,-37.7408,145.0972,66.1
kilmore-gap,Wallan (Kilmore Gap),-37.3807,144.9654,527.8
walpeup,Walpeup Research,-35.1201,142.004,105
wangaratta,Wangaratta Aero,-36.4206,146.3056,152.6
warrnambool,Warrnambool Airport NDB,-38.2867,142.4522,70.8
westmere,Westmere,-37.7067,142.9378,226
wilsons-promontory,Wilsons Promontory Lighthouse,-39.1297,146.4244,95
yarram-airport,Yarram Airport,-38.5647,146.7479,17.9
yarrawonga,Yarrawonga,-36.0294,146.0306,128.9"""

    buf = StringIO(string_of_observation_locations)

    loc_names = []
    list_of_csvs = []

    for i in buf:
        loc = str(i).replace('\n', '').split(',')
        loc_names.append(loc[0].lstrip())

    urladdy = "http://www.bom.gov.au/vic/observations/vicall.shtml"

    reader = urlopen(urladdy)

    full_page = StringIO(str(reader.read()))

    print "Scraping URL"
    for i in full_page:
        line = str(i).replace('\n', '')

        for j in range(0, len(loc_names)):

            if loc_names[j] in line:

                if 'a href=' in line:
                    code = str(line).split('<')[2].split('>')[0].split(
                        '=')[1].split('.')[1]
                    list_of_csvs.append(
                        ("http://www.bom.gov.au/fwo/IDV60801/IDV60801." +
                         str(code) + ".axf", loc_names[j]))
    reader.close()
    for sites in list_of_csvs:
        print
        print "Starting for", sites[1]
        print "Sleeping, don't wake the baby"
        sleep(0.5)
        print "Wah wah wah"
        url_reader = urlopen(sites[0])
        line = url_reader.readline()
        while '[data]' not in line:
            line = url_reader.readline()
        csv_reader = csv.DictReader(url_reader)

        loc_obs = []
        for row in csv_reader:
            if row['name[80]'] == None:
                continue
            code = sites[1]

            try:
                obs_time = arrow_get(row['local_date_time_full[80]'],
                                     'YYYYMMDDHHmmss',
                                     tzinfo='Australia/Melbourne')
            except ValueError:
                continue

            day_start = arrow_get(str(arrow_now().format('YYYY-MM-DD')) +
                                  " 00:00:00",
                                  'YYYY-MM-DD HH:mm:ss',
                                  tzinfo='Australia/Melbourne').timestamp
            iter_time = arrow_get(obs_time).timestamp

            if iter_time < day_start:
                continue

            try:
                temp = float(row['air_temp'])
                if -50 < temp < 70:
                    pass
                else:
                    temp = "-"
            except ValueError:
                temp = "-"
            try:
                dew = float(row['dewpt'])
                if -50 < dew < 50:
                    pass
                else:
                    dew = "-"
            except ValueError:
                dew = "-"

            try:
                rain = float(row['rain_trace[80]'].replace('"', ''))
                if 0 < rain < 400:
                    pass
                else:
                    rain = "0.0"
            except ValueError:
                rain = "0.0"

            try:
                pressure = float(row['press'])
                if 900 < pressure < 1100:
                    pass
                else:
                    pressure = "-"
            except ValueError:
                pressure = "-"

            try:
                rel_hum = int(row['rel_hum'])
                if -100 < rel_hum < 100:
                    pass
                else:
                    rel_hum = "-"
            except ValueError:
                rel_hum = "-"

            try:
                wind_vel = int(row['wind_spd_kmh'])
                if -1 < wind_vel < 300:
                    pass
                else:
                    wind_vel = "-"
            except ValueError:
                wind_vel = "-"

            try:
                wind_dir = row['wind_dir[80]']
            except ValueError:
                wind_dir = "-"

            try:
                lcl = 125 * (temp - dew)
            except TypeError:
                lcl = "-"

            loc_obs.insert(
                0,
                str(obs_time.timestamp) + ',' + str(temp) + ',' + str(dew) +
                ',' + str(rain) + ',' + str(pressure) + ',' + str(lcl) + ',' +
                str(rel_hum) + ',' + str(wind_vel) + ',' + wind_dir)

        for loc_line in loc_obs:
            file_time = arrow_get(loc_line.split(',')[0]).to(
                'Australia/Melbourne').format('HH-mm')

        root_folder = "C:\Users\Nathan\Documents\Storm Chasing\Chases\\"
        date_path = arrow_now().format('YYYY-MM-DD')

        try:
            chdir(root_folder + date_path)
        except WindowsError:
            mkdir(root_folder + date_path)

        try:
            chdir(root_folder + date_path + "\Observations")
        except WindowsError:
            mkdir(root_folder + date_path + "\Observations")

        filename = root_folder + date_path + "\Observations\\" + code + '.csv'

        with open(filename, 'w') as f:
            for loc_line in loc_obs:
                f.writelines(loc_line + '\n')
コード例 #6
0
def give_me_vic_obs():

    string_of_observation_locations = """aireys-inlet,Aireys Inlet,-38.4583,144.0883,105
avalon,Avalon Airport,-38.0287,144.4783,10.6
bairnsdale,Bairnsdale Airport,-37.8817,147.5669,49.4
ballarat,Ballarat Aerodrome,-37.5127,143.7911,435.2
bendigo,Bendigo Airport,-36.7395,144.3266,208
geelong-racecourse,Breakwater (Geelong Racecourse),-38.1737,144.3765,12.9
cape-nelson,Cape Nelson Lighthouse,-38.4306,141.5437,45.4
cape-otway,Cape Otway Lighthouse,-38.8556,143.5128,82
casterton,Casterton,-37.583,141.3339,130.6
cerberus,Cerberus,-38.3646,145.1785,12.69
charlton,Charlton,-36.2847,143.3341,131.7
mount-gellibrand,Colac (Mount Gellibrand),-38.2333,143.7925,261
coldstream,Coldstream,-37.7239,145.4092,83
combienbar,Combienbar AWS,-37.3417,149.0228,640
yanakie,Corner Inlet (Yanakie),-38.8051,146.1939,13.3
dartmoor,Dartmoor,-37.9222,141.2614,51
mount-hotham-airport,Dinner Plain (Mount Hotham Airport),-37.0491,147.3347,1295.4
east-sale,East Sale Airport,-38.1156,147.1322,4.6
edenhope,Edenhope Airport,-37.0222,141.2657,155
eildon-fire-tower,Eildon Fire Tower,-37.2091,145.8423,637
essendon-airport,Essendon Airport,-37.7276,144.9066,78.4
falls-creek,Falls Creek,-36.8708,147.2755,1765
ferny-creek,Ferny Creek,-37.8748,145.3496,512.9
frankston,Frankston AWS,-38.1481,145.1156,6
gabo-island,Gabo Island Lighthouse,-37.5679,149.9158,15.2
gelantipy,Gelantipy,-37.22,148.2625,755
mount-william,Grampians (Mount William),-37.295,142.6039,1150
hamilton,Hamilton Airport,-37.6486,142.0636,241.1
hopetoun-airport,Hopetoun Airport,-35.7151,142.3569,77.3
hunters-hill,Hunters Hill,-36.2136,147.5394,981
kanagulk,Kanagulk,-37.1169,141.8031,188.8
warracknabeal-airport,Kellalac (Warracknabeal Airport),-36.3204,142.4161,118.3
kyabram,Kyabram,-36.335,145.0638,105
laverton,Laverton RAAF,-37.8565,144.7566,20.1
longerenong,Longerenong,-36.6722,142.2991,133
mallacoota,Mallacoota,-37.5976,149.7289,22
mangalore,Mangalore Airport,-36.8886,145.1859,140.8
melbourne-olympic-park,Melbourne (Olympic Park),-37.8255,144.9816,7.53
melbourne-airport,Melbourne Airport,-37.6655,144.8321,113.4
mildura,Mildura Airport,-34.2358,142.0867,50
moorabbin-airport,Moorabbin Airport,-37.98,145.0962,12.1
mortlake,Mortlake Racecourse,-38.0737,142.7744,130
latrobe-valley,Morwell (Latrobe Valley Airport),-38.2094,146.4747,55.7
mount-baw-baw,Mount Baw Baw,-37.8383,146.2747,1561
mount-buller,Mount Buller,-37.145,146.4394,1707
mount-moornapa,Mount Moornapa,-37.7481,147.1428,480
mount-nowa-nowa,Mount Nowa Nowa,-37.6924,148.0908,350
nhill-aerodrome,Nhill Aerodrome,-36.3092,141.6486,138.9
omeo,Omeo,-37.1017,147.6008,689.8
orbost,Orbost,-37.6922,148.4667,62.65
port-fairy,Port Fairy AWS,-38.3906,142.2347,10
portland-airport,Portland (Cashmore Airport),-38.3148,141.4705,80.9
portland-harbour,Portland NTC AWS,-38.3439,141.6136,0
pound-creek,Pound Creek,-38.6297,145.8107,3
redesdale,Redesdale,-37.0194,144.5203,290
rhyll,Rhyll,-38.4612,145.3101,13.4
rutherglen,Rutherglen Research,-36.1047,146.5094,175
scoresby,Scoresby Research Institute,-37.871,145.2561,80
sheoaks,Sheoaks,-37.9075,144.1303,236.7
shepparton,Shepparton Airport,-36.4289,145.3947,113.9
stawell,Stawell Aerodrome,-37.072,142.7402,235.36
swan-hill,Swan Hill Aerodrome,-35.3766,143.5416,71
tatura,Tatura Inst Sustainable Ag,-36.4378,145.2672,114
viewbank,Viewbank,-37.7408,145.0972,66.1
kilmore-gap,Wallan (Kilmore Gap),-37.3807,144.9654,527.8
walpeup,Walpeup Research,-35.1201,142.004,105
wangaratta,Wangaratta Aero,-36.4206,146.3056,152.6
warrnambool,Warrnambool Airport NDB,-38.2867,142.4522,70.8
westmere,Westmere,-37.7067,142.9378,226
wilsons-promontory,Wilsons Promontory Lighthouse,-39.1297,146.4244,95
yarram-airport,Yarram Airport,-38.5647,146.7479,17.9
yarrawonga,Yarrawonga,-36.0294,146.0306,128.9"""

    print "Beginning URL Load"
    buf = StringIO(string_of_observation_locations)

    loc_names = []
    obs_list = []

    for i in buf:
        loc = str(i).replace('\n', '').split(',')
        obs_list.append(
            ObservationLocation(loc[0].lstrip(), loc[1], float(loc[2]),
                                float(loc[3]), float(loc[4])))
        loc_names.append(loc[0].lstrip())

    urladdy = "http://www.bom.gov.au/vic/observations/vicall.shtml"

    print "Opening URL"
    reader = urlopen(urladdy)

    full_page = StringIO(str(reader.read()))

    print "Scraping URL"
    for i in full_page:
        line = str(i).replace('\n', '')

        for j in range(0, len(obs_list)):

            if obs_list[j].code in line:

                if '-datetime ' in line:
                    obs_list[j].time = line.split('>')[1].split('<')[0].split(
                        '/')[1]

                if '-tmp ' in line:
                    obs_list[j].temp = line.split('>')[1].split('<')[0]

                if '-dewpoint ' in line:
                    obs_list[j].dew = line.split('>')[1].split('<')[0]

                if '-rainsince9am ' in line:
                    obs_list[j].rain = line.split('>')[1].split('<')[0]

                if '-press ' in line:
                    obs_list[j].pressure = line.split('>')[1].split('<')[0]

                if '-relhum ' in line:
                    obs_list[j].rel_hum = line.split('>')[1].split('<')[0]

                if '-wind-spd-kmh ' in line:
                    obs_list[j].wind_vel = line.split('>')[1].split('<')[0]

                if '-wind-dir ' in line:
                    obs_list[j].wind_dir = line.split('>')[1].split('<')[0]

    for i in range(0, len(obs_list)):

        temp_ = obs_list[i].temp
        dp_ = obs_list[i].dew

        if temp_ == "-" or dp_ == "-":
            obs_list[i].lcl = "-"
            continue

        lcl = 125 * (float(temp_) - float(dp_))

        obs_list[i].lcl = lcl + obs_list[i].height

    print "Saving CSV Files"

    temp = []
    dew = []
    rain = []
    pressure = []
    rel_hum = []
    lcl = []
    wind_vel = []

    for j in range(0, len(obs_list)):

        root_folder = "C:\Users\Nathan\Documents\Storm Chasing\Chases\\"
        date_path = arrow_now().format('YYYY-MM-DD')

        try:
            chdir(root_folder + date_path)
        except WindowsError:
            mkdir(root_folder + date_path)

        try:
            chdir(root_folder + date_path + "\Observations")
        except WindowsError:
            mkdir(root_folder + date_path + "\Observations")

        try:
            temp.append(float(obs_list[j].temp))
        except ValueError:
            pass

        try:
            dew.append(float(obs_list[j].dew))
        except ValueError:
            pass

        try:
            rain.append(float(obs_list[j].rain))
        except ValueError:
            pass

        try:
            pressure.append(float(obs_list[j].pressure))
        except ValueError:
            pass

        try:
            rel_hum.append(float(obs_list[j].rel_hum))
        except ValueError:
            pass

        try:
            lcl.append(float(obs_list[j].lcl))
        except ValueError:
            pass

        try:
            wind_vel.append(float(obs_list[j].wind_vel))
        except ValueError:
            pass

        last_line = ""
        csv_filename = root_folder + date_path + "\Observations\\" + obs_list[
            j].code + ".csv"

        print obs_list[j].time

        local_time = str(arrow_now().format('YYYY-MM-DD')) + " " + str(
            arrow_get(obs_list[j].time, 'HH:mmA').format('HH:mm'))
        ts = arrow_get(local_time,
                       'YYYY-MM-DD HH:mm',
                       tzinfo='Australia/Melbourne').timestamp

        write_line = str(ts) + "," + str(obs_list[j].temp) + "," + str(
            obs_list[j].dew) + "," + str(obs_list[j].rain) + "," + str(
                obs_list[j].pressure) + "," + str(obs_list[j].lcl) + "," + str(
                    obs_list[j].rel_hum) + "," + str(
                        obs_list[j].wind_vel) + "," + str(
                            obs_list[j].wind_dir) + "\n"

        try:
            with open(csv_filename, 'r') as f:
                for line in f:
                    last_line = line

            if write_line == last_line:
                pass
            else:
                with open(csv_filename, 'a') as g:
                    g.write(write_line)

        except IOError:
            with open(csv_filename, 'a') as g:
                g.write(write_line)

    config_filename = root_folder + date_path + "\Observations\\" + "range.cfg"

    min_temp = "-"
    max_temp = "-"
    min_dew = "-"
    max_dew = "-"
    min_pressure = "-"
    max_pressure = "-"
    min_rain = "-"
    max_rain = "-"
    min_rel_hum = "-"
    max_rel_hum = "-"
    min_lcl = "-"
    max_lcl = "-"
    min_wind_vel = "-"
    max_wind_vel = "-"

    try:
        with open(config_filename, 'r') as f:
            # Determine min / max temp
            line = f.readline()
            line_vals = line.split(',')
            try:
                if min(temp) < float(line_vals[0]):
                    min_temp = min(temp)
                else:
                    min_temp = float(line_vals[0])
                if max(temp) > float(line_vals[1]):
                    max_temp = max(temp)
                else:
                    max_temp = float(line_vals[1])
            except ValueError:
                pass

            # Determine min / max dew
            line = f.readline()
            line_vals = line.split(',')
            try:
                if min(dew) < float(line_vals[0]):
                    min_dew = min(dew)
                else:
                    min_dew = float(line_vals[0])
                if max(dew) > float(line_vals[1]):
                    max_dew = max(dew)
                else:
                    max_dew = float(line_vals[1])
            except ValueError:
                pass

            # Determine min / max rain
            line = f.readline()
            line_vals = line.split(',')
            try:
                if min(rain) < float(line_vals[0]):
                    min_rain = min(rain)
                else:
                    min_rain = float(line_vals[0])
                if max(rain) > float(line_vals[1]):
                    max_rain = max(rain)
                else:
                    max_rain = float(line_vals[1])
            except ValueError:
                pass

            # Determine min / max pressure
            line = f.readline()
            line_vals = line.split(',')
            try:
                if min(pressure) < float(line_vals[0]):
                    min_pressure = min(pressure)
                else:
                    min_pressure = float(line_vals[0])
                if max(pressure) > float(line_vals[1]):
                    max_pressure = max(pressure)
                else:
                    max_pressure = float(line_vals[1])
            except ValueError:
                pass

            # Determine min / max rel_hum
            line = f.readline()
            line_vals = line.split(',')
            try:
                if min(rel_hum) < float(line_vals[0]):
                    min_rel_hum = min(rel_hum)
                else:
                    min_rel_hum = float(line_vals[0])
                if max(rel_hum) > float(line_vals[1]):
                    max_rel_hum = max(rel_hum)
                else:
                    max_rel_hum = float(line_vals[1])
            except ValueError:
                pass

            # Determine min / max lcl
            line = f.readline()
            line_vals = line.split(',')
            try:
                if min(lcl) < float(line_vals[0]):
                    min_lcl = min(lcl)
                else:
                    min_lcl = float(line_vals[0])
                if max(lcl) > float(line_vals[1]):
                    max_lcl = max(lcl)
                else:
                    max_lcl = float(line_vals[1])
            except ValueError:
                pass

            # Determine min / max wind_vel
            line = f.readline()
            line_vals = line.split(',')
            try:
                if min(wind_vel) < float(line_vals[0]):
                    min_wind_vel = min(wind_vel)
                else:
                    min_wind_vel = float(line_vals[0])
                if max(wind_vel) > float(line_vals[1]):
                    max_wind_vel = max(wind_vel)
                else:
                    max_wind_vel = float(line_vals[1])
            except ValueError:
                pass

    except IOError:
        min_temp = 0
        max_temp = 45
        min_dew = -10
        max_dew = 25
        min_pressure = 995
        max_pressure = 1030
        min_rain = 0
        max_rain = 40
        min_rel_hum = 0
        max_rel_hum = 100
        min_lcl = 0
        max_lcl = 3000
        min_wind_vel = 0
        max_wind_vel = 5

    with open(config_filename, 'w') as f:
        line = str(min_temp) + "," + str(max_temp) + "\n"
        f.writelines(line)
        line = str(min_dew) + "," + str(max_dew) + "\n"
        f.writelines(line)
        line = str(min_rain) + "," + str(max_rain) + "\n"
        f.writelines(line)
        line = str(min_pressure) + "," + str(max_pressure) + "\n"
        f.writelines(line)
        line = str(min_rel_hum) + "," + str(max_rel_hum) + "\n"
        f.writelines(line)
        line = str(min_lcl) + "," + str(max_lcl) + "\n"
        f.writelines(line)
        line = str(min_wind_vel) + "," + str(max_wind_vel) + "\n"
        f.writelines(line)

    print "Finished Saving CSV Files"

    return obs_list, loc_names