def ask_for_yn( txt, default, space=False ): ok = False t = f'{txt}\n' t += '\t1) Yes\n' t += '\t2) No' while True: answ = ask_for_txt( t, default=default, space=space ) if not answ or utils.is_empthy(answ): # 'ie. y, n, no, yes 1, 2' answ = str(default) answ = answ.lower() if utils.is_quit(answ): return config.answer_quit[0] # Return first el for quit elif utils.is_yes(answ): return config.answer_yes[0] elif utils.is_no(answ): return config.answer_no[0] else: try: answi = int(answ) except ValueError: # Input was not a number pass else: # Input is a number if answi == 1: return config.answer_yes[0] elif answi == 2: return config.answer_no[0] console.log(f'Unknown option: {answ} ?\nTry again.', True)
def ask_for_a_month( txt, space=False ): l = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', 'january', 'februari', 'march', 'april', 'mai', 'june', 'july', 'august', 'september', 'oktober', 'november', 'december', 'jan', 'feb', 'mar', 'apr', 'mai', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec' ] t = f'{txt}\n' t += vt.list_to_col( l, align='center', col=6, col_width='10' ) t += 'To select a month. Type in a month as above.' while True: answ = ask_for_txt( t, default=False, space=space ) if not answ or utils.is_empthy(answ): console.log('Please type in something ...', True) elif utils.is_quit(answ): res = config.answer_quit[0] # Return first el for quit break else: answ = answ.lower() if answ in l: res = vt.month_to_num( answ ) break else: ask( f'Month {answ} unknown !\nPress a key to try again...', True ) return res
def ask_for_a_day( txt, space=False ): days = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] months = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] l = list() for i in range( len(months) ): m = months[i] mm = f'0{m}' if m < 10 else f'{m}' for d in range(1, days[m-1]+1, 1): dd = f'0{d}' if d < 10 else f'{d}' l.append( f'{mm}{dd}' ) t = f'{txt}\n' t += vt.list_to_col( l, align='left', col=14, col_width=5 ) t += 'To select a day. Type in the day as above.' while True: answ = ask_for_txt( t, default=False, space=space ) if not answ or utils.is_empthy(answ): console.log('Please type in something ...', True) elif utils.is_quit(answ): res = config.answer_quit[0] # Return first el for quit break elif answ in l: res = answ break else: ask( f'Day {answ} unknown !\nPress a key to try again...', True ) return res
def ask_for_one_station( txt, l_map=False, space=False): result = False if l_map == False: l_map = utils.only_existing_stations_in_map() # Update l if len(l_map) != 0: t = f'{txt}\n' t += vt.list_to_col(l, 'left', 3, 25) t += 'To add a station, give a wmo-number or a city name' while True: answ = ask_for_txt( t, default=False, space=space ) if utils.is_empthy(answ): console.log('Please type in something ...', True) elif utils.is_quit(answ): result = config.answer_quit[0] # Return first el for quit break elif stations.name_in_list(answ, l_map) or \ stations.wmo_in_list(answ, l_map): result = stations.find_by_wmo_or_name(answ, l_map) console.log(f'{result.wmo}: {result.place} {result.province} selected', True) break else: ask( f'Station: {answ} unknown !\nPress a key to try again...', True ) else: t = 'No weatherdata found in data map. Download weatherdata first.' console.log(t, True) return result
def ask_for_file_name(txt, default='x-x-x-x', space=False): answ = ask_for_txt( txt, default, space ) if not answ or utils.is_empthy(answ): return str(default) elif utils.is_quit(answ): return config.answer_quit[0] # Return first el for quit return answ
def ask_for_entities( txt, space=False): l, info = [], False ok, t = fio.read(utils.mk_path(config.dir_app, config.knmi_dayvalues_info_txt)) inf = f'INFO:\n{t}' if ok else f'Info file {config.knmi_dayvalues_info_txt} not found...' txt += '\n' + vt.dayvalues_entities( sep=',' ) txt += 'To add one weather entity, type in the enitity name. e.g. TX\n' txt += 'To add one more stations, give entity name separated by a comma. e.g TX, TG, TN\n' while True: t = txt t += inf if config.help_info or info else "Type 'i' for more info..." if len(l) > 0: t += "\nPress 'n' to move to the next !" answ = ask_for_txt( t, default=False, space=space ) if not answ or utils.is_empthy(answ): console.log('Please type in something ...', True) continue # Again else: answ = clear( answ ) if answ == 'i': info = True elif utils.is_quit(answ): return config.answer_quit[0] # Return first el for quit elif answ == 'n' and len(l) > 0: return l elif answ.find(',') != -1: lt = answ.split(',') for e in lt: if e: e = clear(e) # Clean input ok, ent = daydata.is_ent( e ) if ok: l.append(e) else: ok, ent = daydata.is_ent( answ ) if ok: l.append(ent) else: console.log(f"Unknown option {ent} given. Fill in one or more entities separated by an ',' ", True) if len(l) > 0: cnt, kol, t = 1, 10, 'All weather entities who are added are: ' for ent in l: t += ent + ', ' if cnt % kol == 0: t += '\n' t = t[0:-2] # Remove space and comma console.log( t, True ) return l
def ask_for_query( txt, space=False ): info = False while True: t = f'{txt}\n' t += 'For example: TX > 35\n' if config.help_info or info: t += 'Possible properties are\n' t += "' gt', '> ' = greater than ie TG > 20 Warm nights\n" t += "' ge', '>=', ' ≥' = greater than and equal ie TX >= 30 Tropical days\n" t += "' lt', '< ' = less than ie TN < 0 Frosty days\n" t += "' le', '<=', ' ≤' = less than equal ie TX <= 0 Icy days\n" t += "' eq', '==' = equal ie DDVEC == 90 A day with a wind from the east\n" t += "' ne', '!=', '<>' = not equal ie RH != 0 A day with rain\n" t += "' or', '||' 'or ' ie SQ > 10 or TX >= 25 Sunny and warm days\n" t += "'and', '&&' 'and' ie RH > 10 and TX < 0 Most propably a day with snow\n" t += '\nPossible entities are\n' t += "'DDVEC' = Vector mean wind direction (degrees) 'FHVEC' = Vector mean windspeed (m/s)\n" t += "'FG' = Daily mean windspeed (in 0.1 m/s) 'FHX' = Maximum hourly mean windspeed (m/s)\n" t += "'FHN' = Minimum hourly mean windspeed (m/s) 'FXX' = Maximum wind gust (m/s)\n" t += "'TG' = Daily mean temperature in (°C) 'TN' = Minimum temperature (°C)\n" t += "'TX' = Maximum temperature (°C) 'T10N' = Minimum temperature at 10 cm (°C)\n" t += "'SQ' = Sunshine duration (hour) 'SP' = % of maximum sunshine duration\n" t += "'Q' = Global radiation (J/cm2) 'DR' = Precipitation duration (hour)\n" t += "'RH' = Daily precipitation amount (mm) 'RHX' = Maximum hourly precipitation (mm)\n" t += "'PG' = Daily mean sea level pressure (hPa) 'PX' = Maximum hourly sea level pressure (hPa)\n" t += "'PN' = Minimum hourly sea level pressure (hPa) 'EV24' = Potential evapotranspiration (mm)\n" t += "'VVN' = Minimum visibility 0: <100m, 1:100-200m, 2:200-300m,..., 49:4900-5000m, 50:5-6 km, \n" t += ' 56:6-7km, 57:7-8km,..., 79:29-30km, 80:30-35km, 81:35-40km,..., 89: >70km)\n' t += "'VVX' = Maximum visibility 0: <100 m, 1:100-200 m, 2:200-300 m,..., 49:4900-5000 m, 50:5-6 km, \n" t += ' 56:6-7km, 57:7-8km,..., 79:29-30km, 80:30-35km, 81:35-40km,..., 89: >70km)\n' t += "'NG' = Mean daily cloud cover (octants) 'UG' = Daily mean relative atmospheric humidity (%)\n" t += "'UX' = Maximum atmospheric humidity (%) 'UN' = Minimum relative atmospheric humidity (%)" else: t += "Type 'i' for more info..." answ = ask_for_txt( t, default=False, space=space ) # TODO MAKE ADVANCED CHECKS if not answ or utils.is_empthy(answ): console.log('Please type in something ...', True) elif answ == 'i': info = True elif utils.is_quit(answ): return config.answer_quit[0] # Return first el for quit # TODO # elif validate.query(answ): # TODO # return answ else: return utils.make_query_txt_only( answ ) return answ # No checking for now
def ask_for_date( txt, space=False): while True: answ = ask_for_txt( txt, default=False, space=space ) if not answ or utils.is_empthy(answ): console.log('Please type in something ...', True) continue elif utils.is_quit(answ): answ = config.answer_quit[0] # Return first el for quit break elif validate.yyyymmdd(answ): break else: console.log(f'Error in date: {answ}\n', True) return answ
def ask_for_period( txt='', space=False ): info = False while True: t= f'{txt} \n' t += 'Period start - end \n' t += 'Default format yyyymmdd-yyyymmdd \n' t += 'For example: 20200510-20200520 \n' if config.help_info or info: t += 'Formats with a wild card * \n' t += ' --- still in development --- \n' t += ' ******** selects all the available data (=8x*)\n' t += ' **** selects (current) year (=4x*)\n' t += ' ** selects (current) month (=2x*)\n' t += ' yyyy**** selects the whole year yyyy\n' t += ' yyyymm** selects the month mm in the year yyyy\n' t += ' ****mm** selects a month mm for every year \n' t += ' ****mmdd selects a monthday mmdd for every year \n' t += ' yyyy****-yyyy**** selects a full year from yyyy untill yyyy \n' t += ' yyyymmdd-yyyy*mmdd selects a day mmdd in a year from start day to endyear\n' t += ' yyyymmdd-yyyy*mmdd* selects a certain period from mmdd=mmdd* in a year from yyyy to yyyy\n' t += 'Examples wildcard * \n' t += ' 2015**** selects the year 2015 \n' t += ' 201101**-202001** selects all januarys from 2011 unto 2020 \n' t += ' ****07** selects all julys from avalaible data \n' t += ' ****1225 selects all 25 decembers from avalaible data' else: t+= "Type 'i' for more info..." answ = ask_for_txt( t, default=False, space=space ) if not answ or utils.is_empthy(answ): console.log('\nPlease type in something...\n', True) elif utils.is_quit(answ): answ = config.answer_quit[0] # Return first el for quit break elif answ == 'i': info = True else: if daydata.check_periode( answ ): # TODO answ = answ.replace(' ', '') break else: err = f'Period of format {answ} is unknown... Press a key... ' ask(err) return answ
def ask_for_int(txt, default=False, space=False): if space: console.log(' ', space) while True: answ = ask_for_txt(txt, default, space) if utils.is_empthy(answ): answ = default break elif utils.is_quit(answ): answ = config.answer_quit[0] # Return first el for quit break else: answ = re.sub( '\D', '', answ ) # Remove non-digits try: answ = int(answ) except ValueError: console.log('Give an real integer...', True) continue else: break if space: console.log(' ', space) return answ
def ask_type_options(txt, l, default=config.default_output, space=False): t = f'{txt}\n' i, max = 1, len(l) while True: t += f'\t{i}) {l[i-1]}' if i < max: i, t = i + 1, t + '\n' else: break while True: answ = ask_for_txt( t, default=default, space=space ) if not answ or utils.is_empthy(answ): i = 0 while i < max: if l[i] == default: return default elif str(i) == str(default): return l[i] i += 1 if utils.is_quit(answ): return config.answer_quit[0] # Return first el for quit else: try: answi = int(answ) except ValueError: # Input is not a number if answ in l: return answ else: # Input is a number i = 0 while i < max: if i + 1 == answi: return l[i] i += 1 console.log(f'Unknown option: {answ} ?\nTry again.', True)
def ask_for_stations( txt, l_map=False, space=False): result = list() if l_map == False: l_map = utils.only_existing_stations_in_map() # Update l cnt_map = len(l_map) if cnt_map > 0: l = [f'{el.wmo} {el.place}' for el in l_map] t = f'{txt}\n' t += vt.list_to_col( l, 'left', 4, 23 ) t += 'To add one station, give a wmo-number or a city name\n' t += 'To add more stations, give a wmo-number or a city name separated by a comma\n' t += "Press '*' to add all available weather stations" while True: ask_txt = t cnt_result = len(result) if cnt_result > 0: ask_txt += "\nPress 'n' to move to the next !" answ = ask_for_txt( ask_txt + '\n', default=False, space=space ) if not answ or utils.is_empthy(answ): console.log('Please type in something ...', True) elif utils.is_quit(answ): result = config.answer_quit[0] # Return first el for quit break elif answ == '*': result = l_map break elif answ == 'n' and cnt_result > 0: break else: ll = list() # Make a list with stations if answ.find(',') != -1: ll = [clear(e) for e in answ.split(',')] # Clean input else: if stations.name_in_list(answ, l_map): ll.append(answ) elif stations.wmo_in_list(answ, l_map): ll.append(answ) else: console.log(f'Station: {answ} is unknown !') # Add all added stations for s in ll: st = stations.find_by_wmo_or_name(s, l_map) if st != False: all = f'{st.wmo} {st.place} {st.province}' if stations.check_if_station_already_in_list( st, result ) != True: result.append(st) console.log(f'Station: {all} added...') else: console.log(f'Station: {all} already added...') else: console.log(f'Station: {answ} not found...') cnt_result = len(result) # New count if cnt_result == cnt_map: console.log('\nAll available weatherstations added...', True) break elif cnt_result > 0: console.log('\nAll weatherstation(s) who are added are: ', True) for s in result: console.log(f'{s.wmo}: {s.place} {s.province}', True) else: t = 'No weatherdata found in data map. Download weatherdata first.' console.log(t, True) result = False return result