Created
August 24, 2016 17:42
-
-
Save pmarshwx/2aa434b1bf26117349bb7e697730b215 to your computer and use it in GitHub Desktop.
Script to Parse GEMPAK GDLIST Output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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