This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nc-to-img.py
93 lines (57 loc) · 1.77 KB
/
nc-to-img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import netCDF4
import png
import struct
import sys
import os
from numpy import *
width=495
height=309
levels=39
nvals=width*height
frame = sys.argv[1]
data = netCDF4.Dataset('data_tmp.nc', 'r')
### Ground level data
params = ['wind10m_u', 'wind10m_v', 'temp2m']
for var in params:
if var not in data.variables:
continue
if not os.path.exists('public/data/' + frame):
os.makedirs('public/data/' + frame)
f = open('public/data/' + frame + '/' + var + '.png', 'wb')
w = png.Writer(width=width, height=height, alpha=True, compression=9)
bitmap = list()
data_array = list(data.variables[var])
for y in range(height - 1, -1, -1):
for x in range(width):
bytes = struct.pack('<f', data_array[y][x])
colors = struct.unpack('4B', bytes)
bitmap.append(colors[0])
bitmap.append(colors[1])
bitmap.append(colors[2])
bitmap.append(colors[3])
w.write_array(f, bitmap)
f.close()
### Elevation data
# for level in range(0, levels):
if False:
params = ['wind_u', 'wind_v', 'temp']
for var in params:
if var not in data.variables:
continue
if not os.path.exists('public/data/' + frame):
os.makedirs('public/data/' + frame)
f = open('public/data/' + frame + '/' + var + '_' + str(level) + '.png', 'wb')
w = png.Writer(width=width, height=height, alpha=True, compression=9)
bitmap = list()
data_array = list(data.variables[var])
for y in range(height - 1, -1, -1):
for x in range(width):
bytes = struct.pack('<f', data_array[level][y][x])
colors = struct.unpack('4B', bytes)
bitmap.append(colors[0])
bitmap.append(colors[1])
bitmap.append(colors[2])
bitmap.append(colors[3])
w.write_array(f, bitmap)
f.close()
data.close()