-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_netcdfs.py
executable file
·103 lines (90 loc) · 3.41 KB
/
create_netcdfs.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
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
"""
Created by: Chris Terai - [email protected]
This is a library of functions that are used in the creation of new netcdf files.
Many functions in this libary attach meta-data onto netcdf files.
Tested on python-2.7 with UVCDAT.
"""
import cdat_info,cdtime,code,datetime,gc,inspect,os,pytz,re,string,sys
import cdms2 as cdm
import MV2 as MV #stuff for dealing with masked values.
import cdutil as cdu
import glob
import os
from string import replace
import numpy as np
from socket import gethostname
import subprocess
def globalAttWrite(file_handle):
"""
Documentation for globalAttWrite():
-------
The globalAttWrite() function writes standard global_attributes to an
open netcdf specified by file_handle
Original author: Paul J. Durack : [email protected]
Originally in durolib.py
Modifed by: Chris Terai - [email protected]
Returns:
-------
Updated file_handle
Usage:
------
>>> from create_netcdfs import globalAttWrite
>>> file_handle=globalAttWrite(file_handle)
Where file_handle is a handle to an open, writeable netcdf file
Examples:
---------
>>> from create_netcdfs import globalAttWrite
>>> f = cdms2.open('data_file_name','w')
>>> f = globalAttWrite(f)
# Writes standard global attributes to the netcdf file specified by file_handle
Notes:
-----
When ...
"""
# Create timestamp, corrected to UTC for history
local = pytz.timezone("America/Los_Angeles")
time_now = datetime.datetime.now();
local_time_now = time_now.replace(tzinfo = local)
utc_time_now = local_time_now.astimezone(pytz.utc)
time_format = utc_time_now.strftime("%d-%m-%Y %H:%M:%S %p")
setattr(file_handle,'institution',"Department of Earth System Science, UC-Irvine")
setattr(file_handle,'data_contact',"Chris Terai; [email protected]")
setattr(file_handle,'history',"".join(['File processed: ',time_format,' UTC; Irvine, CA, USA']))
#setattr(file_handle,'analysis_host',"".join([gethostname(),'; UVCDAT version: ',".".join(["%s" % el for el in cdat_info.version()]), \
# '; Python version: ',replace(replace(sys.version)),') ;',')'])
return file_handle
def transfer_attributes(f_in,f_out):
"""
f_out = transfer_attributes(f_in,f_out)
f_in and f_out point to netcdfs
This function transfers the netcdf attributes from f_in to f_out
"""
att_keys = f_in.attributes.keys()
att_dic = {}
for i in range(len(att_keys)):
att_dic[i]=att_keys[i],f_in.attributes[att_keys[i]]
to_out = att_dic[i]
setattr(f_out,to_out[0],to_out[1])
return f_out
def add_git_hash(f_out):
"""
Adds the git hash (version of the code) to the f_out
"""
label = subprocess.check_output(["git", "describe","--always"]).strip()
setattr(f_out,'git_hash',label)
return f_out
def add_scriptname(f_out):
"""
Adds the name of the script that is used to create the output in the global
attributes of the file
"""
filename=os.path.basename(__file__)
setattr(f_out,'script_used',filename)
return f_out
def return_git_hash():
"""
Returns the git hash of the current commit
"""
label = subprocess.check_output(["git", "describe","--always"]).strip()
return label