-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_temps
executable file
·62 lines (49 loc) · 2.34 KB
/
convert_temps
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
#!/usr/bin/env python
import numpy as np
import sys
import netCDF4 as nc
import time
import re
import datetime
labels = {"AABW" : "m^3 / s", "NADW": "m^3 / s", "SST" : "deg C", "SSS" : "PSU" , "WFL" : "m^3 / s", "ICEVO" : "m^3", "ICEAR" : "m^2", "T" : "deg C" , "S" : "PSU" , "HFL" : "W", "TVQUER" : "W", "SVQUER": "PSU m^3 / s" , "TDENMARK" : "m^3 / s","TFAAROE" : "m^3 / s", "SFRAM" : "m^3 ice / s" , "PSIGOLF" : "m^3 / s", "PSIKURO" : "m^3 / s", "PSIBANDA" : "m^3 / s", "PSIDRAKE" : "m^3 / s" , "PSIBERING" : "m^3 / s", "AMAXLAT" : "m^3 / s", "TMERCI" : "m^3 / s", "Date" : "YYYYMMDD" , "T": "deg C"}
names=["time", "T_max", "T_min"]
def convert (filename):
data = np.squeeze(np.array([ np.genfromtxt(filename, unpack=True, delimiter=",") ] ))[:,1:]
names = open(filename, "r").readline()[:-1].split(",")
outfilename = filename + ".nc"
f = nc.Dataset(outfilename, 'w', format = "NETCDF3_CLASSIC")
f.history = 'Created ' + time.ctime(time.time())
# f = Nio.open_file(outfilename,"w",opt,"created "+datetime.datetime.now().isoformat() + " by findmax ")
my_time = f.createDimension('DATE', None)
vars = names
for (num,var) in enumerate(vars):
print "creating variable \"%s\""%(var)
print data[num,3].dtype
v = f.createVariable(var,'f4',('DATE',), fill_value=-9999)
# f.variables['moc_glo'].units= "m^3/s"
pattern=re.compile("[A-Z]*")
# unit=labels.get(pattern.match(var).group(), "")
# v.units = unit
v.long_name = names[num]
# f.variables['DATE'].units = 'years since 1-1-1 0:0:0'
f.variables['DATE'].calendar="proleptic_gregorian "
ld=data.shape[1]
for (num,var) in enumerate (vars):
print (num,var)
if var == "DATE":
dt=data[num,:]
#print dt
dt=["%08i"%(x) for x in dt]
#print dt
f.variables[var].units = "days since 1900-00-00 00:00:00"
f.variables[var][0:ld] = nc.date2num([datetime.datetime(int(x[0:4]),int(x[4:6]),int(x[6:8])) for x in dt],f.variables[var].units)
print f.variables[var][:]
else:
dt=data[num,:].astype('float32')
print dt
f.variables[var][0:ld] = dt
def main(args):
print "in main"
convert (args[0])
if __name__ == "__main__":
main(sys.argv[1:])