Beispiel #1
0
def process_xray_data():
	print '\nWorking - xray...'

	dow_ftp = ftp_init()

	if dow_ftp is None:
		return

	dt_util = DateUtil()

	xray_curr_file = dt_util.get_utc_ymd_str() + config.XRAY_FILE_SUFFIX
	xray_prev_file = dt_util.get_utc_prev_day_ymd_str() + config.XRAY_FILE_SUFFIX

	if dow_ftp.download_file(xray_curr_file, config.FILES_WORK_DIR):
		xray_curr_file = config.FILES_WORK_DIR + '/' + xray_curr_file
	else:
		xray_curr_file = ''

	dow_ftp.close()

	left_hours = dt_util.get_left_hours_in_curr_day() #- dt_util.get_local_offset_from_utc()

	if left_hours < 0:
		left_hours = 0

	if left_hours > 0:
		dow_ftp = ftp_init()

		if dow_ftp is None:
			return

		if dow_ftp.download_file(xray_prev_file, config.FILES_WORK_DIR):
			xray_prev_file = config.FILES_WORK_DIR + '/' + xray_prev_file
		else:
			xray_prev_file = ''
	else:
		xray_prev_file = ''

	result_plot_img = build_xray_graph(xray_curr_file, xray_prev_file, left_hours, dt_util)

	delete_file(xray_curr_file)
	delete_file(xray_prev_file)

	if not result_plot_img:
		print 'Failed to process xray data!'
		return

	file_uploader(result_plot_img)
Beispiel #2
0
def process_proton_flux_data():
	print '\nWorking - proton flux...'

	dow_ftp = ftp_init()

	if dow_ftp is None:
		return

	dt_util = DateUtil()

	proton_flux_curr_file = dt_util.get_utc_ymd_str() + config.PROTON_FLUX_FILE_SUFFIX
	proton_flux_prev_file = dt_util.get_utc_prev_day_ymd_str() + config.PROTON_FLUX_FILE_SUFFIX

	if dow_ftp.download_file(proton_flux_curr_file, config.FILES_WORK_DIR):
		proton_flux_curr_file = config.FILES_WORK_DIR + '/' + proton_flux_curr_file
	else:
		proton_flux_curr_file = ''

	dow_ftp.close()

	left_hours = dt_util.get_left_hours_in_curr_day() #- dt_util.get_local_offset_from_utc()

	if left_hours < 0:
		left_hours = 0

	if left_hours > 0:
		dow_ftp = ftp_init()

		if dow_ftp is None:
			return

		if dow_ftp.download_file(proton_flux_prev_file, config.FILES_WORK_DIR):
			proton_flux_prev_file = config.FILES_WORK_DIR + '/' + proton_flux_prev_file
		else:
			proton_flux_prev_file = ''
	else:
		proton_flux_prev_file = ''

	result_plot_img = build_proton_flux_graph(proton_flux_curr_file, proton_flux_prev_file, left_hours, dt_util)

	delete_file(proton_flux_curr_file)
	delete_file(proton_flux_prev_file)

	if result_plot_img is None:
		print 'Unable to process proton flux data!'
		return

	file_uploader(result_plot_img)
Beispiel #3
0
    def __init__(self, git_repo_path, logger):
        """
        :type git_repo_path: str
        :param git_repo_path: local path of the Git repository

        :type logger: Object
        :param logger: logger
        """
        try:
            self._logger = logger
            self._repo = Repo(git_repo_path, odbt=GitCmdObjectDB)
            self._gitt = self._repo.git
            self._date_util = DateUtil()
        except:
            self._logger.error("GitQuerier init failed")
            raise
Beispiel #4
0
def build_geomag_plot(week_geomag_file, two_days_geomag_file):
	print 'Generating plot...'

	week_file_lines = None
	two_days_geomag_lines = None

	try:
		week_file_lines = open(week_geomag_file).readlines()
		two_days_geomag_lines = open(two_days_geomag_file).readlines()
	except:
		print 'No data for geomag!'
		return None

	week_data = parse_geomag_lines(week_file_lines)
	today_data = parse_geomag_lines(two_days_geomag_lines, True)

	data = week_data + today_data

	y = np.array(data)
	t = np.arange(0, len(data), 1)

	fig, ax1 = plt.subplots()

	plt.xlim(0, len(data))
	plt.ylim(0, 9)

	x_day_labels = []

	for x in range(0, 8):
		dt_util = DateUtil(x)
		x_day_labels.append(dt_util.get_utc_prev_day_ymd_str_pretty())
	
	x_day_labels = x_day_labels[::-1]

	x_formatter = matplotlib.ticker.FixedFormatter(x_day_labels)

	plt.plot(t, y, color='g', linewidth=4)
	plt.grid(axis='y', linestyle='-')
	plt.grid(axis='x', linestyle='-')

	plt.ylabel(u'Kp')

	ax2 = ax1.twinx()
	ax2.set_ylim(0, 9)
	y2_formatter = matplotlib.ticker.FixedFormatter(strings.geomag_y_ticks_right)
	ax2.yaxis.set_major_formatter(y2_formatter)

	ax1.xaxis.set_major_formatter(x_formatter)

	for tick in ax1.get_xaxis().get_major_ticks():
		tick.set_pad(9.)
		tick.label1 = tick._get_text1()

	for tick in ax1.get_xticklabels():
		tick.set_rotation(90)

	plt.xticks(np.arange(0, t[-1] + 1, 7.75))

	plt.title(strings.geomag_plot_title)
	plt.tight_layout()

	out_file = config.FILES_WORK_DIR + '/' + config.GEOMAG_OUT_FILE

	print 'Plot was generated and saved as ' + out_file

	plt.savefig(out_file, dpi = config.PLOT_DPI, bbox_inches='tight')

	#plt.show()

	return out_file
Beispiel #5
0
def create_calendar(year, font_file, country, lang, draw_line, white_text):
    config_dict = get_dict_from_yml("config.yml")
    cell_dim = 300
    date_util = DateUtil(country=country)
    TITLE_FONT_SIZE = 120
    title_font = ImageFont.truetype(font_file, TITLE_FONT_SIZE)

    CELL_FONT_SIZE = 80
    cell_font = ImageFont.truetype(font_file, CELL_FONT_SIZE)
    if white_text:
        text_fill = (250, 250, 250)
    else:
        text_fill = (0, 0, 0)
    for m in range(1, 13):

        month_name = config_dict['language'][lang]['months'][m]
        canvas_np = draw_blank_canvas(cell_dim=cell_dim)
        canvas_img = Image.fromarray(canvas_np.astype('uint8'), 'RGB')
        if draw_line:
            canvas_img = draw_lines(canvas_img)
        canvas_img = write_month_title(title=month_name,
                                       canvas_img=canvas_img,
                                       img_font=title_font,
                                       fill=text_fill)

        draw = ImageDraw.Draw(canvas_img)

        n_days = config_dict['days'][m]
        if m == 2 and date_util.is_leap_year(year):
            n_days += 1

        week = 1
        weekdays = [6, 0, 1, 2, 3, 4, 5]
        for d in range(1, n_days + 1):
            date = datetime.datetime(year, m, d)
            weekday = date.weekday()
            fill = text_fill
            if weekday == 6 or date == date_util.is_holiday(
                    date=(year, m, d)):  # If Sunday
                # TODO: Write in red
                fill = (255, 0, 0)

            date_text = f"{d}"
            text_width, text_height = cell_font.getsize(date_text)
            cell_corner_x = weekdays.index(weekday) * cell_dim
            cell_corner_y = week * cell_dim

            x = cell_corner_x + cell_dim // 2 - text_width // 2
            y = cell_corner_y + cell_dim // 2 - text_height // 2
            draw.text(xy=(x, y), text=date_text, font=cell_font, fill=fill)
            if weekday == 5:  # change to new row when saturday is hit
                week += 1

        calendar_done_np = np.array(canvas_img)

        calendar_done_grayscale = np.array(canvas_img.convert(
            'L'))  # convert to grayscale for finding alpha map
        transparency_mask = make_transparency_mask(
            calendar_done_grayscale[..., np.newaxis].astype('uint8'))
        calendar_png_np = np.concatenate([calendar_done_np, transparency_mask],
                                         axis=-1)
        calendar_png_img = Image.fromarray(calendar_png_np, mode="RGBA")

        if white_text:
            calendar_png_img.save(
                f"saved_calendars/{m}_{month_name}_{year}_{country}_{lang}_white.png"
            )
        else:
            calendar_png_img.save(
                f"saved_calendars/{m}_{month_name}_{year}_{country}_{lang}_black.png"
            )

    return
Beispiel #6
0
day of week 	day of week 2

Output file: preocessed_2.csv
Every row is corresponding to each mid-class item in every day
(from 1st Jan. to 30th April).
The last item in each row is # of unique orders of a mid-class item on that day.
For prediction, 18 features are selected: 14 of them are # of unique orders
in past 2 weeks;
While other 4 are # of unique orders on the same day of week in past 4 weeks.
"""

import pandas as pd
import numpy as np
from date_util import DateUtil

du = DateUtil()
num_past_days = 28
num_past_weeks = 0
start_date_int = 20150101
end_date_int = 20150831


def make_dates() -> [np.int64]:
    date = end_date_int
    sequence = [date]
    while date > start_date_int:
        date = du.minus(1, date)
        sequence.append(date)
    sequence.reverse()
    return sequence
Beispiel #7
0
 def test_day_of_year_2015_12_31(self):
     self.assertEqual(DateUtil.day_of_year(2015, 12, 31), 365)
Beispiel #8
0
 def test_day_of_year_2400_12_31(self):
     self.assertEqual(DateUtil.day_of_year(2400, 12, 31), 366)
Beispiel #9
0
 def test_day_of_year_2015_2_28(self):
     self.assertEqual(DateUtil.day_of_year(2015, 2, 28), 59)
Beispiel #10
0
 def test_day_of_year_2015_3_1(self):
     self.assertEqual(DateUtil.day_of_year(2015, 3, 1), 60)
Beispiel #11
0
 def test_day_of_year_2016_2_29(self):
     self.assertEqual(DateUtil.day_of_year(2016, 2, 29), 60)
Beispiel #12
0
 def test_day_of_year_1_1_1(self):
     self.assertEqual(DateUtil.day_of_year(1, 1, 1), 1)
Beispiel #13
0
 def test_is_leap_year_2200(self):
     self.assertFalse(DateUtil.is_leap_year(2200))
Beispiel #14
0
 def test_is_leap_year_2000(self):
     self.assertTrue(DateUtil.is_leap_year(2000))
Beispiel #15
0
 def test_is_leap_year_2015(self):
     self.assertFalse(DateUtil.is_leap_year(2015))