-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcalculateMonth.py
More file actions
35 lines (27 loc) · 1.05 KB
/
calculateMonth.py
File metadata and controls
35 lines (27 loc) · 1.05 KB
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
#calculateMonth.py
#Purpose: Calculates the Month from the date field for each record in the Spatially Joined AIS point data with Sea Ice
#June 20, 2017
#gfiske
# Import system modules
import arcpy
# Set environment settings
arcpy.env.workspace = "C:/Data/Arctic/ArcticOptions/AIS_w_ancillary_data_Apr_2017.gdb"
#choose a year
inTable = "spJoined_merged_w_Ice"
try:
#Add new field and calculate it to yearJD
fieldName = "ym"
expression = "ymCalc(!time!)"
codeblock = """def ymCalc(mydate):
from datetime import datetime
date_obj = datetime.strptime(mydate, "%m/%d/%Y")
#print date_obj
return '%d%02d' % (date_obj.timetuple().tm_year,date_obj.timetuple().tm_mon)"""
# Execute AddField
arcpy.AddField_management(inTable, fieldName, "LONG")
# Execute CalculateField
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON_9.3",
codeblock)
except Exception:
e = sys.exc_info()[1]
print(e.args[0])