Skip to content

Commit f35d1bf

Browse files
committed
issue #5: Continue work on new forecast point webservice; Need to get the CalculateField_management tool to work
1 parent 15f17a8 commit f35d1bf

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

get_AWDB_stations_3.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
__DEBUG__ = False # true outputs debug-level messages to stderr
4343

4444
# NRCS AWDB network codes to download
45-
NETWORKS = ["SNTL", "SNOW", "USGS", "COOP", "SCAN", "SNTLT", "OTHER", "BOR",
46-
"MPRC", "MSNT"]
47-
#NETWORKS = ["USGS"]
45+
#NETWORKS = ["SNTL", "SNOW", "USGS", "COOP", "SCAN", "SNTLT", "OTHER", "BOR",
46+
# "MPRC", "MSNT"]
47+
NETWORKS = ["USGS"]
4848

4949

5050
## Dictionaries of the station fields
@@ -719,7 +719,7 @@ def get_USGS_metadata(usgs_fc):
719719
cursor.updateRow(row)
720720

721721
def create_forecast_point_ws():
722-
from arcpy import CopyFeatures_management
722+
from arcpy import CopyFeatures_management, AddField_management, Append_management, Delete_management, AddJoin_management, CalculateField_management
723723
import arcpy
724724

725725
bUSGSExists = False
@@ -733,6 +733,8 @@ def create_forecast_point_ws():
733733
if arcpy.Exists(os.path.join(settings.AWDB_FGDB_PATH, BOR_Active)):
734734
bBORExists = True
735735
FCST_Active = "active_stations_FCST"
736+
FCST_Active_Temp = "active_stations_FCST_Temp"
737+
FCST_Active_Ref = "active_stations_FCST_Ref"
736738

737739
if (bUSGSExists and bBORExists):
738740
client = Client(settings.WDSL_URL)
@@ -749,9 +751,9 @@ def create_forecast_point_ws():
749751
numberofstations = len(data)
750752
LOGGER.info('We processed %d records', numberofstations)
751753
LOGGER.info('%d records in array', len(forecastIDs))
752-
BOR_Temp_Active = "temp_" + BOR_Active
754+
BOR_Active_Temp = BOR_Active + "_Temp"
753755
sourceFc = os.path.join(settings.AWDB_FGDB_PATH, USGS_Active)
754-
targetFc = os.path.join(settings.AWDB_FGDB_PATH, FCST_Active)
756+
targetFc = os.path.join(settings.AWDB_FGDB_PATH, FCST_Active_Temp)
755757
CopyFeatures_management(sourceFc, targetFc)
756758
LOGGER.info("Before %d records", getCount(targetFc))
757759
with arcpy.da.UpdateCursor(targetFc, ('stationTriplet')) as curs:
@@ -761,7 +763,7 @@ def create_forecast_point_ws():
761763
curs.deleteRow()
762764
LOGGER.info("After %d records", getCount(targetFc))
763765
sourceFc = os.path.join(settings.AWDB_FGDB_PATH, BOR_Active)
764-
targetFc = os.path.join(settings.AWDB_FGDB_PATH, BOR_Temp_Active)
766+
targetFc = os.path.join(settings.AWDB_FGDB_PATH, BOR_Active_Temp)
765767
CopyFeatures_management(sourceFc, targetFc)
766768
forecastIDs.clear()
767769
data = client.service.getForecastPoints(networkCds="BOR",logicalAnd="true")
@@ -779,9 +781,29 @@ def create_forecast_point_ws():
779781
if (test_triplet not in forecastIDs):
780782
curs.deleteRow()
781783
LOGGER.info("After %d records", getCount(targetFc))
782-
783-
784-
784+
tmpForecastFc = os.path.join(settings.AWDB_FGDB_PATH, FCST_Active_Temp)
785+
Append_management([os.path.join(settings.AWDB_FGDB_PATH, BOR_Active_Temp)], tmpForecastFc)
786+
Delete_management(os.path.join(settings.AWDB_FGDB_PATH, BOR_Active_Temp))
787+
FCST_FIELDS = [
788+
{"field_name": "winter_start_month", "field_type": "SHORT"}, # 0
789+
{"field_name": "winter_end_month", "field_type": "SHORT"}, # 1
790+
{"field_name": "huc2", "field_type": "TEXT", "field_length": 2} #2
791+
]
792+
LOGGER.info("Adding attribute fields to feature class...")
793+
for field in FCST_FIELDS:
794+
AddField_management(tmpForecastFc, **field)
795+
#This doesn't seem to do anything
796+
#AssignDefaultToField_management(tmpForecastFc, "winter_start_month", "11")
797+
#AssignDefaultToField_management(tmpForecastFc, "winter_end_month", "3")
798+
joined_table = AddJoin_management(tmpForecastFc, "stationTriplet", os.path.join(settings.AWDB_FGDB_PATH, FCST_Active_Ref), "stationTriplet")
799+
expression = "reclass(!active_stations_FCST_Ref.huc2!)"
800+
codeblock = """
801+
def reclass(huc2):
802+
if (huc2 != None):
803+
return huc2
804+
else:
805+
return None"""
806+
CalculateField_management(joined_table, "active_stations_FCST_Temp.huc2", expression, "PYTHON3", codeblock)
785807

786808
else:
787809
LOGGER.error("unable to locate {0} and or {1}. Forecast service will not be updated".format(USGS_Active, BOM_Active))
@@ -932,7 +954,7 @@ def main():
932954

933955
# create forecast webservice
934956
# Commenting out. Not ready to use yet
935-
# create_forecast_point_ws()
957+
create_forecast_point_ws()
936958

937959
if wfsupdatelist:
938960
LOGGER.info("\nUpdating AGOL feature services in update list...")

0 commit comments

Comments
 (0)