RCube
Rcube Rest Server calculates sail routes based on Grib files and sailing boat polar files
Loading...
Searching...
No Matches
option.c
Go to the documentation of this file.
1
2#include <math.h>
3#include <stdbool.h>
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <stdint.h> // uint64_t
8#include "r3types.h"
9#include "grib.h"
10#include "readgriball.h"
11#include "r3util.h"
12#include "engine.h"
13#include "polar.h"
14#include "glibwrapper.h"
15#include "inline.h"
16
18void optionManage (char option) {
19 FILE *f = NULL;
20 char *buffer = NULL;
21 char footer [MAX_SIZE_LINE] = "";
22 double w, twa, tws, lon, lat, lat2, lon2, cog, t, hours;
23 char str [MAX_SIZE_LINE] = "";
24 int sail, intRes, nTries;
25 long dataDate;
26 const long nIter = 1e9;
27
28 if ((buffer = (char *) malloc (MAX_SIZE_BUFFER)) == NULL) {
29 fprintf (stderr, "In optionManage, Error Malloc %d\n", MAX_SIZE_BUFFER);
30 return;
31 }
32 buffer [0] = '\0';
33 printf ("\n");
34
35 switch (option) {
36 case 'c': // cap
37 printf ("Lon1 = ");
38 if (scanf ("%lf", &lon) < 1) break;
39 printf ("Lat1 = ");
40 if (scanf ("%lf", &lat) < 1) break;
41 printf ("Lon2 = ");
42 if (scanf ("%lf", &lon2) < 1) break;
43 printf ("Lat2 = ");
44 if (scanf ("%lf", &lat2) < 1) break;
45 printf ("Ortho cap1: %.2lf°, Ortho cap2: %.2lf°\n", orthoCap (lat, lon, lat2, lon2), orthoCap (lat2, lon2, lat, lon));
46 printf ("Ortho2 cap1: %.2lf°, Ortho2 cap2: %.2lf°\n", orthoCap2 (lat, lon, lat2, lon2), orthoCap2 (lat2, lon2, lat, lon));
47 printf ("Orthodist1 : %.2lf, Orthodist2: %.2lf\n", orthoDist (lat, lon, lat2, lon2), orthoDist (lat2, lon2, lat, lon));
48 printf ("Orthodist1 : %.2lf, Orthodist2: %.2lf\n", orthoDist2 (lat, lon, lat2, lon2), orthoDist (lat2, lon2, lat, lon));
49 printf ("Loxodist1 : %.2lf, Loxodist2 : %.2lf\n", loxoDist(lat, lon, lat2, lon2), loxoDist (lat2, lon2, lat, lon));
50 break;
51 case 'g': // grib
52 gribToStr (&zone, buffer, MAX_SIZE_BUFFER);
53 printf ("%s\n", buffer);
54 printf ("grib print...\n");
56 printf ("\n\nFollowing lines are suspects info...\n");
57 checkGribToStr (true, buffer, MAX_SIZE_BUFFER);
58 printf ("%s\n", buffer);
59 break;
60 case 'G': // grib current
62 printf ("%s\n", buffer);
63 printf ("grib print...\n");
65 printf ("\n\nFollowing lines are suspects info...\n");
66 checkGribToStr (true, buffer, MAX_SIZE_BUFFER);
67 printf ("%s\n", buffer);
68 break;
69 case 'h': // help
70 printf ("Size of size_t : %zu bytes\n", sizeof (size_t));
71 if ((f = fopen (par.cliHelpFileName, "r")) == NULL) {
72 fprintf (stderr, "In optionManage, Error help: Impossible to read: %s\n", par.cliHelpFileName);
73 break;
74 }
75 while ((fgets (str, MAX_SIZE_LINE, f) != NULL ))
76 printf ("%s", str);
77 fclose (f);
78 break;
79 case 'p': // polar
81 printf ("%s\n", buffer);
83 // printf ("%s\n", buffer);
84 while (true) {
85 printf ("twa true wind angle = ");
86 if (scanf ("%lf", &twa) < 1) break;
87 printf ("tws true wind speed = ");
88 if (scanf ("%lf", &tws) < 1) break;
89 printf ("- Speed over ground: %.2lf\n", findPolar (twa, tws, &polMat, &sailPolMat, &sail));
90 printf ("1 Speed over ground: %.2lf\n", findPolar1 (twa, tws, &polMat, &sailPolMat, &sail));
91 printf ("2 Speed over ground: %.2lf\n", findPolar2 (twa, tws, &polMat, &sailPolMat, &sail));
92 printf ("Sail: %d\n", sail);
93
94 double t0 = monotonic ();
95 for (long i = 0; i < nIter; i += 1) findPolar (fmod (twa+=10.0, 180.0), fmod (tws+=13.0, 90.0), &polMat, &sailPolMat, &sail);
96
97 double t1 = monotonic ();
98 for (long i = 0; i < nIter; i += 1) findPolar1 (fmod (twa+=10.0, 180.0), fmod (tws+=13.0, 90.0), &polMat, &sailPolMat, &sail);
99
100 double t2 = monotonic ();
101 for (long i = 0; i < nIter; i += 1) findPolar2 (fmod (twa+=10.0, 180.0), fmod (tws+=13.0, 90.0), &polMat, &sailPolMat, &sail);
102
103 double t3 = monotonic ();
104
105 printf ("✅ findPolar.: %.2lf seconds\n", t1 - t0);
106 printf ("✅ findPolar1: %.2lf seconds\n", t2 - t1);
107 printf ("✅ findPolar2: %.2lf seconds\n", t3 - t2);
108 }
109
110 break;
111 case 'P': // Wave polar
113 printf ("%s\n", buffer);
114 while (true) {
115 printf ("angle = " );
116 if (scanf ("%lf", &twa) < 1) break;
117 printf ("w = ");
118 if (scanf ("%lf", &w) < 1) break;
119 printf ("coeff: %.2lf\n", findPolar (twa, w, &wavePolMat, NULL, &sail) / 100.0);
120 }
121 break;
122 case 'q':
123 polToStr (&polMat, buffer, MAX_SIZE_BUFFER);
124 printf ("%s\n", buffer);
125 while (true) {
126 printf ("tws = " );
127 if (scanf ("%lf", &tws) < 1) break;
128 printf ("newMaxSpeedInPolarAt: %.4lf\n", maxSpeedInPolarAt (tws, &polMat));
129 }
130 break;
131 case 'r': // routing
132 routingLaunch ();
133 routeToStr (&route, buffer, MAX_SIZE_BUFFER, footer, sizeof (footer));
134 printf ("%s\n", buffer);
135 printf ("%s\n", footer);
136 break;
137 case 'R': // routing
138 printf ("nTries: ");
139 if (scanf ("%d", &nTries) != 1) break;
140 for (int i = 0; i < nTries; i += 1) routingLaunch ();
141 routeToStr (&route, buffer, MAX_SIZE_BUFFER, footer, sizeof (footer));
142 printf ("%s\n", buffer);
143 printf ("%s\n", footer);
144 break;
145 case 's': // isIsea
146 if (tIsSea == NULL) printf ("in readIsSea : bizarre\n");
147 // dumpIsSea ();
148 while (1) {
149 printf ("Lat = ");
150 if (scanf ("%lf", &lat) < 1) break;
151 printf ("Lon = ");
152 if (scanf ("%lf", &lon) < 1) break;
153 if (isSea (tIsSea, lat, lon))
154 printf ("Sea\n");
155 else printf ("Earth\n");
156 }
157 break;
158 case 't': // test
159 readGribAll ("/home/rr/routing/grib/GFS_20251121_12Z_144.grb", &zone, WIND);
160 printf ("GribTime: %s\n", gribDateTimeToStr (zone.dataDate[0], zone.dataTime[0], str, sizeof str));
161 while (true) {
162 printf ("Lat = ");
163 if (scanf ("%lf", &lat) < 1) break;
164 printf ("Lon = ");
165 if (scanf ("%lf", &lon) < 1) break;
166 printf ("t = ");
167 if (scanf ("%lf", &t) < 1) break;
168 intRes = isDay (t, zone.dataDate[0], zone.dataTime[0], lat, lon);;
169 printf("res: %s\n", intRes ? "day" : "night");
170 }
171 break;
172 case 'T': // time
173 printf ("GribTime: %s\n", gribDateTimeToStr (zone.dataDate[0], zone.dataTime[0], str, sizeof str));
174 printf ("offsetLocalUTC: %lf s\n", offsetLocalUTC ());
175 time_t t = time (NULL);
176 printf ("now epochToStr: %s\n", epochToStr (t, true, str, sizeof str));
178 printf ("grib start epochToStr: %s\n", epochToStr (t, true, str, sizeof str));
179 while (true) {
180 printf ("date: ");
181 if (scanf ("%ld", &dataDate) < 1) break;
182 printf ("decimal hours: ");
183 if (scanf ("%lf", &hours) < 1) break;
184 printf ("DateUtc: %s\n", newDate (dataDate, hours, str, sizeof str));
185 }
186 break;
187 case 'v': // version
188 printf ("Prog version: %s, %s, %s\n", PROG_NAME, PROG_VERSION, PROG_AUTHOR);
189 printf ("Compilation-date: %s\n", __DATE__);
190 break;
191 case 'w': // twa
192 while (true) {
193 printf ("COG = ");
194 if (scanf ("%lf", &cog) < 1) break;
195 printf ("TWS = ");
196 if (scanf ("%lf", &twa) < 1) break;
197 printf ("fTwa = %.2lf\n",fTwa (cog, twa));
198 }
199 break;
200 case 'z': //
201 printf ("Password %s\n", par.mailPw);
202 // printf ("Password %s\n", dollarSubstitute (par.mailPw, buffer, strlen (par.mailPw)));
203 break;
204 default:
205 printf ("Option unknown: -%c\n", option);
206 break;
207 }
208 free (buffer);
209}
bool routeToStr(const SailRoute *route, char *str, size_t maxLen, char *footer, size_t maxLenFooter)
copy route in a string true if enough space, false if truncated
Definition engine.c:627
void * routingLaunch()
launch routing with parameters
Definition engine.c:1192
SailRoute route
store sail route calculated in engine.c by routing
Definition engine.c:39
char * gribToStr(const Zone *zone, char *str, size_t maxLen)
write Grib information in string
Definition r3grib.c:530
void printGrib(const Zone *zone, const FlowP *gribData)
print Grib u v ... for all lat lon time information
Definition r3grib.c:69
bool checkGribToStr(bool hasCurrentGrib, char *buffer, size_t maxLen)
check Grib information and write report in the buffer return false if something wrong
Definition r3grib.c:316
static double fTwa(double heading, double twd)
return TWA between -180 and 180 note : tribord amure if twa < 0
Definition inline.h:64
static double maxSpeedInPolarAt(double tws, const PolMat *mat)
return max speed of boat at tws for all twa
Definition inline.h:308
static bool isSea(char *isSeaArray, double lat, double lon)
this file contains small inlines functions to be included in source files
Definition inline.h:8
static double findPolar2(double twa, double w, const PolMat *mat, const PolMat *sailMat, int *sail)
find in polar boat speed or wave coeff and sail number if sailMat != NULL
Definition inline.h:277
static double findPolar(double twa, double w, const PolMat *mat, const PolMat *sailMat, int *sail)
find in polar boat speed or wave coeff
Definition inline.h:174
static double orthoDist(double lat1, double lon1, double lat2, double lon2)
return orthodromic distance in nautical miles from origin to destination
Definition inline.h:147
static double orthoCap(double lat1, double lon1, double lat2, double lon2)
return initial orthodromic cap from origin to destination equivalent to : return directCap (lat1,...
Definition inline.h:96
static double orthoDist2(double lat1, double lon1, double lat2, double lon2)
return orthodomic distance in nautical miles from origin to destinationi, Haversine formula time cons...
Definition inline.h:161
static bool isDay(double t, long dataDate, long dataTime, double lat, double lon)
true if day light, false if night
Definition inline.h:421
static double loxoDist(double lat1, double lon1, double lat2, double lon2)
return loxodromic distance in nautical miles from origin to destination
Definition inline.h:119
static double findPolar1(double twa, double w, const PolMat *mat, const PolMat *sailMat, int *sail)
find in polar boat speed or wave coeff and sail number if sailMat != NULL
Definition inline.h:241
static double orthoCap2(double lat1, double lon1, double lat2, double lon2)
return initial orthodromic cap from origin to destination, no givry correction
Definition inline.h:108
void optionManage(char option)
compilation: gcc -c option.c
Definition option.c:18
char * polToStr(const PolMat *mat, char *str, size_t maxLen)
write polar information in string
Definition polar.c:193
#define PROG_NAME
Definition r3types.h:31
#define PROG_VERSION
Definition r3types.h:32
#define MAX_SIZE_LINE
Definition r3types.h:45
#define PROG_AUTHOR
Definition r3types.h:33
@ WIND
Definition r3types.h:77
@ CURRENT
Definition r3types.h:77
#define MAX_SIZE_BUFFER
Definition r3types.h:53
char * epochToStr(time_t t, bool seconds, char *str, size_t maxLen)
convert epoch time to string with or without seconds
Definition r3util.c:247
Zone currentZone
Definition r3util.c:65
double offsetLocalUTC(void)
return offset Local UTC in seconds
Definition r3util.c:263
double monotonic(void)
return seconds with decimals
Definition r3util.c:1099
PolMat wavePolMat
polar matrix for waves
Definition r3util.c:55
char * tIsSea
table describing if sea or earth
Definition r3util.c:61
PolMat polMat
polar matrix description
Definition r3util.c:49
char * newDate(long intDate, double nHours, char *res, size_t maxLen)
return date and time using ISO notation after adding myTime (hours) to the Date
Definition r3util.c:386
PolMat sailPolMat
polar matrix for sails
Definition r3util.c:52
Par par
parameter
Definition r3util.c:58
Zone zone
geographic zone covered by grib file
Definition r3util.c:64
char * gribDateTimeToStr(long date, long time, char *str, size_t maxLen)
return str representing grib date
Definition r3util.c:276
time_t gribDateTimeToEpoch(long date, long hhmm)
convert long date/time from GRIB to time_t (UTC, via timegm)
Definition r3util.c:408
FlowP * tGribData[]
grib data description
bool readGribAll(const char *fileName, Zone *zone, int iFlow)
read grib file using eccodes C API return true if OK
char mailPw[MAX_SIZE_NAME]
Definition r3types.h:409
char cliHelpFileName[MAX_SIZE_FILE_NAME]
Definition r3types.h:351
long dataTime[MAX_N_DATA_TIME]
Definition r3types.h:165
long dataDate[MAX_N_DATA_DATE]
Definition r3types.h:164