radiosonde.html


001| '''Radiosonde data (from Chanhassen station).
001| 
001| Data Request Headers (of import):
001|     | Content-Type: application/geo+json
001|     | Content-Encoding: br
001| 
001| Brotli Compression:
001|     $ pip install brotli
001| 
001| Brotli Docs:
001|     http://python-hyper.org/projects/brotlipy/en/latest/index.html
001| '''

002| ####################################################################################################
003| from sys import stdout
004| from os import path
005| from datetime import datetime
006| from json import loads
007| from brotli import decompress
008| from htget import htget
009| from files import datafile, jsonfile
010| ####################################################################################################
011| BasePath = r'C:\CJS\prj\Python\ws\@sandbox'
012| DataPath = path.join(BasePath, 'dat')
013| DataFilename = path.join(DataPath, 'radiosonde.data')
014| JsonFilename = path.join(DataPath, 'radiosonde.json')
015| 
016| GetLatest = False
017| 
018| URL = 'https://ims.windy.com/radiosonde/data/station/72/649/2023/10/fm35/72649_20231007_120000.geojson.br'
019| 
020| 
021| ##================================================================================================##
022| def main (*args):
023|     # Get either new data or data from file...
024|     if GetLatest:
025|         # Get latest data (and save it to a file)...
026|         rqst = htget(URL)
027|         rqst.save(DataFilename)
028|         # Decompress the request data...
029|         data = decompress(rqst.data)
030|     else:
031|         # Load data from existing file...
032|         fo = datafile(DataFilename)
033|         fo.load()
034|         # Decompress the file data...
035|         data = decompress(fo.data)
036| 
037|     # Convert decompressed bytes to string...
038|     text = str(data, encoding='utf8')
039| 
040|     # Convert the JSON to a dict...
041|     info = loads(text)
042| 
043|     # Save the JSON file (maybe)...
044|     if GetLatest:
045|         fo = jsonfile(JsonFilename)
046|         fo.data = info
047|         fo.save()
048| 
049|     props = info['properties']
050|     feats = info['features']
051| 
052|     hdr = 'Date/time | Temp Dewp Press W/u W/v Alt Flags'
053|     div = '======================================================================'
054|     fmt = '%s| %5.1f %5.1f %5.1f %+5.1f,%+5.1f %7.1f %05x'
055| 
056|     print('Type: %s' % info['type'])
057|     print('Properties:')
058|     for px,pn in enumerate(props):
059|         print('[%2d] %s: %s' % (px+1, pn, props[pn]))
060|     print()
061|     print('Features: %d' % len(feats))
062|     print(hdr)
063|     print(div)
064|     for feat in feats:
065|         geo = feat['geometry']
066|         pps = feat['properties']
067|         if geo['type'] == 'Point':
068|             coords = geo['coordinates']
069|             dtime = datetime.utcfromtimestamp(pps['time'])
070|             temp  = float(pps['temp'])     if 'temp'     in pps else 0.0
071|             dewpt = float(pps['dewpoint']) if 'dewpoint' in pps else 0.0
072|             press = float(pps['pressure']) if 'pressure' in pps else 0.0
073|             windu = float(pps['wind_u'])   if 'wind_u'   in pps else 0.0
074|             windv = float(pps['wind_v'])   if 'wind_v'   in pps else 0.0
075|             gpalt = float(pps['gpheight']) if 'gpheight' in pps else 0.0
076|             flags = int(pps['flags'])      if 'flags'    in pps else 0
077|             print(fmt % (dtime,temp,dewpt,press,windu,windv,gpalt,flags))
078|     print(div)
079|     print()
080| 
081|     return (text, info)
082| 
083| ####################################################################################################
084| if __name__ == '__main__':
085|     print(argv[0])
086|     print()
087|     rv = main(*argv[1:])
088|     print()
089| ####################################################################################################
090| '''eof'''
091|