-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyradInteractive.py
More file actions
762 lines (661 loc) · 30.3 KB
/
pyradInteractive.py
File metadata and controls
762 lines (661 loc) · 30.3 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
###code.interact(local=dict(globals(), **locals()))
import code
import pyradClasses as pyrad
import pyradUtilities as util
import re
import pyradPlanck
existingAtmosphere = False
validValueAndUnits = re.compile('([-])?(\d+)?([.])?(\d+)?(\S+)?')
genericAtmosphere = pyrad.Atmosphere('holding atm for pyrad interactive')
class Menu:
def __init__(self, title, entries, previousMenu=None, menuParams=None, hint=None):
self.title = title
self.entries = entries
self.previousMenu = previousMenu
self.menuParams = menuParams
self.hint = hint
def displayMenu(self):
titleStr = '\t' + self.title
while len(titleStr) < 60:
titleStr += ' '
print('\n%s' % util.underlineCyan(titleStr))
i = 1
validEntry = ['x']
for entry in self.entries:
if entry.selectionKey:
validEntry.append(entry.selectionKey.lower())
print(' %s) %s' % (util.magentaText(entry.selectionKey.upper()), entry.name))
else:
validEntry.append(str(i))
print(' %s) %s' % (util.magentaText(i), entry.name))
i += 1
if 'Main' not in self.title:
print(' %s Previous menu' % util.magentaText('B)'))
validEntry.append('b')
print(' %s Exit' % util.magentaText('X)'))
validChoice = False
if self.hint:
print(util.limeText('**' + self.hint))
while not validChoice:
userInput = input('Choose an option: ')
if userInput.lower() == 'x':
print('Goodbye')
exit(1)
elif userInput.lower() == 'b' and 'Main' not in self.title:
return
elif userInput in validEntry:
try:
userChoice = self.entries[int(userInput) - 1]
except ValueError:
# this means the user entered a letter but it is in the valid choices
# roll through the entries to find the matching choice
userChoice = next(filter(lambda x: x.selectionKey and x.selectionKey.lower() == userInput.lower(), self.entries))
if userChoice.nextFunction:
userChoice.nextFunction(userChoice.functionParams)
validChoice = True
elif userChoice.nextMenu:
userChoice.nextMenu.displayMenu()
validChoice = True
else:
print('Invalid entry. Try again.')
class Entry:
def __init__(self, text, nextMenu=None, nextFunction=None, functionParams=None, previousMenu=None, selectionKey=False):
self.name = text
self.nextMenu = nextMenu
self.nextFunction = nextFunction
self.functionParams = functionParams
self.previousMenu = previousMenu
self.selectionKey = selectionKey
def createPlot(params):
plotList = params['plots']
plotType = params['plotType']
plotTitle = params['title']
pyrad.plot(plotType, plotTitle, plotList)
return
def createLayer(atmosphere):
defaultLayerName = atmosphere.nextLayerName()
getLayerName = '\n%s\n' \
'If no value given, default will be %s : ' \
% (util.underlineCyan('Enter the name of the layer.\t\t'),
util.limeText(defaultLayerName))
depth = inputLayerDepth()
pressure = inputLayerPressure()
temperature = inputLayerTemperature()
rangeMin, rangeMax = inputLayerRange()
validName = False
while not validName:
layerName = input(getLayerName)
if layerName not in genericAtmosphere:
validName = True
else:
print('Name already taken. Please try again.')
layer = atmosphere.addLayer(depth, temperature, pressure, rangeMin, rangeMax, name=layerName)
createMolecule(layer)
return
def createMolecule(layer):
addMoleculeLoop = True
while addMoleculeLoop:
moleculeName = inputMoleculeName()
if moleculeName in XSC_LIST:
params = {'layer': layer,
'xsc': moleculeName}
selectXscFile(params)
else:
concentration, units = inputMoleculeComposition()
tempdict = {units: concentration}
molecule = layer.addMolecule(moleculeName, **tempdict)
while pyrad.totalConcentration(layer) > 1:
print("%s total concentration exceeds 100%%" % util.magentaText('***\tWARNING\t***'))
menuEditComposition(layer)
validInput = False
while not validInput:
ask = input("Add another molecule to the layer %s " % util.magentaText('(y/n) :'))
if ask.strip().lower() == 'y':
validInput = True
elif ask.strip().lower() == 'n':
return
def menuEditLayerParam(layer):
editDepth = Entry('Depth', nextFunction=editLayerDepth, functionParams=layer)
editRange = Entry('Min or max range', nextFunction=editLayerRange, functionParams=layer)
editTemperature = Entry('Temperature',nextFunction=editLayerTemperature, functionParams=layer)
editPressure = Entry('Pressure', nextFunction=editLayerPressure, functionParams=layer)
entryList = [editDepth, editRange, editTemperature, editPressure]
menu = Menu('Choose the parameter to edit for %s' % layer.name, entryList,
previousMenu=menuEditParamsOrComp, menuParams=layer)
menu.displayMenu()
return
def editLayerDepth(layer):
print('Current %s for %s is : %s\n'
% (util.limeText('depth'), util.limeText(layer.name), util.cyanText('%scm' % layer.depth)))
depth = inputLayerDepth(default=layer.depth)
layer.changeDepth(depth)
menuEditLayerParam(layer)
def editLayerTemperature(layer):
print('Current %s for %s is : %s\n'
% (util.limeText('temperature'), util.limeText(layer.name), util.cyanText('%sK' % layer.T)))
temperature = inputLayerTemperature(default=layer.T)
layer.changeTemperature(temperature)
menuEditLayerParam(layer)
def editLayerPressure(layer):
print('Current %s for %s is : %s\n'
% (util.limeText('pressure'), util.limeText(layer.name), util.cyanText('%smbar' % layer.P)))
pressure = inputLayerPressure(default=layer.P)
layer.changePressure(pressure)
menuEditLayerParam(layer)
def editLayerRange(layer):
print('Current %s for %s is %s\n'
% (util.limeText('range'), util.limeText(layer.name),
util.cyanText('%s-%scm-1' % (layer.rangeMin, layer.rangeMax))))
rangeMin, rangeMax = inputLayerRange(defaultMin=layer.rangeMin, defaultMax=layer.rangeMax)
layer.changeRange(rangeMin, rangeMax)
menuEditLayerParam(layer)
def editComposition(molecule):
print('Current concentration for %s is %s\n' % (util.limeText(molecule.name), util.limeText(molecule.concText)))
return inputMoleculeComposition(molecule, default=molecule.concText)
def addXscToLayer(params):
layer = params['layer']
xsc = params['xsc']
file = params['file']
concentration, units = inputMoleculeComposition()
tempdict = {units: concentration}
layer.addMolecule({xsc: file}, **tempdict)
return
def inputLayerDepth(default=None):
if not default:
default = 10
text = 'Enter the thickness of the layer.\t\t\t'
getDepth = '%s\n' \
'If no units are specified, %s will be assumed.\n' \
'Other valid units are %s . If no value given, default will be %s: ' % \
(util.underlineCyan(text),
util.limeText('cm'),
util.limeText('m, in, ft.'),
util.limeText('%scm' % default))
depth = receiveInput(getDepth, validDepth, default=default)
return depth
def inputLayerTemperature(default=None):
if not default:
default = 300
text = 'Enter the temperature of the layer.\t\t\t'
getTemperature = '%s\n' \
'If no units are specified, %s will be assumed.\n' \
'Other valid units are %s . If no value given, default will be %s: ' % \
(util.underlineCyan(text),
util.limeText('K'),
util.limeText('C or F'),
util.limeText('%sK' % default))
temperature = receiveInput(getTemperature, validTemperature, default=default)
return temperature
def inputLayerPressure(default=None):
if not default:
default = 1013.25
text = 'Enter the pressure of the layer.\t\t\t'
getPressure = '%s\n' \
'If no units are specified, %s will be assumed.\n' \
'Other valid units are %s . If no value given, default will be %s: ' % \
(util.underlineCyan(text),
util.limeText('mBar'),
util.limeText('pa, bar, and atm.'),
util.limeText('%smbar' % default))
pressure = receiveInput(getPressure, validPressure, default=default)
return pressure
def inputLayerRange(defaultMin=None, defaultMax=None):
if not defaultMin:
defaultMin = 600
if not defaultMax:
defaultMax = 700
rangeMin = -1
rangeMax = -1
text = 'Enter the minimum range of the layer.\t\t\t'
getRangeMin = '%s\n' \
'If no units are specified, %s will be assumed.\n' \
'Other valid units are %s . If no value given, default will be %s: ' % \
(util.underlineCyan(text),
util.limeText('cm-1'),
util.limeText('um'),
util.limeText('%scm' % defaultMin))
while rangeMin < 0:
rangeMin = receiveInput(getRangeMin, validRange, default=defaultMin)
if rangeMin < 0:
print('Range min must be %s than zero' % util.magentaText('greater'))
text = 'Enter the maximum range of the layer.\t\t\t'
getRangeMax = '%s\n' \
'If no units are specified, %s will be assumed.\n' \
'Other valid units are %s . If no value given, default will be %s: ' % \
(util.underlineCyan(text),
util.limeText('cm-1'),
util.limeText('um'),
util.limeText('%scm' % defaultMax))
while rangeMax <= rangeMin:
rangeMax = receiveInput(getRangeMax, validRange, default=defaultMax)
if rangeMax <= rangeMin:
print('Range min must be %s than range min of %s' % (util.magentaText('greater'), util.cyanText(rangeMin)))
return rangeMin, rangeMax
def validNumber(userInput):
try:
return float(userInput)
except ValueError:
return False
def inputPlanckRange(units):
rangeMin = -1
rangeMax = -1
text = '%s\nUnits are %s. Scientific notation is accepted (1e14):' \
% (util.underlineCyan('Enter the minimum range of the planck spectrum.'),util.limeText(units))
while rangeMin < 0:
rangeMin = receiveInput(text, validNumber)
if rangeMin < 0:
print('Range min must be %s than zero' % util.magentaText('greater'))
text = '%s\nUnits are %s. Scientific notation is accepted (1e14):' \
% (util.underlineCyan('Enter the maximum range of the planck spectrum.'), util.limeText(units))
while rangeMax <= rangeMin:
rangeMax = receiveInput(text, validNumber)
if rangeMax <= rangeMin:
print('Range min must be %s than range min of %s' % (util.magentaText('greater'), util.cyanText(rangeMin)))
return rangeMin, rangeMax
def inputMoleculeName(default=None):
if not default:
default = 'co2'
text = 'Enter the short molecule name.\t\t\t'
moleculeName = receiveInput('%s\n'
'For a full list of options, type %s. For a list of cross-section only molecules, type %s. If no value given, %s will be used: '
% (util.underlineCyan(text),
util.magentaText('help'),
util.magentaText('xsc'),
util.limeText(default)), validMoleculeName, default=default)
return moleculeName
def inputMoleculeComposition(obj=None, default=None):
if not default:
default = '400ppm'
text = 'Enter the molecule composition.\t\t\t'
composition, units = receiveInput('%s\n'
'If no units entered, composition will be assumed %s.\n'
'Other valid units are %s . If no value given, %s will be used: '
% (util.underlineCyan(text),
util.limeText('parts per 1'),
util.limeText('ppm, ppb, or percentage'),
util.limeText(default)), validComposition, default=default)
if obj:
if units == 'ppm':
obj.setPPM(composition)
elif units == 'ppb':
obj.setPPB(composition)
elif 'perc' in units or units == '%':
obj.setPercentage(composition)
else:
obj.setConcentrationPercentage(composition)
return
else:
return composition, units
def inputPlanckTemps():
text = 'Enter the temperature of the planck curves.\t\t\t'
tempList = receiveMultiInput('%s\n'
'If no units entered, temperature will be assumed %s.\n'
'Other valid units are %s .Multiple temperatures can be separated with a comma: '
% (util.underlineCyan(text),
util.limeText('K'),
util.limeText('C and F')), validTemperature)
return tempList
def menuChooseLayerToEdit(empty=None):
entryList = []
for layer in genericAtmosphere:
nextEntry = Entry(layer.name, nextFunction=menuEditParamsOrComp, functionParams=layer)
entryList.append(nextEntry)
editLayerMenu = Menu('Edit layer', entryList)
editLayerMenu.displayMenu()
return
def menuChooseTransmission(plotType):
entryList = []
for layer in genericAtmosphere:
params = {'plots': [layer], 'plotType': plotType, 'title': layer.title}
nextEntry = Entry(layer.name, nextFunction=createTransmission, functionParams=params)
entryList.append(nextEntry)
plotList, title = createObjAndComponents(layer)
params = {'plots': plotList, 'plotType': plotType, 'title': title}
nextEntry = Entry('%s and components' % layer.name, nextFunction=createTransmission, functionParams=params)
entryList.append(nextEntry)
transmissionMenu = Menu('Choose which layers to plot transmission', entryList)
transmissionMenu.displayMenu()
return
def createPlanckCurves(plotType):
plotList = inputPlanckTemps()
rangeMin, rangeMax = inputPlanckRange(plotType)
pyrad.plotSpectrum(title='Planck spectrums', rangeMin=rangeMin, rangeMax=rangeMax, planckTemperatureList=plotList, planckType=plotType)
return
def menuPlanckType(empty=None):
entryList = []
wavenumber = 'wavenumber'
hertz = 'Hz'
wavelength = 'wavelength'
entryList.append(Entry('By %s (cm-1)' % wavenumber, nextFunction=createPlanckCurves, functionParams=wavenumber))
entryList.append(Entry('By %s (um)' % wavelength, nextFunction=createPlanckCurves, functionParams=wavelength))
entryList.append(Entry('By %s (s-1)' % hertz, nextFunction=createPlanckCurves, functionParams=hertz))
planckTypeMenu = Menu('Choose planck type', entryList)
planckTypeMenu.displayMenu()
return
def createTransmission(params):
layer = params['plots'][0]
text = 'A plot for transmission requires an initial surface temperature.\n'\
'Please choose a temperature different from the layer temperature of %sK:' % util.limeText(layer.T)
temperature = receiveMultiInput(text, validTemperature)
objList = []
for item in params['plots']:
objList.append(item)
temperature.append(layer.T)
pyrad.plotSpectrum(layer, objList=objList,
surfaceSpectrum=pyradPlanck.planckWavenumber(layer.xAxis, temperature[0]),
planckTemperatureList=temperature)
return
def menuChoosePlotType(empty=None):
entryList = []
entryList.append(Entry('transmittance', nextFunction=menuChooseLayerToPlot, functionParams='transmittance'))
entryList.append(Entry('absorption coefficient', nextFunction=menuChooseLayerToPlot, functionParams='absorption coefficient'))
entryList.append(Entry('cross section', nextFunction=menuChooseLayerToPlot, functionParams='cross section'))
entryList.append(Entry('absorbance', nextFunction=menuChooseLayerToPlot, functionParams='absorbance'))
entryList.append(Entry('optical depth', nextFunction=menuChooseLayerToPlot, functionParams='optical depth'))
entryList.append(Entry('line survey', nextFunction=menuChooseLayerToPlot, functionParams='line survey'))
entryList.append(Entry('transmission', nextFunction=menuChooseTransmission, functionParams='transmission'))
choosePlotTypeMenu = Menu('Choose plot type', entryList)
choosePlotTypeMenu.displayMenu()
return
def menuChooseLayerToPlot(plotType):
entryList = []
for layer in genericAtmosphere:
params = {'plots': [layer], 'plotType': plotType, 'title': layer.title}
nextEntry = Entry(layer.name, nextFunction=createPlot, functionParams=params)
entryList.append(nextEntry)
plotList, title = createObjAndComponents(layer)
params = {'plots': plotList, 'plotType': plotType, 'title': title}
nextEntry = Entry('%s and components' % layer.name, nextFunction=createPlot, functionParams=params)
entryList.append(nextEntry)
plotLayerMenu = Menu('Plot layer', entryList)
plotLayerMenu.displayMenu()
return
def createObjAndComponents(obj):
plotList = [obj]
for item in obj:
plotList.append(item)
return plotList, obj.title
def menuEditComposition(layer):
moleculeList = layer.returnMoleculeObjects()
entryList = []
for molecule in moleculeList:
newEntry = Entry('%s : %s' % (molecule.name, molecule.concText),
functionParams=molecule, nextFunction=inputMoleculeComposition)
entryList.append(newEntry)
entryList.append(Entry('Add a new molecule(s)', nextFunction=createMolecule, functionParams=layer))
editCompMenu = Menu('Choose a molecule to edit', entryList, previousMenu=menuEditParamsOrComp)
editCompMenu.displayMenu()
return
def menuEditParamsOrComp(layer):
entryList = []
editLayerParamsEntry = Entry('Edit layer parameters', nextFunction=menuEditLayerParam, functionParams=layer)
entryList.append(editLayerParamsEntry)
duplicateLayerEntry = Entry('Duplicate layer', nextFunction=duplicateObj, functionParams=layer)
entryList.append(duplicateLayerEntry)
editCompositionEntry = Entry('Edit composition', nextFunction=menuEditComposition, functionParams=layer)
entryList.append(editCompositionEntry)
chooseParamsMenu = Menu('Edit or duplicate', entryList, previousMenu=menuChooseLayerToEdit)
chooseParamsMenu.displayMenu()
return
def menuMain():
entries = []
entries.append(Entry("Create new gas cell", nextFunction=createLayer, functionParams=genericAtmosphere))
entries.append(Entry("Edit/duplicate gas cell", nextFunction=menuChooseLayerToEdit))
entries.append(Entry("Plot gas cell", nextFunction=menuChoosePlotType))
entries.append(Entry('Plot planck curves', nextFunction=menuPlanckType))
mainMenu = Menu('Main menu', entries)
mainMenu.displayMenu()
return
def menuAddXscToLayer(atm):
entries = []
for layer in atm:
entries.append(Entry(layer.name, nextFunction=menuSelectXscMolecule, functionParams=layer))
menu = Menu('Choose layer to add cross-section to', entries)
menu.displayMenu()
return
def menuSelectXscMolecule(layer):
entries = []
for xsc in XSC_LIST:
entries.append(Entry(xsc, nextFunction=selectXscFile, functionParams={'layer': layer, 'xsc': xsc}))
entries.append(Entry('Manually enter molecule name based on HITRAN list', nextFunction=inputXscName, functionParams=layer))
menu = Menu('Choose xsc from list', entries, hint='Full list is available at https://hitran.org/data/suppl/xsec/cross_section_data/')
menu.displayMenu()
return
def selectXscFile(params):
def relevanceScore(layerT, layerP, fileT, fileP, weightedT=1, weightedP=1.1):
tDiff = abs(layerT - fileT) * weightedT
pDiff = abs(layerP - fileP) * weightedP
total = tDiff + pDiff
return total
layer = params['layer']
xsc = params['xsc']
if 'sort' not in params:
params['sort'] = 'RELEVANT_P'
sort = params['sort']
entries = []
unsortedFiles = util.returnXscFilesInDirectory(xsc)
if unsortedFiles is False or unsortedFiles == []:
question = "Either that directory doesn't exist or it was empty. Would you like to try downloading the data from HITRAN?"
if receiveInput(question, validYorN) == 'y':
filepath = util.downloadXscZipFile(xsc)
util.unzipFile(filepath)
util.mergeXsc(xsc)
selectXscFile(params)
else:
return
else:
hintText = "Layer P and T will be adjusted according to the xsc file"
unsortedValues = list(map(lambda file: util.parseXscFileName(file), unsortedFiles))
if sort == 'TEMP':
sortedValues = sorted(unsortedValues, key = lambda i: (float(i['TEMP']), float(i['PRESSURE'])))
sortedValues.reverse()
hintText += '\nCurrently sorted by temperature with largest values at bottom'
elif sort == 'PRESSURE':
sortedValues = sorted(unsortedValues, key = lambda i: (float(i['PRESSURE']), float(i['TEMP'])))
sortedValues.reverse()
hintText += '\nCurrently sorted by pressure with largest values at bottom'
elif sort == 'RELEVANT_P':
sortedValues = sorted(unsortedValues, key = lambda i: (relevanceScore(layer.T, layer.P, float(i['TEMP']), float(i['PRESSURE']) * 1.31579)))
sortedValues.reverse()
hintText += '\nCurrently sorted closest minimizing pressure difference, with closest match at bottom'
elif sort == 'RELEVANT_T':
sortedValues = sorted(unsortedValues, key = lambda i: (relevanceScore(layer.T, layer.P, float(i['TEMP']), float(i['PRESSURE']) * 1.31579, weightedP=1, weightedT=1.1)))
hintText += '\nCurrently sorted closest minimizing temperature difference, with closest match at bottom'
sortedValues.reverse()
for v in sortedValues:
if sort == 'TEMP' or sort == 'RELEVANT_T':
displayName = 'Temp: %s -- Pressure: %s -- Range: %s' % (util.limeText(v['TEMP'] + 'K'), util.cyanText(v['PRESSURE'] + 'Torr'), util.magentaText(v['RANGE'] + 'cm-1'))
elif sort == 'PRESSURE' or sort == 'RELEVANT_P':
displayName = 'Pressure: %s -- Temp: %s -- Range: %s' % (util.cyanText(v['PRESSURE'] + 'Torr'), util.limeText(v['TEMP'] + 'K'), util.magentaText(v['RANGE'] + 'cm-1'))
entries.append(Entry(displayName, nextFunction=addXscToLayer, functionParams={'layer': layer, 'file': v['LONG_FILENAME'], 'xsc': xsc}))
pressureParams = params.copy()
pressureParams.update({'sort': 'PRESSURE'})
entries.append(Entry('Sort by pressure', nextFunction=selectXscFile, functionParams=pressureParams, selectionKey='P'))
tempParams = params.copy()
tempParams.update({'sort': 'TEMP'})
entries.append(Entry('Sort by temperature', nextFunction=selectXscFile, functionParams=tempParams, selectionKey='T'))
tempParams = params.copy()
tempParams.update({'sort': 'RELEVANT_T'})
entries.append(Entry('Sort by closest temperature', nextFunction=selectXscFile, functionParams=tempParams, selectionKey='minT'))
tempParams = params.copy()
tempParams.update({'sort': 'RELEVANT_P'})
entries.append(Entry('Sort by closest pressure', nextFunction=selectXscFile, functionParams=tempParams, selectionKey='minP'))
menu = Menu('Choose file to use', entries, hint=hintText)
menu.displayMenu()
return
def duplicateObj(obj):
newObj = obj.returnCopy()
if isinstance(newObj, pyrad.Layer):
genericAtmosphere.append(newObj)
else:
print('Unknown object %s, type %s' % (obj.name, type(obj)))
return
def receiveInput(inputText, validInputFunction, default=None):
validInput = False
while validInput is False:
userInput = input('\n%s' % inputText)
if userInput == '':
return validInputFunction(str(default))
validInput = validInputFunction(userInput)
return validInput
def receiveMultiInput(inputText, validInputFunction, default=None):
validInput = False
while not validInput:
userInput = input(inputText)
inputList = userInput.replace(' ', '').split(',')
testInput = True
for item in inputList:
if not validInputFunction(item):
print('%s not recognized as valid. Please try again.')
testInput = False
if testInput:
return inputList
def validYorN(userInput):
if not userInput:
return False
if userInput.strip().lower()[0] == 'y' or userInput.strip().lower()[0] == 'n':
return userInput
else:
print('Invalid option. Please type "%s" or "%s"' % (util.underlineMagenta('y'), util.underlineMagenta('n')))
return False
def validMoleculeName(userInput):
if not userInput:
return False
if userInput.strip().lower() == 'help':
util.displayAllMolecules()
return False
elif userInput.strip().lower() == 'xsc':
print(util.underlineMagenta('Punctuation matters...'))
print(', '.join(XSC_LIST))
return False
elif userInput in pyrad.MOLECULE_ID or userInput in XSC_LIST:
return userInput
else:
print('Invalid molecule name. %s' % (util.underlineMagenta('Please try again.')))
return False
def validPressure(userInput):
if not userInput:
return False
try:
splitInput = validValueAndUnits.match(userInput)
except AttributeError:
print('Invalid input for pressure. Example: %s. %s'
% (util.limeText('1.35atm'), util.underlineMagenta('Please try again.')))
return False
unit = splitInput.group(5)
if not unit:
unit = 'mbar'
unit = unit.lower()
if unit not in PRESSURE_UNITS:
print('Invalid units. Accepted units are %s.' % ', '.join(PRESSURE_UNITS))
return False
textNumber = ''
for i in range(1, 5):
if splitInput.group(i):
textNumber += splitInput.group(i)
value = float(textNumber)
return pyrad.convertPressure(value, unit)
def validComposition(userInput):
if not userInput:
return False
try:
splitInput = validValueAndUnits.match(userInput)
except AttributeError:
print('Invalid input for concentration. Example: %s. %s'
% (util.limeText('15ppb'), util.underlineMagenta('Please try again.')))
return False
unit = splitInput.group(5)
if not unit:
unit = 'concentration'
if unit not in COMPOSITION_UNITS:
print('Invalid units. Accepted units are %s.'
% (util.limeText(', '.join(COMPOSITION_UNITS))))
return False
textNumber = ''
for i in range(1, 5):
if splitInput.group(i):
textNumber += splitInput.group(i)
value = float(textNumber)
if value <= 0:
print('Concentration must be greater than 0')
return False
return value, unit
def validTemperature(userInput):
if not userInput:
return False
try:
splitInput = validValueAndUnits.match(userInput)
except AttributeError:
print('Invalid input for temperature. Example: %s. %s'
% (util.limeText('20C'), util.underlineMagenta('Please try again.')))
return False
unit = splitInput.group(5)
if not unit:
unit = 'K'
unit = unit.upper()[0]
if unit not in TEMPERATURE_UNITS:
print('Invalid units. Accepted units are %s.'
% (util.limeText(', '.join(TEMPERATURE_UNITS))))
return False
textNumber = ''
for i in range(1, 5):
if splitInput.group(i):
textNumber += splitInput.group(i)
value = float(textNumber)
return pyrad.convertTemperature(value, unit)
def validRange(userInput):
if not userInput:
return False
try:
splitInput = validValueAndUnits.match(userInput)
except AttributeError:
print('Invalid input for range. Example: %s. %s'
% (util.limeText('150cm-1'), util.underlineMagenta('Please try again.')))
return False
unit = splitInput.group(5)
if not unit:
unit = 'cm-1'
unit = unit.lower()
if unit not in RANGE_UNITS:
print('Invalid units. Accepted units are %s'
% (util.limeText(', '.join(RANGE_UNITS))))
return False
textNumber = ''
for i in range(1, 5):
if splitInput.group(i):
textNumber += splitInput.group(i)
value = float(textNumber)
return pyrad.convertRange(value, unit)
def validDepth(userInput):
if not userInput:
return False
try:
splitInput = validValueAndUnits.match(userInput)
except AttributeError:
print('Invalid input for depth. Example: %s. %s'
% (util.limeText('10cm'), util.underlineMagenta('Please try again.')))
return False
unit = splitInput.group(5)
if not unit:
unit = 'cm'
unit = unit.lower()
if unit not in DEPTH_UNITS:
print('Invalid units. Accepted units are %s'
% (util.limeText(', '.join(DEPTH_UNITS))))
return False
textNumber = ''
for i in range(1, 5):
if splitInput.group(i):
textNumber += splitInput.group(i)
value = float(textNumber)
return pyrad.convertLength(value, unit)
DEPTH_UNITS = ['cm', 'in', 'inches', 'ft', 'feet', 'meter', 'm']
PRESSURE_UNITS = ['atm', 'bar', 'mbar', 'pa']
TEMPERATURE_UNITS = ['K', 'C', 'F']
RANGE_UNITS = ['um', 'cm-1']
COMPOSITION_UNITS = ['ppm', 'ppb', '%', 'percentage', 'perc', 'concentration']
XSC_LIST = ['CFC-11', 'CFC-12', 'CFC-13', 'CFC-113', 'CFC-113a', 'CFC-114', 'CFC-114a', 'CFC-115',
'HCFC-21', 'HCFC-22', 'HCFC-123', 'HCFC-123a', 'HCFC-124', 'HCFC-141b', 'HCFC-142b', 'HCFC-225ca', 'HCFC-225cb',
'HFC-32', 'HFC-125', 'HFC-134', 'HFC-134a', 'HFC-143a', 'HFC-152a', 'HFE-356mff2']
while True:
menuMain()