Skip to content

Commit 6a4f24a

Browse files
authored
Add files via upload
1 parent da7825f commit 6a4f24a

16 files changed

+5194
-117
lines changed

Data_Inerpolate.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Nov 6 11:41:53 2023
4+
5+
@author: osamatarabih
6+
"""
7+
import math
8+
from calendar import monthrange
9+
import numpy as np
10+
import pandas as pd
11+
from scipy.optimize import fsolve
12+
from scipy import interpolate
13+
import os
14+
import datetime
15+
16+
parameter: str = 'RAD' #'RADP'
17+
units: str = 'n' #'MICROMOLE/m^2/s' #
18+
station: str = 'm' #'L006'#
19+
20+
os.chdir('C:/LOONE_WQ_Clean/Model_Data_Filled_20082023')
21+
Data_In = pd.read_csv('./LO_RADT_20082023.csv')
22+
Data_In = Data_In[Data_In['%s_%s_%s' % (station, parameter, units)]>=0]
23+
Data_In = Data_In.set_index(['date'])
24+
Data_In.index = pd.to_datetime(Data_In.index, unit='ns')
25+
Data_df = Data_In.resample('D').mean()
26+
Data_df = Data_df.dropna(subset=['%s_%s_%s' % (station, parameter, units)])
27+
Data_df = Data_df.reset_index()
28+
Data_df['Yr_M'] = pd.to_datetime(Data_df['date']).dt.to_period('M')
29+
start_date = Data_df['date'].iloc[0]
30+
end_date = Data_df['date'].iloc[-1]
31+
date_rng = pd.date_range(start=start_date, end=end_date, freq='M')
32+
date_rng = date_rng.union([date_rng[-1] + pd.DateOffset(months=1)])
33+
Monthly_df = pd.DataFrame(date_rng, columns=['date'])
34+
Monthly_df['Yr_M'] = pd.to_datetime(Monthly_df['date']).dt.to_period('M')
35+
New_date = []
36+
New_data = []
37+
Days = []
38+
Days_cum = []
39+
# Set index for the two dataframes
40+
Data_df = Data_df.set_index(['Yr_M'])
41+
Monthly_df = Monthly_df.set_index(['Yr_M'])
42+
for i in Monthly_df.index:
43+
if i in Data_df.index:
44+
if type(Data_df.loc[i]['date']) == pd.Timestamp:
45+
New_date.append(Data_df.loc[i]['date'])
46+
New_data.append(Data_df.loc[i]['%s_%s_%s' % (station, parameter, units)])
47+
else:
48+
for j in range(len(Data_df.loc[i]['date'])):
49+
New_date.append(Data_df.loc[i]['date'][j])
50+
New_data.append(Data_df.loc[i]['%s_%s_%s' % (station, parameter, units)][j])
51+
elif i not in Data_df.index:
52+
New_date.append(datetime.datetime(Monthly_df.loc[i]['date'].year, Monthly_df.loc[i]['date'].month, 1))
53+
New_data.append(np.NaN)
54+
55+
New_date = pd.to_datetime(New_date, format='%Y-%m-%d')
56+
Days = New_date.strftime("%d").astype(float)
57+
for i in range(len(Days)):
58+
if i == 0:
59+
Days_cum.append(Days[i])
60+
elif New_date[i].month == New_date[i-1].month:
61+
Days_cum.append(Days_cum[i-1]+(Days[i]-Days[i-1]))
62+
elif New_date[i].month != New_date[i-1].month:
63+
Days_cum.append(Days_cum[i-1]+Days[i]+monthrange(New_date[i-1].year, New_date[i-1].month)[1]-Days[i-1])
64+
Final_df = pd.DataFrame()
65+
Final_df['date'] = New_date
66+
Final_df['Data'] = New_data
67+
Final_df['Days'] = Days
68+
Final_df['Days_cum'] = Days_cum
69+
# Final_df.to_csv('C:/Work/Research/LOONE/Nitrogen Module/Interpolated_Data/In-Lake/L008_DO_No_Months_Missing_Trial.csv') # noqa: E501
70+
# Remove Negative Data Values
71+
Final_df = Final_df[Final_df['Data'] >= 0]
72+
Final_df['date'] = pd.to_datetime(Final_df['date'], format='%Y-%m-%d')
73+
start_date = Final_df['date'].iloc[0]
74+
end_date = Final_df['date'].iloc[-1]
75+
date_rng_TSS_1 = pd.date_range(start=start_date, end=end_date, freq='D')
76+
# Create a data frame with a date column
77+
Data_df = pd.DataFrame(date_rng_TSS_1, columns=['date'])
78+
Data_len = len(Data_df.index)
79+
Cum_days = np.zeros(Data_len)
80+
Data_daily = np.zeros(Data_len)
81+
# Set initial values
82+
Cum_days[0] = Data_df['date'].iloc[0].day
83+
Data_daily[0] = Final_df['Data'].iloc[0]
84+
for i in range(1, Data_len):
85+
Cum_days[i] = Cum_days[i-1]+1
86+
# Data_daily[i] = interpolate.interp1d(Final_df['Days'], Final_df['TSS'] , kind = 'linear')(Cum_days[i])
87+
Data_daily[i] = np.interp(Cum_days[i], Final_df['Days_cum'], Final_df['Data'])
88+
Data_df['Data_%s'%station] = Data_daily
89+
Data_df.to_csv('./LO_RADT_20082023.csv', index=False)

Define_Dates.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Jan 17 13:35:40 2024
4+
5+
@author: osama
6+
"""
7+
8+
import pandas as pd
9+
import numpy as np
10+
from datetime import date, timedelta
11+
import os
12+
#Directory where the Script loactes
13+
os.chdir('C:/Work/Research/Data Analysis/Tools/Python_Scripts')
14+
from Data_Analyses_Fns import *
15+
os.chdir('C:/LOONE_WQ/Data/LORS19722023/ts_data')
16+
17+
# Specify the folder path
18+
folder_path = 'C:/LOONE_WQ/Data/LORS19722023/ts_data'
19+
20+
# Get a list of all CSV files in the folder
21+
csv_files = [file for file in os.listdir(folder_path) if file.endswith('.csv')]
22+
23+
# Iterate through each CSV file
24+
for file in csv_files:
25+
file_path = os.path.join(folder_path, file)
26+
data = pd.read_csv(file_path)
27+
28+
# Check if the date in the first row is '1/1/1972'
29+
if 'date' in data.columns:
30+
data['date'] = pd.to_datetime(data['date'])
31+
if data['date'].iloc[0] == pd.to_datetime('1/1/1972'):
32+
# Apply your custom function to change the date range
33+
data = DF_Date_Range(data, 2007, 1, 1, 2023, 6, 30)
34+
elif data['date'].iloc[0] == pd.to_datetime('12/30/1971'):
35+
# Apply your custom function to change the date range
36+
data = DF_Date_Range(data, 2006, 12, 30, 2023, 6, 30)
37+
else:
38+
pass
39+
else:
40+
pass
41+
# Save the modified DataFrame to a new CSV file
42+
data.to_csv('C:/LOONE_WQ/Data/LORS20072023/ts_data/%s_20072023.csv' % file, index=False)
43+
44+
45+
def rename_files(folder_path, old_part, new_part):
46+
# Navigate to the folder containing the files
47+
os.chdir('C:/LOONE_WQ/Data/LORS20072023/ts_data')
48+
49+
# Iterate through all files in the folder
50+
for filename in os.listdir(folder_path):
51+
# Check if the old_part exists in the filename
52+
if old_part in filename:
53+
# Create the new filename by replacing old_part with new_part
54+
new_filename = filename.replace(old_part, new_part)
55+
56+
# Construct the full paths
57+
old_path = os.path.join(folder_path, filename)
58+
new_path = os.path.join(folder_path, new_filename)
59+
60+
# Rename the file
61+
os.rename(old_path, new_path)
62+
print(f'Renamed: {filename} to {new_filename}')
63+
64+
# Example usage
65+
folder_path = 'C:/LOONE_WQ/Data/LORS20072023/ts_data'
66+
old_part = 'LORS20082023.csv_20072023'
67+
new_part = 'LORS20072023'
68+
69+
rename_files(folder_path, old_part, new_part)

LOONE_Nut.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from TP_Variables_Regions import TP_Variables
2020
import TP_Mass_Balance_Functions_Regions as TP_MBFR
2121

22-
def LOONE_Nut(LOONE_Q_Outputs):
22+
def LOONE_Nut():
2323
print("LOONE Nut Module is Running!")
2424
# Based on the defined Start and End year, month, and day on the Pre_defined_Variables File, Startdate and enddate are defined.
2525
year, month, day = map(int, Pre_defined_Variables.startdate_entry)
@@ -432,3 +432,5 @@ def LOONE_Nut(LOONE_Q_Outputs):
432432
# return[P_Loads_M,P_Lake_M,Smr_Mnth_StL_arr,Smr_Mnth_Cal_arr]
433433
# else:
434434
# return[Smr_Mnth_StL_arr,Smr_Mnth_Cal_arr,P_Lake_df]
435+
P_NS = LOONE_Nut()
436+
P_NS.to_csv('./P_NS_June2023_Monthly.csv')

0 commit comments

Comments
 (0)