Skip to content

Instantly share code, notes, and snippets.

@practice
Created November 7, 2022 02:37
Show Gist options
  • Save practice/34ef18f0679fc607c37f027c5d5e642e to your computer and use it in GitHub Desktop.
Save practice/34ef18f0679fc607c37f027c5d5e642e to your computer and use it in GitHub Desktop.
pandora download and image gen
def plot():
start_code = time.time()
# pan_num_list = [20, 54, 149, 150, 163, 164]
pan_num_list = [149, 150, 164, 20, 54]
product_list = ['Surf_NO2', 'TCN', 'TCO']
save_path = '/data/finepart/pandora/temp/data/'
fig_savepath = '/data/finepart/pandora/temp/'
if not os.path.isdir(save_path):
os.makedirs(save_path)
if not os.path.isdir(fig_savepath):
os.makedirs(fig_savepath)
# %% 당일 날짜 호출
today = datetime.today().date()
# today = datetime(2021,10,23,0)
start = today - timedelta(days=5)
end = today + timedelta(days=1)
today_str = datetime.strftime(today, format='%Y%m%d')
# %% 루프 시작
for pan_num in pan_num_list:
for product in product_list:
if product == 'Surf_NO2':
product_s = 'NO2_Surface'
mol2g = 46.0055*1e6
elif product == 'TCN':
product_s = 'NO2_TCD'
elif product == 'TCO':
product_s = 'O3_TCD'
filename, url, site_name = pandora.get_filename(
pan_num, product) # 대상 파일명 및 다운로드 url생성
print(filename)
# '''
pandora.download(save_path, url) # 대상 파일 다운로드
skiprows, usecols, names, cf = pandora.get_index(
pan_num, product) # 파일별 읽을 칼럼 및 헤더줄 수 변수화
pandora_df = pd.read_csv(save_path+filename, skiprows=skiprows, delimiter='\s+',
usecols=usecols, names=names,
engine='python', encoding='cp949') # Pandora 파일 읽기(pandas dataframe)
if np.size(pandora_df) == 0:
fig1 = plt.figure(figsize=(16, 4))
# plt.grid()
plt.text(0.25, 0.45, 'No Data in PGN Data Server', fontsize=35)
plt.tick_params(axis='x', which='both',
bottom=False, top=False, labelbottom=False)
plt.tick_params(axis='y', which='both',
right=False, left=False, labelleft=False)
fig1.savefig(fig_savepath+'%s_P%d_%s_hourly_%s.png' %
(site_name, pan_num, product_s, today_str), dpi=500)
fig2 = plt.figure(figsize=(16, 4))
plt.text(0.25, 0.45, 'No Data in PGN Data Server', fontsize=35)
plt.tick_params(axis='x', which='both',
bottom=False, top=False, labelbottom=False)
plt.tick_params(axis='y', which='both',
right=False, left=False, labelleft=False)
fig2.savefig(fig_savepath+'%s_P%d_%s_daily_%s.png' %
(site_name, pan_num, product_s, today_str), dpi=500)
continue
# %%
else:
pandora_df['KST'] = pd.to_datetime(pandora_df['UTC'], format='%Y%m%dT%H%M%S', exact=False)\
+ timedelta(hours=9)
pandora_df.set_index('KST', inplace=True) # 한국시간 인덱스 생성 및 적용
pandora_df[product] = pandora_df[product]/cf # 단위 변환
pandora_df[pandora_df['QF'] %
10 != 0] = np.nan # Quality control
pandora_df[pandora_df[product] < 0] = np.nan # Quality control
# %% 일평균 및 시간평균 자료 생성
hourly_df = pandora_df.resample(rule='H').mean()
daily_df = pandora_df.resample(rule='D').mean()
hourly_plot_df = hourly_df.loc[start:end]
daily_plot_df = daily_df.loc[start:today]
hourly_plot_df.reset_index(inplace=True)
daily_plot_df.reset_index(inplace=True)
# %% 일평균 및 시간평균 시각화 축 생성
lim, ylabel, title = pandora.plot_parameter(product)
daily_tick = pd.Series(pd.date_range(
start, today, freq='1D'), name='KST')
hourly_main_tick = pd.Series(
pd.date_range(start, end, freq='1D'), name='KST')
hourly_minor_tick = pd.Series(
pd.date_range(start, end, freq='1H'), name='KST')
hourly_merge_df = pd.merge(
hourly_minor_tick, hourly_plot_df, how='left', on='KST')
daily_merge_df = pd.merge(
daily_tick, daily_plot_df, how='left', on='KST')
# %% 데이터가 없는 경우 그림 생성
if np.size(hourly_plot_df.dropna(axis=0)) == 0:
fig1 = plt.figure(figsize=(16, 4))
# plt.grid()
plt.text(
0.25, 0.45, 'No Data in PGN Data Server', fontsize=35)
plt.tick_params(axis='x', which='both',
bottom=False, top=False, labelbottom=False)
plt.tick_params(axis='y', which='both',
right=False, left=False, labelleft=False)
fig1.savefig(fig_savepath+'%s_P%d_%s_hourly_%s.png' % (site_name,
pan_num, product_s, today_str), dpi=500, bbox_inches='tight')
fig2 = plt.figure(figsize=(16, 4))
plt.text(
0.25, 0.45, 'No Data in PGN Data Server', fontsize=35)
plt.tick_params(axis='x', which='both',
bottom=False, top=False, labelbottom=False)
plt.tick_params(axis='y', which='both',
right=False, left=False, labelleft=False)
fig2.savefig(fig_savepath+'%s_P%d_%s_daily_%s.png' % (site_name,
pan_num, product_s, today_str), dpi=500, bbox_inches='tight')
# %% 데이터 시각화
else:
# 시간평균 시계열 시각화
fig1, ax1 = plt.subplots(figsize=(16, 4))
if product == 'Surf_NO2' and pan_num in [20, 150]:
ax1.plot(hourly_minor_tick,
hourly_merge_df[product]*mol2g, '-ok')
else:
ax1.plot(hourly_minor_tick,
hourly_merge_df[product], '-ok')
# ax1.plot(hourly_plot_df['KST'], hourly_plot_df[product], '-ok')
ax1.set_xticks(hourly_main_tick)
ax1.set_xticks(hourly_minor_tick, minor=True)
ax1.tick_params(axis='x', length=12, width=2)
ax1.tick_params(axis='x', which='minor', length=5, width=1)
xfmt = mdates.DateFormatter('%Y-%m-%d %Hh')
ax1.xaxis.set_major_formatter(xfmt)
ax1.set_title('P%d %s' % (pan_num, title))
ax1.set_ylim(lim)
ax1.set_ylabel(ylabel)
ax1.set_xlabel('Date (KST)')
fig1.savefig(fig_savepath+'%s_P%d_%s_hourly_%s.png' % (site_name,
pan_num, product_s, today_str), dpi=500, bbox_inches='tight')
# %% 일평균 시계열 시각화
fig2, ax2 = plt.subplots(figsize=(16, 4))
# ax2.plot(daily_plot_df['KST'], daily_plot_df[product], '-ok')
if product == 'Surf_NO2' and pan_num in [20, 150]:
ax2.plot(
daily_tick, daily_merge_df[product]*mol2g, '-ok')
else:
ax2.plot(daily_tick, daily_merge_df[product], '-ok')
ax2.set_xticks(daily_tick)
ax2.tick_params(axis='x', length=12, width=2)
xfmt = mdates.DateFormatter('%Y-%m-%d')
ax2.xaxis.set_major_formatter(xfmt)
ax2.set_title('P%d %s' % (pan_num, title))
ax2.set_ylim(lim)
ax2.set_ylabel(ylabel)
ax2.set_xlim([start-timedelta(hours=6),
today+timedelta(hours=6)])
ax2.set_xlabel('Date (KST)')
fig2.savefig(fig_savepath+'%s_P%d_%s_daily_%s.png' % (site_name,
pan_num, product_s, today_str), dpi=500, bbox_inches='tight')
plt.close(fig1)
plt.close(fig2)
print(time.time()-start_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment