# graphradalb.py
# 2023-12-25, 2026-05-22 masudako
import pandas as pd
import numpy as np
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)

rsmin  =  5.0
albedo = np.full(nrec, np.nan, dtype=np.float32)

for krec in range(nrec):
    df.iloc[krec,0] = datetime.datetime.strptime(df.iloc[krec,0], '%Y-%m-%d %H:%M:%S')
    if pd.notna(df.iloc[krec,2]) and pd.notna(df.iloc[krec,4]):
        if (float(df.iloc[krec,2]) >= rsmin) and (float(df.iloc[krec,4]) >= rsmin):
            albedo[krec] = float(df.iloc[krec,4])/float(df.iloc[krec,2])

dtstart = df.iloc[     0, 0]
dtend   = df.iloc[nrec-1, 0]
dtmargin = datetime.timedelta(hours=6)

##ylim0   = -300
##ylim1   = 1200

##### 作図
fig = plt.figure()

##### 図 (1)
ax  = fig.add_subplot(2, 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+'-')

##### 図 (2)
ax  = fig.add_subplot(2, 1, 2)

ylim0   =  0.0
ylim1   =  0.5
ax.set_ylim(ylim0, ylim1)

ax.grid()

ax.plot(df[0], albedo, 'o-', \
        markersize=1, markeredgecolor='cyan', markerfacecolor='None',\
        color='cyan', linewidth=0.5)

ax.xaxis.set_major_formatter(formatter)

if savefig:
#### 図を画像ファイル (png) に保存 (imagefilepath につくられる)
     imagefilepath = './'
     symd    = datetime.datetime.strftime(dtstart, '%Y%m%d')
     imagefilename = 'graphradalb'+symd+'.png'
     imagefilepathname = imagefilepath + imagefilename
     print('Writing ', imagefilepathname)
     plt.savefig(imagefilepathname)
     plt.clf()
     plt.close()
else:
#### 画面表示
     plt.show()
##### END OF PROGRAM #####
