-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp2nc
executable file
·65 lines (54 loc) · 2.06 KB
/
bmp2nc
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
#!/usr/bin/env python
import netCDF4 as nc
from PIL import Image
import numpy as np
from argparse import ArgumentParser
def write_field(filename, data):
file=nc.Dataset(filename,"w")
file.createDimension("y",data.shape[0])
file.createDimension("x",data.shape[1])
file.createVariable("imagedata",'i4',("y","x"))
file.variables["imagedata"][:]=data
# def rescale_data(data, prescale_function=False):
# if prescale_function:
# data=prescale_function(data)
# dmin = data.min()
# dmax = data.max()
# data = ((data+dmin)/(dmax-dmin)*255).round().astype(np.uint8)
# return data
def read_image(filename, opts=False):
pic = Image.open(filename)
pix = np.asarray(pic) # .reshape(pic.size[0], pic.size[1], 3)
if len(pix.shape) == 3:
pix= pix[:,:,0]*(256*256)+pix[:,:,1]*256+pix[:,:,2]
# values=np.unique(pix)
# pix2=np.zeros(pix.shape,dtype='i8')
# for (n,x) in enumerate (values):
# pix2[pix==x]=n
# return pix2
return pix
def parse_options():
# initialize parser
parser = ArgumentParser()
parser.description = "Convert a netcdf field into a bmp image"
# specify options
parser.add_argument("-f" , "--filename", help = "filename", required = True)
parser.add_argument("-t" , "--transpose", help = "transpose field", action = "store_true")
# parser.add_argument("-n" , "--var_name", help = "variable name", required = True)
parser.add_argument("-o" , "--out_name", help = "filename for output, default file.nc for input name file.bmp ", default = None )
parser.add_argument("-v", "--verbose",
help='''Be verbose''', action="store_true")
# run argparse
options = parser.parse_args()
return options
def main():
options = parse_options()
data = read_image(options.filename, opts=False)
outfilename = options.filename[:-4]+".nc"
if options.out_name:
outfilename = options.out_name
if options.transpose:
data=data.transpose()
write_field(outfilename, data)
if __name__ == "__main__":
main()