|
POST
|
I'm running ArcGIS 10.2.1 Advanced with SA and Python 2.7 I'm trying to replicate in Python something done in excel: I have a set of 12 rasters with values 0 and 1, each raster representing a month of the year. I need to create an output raster that will count the maximum number of consecutive months occurring. In the excel example above the result is 4 as it starts in December and accumulates through until March. So it is looping through all months. I have the column called "Rating Count" above sorted. The problem I encounter is with Jan in the "Consec Mo" column as this value is not fixed because it is fed from the December value in the same column. I have tried this: # Set monthly values to 0 and 1
jan = Con(IsNull(Rating_jan), 0, 1)
feb = Con(IsNull(Rating_feb), 0, 1)
mar = Con(IsNull(Rating_mar), 0, 1)
apr = Con(IsNull(Rating_apr), 0, 1)
may = Con(IsNull(Rating_may), 0, 1)
jun = Con(IsNull(Rating_jun), 0, 1)
jul = Con(IsNull(Rating_jul), 0, 1)
aug = Con(IsNull(Rating_aug), 0, 1)
sep = Con(IsNull(Rating_sep), 0, 1)
oct = Con(IsNull(Rating_jan), 0, 1)
nov = Con(IsNull(Rating_jan), 0, 1)
dec = Con(IsNull(Rating_jan), 0, 1)
# Calculate consecutive and MAX value
jan1 = jan
feb2 = Con((feb) == 1, (jan1) + 1, 0)
mar3 = Con((mar) == 1, (feb2) + 1, 0)
apr4 = Con((apr) == 1, (mar3) + 1, 0)
may5 = Con((may) == 1, (apr4) + 1, 0)
jun6 = Con((jun) == 1, (may5) + 1, 0)
jul7 = Con((jul) == 1, (jun6) + 1, 0)
aug8 = Con((aug) == 1, (jul7) + 1, 0)
sep9 = Con((sep) == 1, (aug8) + 1, 0)
oct10 = Con((oct) == 1, (sep9) + 1, 0)
nov11 = Con((nov) == 1, (oct10) + 1, 0)
dec12 = Con((dec) == 1, (nov11) + 1, 0)
Final_Output = CellStatistics([(jan1), (feb2), (mar3), (apr4), (may5), (jun6), (jul7), (aug8), (sep9), (oct10), (nov11), (dec12)]
, "MAXIMUM", "NODATA")
Final_Output.save(OUTWorkspace + "\\" + "Final_Output") But as I said the "jan" value (row 23) is not fixed as in the script so it is not quite what I'm after. I wonder if someone could give me some hints about how to solve this issue. Thanks in advanced Tony
... View more
10-22-2015
06:12 AM
|
0
|
17
|
7579
|
|
POST
|
Thanks Neil. That's precisely what I needed. I hand't noticed this tool.
... View more
10-20-2015
05:17 AM
|
0
|
0
|
1464
|
|
POST
|
Hi, I wonder if someone could give me some hints about how to tackle this issue: I have 12 raster layers with values each representing a month (see table below). I need to create in Python a new raster that gives me the number of the month with the highest values for a particular cell. That is, if for example a cell has the value "32" and that is the highest of all the 12 cells representing the 12 months, then the output raster for that cell would have the value "9" because that is the month holding that highest value. My guess is to combine dictionary and cell statistics but not quite sure how. Hope it makes sense and thanks in advance Tony
... View more
10-20-2015
04:17 AM
|
0
|
2
|
3568
|
|
POST
|
Looks as if I finally managed to get it running. Code looks like this: env.workspace = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL\00_UPWELLING\OUTPUT_Test.gdb"
raster = env.workspace + "\\" + "percentile"
array = arcpy.RasterToNumPyArray(raster,nodata_to_value=10000000)
marray = numpy.ma.masked_values(array,10000000)
sarray = scipy.sort(marray)
per = 40
percen = numpy.percentile(sarray.compressed(),(per))
print percen Thanks for your comments/help
... View more
07-17-2015
07:12 AM
|
0
|
0
|
3717
|
|
POST
|
Hi again, I've been trying also to set NoData to a value and then use "ma.masked_values" to exclude that value from the percentile calculation but it looks as if the value is not excluded.
... View more
07-17-2015
03:53 AM
|
0
|
1
|
3717
|
|
POST
|
Thanks Todd/Dan, I'm trying to use numpy mask module. Not sure I'm getting the right syntax when calling the "NoData" (Null, NoData...) values. I don't quite see how to do so in the documentation. This is how my code looks at the moment. Any further help would be greatly appreciate it. env.workspace = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL\00_UPWELLING\OUTPUT_Test.gdb"
raster = env.workspace + "\\" + "percentile"
array = arcpy.RasterToNumPyArray(raster)
marray = numpy.ma.masked_values(array,NULL)
sarray = scipy.sort(marray)
per = 20
a = scipy.percentile(sarray,per)
print a
... View more
07-17-2015
02:31 AM
|
0
|
2
|
3717
|
|
POST
|
Hi Todd, Many thanks for your reply. I'm aware NoData can be assigned to a particular value. But, wouldn't that then "corrupt" the percentile calculation? as in this case there would be a lot of say value 1 (if for example I assign NoData to 1). I'm working with global rasters with values present only 250km from the coast. I think for the percentile calculation to be correct only the real list of values should be taken into account excluding the NoData values. Or maybe I'm not quite right... Cheers
... View more
07-16-2015
07:52 AM
|
0
|
5
|
3717
|
|
POST
|
Hi, I'm trying to calculate percentiles out of a floating point raster. It works but it includes NoData in the calculation so I'm not getting what I need. I wonder if someone knows how to exclude the "NoData" from the calculation. Many thanks env.workspace = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL\00_UPWELLING\OUTPUT_Test.gdb"
raster = env.workspace + "\\" + "percentile"
array = arcpy.RasterToNumPyArray(raster)
array = scipy.sort(array)
per = 10
a = scipy.percentile(array,per)
print a
... View more
07-16-2015
07:43 AM
|
0
|
1
|
3975
|
|
POST
|
Hi, I'm trying to calculate percentiles out of a floating point raster. It works BUT it includes the NoData in the calculation so I don't get what I need. Is there a way to exclude the NoData from this calculation? Many thanks env.workspace = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL\00_UPWELLING\OUTPUT_Test.gdb"
raster = env.workspace + "\\" + "percentile"
array = arcpy.RasterToNumPyArray(raster)
array = scipy.sort(array)
per = 10
a = scipy.percentile(array,per)
print a
... View more
07-16-2015
07:31 AM
|
0
|
7
|
6803
|
|
POST
|
Thank you ever so much Owain. I understand now the logic behind it.
... View more
12-18-2014
03:53 AM
|
0
|
0
|
2646
|
|
POST
|
Hi again, I'm dealing with a bit more complex expression now but I don't quite get it right. I got the first part (in bold) but I cannot quite figure out the second part of the expression. I would much appreciate some help. Many thanks import arcpy
import string
import math
from arcpy import env
arcpy.env.overwriteOutput=True
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)
INWorkspace = arcpy.GetParameterAsText(0)
StageAge = "Maas"
TOCDrySedDen = arcpy.GetParameterAsText(1)
Porosity = arcpy.GetParameterAsText(2)
GridPoints_ft2_qs = (INWorkspace + "\\" + StageAge + "_nodes_variable_ft_2")
arcpy.AddField_management(GridPoints_ft2_qs,"cm_yr","DOUBLE")
Expression = ("!g_cm2_yr! / {}".format(float(TOCDrySedDen)))
arcpy.CalculateField_management(GridPoints_ft2_qs,"cm_yr", Expression, "PYTHON_9.3") # EXPRESSION CALCULATION: (!g_cm2_yr! / float(arcpy.GetParameterAsText(1))) * ((float(arcpy.GetParameterAsText(2))/100) / (1-float(arcpy.GetParameterAsText(2))/100))
... View more
12-18-2014
02:57 AM
|
0
|
3
|
2646
|
|
POST
|
Hi, Please see code below. I need to substitute the value "90" in Expression with user input, that is: LSARqssedret I have tried: Expression = "!Qs_Mt_a!" * float(LSARqssedret) but I'm not getting the right syntax within the expression. Any help would be greatly appreciated. Many thanks import arcpy
import string
import math
from arcpy import env
arcpy.env.overwriteOutput=True
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)
INWorkspace = arcpy.GetParameterAsText(0)
StageAge = "Maas"
LSARqssedret = arcpy.GetParameterAsText(1)
GridPoints_ft2_qs = (INWorkspace + "\\" + StageAge + "_nodes_variable_ft_2")
arcpy.AddField_management(GridPoints_ft2_qs,"MASS_g_a","DOUBLE")
Expression = "!Qs_Mt_a! * 90"
arcpy.CalculateField_management(GridPoints_ft2_qs,"Mass_g_a",Expression,"PYTHON")
... View more
12-17-2014
06:46 AM
|
0
|
6
|
4307
|
|
POST
|
Thanks a lot Luis. I put the parameter at the beginning of the script (as I normally do actually) and now IT WORKS! That "rule" seems obviously to be important and I'm sure there is an explanation for it... Many thanks!
... View more
11-12-2014
05:27 AM
|
1
|
0
|
2491
|
|
POST
|
Hi, I was being too optimistic. Here is the whole code: =============================================================================== import arcpy import string import math from arcpy import env arcpy.env.overwriteOutput=True from arcpy.sa import * arcpy.CheckOutExtension("Spatial") # Set workspace location, extent and variables INWorkspace = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL\00_LFP_GLOBAL_Python\WORK_TEST_space\ISLAND_Effect\INPUT.gdb" arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0) inFeature = INWorkspace + "\\" + "G_SL_HiStand_20M_J_Tith_m2_0" outFeature = INWorkspace + "\\" + "HiStand_polygon" outFeatureAREA = INWorkspace + "\\" + "HiStand_polygonAREA" # Convert polyline feature to polygon arcpy.FeatureToPolygon_management(inFeature, outFeature, "","NO_ATTRIBUTES", "") # Calculate area in KM2 arcpy.CalculateAreas_stats(outFeature, outFeatureAREA) Expr = "(!F_AREA!) / 1000000" arcpy.CalculateField_management(outFeatureAREA,"F_AREA",Expr,"PYTHON") # Filter by Area AreaFilter = float(arcpy.GetParameterAsText(0)) cur = arcpy.UpdateCursor(outFeatureAREA) for row in cur: if row.F_AREA <= AreaFilter: cur.deleteRow(row) cur.updateRow(row) del row del cur =============================================================================== If I run the script up to "# Filter by Area" it will create a Polygon Feature Class (HiStand_polygonAREA) with a field called "F_Area" holding all area values. If I then run the script only from "# Filter by Area" with "HiStand_polygonAREA" as the input feature class (outFeatureAREA) it works. If I run the whole script at once it still produces the output required (HiStand_polygonAREA) but it doesn't perform anything within the cursor part, for example, deleting those rows <= than 1000; and I get this: So, I'm lost now.
... View more
11-12-2014
04:23 AM
|
0
|
2
|
2491
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 11-06-2015 05:13 AM | |
| 1 | 11-09-2015 01:25 AM | |
| 1 | 11-12-2014 05:27 AM |
| Online Status |
Offline
|
| Date Last Visited |
07-12-2022
09:52 AM
|