-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
executable file
·419 lines (311 loc) · 15.7 KB
/
compile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python
from __future__ import print_function
from string import Template
import sys
# Characters
TEMPLATE_CHARS_UTF = {
"chr_degree": 'CHAR(176)', # degree sign
"chr_space": 'CHAR(160)', # non-breaking space
"chr_min": 'CHAR(39)', # '
"chr_sec": 'CHAR(34)', # "
"chr_dot": 'CHAR(44)', # .
"chr_minus": 'CHAR(45)', # -
"chr_comma": 'CHAR(46)', # ,
"chr_zero": 'CHAR(48)', # 0
}
# Characters
TEMPLATE_CHARS_ASCII = {
"chr_degree": 'CHAR(100)', # d
"chr_space": 'CHAR(32)', # regular space
"chr_min": 'CHAR(39)', # '
"chr_sec": 'CHAR(34)', # "
"chr_dot": 'CHAR(44)', # .
"chr_minus": 'CHAR(45)', # -
"chr_comma": 'CHAR(46)', # ,
"chr_zero": 'CHAR(48)', # 0
}
TEMPLATE_INPUTS = {
"in_deg_dec": '(A2)', # Decimal degrees
"in_round_min": '(B2)', # Rounding for minutes
"in_round_sec": '(C2)', # Rounding for seconds
"in_cpt_deg": '(D2)', # Component: degrees
"in_cpt_min": '(E2)', # Component: minutes
"in_cpt_sec": '(F2)', # Component: seconds
}
TEMPLATE_COMPUTE = {
# Sign
"sign": 'IF(SIGN(${in_deg_dec})=-1;${chr_minus};${chr_space})',
# Conversions
"degrees_int": '(FLOOR(ABS(${in_deg_dec});1))',
"minutes_dec": '(60*(ABS(${in_deg_dec})-${degrees_int}))',
"minutes_int": '(FLOOR(${minutes_dec};1))',
"seconds_dec": '(60*(${minutes_dec}-${minutes_int}))',
"seconds_int": 'FLOOR(${seconds_dec};1)',
# - rounding
"minutes_decimals_raw": 'ROUND(POWER(10;${in_round_min})*(${minutes_dec} - ${minutes_int});0)',
"minutes_decimals_padding_len": '(${in_round_min}-LEN(${minutes_decimals_raw}))',
"minutes_decimals_padding": 'REPT(${chr_zero}; MAX(0; ${minutes_decimals_padding_len}))',
"minutes_dec_rounded": 'IF(${in_round_min} > 0; CONCATENATE(${minutes_int}; ${chr_comma}; ${minutes_decimals_padding}; ${minutes_decimals_raw}); ${minutes_int})',
"minutes_padding": 'IF(${minutes_dec} < 10; 0; REPT(CHAR(32); 0))',
# - rounding
"seconds_decimals_raw": 'ROUND(POWER(10;${in_round_sec})*(${seconds_dec} - ${seconds_int});0)',
"seconds_decimals_padding_len": '(${in_round_sec}-LEN(${seconds_decimals_raw}))',
"seconds_decimals_padding": 'REPT(${chr_zero}; MAX(0; ${seconds_decimals_padding_len}))',
"seconds_dec_rounded": 'IF(${in_round_sec} > 0; CONCATENATE(${seconds_int}; ${chr_comma}; ${seconds_decimals_padding}; ${seconds_decimals_raw}); ${seconds_int})',
"seconds_padding": 'IF(${seconds_dec} < 10; 0; REPT(CHAR(32); 0))',
}
TEMPLATE_OUTPUTS = {
# Final formulas
"form_deg2ddm_rounded": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_dec_rounded}; ${chr_min})',
"form_deg2ddm_rounded_p": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_padding}; ${minutes_dec_rounded}; ${chr_min})',
"form_deg2ddm_rounded_w": 'IF(ISBLANK(${in_deg_dec}); ${in_deg_dec}; IF(${in_deg_dec} = ${chr_minus}; ${in_deg_dec}; ${form_deg2ddm_rounded_p}))',
"form_deg2ddm_unrounded": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_dec}; ${chr_min})',
"form_deg2ddm_int": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_int}; ${chr_min})',
"form_deg2dms_rounded": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_int}; ${chr_min}; ${chr_space}; ${seconds_dec_rounded}; ${chr_sec})',
"form_deg2dms_rounded_p": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_padding}; ${minutes_int}; ${chr_min}; ${chr_space}; ${seconds_padding}; ${seconds_dec_rounded}; ${chr_sec})',
"form_deg2dms_rounded_w": 'IF(ISBLANK(${in_deg_dec}); ${in_deg_dec}; IF(${in_deg_dec} = ${chr_minus}; ${in_deg_dec}; ${form_deg2dms_rounded_p}))',
"form_deg2dms_unrounded": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_int}; ${chr_min}; ${chr_space}; ${seconds_dec}; ${chr_sec})',
"form_deg2dms_int": 'CONCATENATE(${sign}; ${degrees_int}; ${chr_degree}; ${chr_space}; ${minutes_int}; ${chr_min}; ${chr_space}; ${seconds_int}; ${chr_sec})',
"form_dms2deg": '(${in_cpt_deg}+(${in_cpt_min}/60)+(${in_cpt_sec}/3600))',
# Component values
"cpt_degrees_int": '(SIGN(${in_deg_dec})*${degrees_int})',
"cpt_minutes_dec": '(SIGN(${in_deg_dec})*${minutes_dec})',
"cpt_minutes_int": '(SIGN(${in_deg_dec})*${minutes_int})',
"cpt_seconds_dec": '(SIGN(${in_deg_dec})*${seconds_dec})',
# Helpers for the CSV templates
"csv_line": '"=${form_deg2ddm_rounded}","=${form_deg2ddm_unrounded}","=${form_deg2ddm_int}","=${form_deg2dms_rounded}","=${form_deg2dms_unrounded}","=${form_deg2dms_int}"',
"csv_head": 'DDM_Round,DDM_URound,DDM_Int,DMS_Round,DMS_URound,DMS_Int',
"csv_debug_head": 'DDM_Round,DDM_URound,DDM_Int,DMS_Round,DMS_URound,DMS_Int,degrees_int,chr_degree,chr_space,minutes_int,chr_min,chr_space,seconds_dec_rounded,chr_sec,in_round_sec,seconds_int,chr_comma,seconds_decimals_padding,seconds_decimals_raw,seconds_decimals_padding_len',
"csv_debug_line": '"=${form_deg2ddm_rounded}","=${form_deg2ddm_unrounded}","=${form_deg2ddm_int}","=${form_deg2dms_rounded}","=${form_deg2dms_unrounded}","=${form_deg2dms_int}","=${degrees_int}","=${chr_degree}","=${chr_space}","=${minutes_int}","=${chr_min}","=${chr_space}","=${seconds_dec_rounded}","=${chr_sec}","=${in_round_sec}","=${seconds_int}","=${chr_comma}","=${seconds_decimals_padding}","=${seconds_decimals_raw}","=${seconds_decimals_padding_len}"',
}
# README.md document
TEXT_README_MD = '''\
# 10,123° => 10° 07" 22,800'
This page contains formulas for spreadsheet programs for converting angles in decimal degrees to
fractional (degrees + minutes + seconds and degrees + decimal minutes) formats.
Background: [https://en.wikipedia.org/wiki/Geographic coordinate conversion](https://en.wikipedia.org/wiki/Geographic_coordinate_conversion)
## Bugs & Caveats
- The `form_deg2dms_rounded` formula causes a "Formula overflow" error in OpenOffice.org 4.1.5
- Some inputs cause a _"FLOOR requires both arguments to be positive or negative"_ error, probably some float rounding issue:
- 4.1
- 3.9
- 12.95
- The whole thing is hastily put together and not thoroughly tested
## Usage
Simply copy and paste the formulas from below, or use one of the included files. Modify the
input cells as necessary. Each formula is self-contained and only depends on (one or more of)
the input fields.
Use `./compile.py -f ... -i ... -i ...` to print out a single formula with custom input fields.
The decimal -> DDM / DMS formulas use three input cells (`${in_deg_dec}`, `${in_round_min}`
and `${in_round_sec}`) for the decimal degrees, number of decimals in the produced minutes
and seconds, respectively. The trivial DMS -> decimal formula uses `${in_cpt_deg}`, `${in_cpt_min}` and
`${in_cpt_sec}` for degrees, minutes and seconds.
Import options for CSV:
- Separated by: comma
- Text delimiter: "
### Files
With Unicode values in `CHAR()` calls (for spaces and the degree sign):
- [README.md](README.md) (this file)
- [demo-unicode.csv](demo-unicode.csv)
- [debug-unicode.csv](debug-unicode.csv)
With ASCII values in `CHAR()` calls (for spaces and the degree sign):
- [formulas-ascii.md](formulas-ascii.md)
- [demo-ascii.csv](demo-ascii.csv)
- [debug-ascii.csv](debug-ascii.csv)
### `compile.py`
The formulas and the README.md you're reading now are generated by the `compile.py`script,
where the former are also found in human-readable form. It should work on Python 2.7+
without 3rd party libraries.
```text
${compile_py_usage}
```
'''
TEXT_FORMULAS_MD = '''\
## Formulas (degrees to fractions)
### Inputs
For decimal degrees => fractions conversion:
- Decimal degrees: `${in_deg_dec}`
- Nr. of decimals to round minutes to: `${in_round_min}`
- Nr. of decimals to round seconds to: `${in_round_sec}`
### Rounded values
Use these if you need the DDM or DMS representation as a human-readable string in a single cell.
#### (string) `form_deg2ddm_rounded`
_Returns a string representing the decimal degrees in `${in_deg_dec}` in DDM (Degrees, Decimal Minutes) format, minutes rounded to `${in_round_min}` digits. Blank inputs are interpreted as zero._
- (-10,123°, 3, 3) => -10°7.380"
`${form_deg2ddm_rounded}`
#### (string) `form_deg2ddm_rounded_w`
_Returns a string representing the decimal degrees in `${in_deg_dec}` in DDM (Degrees, Decimal Minutes) format, minutes rounded to `${in_round_min}` digits. Blank and "-" inputs are returned as-is. Minutes and seconds are zero-padded to 2 digits._
- (-10,123°, 3, 3) => -10°7.380"
`${form_deg2ddm_rounded_w}`
#### (string) `form_deg2dms_int`
_Returns a string representing the decimal degrees in `${in_deg_dec}` in DMS (Degrees, Minutes, Seconds) format, seconds truncated to their integer value_
- (-10,123°, 3, 3) => -10° 7" 22'
`${form_deg2dms_int}`
#### (string) `form_deg2dms_rounded`
_Returns a string representing the decimal degrees in `${in_deg_dec}` in DMS (Degrees, Minutes, Seconds) format, seconds rounded to `${in_round_sec}` digits. Blank inputs are intepreted as zero._
- (-10,123°, 3, 3) => -10° 7" 22.800'
`${form_deg2dms_rounded}`
#### (string) `form_deg2dms_rounded_w`
_Returns a string representing the decimal degrees in `${in_deg_dec}` in DMS (Degrees, Minutes, Seconds) format, seconds rounded to `${in_round_sec}` digits. Blank and "-" inputs are returned as-is. Minutes and seconds are zero-padded to 2 digits._
- (-10,123°, 3, 3) => -10° 7" 22.800'
`${form_deg2dms_rounded_w}`
### Component values
Use these if you need to populate individual cells with the numerical component values
#### DDM
##### (int) `cpt_degrees_int`
_Returns a number representing the integer degree component of the decimal degree value in `${in_deg_dec}`_
`${cpt_degrees_int}`
##### (decimal) `cpt_minutes_dec`
_Returns a number representing the decimal minute component of the decimal degree value in `${in_deg_dec}`_
`${cpt_minutes_dec}`
#### DMS
##### (int) `cpt_degrees_int`
_Returns a number representing the integer degree component of the decimal degree value in `${in_deg_dec}`_
`${cpt_degrees_int}`
##### (int) `cpt_minutes_int`
_Returns a number representing the integer minute component of the decimal degree value in `${in_deg_dec}`_
`${cpt_minutes_int}`
##### (decimal) `cpt_seconds_dec`
_Returns a number representing the decimal seconds component of the decimal degree value in `${in_deg_dec}`_
`${cpt_seconds_dec}`
### Unrounded / raw values
Included for completeness, not necessarily useful.
#### (string) `form_deg2ddm_unrounded`
_Decimal degrees to DDM (Degrees, Decimal Minutes) with unrounded minutes_
`${form_deg2ddm_unrounded}`
#### (string) `form_deg2ddm_int`
_Decimal degrees to DDM (Degrees, Decimal Minutes) with integer minutes (NOTE: loss of precision)_
`${form_deg2ddm_int}`
#### (string) `form_deg2dms_unrounded`
_Decimal degrees to DMS (Degrees, Minutes, Seconds) with unrounded decimal seconds_
`${form_deg2dms_unrounded}`
#### (string) `form_deg2dms_int`
_Decimal degrees to DMS (Degrees, Minutes, Seconds) with integer seconds (NOTE: loss of precision)_
`${form_deg2dms_int}`
## Formulas (fractions to degrees)
### Inputs
- Degrees: `${in_cpt_deg}`
- Minutes: `${in_cpt_min}`
- Seconds: `${in_cpt_sec}`
### (number) `form_dms2deg`
_Returns the decimal degrees value corresponding to the input DMS components `${in_cpt_deg}`, `${in_cpt_min}`, `${in_cpt_sec}`_
`${form_dms2deg}`
'''
# CSV document
TEXT_DEMO_CSV = '''\
Decimal_degr,Min_rounding,Sec_rounding,CMP_DDM_Round,CMP_DMS_Int,CMP,${csv_head}
-10.123,4,4,"-10d 7.3800m","-10d 7m 22s","-10d 7m 22.8000s",${csv_line}
'''
# CSV document with debug info
TEXT_DEBUG_CSV = '''\
Decimal_degr,Min_rounding,Sec_rounding,${csv_debug_head}
-10.123,3,3,${csv_debug_line}
'''
USAGE = '''\
Usage:
./compile.py <-t|--text> <text name> [options...]
./compile.py <-f|--formula> <formula name> [options...]
./compile.py <-h|--help>
Modes:
-t|--text <text name>: Prints out the named text
-f|--formula <formula name>: Prints out the named output formula
Options:
-c|--charset <ascii|unicode>
Choose ASCII or Unicode values for CHAR() calls (default: ascii)
-i|--input input_name=input_value
Override values for input parameters (spreadsheet cell names or
static values)
Valid arguments for -t|--text:
README.md, formulas.md, demo.csv, debug.csv
Valid arguments for -f|--formula: ${formula_names}
Valid arguments and default values for -i|--input: ${input_names}
Example:
Compile the "form_deg2dms_rounded_w" formula with input
from mytable::E4 and rounding set to 0 digits:
./compile.py -f form_deg2dms_rounded_w -i in_deg_dec=mytable::E4 \\
-i in_round_min=0 -i in_round_sec=0
'''
# Renderer
def render(text, templates):
new_text = Template(text).substitute(**templates)
if text == new_text:
return text
return render(new_text, templates)
# Abort w/ error
def die(error):
print(error + " (try -h|--help)")
sys.exit(1)
# Abort w/ help message
def get_usage():
tpl = {
"formula_names": "",
"input_names": ""
}
for key in sorted(TEMPLATE_OUTPUTS.keys()):
tpl["formula_names"] += "\n {}".format(key)
for key in sorted(TEMPLATE_INPUTS.keys()):
tpl["input_names"] += "\n {}={}".format(key, TEMPLATE_INPUTS[key])
return render(USAGE, tpl)
# Main
def main():
templates = TEMPLATE_COMPUTE
templates.update(TEMPLATE_CHARS_ASCII)
templates.update(TEMPLATE_INPUTS)
templates.update(TEMPLATE_OUTPUTS)
mode = ""
next_arg = ""
for arg in sys.argv[1:]:
if arg in ["-h", "--help"]:
print(get_usage())
sys.exit(0)
if next_arg in ["-t", "--text", "-f", "--formula"]:
mode = next_arg
mode_arg = arg
next_arg = ""
continue
if next_arg in ["-c", "--charset"]:
if arg == "unicode":
templates.update(TEMPLATE_CHARS_UTF)
elif arg == "ascii":
templates.update(TEMPLATE_CHARS_ASCII)
else:
die("Unknown charset {}".format(arg))
next_arg = ""
continue
if next_arg in ['-i', '--input']:
try:
input_name, input_value = arg.split("=", 1)
except ValueError:
die("Missing/malformed argument to --input")
templates[input_name] = input_value
next_arg = ""
continue
if arg in ["-t", "--text", "-f", "--formula", "-c", "--charset", '-i', '--input']:
next_arg = arg
continue
else:
die("Unknown parameter {}".format(arg))
if next_arg != "":
die("Missing argument to {}".format(next_arg))
# Mode
if mode in ["-t", "--text"]:
if mode_arg == 'README.md':
templates.update({
"compile_py_usage": get_usage()
})
print(render(TEXT_README_MD, templates))
print(render(TEXT_FORMULAS_MD, templates))
elif mode_arg == 'formulas.md':
print(render(TEXT_FORMULAS_MD, templates))
elif mode_arg == 'demo.csv':
print(render(TEXT_DEMO_CSV, templates))
elif mode_arg == 'debug.csv':
print(render(TEXT_DEBUG_CSV, templates))
else:
die("Unknown text name: {}".format(mode_arg))
return
if mode in ["-f", "--formula"]:
print(render("${" + mode_arg + "}", templates))
return
die("Unknown/missing mode argument")
main()