Skip to content

Instantly share code, notes, and snippets.

@pmarshwx
Created August 24, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarshwx/2aa434b1bf26117349bb7e697730b215 to your computer and use it in GitHub Desktop.
Save pmarshwx/2aa434b1bf26117349bb7e697730b215 to your computer and use it in GitHub Desktop.
Script to Parse GEMPAK GDLIST Output
import numpy as np
def parse(lines, mask=-9999.):
'''
A simple routine to parse the output of GEMPAK's GDLIST output.
Parameters
----------
lines : string, sequence
The output from GDLIST. If a string, will parse on the '\n' character.
If a list, will assume the file has already been split.
mask : scalar
The value of the mask. If no mask, use None or False.
Returns
-------
An 2D numpy array
'''
begin = False
lonsize = None; latsize = None
vals = []
if isinstance(lines, str):
lines = lines.split('\n')
for line in lines:
if not lonsize:
if 'GRID SIZE' not in line: continue
parts = line.split()
parts = [p.strip() for p in parts]
lonsize = int(parts[-2])
latsize = int(parts[-1])
if 'ROW'+str(latsize) in line or (begin and 'ROW' in line):
begin = True
val_tmp = line.split('ROW')[1].split()[1:]
elif begin:
val_tmp = line.split()
else:
continue
for val in val_tmp:
vals.append(float(val))
vals = np.array(vals)
if mask:
vals = np.ma.masked_where(vals == mask, vals)
vals = vals.reshape(latsize, -1)
return vals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment