The data is kind of old fashioned in its formatting. It has to be read in raw, line by line, and information extracted as characters at specific 4-width integers packed in rows of 20. Watch out for lines which seperate the latitude bands, these are diffrent. Go line by line and turn into an array.
In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib inline
In [ ]:
def grid_raw(data_file):
"""
Convert raw ASC format data into a gridded dataset.
Return header info, lat_info, and a grid
e.g.:
>>> output = grid_raw('Data/201605.asc')
output
"""
raw = []
with open(data_file) as fp:
for line in fp:
raw.append(line)
header = raw[0:5]
raw = raw[5:]
bad =[]
globe = []
lat_band = []
lat_atts=[]
for line in raw:
tmp = line.split('\n')[0]
if 'lat' in tmp:
lat_atts.append(tmp)
globe.append(lat_band)
lat_band = []
else:
for pos in range(20):
start = pos * 4
end = pos*4 + 4
try:
single_pixel = int(tmp[start: end])
if single_pixel == -999:
lat_band.append(np.nan)
else:
lat_band.append(single_pixel)
except:
bad.append(tmp[start: end])
assert len(bad) == 0, "bad value list should be empty. Something went wrong..."
globe = np.array(globe)
return (header, lat_atts, globe)
In [ ]:
# Example of plotting the data
output = grid_raw('Data/201605.asc')
In [ ]:
# show header
output[0]
In [ ]:
# show an example piece from lats...
for info in output[1][0:5]:
print(info)
In [ ]:
# Show an example of plotting the array (stored in the output tuple position 2)
plt.imshow(output[2], origin=1, interpolation=None, cmap=cm.jet)
In [ ]: