feat: ✨ CSV output near complete
This commit is contained in:
@@ -1,89 +1,127 @@
|
||||
from __future__ import division, print_function
|
||||
import numpy as np
|
||||
import glob
|
||||
import pandas as pd
|
||||
from datetime import datetime
|
||||
|
||||
# Configuration
|
||||
asc_path = "asc_files/"
|
||||
asc_wildcard_file = "*.asc"
|
||||
asc_mult_source = asc_path + asc_wildcard_file
|
||||
|
||||
five_min_rainfall_spatial_timeseries_name = 'timeseries_data.txt'
|
||||
def read_ascii_header(ascii_raster_file: str) -> list:
|
||||
"""Reads header information from an ASCII DEM
|
||||
|
||||
things = [
|
||||
# loc name, loc id, x loc, y loc, resolution
|
||||
[1725 , 2175 , 608500 , 216500 , 1000 , -1 ], # 'BRICSC', 'TM0816'
|
||||
[1725 , 2175 , 568500 , 342500 , 1000 , -1 ], # 'HEACSC', 'TF6842'
|
||||
[1725.0, 2175.0, -404500.0, -624500.0, 1000.0, -1.0]# example
|
||||
]
|
||||
Args:
|
||||
ascii_raster_file (str): Path to the ASCII raster file
|
||||
|
||||
def read_ascii_header(ascii_raster_file):
|
||||
"""Reads header information from an ASCII DEM"""
|
||||
Returns:
|
||||
list: Header data as a list of floats
|
||||
"""
|
||||
with open(ascii_raster_file) as f:
|
||||
header_data = [float(f.__next__().split()[1]) for x in range(6)]
|
||||
return header_data
|
||||
|
||||
def calculate_crop_coords(basin_header, radar_header):
|
||||
"""Calculate crop coordinates based on header data"""
|
||||
|
||||
def calculate_crop_coords(basin_header: list, radar_header: list) -> tuple:
|
||||
"""Calculate crop coordinates based on header data
|
||||
|
||||
Args:
|
||||
basin_header (list): Basin header data
|
||||
radar_header (list): Radar header data
|
||||
|
||||
Returns:
|
||||
tuple: (start_col, start_row, end_col, end_row) as integers
|
||||
"""
|
||||
y0_radar = radar_header[3]
|
||||
x0_radar = radar_header[2]
|
||||
|
||||
|
||||
y0_basin = basin_header[3]
|
||||
x0_basin = basin_header[2]
|
||||
|
||||
|
||||
nrows_radar = radar_header[1]
|
||||
|
||||
nrows_basin = basin_header[1]
|
||||
ncols_basin = basin_header[0]
|
||||
|
||||
nrows_basin = 2 # hardcoded, we always expect 2 rows
|
||||
ncols_basin = 2 # hardcoded, we always expect 2 columns
|
||||
|
||||
cellres_radar = radar_header[4]
|
||||
cellres_basin = basin_header[4]
|
||||
|
||||
|
||||
xp = x0_basin - x0_radar
|
||||
yp = y0_basin - y0_radar
|
||||
|
||||
|
||||
xpp = ncols_basin * cellres_basin
|
||||
ypp = nrows_basin * cellres_basin
|
||||
|
||||
start_col = np.floor( xp / cellres_radar )
|
||||
end_col = np.ceil( (xpp + xp) / cellres_radar )
|
||||
|
||||
start_row = np.floor(nrows_radar - ( (yp + ypp)/cellres_radar ))
|
||||
end_row = np.ceil(nrows_radar - (yp/cellres_radar))
|
||||
|
||||
print(start_col, start_row, end_col, end_row)
|
||||
|
||||
start_col = np.floor(xp / cellres_radar)
|
||||
end_col = np.ceil((xpp + xp) / cellres_radar)
|
||||
|
||||
start_row = np.floor(nrows_radar - ((yp + ypp) / cellres_radar))
|
||||
end_row = np.ceil(nrows_radar - (yp / cellres_radar))
|
||||
|
||||
#print(start_col, start_row, end_col, end_row)
|
||||
return int(start_col), int(start_row), int(end_col), int(end_row)
|
||||
|
||||
|
||||
def extract_cropped_rain_data():
|
||||
"""Extract cropped rain data and create rainfall timeseries"""
|
||||
def extract_cropped_rain_data(location):
|
||||
"""Extract cropped rain data and create rainfall timeseries
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
rainfile = []
|
||||
#basin_header = read_ascii_header(basinsource)
|
||||
basin_header = things[0] # just BRICSC for now
|
||||
|
||||
# Create datetime list
|
||||
datetime_list = []
|
||||
print(location)
|
||||
for f in glob.iglob(asc_mult_source):
|
||||
print(f)
|
||||
# print(f)
|
||||
radar_header = read_ascii_header(f)
|
||||
# print(radar_header)
|
||||
start_col, start_row, end_col, end_row = calculate_crop_coords(
|
||||
location, radar_header
|
||||
)
|
||||
|
||||
start_col, start_row, end_col, end_row = calculate_crop_coords(basin_header, radar_header)
|
||||
|
||||
start_col = int(round(start_col))
|
||||
start_row = int(round(start_row) )
|
||||
end_col = int(round(end_col) )
|
||||
end_row = int(round(end_row) )
|
||||
|
||||
cur_rawgrid = np.genfromtxt(f, skip_header=6, filling_values=0.0, loose=True, invalid_raise=False)
|
||||
|
||||
start_row = int(round(start_row))
|
||||
end_col = int(round(end_col))
|
||||
end_row = int(round(end_row))
|
||||
|
||||
cur_rawgrid = np.genfromtxt(
|
||||
f, skip_header=6, filling_values=0.0, loose=True, invalid_raise=False
|
||||
)
|
||||
|
||||
cur_croppedrain = cur_rawgrid[start_row:end_row, start_col:end_col]
|
||||
print(cur_croppedrain)
|
||||
# Flatten the cropped rain data into a 1D array
|
||||
cur_rainrow = cur_croppedrain.flatten()
|
||||
print(cur_rainrow)
|
||||
rainfile.append(cur_rainrow)
|
||||
|
||||
|
||||
# Extract datetime from filename
|
||||
filename = f.split("/")[-1] # Get just the filename
|
||||
# 20240929 0015
|
||||
date_str = filename[:8] # YYYYMMDD
|
||||
time_str = filename[8:12] # HHMM
|
||||
|
||||
# Parse datetime
|
||||
parsed_date = datetime.strptime(f"{date_str}{time_str}", "%Y%m%d%H%M")
|
||||
datetime_list.append(parsed_date)
|
||||
|
||||
rainfile_arr = np.vstack(rainfile)
|
||||
np.savetxt(five_min_rainfall_spatial_timeseries_name, rainfile_arr, delimiter=' ', fmt='%1.1f')
|
||||
|
||||
# Create DataFrame with datetime index
|
||||
df = pd.DataFrame(rainfile_arr, index=datetime_list)
|
||||
# sort the dataframe into date order
|
||||
sorted_df = df.sort_index()
|
||||
# add headers
|
||||
header_row = ['rainfall_1', 'rainfall_2', 'rainfall_3', 'rainfall_4']
|
||||
file_name = f"csv_files/{location[0]}_timeseries_data.csv"
|
||||
sorted_df.to_csv(file_name, sep=",", float_format="%1.4f", header=header_row, index_label='datetime')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
extract_cropped_rain_data()
|
||||
if __name__ == "__main__":
|
||||
locations = [
|
||||
# loc name, loc id, x loc, y loc, resolution
|
||||
["BRICSC", "TM0816", 608500, 216500, 1000],
|
||||
["HEACSC", "TF6842", 568500, 342500, 1000],
|
||||
]
|
||||
for place in locations:
|
||||
extract_cropped_rain_data(place)
|
||||
|
||||
Reference in New Issue
Block a user