-
Notifications
You must be signed in to change notification settings - Fork 33
Add csv_write() to fix issue #45 #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,4 +8,5 @@ venv | |
| dev.py | ||
| /.pytest_cache | ||
| /__pycache__/ | ||
| .pytest_cache | ||
| .pytest_cache | ||
| *.csv | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,60 @@ def json_write(data, fname): | |
| json.dump(data, json_file, ensure_ascii=False, indent=2, sort_keys=False) | ||
|
|
||
|
|
||
| # def write_csv(VPP, fname): | ||
| def csv_write(tws_range, twa_range, store, fname): | ||
| import csv | ||
|
|
||
| # Init a counter for how many values have been used to average the boat speed for a given wind direction and speed: | ||
| avg_counter = np.zeros((37, 20)) | ||
|
|
||
| with open(fname, mode="w", newline="") as file: | ||
| writer = csv.writer(file, delimiter=";") | ||
| # Write header with TWS in knots | ||
| writer.writerow(["TWA\TWS", "2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "32", "34", "36", "38", "40"]) | ||
| # Loop through every degree from 0°to 180° in 5° increments | ||
| for angle in range(0, 181, 5): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, not general, use the |
||
| # Init row to write | ||
| row = [None] * 21 | ||
| # Set angle | ||
| row[0] = angle | ||
| # Go through each TWS and get the corresponding boat speed for the current angle | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you the |
||
| for i, tws in enumerate(tws_range): | ||
| # Change tws to knots | ||
| tws = tws / KNOTS_TO_MPS | ||
| for j, twa in enumerate(twa_range): | ||
| # Get closest wind speed and find column in openCPN. Note we are stuck using these until this issue (https://github.com/rgleason/polar_pi/issues/34) gets dealt with | ||
| closest_wind_speed_column = round(tws/2) | ||
| # If closest wind speed column is zero, increment by one so we don't overwrite the angle column. | ||
| if closest_wind_speed_column == 0: | ||
| closest_wind_speed_column += 1 | ||
| # Get closest angle and find see if it matches the current angle we are looking for | ||
| closest_angle = round(twa/5)*5 | ||
| # If the closest_angle matches the angle we're currently working with then get the speeds, otherwise, skip to next entry | ||
| if closest_angle == angle: | ||
| # Get boat speed for give wind angle and speed | ||
| boat_speed = store[i, j, 0, 0] | ||
| # Get row number | ||
| row_number = int(angle/5) | ||
|
|
||
| # Update the row with the boat speed for this angle and wind speed | ||
| # If there has not been a value used yet, set the value to the boat speed, otherwise, average the current value with the new boat speed | ||
| if row[closest_wind_speed_column] is None: | ||
| row[closest_wind_speed_column] = boat_speed | ||
| else: | ||
| # Find out how many values have been used to make this average | ||
| num_values = avg_counter[row_number, closest_wind_speed_column] | ||
| # Average the current boat speed with the already logged average boat speed | ||
| row[closest_wind_speed_column] = (row[closest_wind_speed_column] * num_values + boat_speed) / (num_values + 1) | ||
|
|
||
| # Update the average counter for this angle and wind speed | ||
| avg_counter[row_number, closest_wind_speed_column] += 1 | ||
| # Print out the speed of the vessel given the angle and wind speed | ||
|
|
||
| # Write the row to the csv file | ||
| writer.writerow(row) | ||
|
|
||
|
|
||
| def build_interp_func(fname, i=1, kind="linear"): | ||
| """ | ||
| build interpolatison function and returns it in a list | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not general, it should use the
tws_rangearray to generate the header of the CSV, then in your specific case, you can make sure you run these TWS.