# graphrad.py
# 2023-12-25, 2026-06-22 masudako
import pandas as pd
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

savefig = False

df = pd.read_csv('readselradout.csv', skiprows=1, header=None)
nrec = len(df)

for krec in range(nrec):
    df.iloc[krec,0] = datetime.datetime.strptime(df.iloc[krec,0], '%Y-%m-%d %H:%M:%S')

dtstart = df.iloc[     0, 0]
dtend   = df.iloc[nrec-1, 0]
##dtstart = datetime.datetime(2016, 11,  3, 21, 20,  0)
##dtend   = dtstart + datetime.timedelta(days=8)
dtmargin = datetime.timedelta(hours=6)

ylim0   = -300
ylim1   = 1200

fig = plt.figure()
ax  = fig.add_subplot(1, 1, 1)
ax.set_xlim(dtstart-dtmargin, dtend+dtmargin)
ax.set_ylim(ylim0, ylim1)

ax.grid()

ax.plot(df[0], df[2], 'o-', \
        markersize=1, markeredgecolor='blue', markerfacecolor='None',\
        color='blue', linewidth=0.5)

ax.plot(df[0], df[4], 'o-', \
        markersize=1, markeredgecolor='cyan', markerfacecolor='None',\
        color='cyan', linewidth=0.5)

ax.plot(df[0], df[6], 'o-', \
        markersize=1, markeredgecolor='red', markerfacecolor='None',\
        color='red', linewidth=0.5)

ax.plot(df[0], df[7], 'o-', \
        markersize=1, markeredgecolor='orange', markerfacecolor='None',\
        color='orange', linewidth=0.5)

formatter = mdates.DateFormatter("%m-%d")
ax.xaxis.set_major_formatter(formatter)

dtstart = df.iloc[     0, 0]
symd = datetime.datetime.strftime(dtstart, '%Y %m %d')
ax.set_title('Radiative fluxes at Kumagaya (Rissho Univ.) '+symd+'-')

if savefig:
#### 図を画像ファイル (png) に保存 (imagefilepath につくられる)
     imagefilepath = './'
     symd    = datetime.datetime.strftime(dtstart, '%Y%m%d')
     imagefilename = 'graphrad'+symd+'.png'
     imagefilepathname = imagefilepath + imagefilename
     print('Writing ', imagefilepathname)
     plt.savefig(imagefilepathname)
     plt.clf()
     plt.close()
else:
#### 画面表示
     plt.show()
##### END OF PROGRAM #####
