Skip to content

Commit

Permalink
added reading trk functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
keesnederhoff committed Mar 27, 2024
1 parent ea92338 commit d869f28
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 66 deletions.
198 changes: 194 additions & 4 deletions cht_cyclones/tropical_cyclone.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def __init__(self, name=None):


# Reading
def from_jmv30(self, filename):
self.read_track(filename, "jmv30")
def from_trk(self, filename):
self.read_track(filename, "trk")

def from_ddb_cyc(self, filename):
self.read_track(filename, "ddb_cyc")
Expand Down Expand Up @@ -218,8 +218,188 @@ def read_track(self, filename, fmt):
self.track = self.track.reset_index(drop=True)
if self.debug == 1: print('Successfully read track - ddb_cyc')

elif fmt == 'jmv30':
print("to do: work on progress")
elif fmt == 'trk':

# Initialize variables
lasttime = np.datetime64(datetime.strptime("2000010118", "%Y%m%d%H"))
newtime2 = lasttime.astype('O')
tc_time_string = newtime2.strftime('%Y%m%d %H%M%S')
it = 0
point = Point(0,0)
gdf = gpd.GeoDataFrame({"datetime": [tc_time_string],"geometry": [point], "vmax": [-999], "pc": [-999], "RMW": [-999],
"R35_NE": [-999], "R35_SE": [-999], "R35_SW": [-999], "R35_NW": [-999],
"R50_NE": [-999], "R50_SE": [-999], "R50_SW": [-999], "R50_NW": [-999],
"R65_NE": [-999], "R65_SE": [-999], "R65_SW": [-999], "R65_NW": [-999],
"R100_NE": [-999], "R100_SE": [-999],"R100_SW": [-999], "R100_NW": [-999]})
gdf.set_crs(epsg=self.EPSG, inplace=True)
self.track = gdf # Always start clean

# Open the file
with open(filename, 'r') as fid:
# Read line by line
for s0 in fid:
s0 = s0.strip()
if not s0:
continue

# Start
s = s0.split(',')
tc = {'name': 'not_known'}
tc['basin'] = s[0]
tc['storm_number'] = int(s[1])

# Read time
tstr = str(s[2])
tstr = tstr.replace(" ", "")
time = datetime.strptime(tstr, "%Y%m%d%H")
newtime = np.datetime64(time)

# Add forecasted time
hrs = float(s[5])
newtime = newtime + np.timedelta64(int(hrs*3600), 's')
if newtime > lasttime + np.timedelta64(1, 's'):

# Update all variables in gdf (when it is not the first one)
if it > 0:
gdf['geometry'] = [Point(x,y)] # Assign the new geometry
gdf.vmax = vmax
gdf.pc = pc
gdf.RMW = rmax
if R35_NE > 0: gdf.R35_NE = R35_NE
if R35_SE > 0: gdf.R35_SE = R35_SE
if R35_SW > 0: gdf.R35_SW = R35_SW
if R35_NW > 0: gdf.R35_NW = R35_NW

if R50_NE > 0: gdf.R50_NE = R50_NE
if R50_SE > 0: gdf.R50_SE = R50_SE
if R50_SW > 0: gdf.R50_SW = R50_SW
if R50_NW > 0: gdf.R50_NW = R50_NW

if R65_NE > 0: gdf.R65_NE = R65_NE
if R65_SE > 0: gdf.R65_SE = R65_SE
if R65_SW > 0: gdf.R65_SW = R65_SW
if R65_NW > 0: gdf.R65_NW = R65_NW

if R100_NE > 0: gdf.R100_NE = R100_NE
if R100_SE > 0: gdf.R100_SE = R100_SE
if R100_SW > 0: gdf.R100_SW = R100_SW
if R100_NW > 0: gdf.R100_NW = R100_NW

# New time point found
it += 1
lasttime = newtime

# Append self
self.track = pd.concat([self.track,gdf])

# Make TC string
newtime2 = newtime.astype('O')
tc_time_string = newtime2.strftime('%Y%m%d %H%M%S')

# Start fresh
gdf = gpd.GeoDataFrame({"datetime": [tc_time_string],"geometry": [point], "vmax": [-999], "pc": [-999], "RMW": [-999],
"R35_NE": [-999], "R35_SE": [-999], "R35_SW": [-999], "R35_NW": [-999],
"R50_NE": [-999], "R50_SE": [-999], "R50_SW": [-999], "R50_NW": [-999],
"R65_NE": [-999], "R65_SE": [-999], "R65_SW": [-999], "R65_NW": [-999],
"R100_NE": [-999], "R100_SE": [-999],"R100_SW": [-999], "R100_NW": [-999]})
gdf.set_crs(epsg=self.EPSG, inplace=True)

# Reset all values
x = 0
y = 0
vmax = -999
pc = -999
RMW = -999
R35_NE = -999
R35_SE = -999
R35_SW = -999
R35_NW = -999

R50_NE = -999
R50_SE = -999
R50_SW = -999
R50_NW = -999

R65_NE = -999
R65_SE = -999
R65_SW = -999
R65_NW = -999

R100_NE = -999
R100_SE = -999
R100_SW = -999
R100_NW = -999

# Latitude
if s[6][-1] == 'N':
y = 0.1 * float(s[6][:-1])
else:
y = -0.1 * float(s[6][:-1])
# Longitude
if s[7][-1] == 'E':
x = 0.1 * float(s[7][:-1])
else:
x = -0.1 * float(s[7][:-1])

# vmax
if float(s[8]) > 0:
vmax = float(s[8])

# Pressure and raddi
if len(s) > 9:

# Pressure
if float(s[9]) > 0:
pc = float(s[9])

# Radii
r = float(s[11])
if r in [34, 35]:
R35_NE = float(s[13])
R35_SE = float(s[14])
R35_SW = float(s[15])
R35_NW = float(s[16])
elif r == 50:
R50_NE = float(s[13])
R50_SE = float(s[14])
R50_SW = float(s[15])
R50_NW = float(s[16])
elif r in [64, 65]:
R65_NE = float(s[13])
R65_SE = float(s[14])
R65_SW = float(s[15])
R65_NW = float(s[16])
elif r == 100:
R100_NE = float(s[13])
R100_SE = float(s[14])
R100_SW = float(s[15])
R100_NW = float(s[16])

# Other things
if len(s) > 17:
if s[17]:
pressure_last_closed_isobar = float(s[17])
if s[18]:
radius_last_closed_isobar = float(s[18])
if s[19]:
try:
if float(s[19]) > 0:
rmax = float(s[19])
except ValueError:
print("Error: Unable to convert to float for RMW")
rmax = -999
if len(s) >= 28:
if s[27]:
name = s[27]

# Done with this => rest so track looks good
self.track = self.track.reset_index(drop=True)
self.track = self.track.drop([0]) # remove the dummy
self.track = self.track.reset_index(drop=True)
self.track = self.track.drop([0]) # remove the dummy
self.track = self.track.reset_index(drop=True)
if self.debug == 1: print('Successfully read track - trk')

else:
raise Exception('This file format is not supported as read track!')

Expand Down Expand Up @@ -1291,6 +1471,11 @@ def write_spiderweb_netcdf(self, spiderweb, filename):
root_grp = Dataset(filename, 'w', format='NETCDF4')
root_grp.description = 'Tropical cyclone spiderweb by Coastal Hazards Toolkit'

# Define dimensions
root_grp.createDimension('time', ntime)
root_grp.createDimension('range', nrows)
root_grp.createDimension('azimuth', ncols)

# Define variables
time_var = root_grp.createVariable('time', 'f8', ('time',))
range_var = root_grp.createVariable('range', 'f8', ('range',))
Expand Down Expand Up @@ -1429,6 +1614,9 @@ def __init__(self, name=None, TropicalCyclone=None):
# Methods
self.position_method = 0

# We assume that we are in the imperial system, so convert if needed
TropicalCyclone.convert_units_metric_imperial()

# Define best-track
self.best_track = TropicalCyclone
self.tstart = datetime.strptime(self.best_track.track.datetime[0], dateformat_module) # starting time of the track
Expand All @@ -1440,6 +1628,7 @@ def __init__(self, name=None, TropicalCyclone=None):

# Compute ensemble member
def compute_ensemble(self, number_of_realizations=None):

# First set time variables based on best track
if self.debug == 1: print("Started with making ensembles")
self.number_of_realizations = number_of_realizations
Expand Down Expand Up @@ -1679,6 +1868,7 @@ def compute_ensemble(self, number_of_realizations=None):
dy = dy / 110540
ensemble_lon[it,:] = best_track_lon2[it] + dx
ensemble_lat[it,:] = best_track_lat2[it] + dy

elif self.position_method == 1:
# Compute new position based on best track position and ate/cte
lon0 = best_track_lon2[items0]
Expand Down
66 changes: 66 additions & 0 deletions tests/TRK_COAMPS_CTCX_3_2017092006_15L
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
AL, 15, 2017092006, 03, CTCX, 000, 175N, 651W, 135, 914, XX, 34, NEQ, 0142, 0134, 0111, 0141, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 000, 175N, 651W, 135, 914, XX, 50, NEQ, 0097, 0081, 0064, 0093, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 000, 175N, 651W, 135, 914, XX, 64, NEQ, 0063, 0053, 0045, 0061, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 006, 180N, 660W, 115, 935, XX, 34, NEQ, 0167, 0201, 0103, 0124, 0, 0, 022
AL, 15, 2017092006, 03, CTCX, 006, 180N, 660W, 115, 935, XX, 50, NEQ, 0115, 0073, 0059, 0084, 0, 0, 022
AL, 15, 2017092006, 03, CTCX, 006, 180N, 660W, 115, 935, XX, 64, NEQ, 0055, 0046, 0041, 0058, 0, 0, 022
AL, 15, 2017092006, 03, CTCX, 012, 186N, 667W, 107, 949, XX, 34, NEQ, 0147, 0135, 0108, 0127, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 012, 186N, 667W, 107, 949, XX, 50, NEQ, 0093, 0069, 0068, 0082, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 012, 186N, 667W, 107, 949, XX, 64, NEQ, 0068, 0042, 0044, 0066, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 018, 192N, 675W, 115, 951, XX, 34, NEQ, 0155, 0141, 0104, 0141, 0, 0, 023
AL, 15, 2017092006, 03, CTCX, 018, 192N, 675W, 115, 951, XX, 50, NEQ, 0104, 0065, 0068, 0089, 0, 0, 023
AL, 15, 2017092006, 03, CTCX, 018, 192N, 675W, 115, 951, XX, 64, NEQ, 0059, 0045, 0050, 0061, 0, 0, 023
AL, 15, 2017092006, 03, CTCX, 024, 196N, 682W, 118, 951, XX, 34, NEQ, 0162, 0137, 0113, 0136, 0, 0, 020
AL, 15, 2017092006, 03, CTCX, 024, 196N, 682W, 118, 951, XX, 50, NEQ, 0097, 0098, 0059, 0077, 0, 0, 020
AL, 15, 2017092006, 03, CTCX, 024, 196N, 682W, 118, 951, XX, 64, NEQ, 0063, 0047, 0039, 0054, 0, 0, 020
AL, 15, 2017092006, 03, CTCX, 030, 201N, 688W, 116, 951, XX, 34, NEQ, 0148, 0145, 0092, 0126, 0, 0, 019
AL, 15, 2017092006, 03, CTCX, 030, 201N, 688W, 116, 951, XX, 50, NEQ, 0090, 0101, 0060, 0082, 0, 0, 019
AL, 15, 2017092006, 03, CTCX, 030, 201N, 688W, 116, 951, XX, 64, NEQ, 0058, 0052, 0044, 0052, 0, 0, 019
AL, 15, 2017092006, 03, CTCX, 036, 206N, 693W, 117, 949, XX, 34, NEQ, 0153, 0153, 0109, 0188, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 036, 206N, 693W, 117, 949, XX, 50, NEQ, 0095, 0086, 0061, 0080, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 036, 206N, 693W, 117, 949, XX, 64, NEQ, 0071, 0068, 0041, 0065, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 042, 212N, 696W, 113, 951, XX, 34, NEQ, 0162, 0195, 0104, 0127, 0, 0, 026
AL, 15, 2017092006, 03, CTCX, 042, 212N, 696W, 113, 951, XX, 50, NEQ, 0119, 0088, 0068, 0082, 0, 0, 026
AL, 15, 2017092006, 03, CTCX, 042, 212N, 696W, 113, 951, XX, 64, NEQ, 0071, 0074, 0038, 0066, 0, 0, 026
AL, 15, 2017092006, 03, CTCX, 048, 217N, 700W, 117, 953, XX, 34, NEQ, 0166, 0160, 0095, 0138, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 048, 217N, 700W, 117, 953, XX, 50, NEQ, 0106, 0108, 0063, 0098, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 048, 217N, 700W, 117, 953, XX, 64, NEQ, 0080, 0055, 0046, 0058, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 054, 225N, 704W, 108, 954, XX, 34, NEQ, 0167, 0171, 0150, 0143, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 054, 225N, 704W, 108, 954, XX, 50, NEQ, 0116, 0116, 0068, 0076, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 054, 225N, 704W, 108, 954, XX, 64, NEQ, 0085, 0050, 0053, 0062, 0, 0, 018
AL, 15, 2017092006, 03, CTCX, 060, 233N, 707W, 104, 957, XX, 34, NEQ, 0165, 0175, 0137, 0140, 0, 0, 023
AL, 15, 2017092006, 03, CTCX, 060, 233N, 707W, 104, 957, XX, 50, NEQ, 0117, 0126, 0080, 0091, 0, 0, 023
AL, 15, 2017092006, 03, CTCX, 060, 233N, 707W, 104, 957, XX, 64, NEQ, 0060, 0057, 0042, 0058, 0, 0, 023
AL, 15, 2017092006, 03, CTCX, 066, 239N, 709W, 92, 958, XX, 34, NEQ, 0177, 0194, 0136, 0147, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 066, 239N, 709W, 92, 958, XX, 50, NEQ, 0117, 0134, 0085, 0095, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 066, 239N, 709W, 92, 958, XX, 64, NEQ, 0068, 0055, 0049, 0074, 0, 0, 025
AL, 15, 2017092006, 03, CTCX, 072, 247N, 713W, 97, 960, XX, 34, NEQ, 0175, 0223, 0167, 0157, 0, 0, 030
AL, 15, 2017092006, 03, CTCX, 072, 247N, 713W, 97, 960, XX, 50, NEQ, 0111, 0102, 0090, 0120, 0, 0, 030
AL, 15, 2017092006, 03, CTCX, 072, 247N, 713W, 97, 960, XX, 64, NEQ, 0074, 0065, 0029, 0054, 0, 0, 030
AL, 15, 2017092006, 03, CTCX, 078, 254N, 717W, 91, 961, XX, 34, NEQ, 0197, 0183, 0161, 0151, 0, 0, 028
AL, 15, 2017092006, 03, CTCX, 078, 254N, 717W, 91, 961, XX, 50, NEQ, 0150, 0128, 0070, 0105, 0, 0, 028
AL, 15, 2017092006, 03, CTCX, 078, 254N, 717W, 91, 961, XX, 64, NEQ, 0073, 0046, 0041, 0051, 0, 0, 028
AL, 15, 2017092006, 03, CTCX, 084, 261N, 719W, 77, 960, XX, 34, NEQ, 0203, 0193, 0167, 0160, 0, 0, 021
AL, 15, 2017092006, 03, CTCX, 084, 261N, 719W, 77, 960, XX, 50, NEQ, 0128, 0143, 0074, 0102, 0, 0, 021
AL, 15, 2017092006, 03, CTCX, 084, 261N, 719W, 77, 960, XX, 64, NEQ, 0061, 0112, 0042, 0052, 0, 0, 021
AL, 15, 2017092006, 03, CTCX, 090, 268N, 718W, 79, 959, XX, 34, NEQ, 0199, 0193, 0175, 0198, 0, 0, 032
AL, 15, 2017092006, 03, CTCX, 090, 268N, 718W, 79, 959, XX, 50, NEQ, 0135, 0138, 0081, 0115, 0, 0, 032
AL, 15, 2017092006, 03, CTCX, 090, 268N, 718W, 79, 959, XX, 64, NEQ, 0112, 0052, 0045, 0052, 0, 0, 032
AL, 15, 2017092006, 03, CTCX, 096, 277N, 717W, 79, 961, XX, 34, NEQ, 0198, 0220, 0156, 0154, 0, 0, 021
AL, 15, 2017092006, 03, CTCX, 096, 277N, 717W, 79, 961, XX, 50, NEQ, 0124, 0127, 0080, 0101, 0, 0, 021
AL, 15, 2017092006, 03, CTCX, 096, 277N, 717W, 79, 961, XX, 64, NEQ, 0066, 0063, 0053, 0054, 0, 0, 021
AL, 15, 2017092006, 03, CTCX, 102, 285N, 715W, 82, 963, XX, 34, NEQ, 0204, 0205, 0169, 0154, 0, 0, 022
AL, 15, 2017092006, 03, CTCX, 102, 285N, 715W, 82, 963, XX, 50, NEQ, 0138, 0133, 0094, 0105, 0, 0, 022
AL, 15, 2017092006, 03, CTCX, 102, 285N, 715W, 82, 963, XX, 64, NEQ, 0058, 0069, 0024, 0033, 0, 0, 022
AL, 15, 2017092006, 03, CTCX, 108, 294N, 713W, 76, 963, XX, 34, NEQ, 0202, 0216, 0165, 0165, 0, 0, 026
AL, 15, 2017092006, 03, CTCX, 108, 294N, 713W, 76, 963, XX, 50, NEQ, 0142, 0161, 0092, 0110, 0, 0, 026
AL, 15, 2017092006, 03, CTCX, 108, 294N, 713W, 76, 963, XX, 64, NEQ, 0129, 0063, 0000, 0039, 0, 0, 026
AL, 15, 2017092006, 03, CTCX, 114, 302N, 710W, 71, 966, XX, 34, NEQ, 0210, 0221, 0163, 0166, 0, 0, 042
AL, 15, 2017092006, 03, CTCX, 114, 302N, 710W, 71, 966, XX, 50, NEQ, 0143, 0147, 0082, 0112, 0, 0, 042
AL, 15, 2017092006, 03, CTCX, 114, 302N, 710W, 71, 966, XX, 64, NEQ, 0099, 0049, 0049, 0062, 0, 0, 042
AL, 15, 2017092006, 03, CTCX, 120, 311N, 707W, 72, 967, XX, 34, NEQ, 0196, 0224, 0163, 0159, 0, 0, 032
AL, 15, 2017092006, 03, CTCX, 120, 311N, 707W, 72, 967, XX, 50, NEQ, 0126, 0128, 0105, 0091, 0, 0, 032
AL, 15, 2017092006, 03, CTCX, 120, 311N, 707W, 72, 967, XX, 64, NEQ, 0069, 0043, 0000, 0046, 0, 0, 032
AL, 15, 2017092006, 03, CTCX, 126, 317N, 703W, 76, 969, XX, 34, NEQ, 0201, 0207, 0184, 0165, 0, 0, 030
AL, 15, 2017092006, 03, CTCX, 126, 317N, 703W, 76, 969, XX, 50, NEQ, 0143, 0119, 0092, 0111, 0, 0, 030
AL, 15, 2017092006, 03, CTCX, 126, 317N, 703W, 76, 969, XX, 64, NEQ, 0082, 0089, 0000, 0000, 0, 0, 030
Loading

0 comments on commit d869f28

Please sign in to comment.