-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc2ascii
executable file
·76 lines (58 loc) · 1.83 KB
/
nc2ascii
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
#!/usr/bin/env python
# import gdal
# import math
# from gdalconst import *
# import struct
from netCDF4 import Dataset
# import matplotlib.pylab as mpl
import numpy as np
import sys
# import glob
# fill in times.
# from datetime import datetime, timedelta
# from netCDF4 import num2date, date2num
from argparse import ArgumentParser
def convert_var(filename, varname):
infile=Dataset(filename,"r")
var = infile.variables[varname]
x = infile.variables["x"]
y = infile.variables["y"]
newname="%s_%s.asc"%(filename[:-3],varname)
ncols = len(x)
nrows = len(y)
x_off = x[0]-(x[1]-x[0])/2.
y_off = y[0]-(y[1]-y[0])/2.
cellsize = x[1]-x[0]
# NODATA_VALUE = var._FillValue
NODATA_VALUE=-9999
outfile=open(newname,"w")
outfile.write ("ncols %i\n"%ncols)
outfile.write("nrows %i\n"%nrows)
outfile.write("xllcorner %i\n"%x_off)
outfile.write("yllcorner %i\n"%y_off)
outfile.write("cellsize %i\n"%cellsize)
outfile.write("NODATA_VALUE %i\n"%(int(NODATA_VALUE)))
ascii= [" ".join([ "%.2f"%y for y in x ])+"\n" for x in np.squeeze(var[:])]
ascii.reverse()
outfile.writelines(ascii)
outfile.close()
# print (dir(driver))
# src_ds = gdal.Open( template )
# outDataset = driver.CreateCopy(newname, src_ds, 0 )
# print outDataset
# outBand = outDataset.GetRasterBand(1)
# outBand.WriteArray(np.squeeze(var[:]), 0,0)
def parse_args():
parser = ArgumentParser()
parser.description = "Convert netcdf to ascii"
parser.add_argument("FILE", nargs='*')
parser.add_argument("-v", "--variable",
help='''Variables to convert, e.g. = 'usurf,cbar,...'.''', default=None)
options = parser.parse_args()
return options
def main(argv):
options = parse_args()
# print (dir(options))
convert_var(options.FILE[0], options.variable)
if __name__ == "__main__":
main(sys.argv)