-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_corners
executable file
·101 lines (75 loc) · 2.79 KB
/
add_corners
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
94
95
96
97
98
99
100
101
#!/usr/bin/env python
import osr
import netCDF4 as nc
import numpy as np
import sys
import getopt
from argparse import ArgumentParser
def add_corners (filename, opts={}):
infile=nc.Dataset(filename,"r")
#infile=nc.Dataset("test.nc","r")
outfile=nc.Dataset("ll_%s"%(filename), "w", format='NETCDF3_64BIT')
x=infile.variables["lon"][:]
y=infile.variables["lat"][:]
xx = np.expand_dims(x,axis=0).repeat(len(y),axis=0)
yy = np.expand_dims(y,axis=0).repeat(len(x),axis=0).transpose()
fdx = outfile.createDimension("x", len(x))
fdy = outfile.createDimension("y", len(y))
fdy = outfile.createDimension("grid_corners", 4)
fvx = outfile.createVariable("x","f4",("x",))
fvy = outfile.createVariable("y","f4",("y",))
fvx[:] = x
fvy[:] = y
fvx.units = "degrees"
fvy.units = "degrees"
lon = outfile.createVariable("lon","f4",("y","x"), fill_value=-9.e9)
lat = outfile.createVariable("lat","f4",("y","x"), fill_value=-9.e9)
lon.units = "degrees"
lon.long_name = "longitude"
lon.standard_name = "longitude"
lon.bounds = "grid_corner_lon"
lon._CoordinateAxisType = "Lon"
lat.units = "degrees"
lat.long_name = "latitude"
lat.standard_name = "latitude"
lat.bounds = "grid_corner_lat"
lat._CoordinateAxisType = "Lat"
lon[:] = xx
lat[:] = yy
xoff=(x[1]-x[0])/2.
yoff=(y[1]-y[0])/2.
print xoff
print yoff
addx = [ xoff, xoff,-xoff,-xoff]
addy = [-yoff, yoff, yoff,-yoff]
grid_corner_lat = outfile.createVariable("grid_corner_lat","f4", ("y", "x", "grid_corners"), fill_value=-9.e9)
grid_corner_lon = outfile.createVariable("grid_corner_lon","f4", ("y", "x", "grid_corners"), fill_value=-9.e9)
grid_corner_lat.units = "degrees"
grid_corner_lon.units = "degrees"
for i in xrange(4):
grid_corner_lon[:,:,i] = xx+addx[i]
grid_corner_lat[:,:,i] = yy+addy[i]
outfile.close()
def parse_args():
parser = ArgumentParser()
parser.description = "compare slopes of two variables from two files"
parser.add_argument("FILES", nargs=1)
parser.add_argument("-v", "--verbose",
help='''Be verbose''', action="store_true")
parser.add_argument("-u", "--utm",
help='''File is using utm coordinates''', default = None, type = int)
# parser.add_argument("-s", "--state",
# help='''file with reference values''', required = True)
# parser.add_argument("-b", "--var_b",
# help='''variable b''', default="data")
options = parser.parse_args()
return options
def main():
options = parse_args()
if options.verbose:
print (dir(options))
print options.FILES
fu.debug=True
add_corners(options.FILES[0], vars(options))
if __name__ == "__main__":
main()