1 """Fire Perimeter Functions
2
3 This module contains two convienence functions (Import and Extract)
4 and a host of low level functions for dealing with fire perimeter
5 data. The import function is called by the user in the configuration
6 script and includes the input data parameters and analysis options.
7 the extract function is called for the fire perimeter data during
8 the more general call to Study.Extract().
9
10 """
11
12
13
14 -def Import(study, inshape, datefield, datetype, count=True, pointinterval=True, adddates=True, exporttable=True):
15 """Fire perimeter shapefile import convienence function
16
17 Inputs
18 study the study object
19 inshape fire perimeter polygon shapefile
20 datefield name of fire date field
21 datetype format of date field ( YYYY | YYYYMMDD )
22
23 Options (see named option functions for specifics)
24 count create summary rasters (count,min,max,range)
25 pointinterval Compute the previous fire date for datagrid
26 extract Extract the case history data (rasters)
27 adddates add unique dates to study
28 exporttable export the point-event table
29
30 This function first calls preparation function to copy, standardize
31 and intersect overlapping fires. Then all the option functions
32 except for extract are called. The extract function is called after
33 all the data is imported and any additional sample dates are added
34 to the date list using option [adddates=True].
35
36 """
37
38 vardict = PrepFirePerims(study, inshape, datefield, datetype)
39
40 if pointinterval == True:
41 PopulatePointIntervals(vardict['datagrid'])
42
43 if exporttable == True:
44 ExportShapeTable(
45 inshape = vardict['datagrid'],
46 fieldlist = vardict['fields'],
47 outfile = vardict['varname'],
48 sortexpr = 'POINTID A; EVTDATE A',
49 )
50
51 if count == True:
52 FireCountRaster(
53 rawdatagrid = vardict['rawdatagrid'],
54 cellsize = study.cellsize
55 )
56
58 """Extract Case History Data from a Fire Perimeter Series
59
60 This Function takes an imported fire perimeter polygon history
61 and extracts three reserved data variable names for each point
62 and date combination in the study. This means there should only
63 be one fire history imported into a study. Previous data will be
64 overwritten with a second call to extract.
65
66 Input
67 study study object
68 varname name of the source variable ("FIRE")
69
70 Output: (none) Adds paths to data files to study variables list
71 BURN Fire occurs during [Start < FireDate <= End]
72 SAGE Time since fire at SDATE (0 if fire on SDATE)
73 EAGE Time since fire at EDATE (pre-fire tsf if BURN is 1)
74
75
76 """
77 vardict = study.infodict[varname]
78
79 burndict = ExtractBurn(
80 datagrid = vardict['datagrid'],
81 sdates = study.startdates,
82 edates = study.enddates,
83 cellsize = study.cellsize, )
84 study.AddSeries(burndict)
85
86 (sdict,edict) = ExtractTSF(
87 datagrid = vardict['datagrid'],
88 sdates = study.startdates,
89 edates = study.enddates,
90 cellsize = study.cellsize, )
91
92 study.AddSeries(sdict)
93 study.AddSeries(edict)
94
95 -def PrepFirePerims(study, inshape, datefield, datetype, varname='FIRE',adddates=True):
96 """Prepare / Import the input fire perimeter history.
97
98 Inputs
99 Input variables defined in ImportFirePerims()
100
101 Procedure
102
103 Create data directory at StudyDir/FIRE
104 Copy perimeter shapefile to [datashp]
105 Copy and convert date field to EVTDATE
106 Add study area polygons at [startdate] and [enddate]
107 Intersect [datashp] * [pointgrid] -> [rawdatagrid]
108 adddates add fire occurrence dates to study dates
109
110 Outputs
111 datashape.shp Fire perimeter shapefile
112 datagrid.shp Fire occurrence point grid
113
114
115 """
116
117 log.info("Data Prep ("+varname+")")
118
119 os.chdir(study.workingdir)
120 workingdir = os.path.abspath(varname)
121 MakeFolder(workingdir)
122 gp.Workspace = workingdir
123
124 datashp = os.path.abspath("datashape.shp")
125 datagrid = os.path.abspath("datagrid.shp")
126 rawdatagrid = os.path.abspath("rawdatagrid.shp")
127
128
129 gp.copy(inshape,datashp)
130 CopyField(datashp,'EVTDATE',datefield)
131 ConvertDateField(datashp,'EVTDATE',datetype)
132 CopyField(datashp,'STATE','FID')
133 FilterFields(datashp,['EVTDATE','STATE'])
134
135
136 gp.Intersect(study.pointgrid+';'+ datashp, rawdatagrid)
137
138
139 gp.Append_management(study.studyshp,datashp,"NO_TEST")
140 gp.Append_management(study.finstudyshp,datashp,"NO_TEST")
141
142
143 gp.Intersect(study.pointgrid+';'+ datashp, datagrid)
144
145
146 gp.AddField_management(datagrid,'PREVDATE',"DOUBLE")
147 gp.AddField_management(datagrid,'PREVSTATE',"DOUBLE")
148
149
150
151 uniquedates = num.unique(GetShpField(datashp,'EVTDATE'))
152 if adddates == True:
153 study.UpdateDates(uniquedates)
154
155 fields = ['EVTDATE','STATE','PREVDATE','PREVSTATE']
156
157
158 vardict = {
159 "type" : 'fire',
160 "varname" : varname,
161 "workingdir" : workingdir,
162 "datashape" : datashp,
163 "datagrid" : datagrid,
164 "rawdatagrid" : rawdatagrid,
165 "uniquedates" : uniquedates,
166 "fields" : fields
167 }
168
169 study.AddSource(varname,vardict)
170 return vardict
171
172
173
174
176 """Compute rasters summarizing the period [start <= t < end]
177
178 Inputs
179 rawdatagrid fire point grid
180 cellsize output cellsize
181
182 Outputs: rasters of the same size as studyarea giving
183 count : count of fires
184 min : first fire
185 max : last fire
186 range : max - min
187 meanint : [(count-1) / (max-min)] """
188
189 log.info('Extracting Fire Count Map Summary')
190 gp.Workspace = os.path.dirname(rawdatagrid)
191
192
193 gp.MakeFeatureLayer(rawdatagrid, "lyr")
194
195
196 gp.PointToRaster_conversion('lyr',"FID", "count", 'COUNT', "NONE", cellsize)
197
198
199 gp.PointToRaster_conversion('lyr',"EVTDATE", "min", 'MINIMUM', "NONE", cellsize)
200 gp.PointToRaster_conversion('lyr',"EVTDATE", "max", 'MAXIMUM', "NONE", cellsize)
201
202
203 gp.PointToRaster_conversion('lyr',"EVTDATE", "range", 'RANGE', "NONE", cellsize)
204 gp.minus_sa("count", 1, "countminus" )
205 gp.Divide_sa("range","countminus","meanint")
206 gp.delete_management("countminus")
207
209 """Populate the fire points with the data about the previous fire
210
211 PREVSTATE : Id number of previous fire
212 PREVDATE : date of previous fire
213 """
214 log.info("Populating Point Intervals")
215
216 rows = gp.Updatecursor(datagrid,"","","","POINTID A; EVTDATE A")
217 row = rows.Next()
218 prevpid = -1
219 while row:
220
221 if prevpid != row.POINTID:
222 prevdate = -9999
223 prevstate = -9999
224
225
226 row.PREVDATE = prevdate
227 row.PREVSTATE = prevstate
228
229
230 prevdate = row.EVTDATE
231 prevstate = row.STATE
232 prevpid = row.POINTID
233
234 rows.UpdateRow(row)
235 row = rows.Next()
236
238 """Extract fire occurrence rasters for each sample period
239
240 BURN = (0|1) Fire occurs during [SDATE < t <= EDATE]"""
241
242 os.chdir(os.path.dirname(datagrid))
243 gp.Workspace = MakeFolder(os.path.abspath('BURN'))
244
245 files = [os.path.abspath('burn'+str(d)) for d in range(len(edates))]
246 gp.MakeFeatureLayer(datagrid,"lyr")
247 prog = ProgDots(len(edates), title='Extract Burn')
248 for (sdate,edate,bfile) in zip(sdates,edates,files):
249 gp.SelectLayerByAttribute("lyr", "NEW_SELECTION",'"EVTDATE" <= '+str(edate) )
250 gp.SelectLayerByAttribute("lyr", "SUBSET_SELECTION",'"EVTDATE" > '+str(sdate))
251 gp.PointToRaster_conversion('lyr',"EVTDATE", 'burnraster', 'MAXIMUM',"NONE", cellsize)
252 gp.IsNull_sa('burnraster','nullraster')
253 gp.LessThan_sa('nullraster', 1, bfile)
254 gp.delete_management('burnraster')
255 gp.delete_management('nullraster')
256 prog.step()
257
258 burnNpy = RasterToNpySeries(files,cleanup=True)
259 bfile = JoinDateRasters(burnNpy,'BURN')
260
261 bdict = {'name':'BURN','file':bfile,'list':burnNpy}
262
263 return bdict
264
266 """Extract Time Since Fire for each sample period
267
268 SAGE = Time Since Fire at SDATE (0 for fires on SDATE)
269 EAGE = Time Since Fire at EDATE (Age at Burn for fires on EDATE)"""
270
271 os.chdir(os.path.dirname(datagrid))
272 gp.Workspace = MakeFolder(os.path.abspath('AGE'))
273
274 sf = [os.path.abspath('sdate'+str(d)) for d in range(len(edates))]
275 ef = [os.path.abspath('edate'+str(d)) for d in range(len(edates))]
276 gp.MakeFeatureLayer(datagrid,"lyr")
277 prog = ProgDots(len(edates), title='Extract Time Since Fire')
278 for (sdate,edate,sfile,efile) in zip(sdates,edates,sf,ef):
279
280 gp.SelectLayerByAttribute("lyr", "NEW_SELECTION",'"EVTDATE" <= '+str(sdate) )
281 gp.PointToRaster_conversion('lyr',"EVTDATE", 'sdate', 'MAXIMUM', "NONE", cellsize)
282 gp.Minus_sa(sdate,'sdate',sfile)
283
284 gp.SelectLayerByAttribute("lyr", "NEW_SELECTION",'"EVTDATE" < '+str(edate) )
285 gp.PointToRaster_conversion('lyr',"EVTDATE", 'edate', 'MAXIMUM', "NONE", cellsize)
286 gp.Minus_sa(edate,'edate',efile)
287
288 prog.step()
289
290 gp.delete_management('sdate')
291 gp.delete_management('edate')
292
293 sageNpy = RasterToNpySeries(sf,cleanup=False)
294 eageNpy = RasterToNpySeries(ef,cleanup=False)
295
296 sfile = JoinDateRasters(sageNpy,'SAGE')
297 efile = JoinDateRasters(eageNpy,'EAGE')
298
299 sdict = {'name':'SAGE','file':sfile,'list':sageNpy}
300 edict = {'name':'EAGE','file':efile,'list':eageNpy}
301 return (sdict,edict)
302
303
304
306 - def __init__ (self, study, inshape, datefield, datetype, varname = 'FIRE', count=True, pointinterval=True, adddates=True, exporttable=True):
307
308 log.info("Data Prep (FIRE)")
309 self.study = study
310 self.inshape = inshape
311 self.datefield = datefield
312 self.datetype = datetype
313 self.varname = varname
314
315 self.count = count
316 self.pointinterval = pointinterval
317 self.adddates = adddates
318 self.exporttable = exporttable
319
320 self.workingdir = os.path.join(study.workingdir, self.varname)
321
322
323 pdict = PrepFirePerims(workingdir = self.workingdir,
324 pointgrid = study.pointgrid,
325 inshape = self.inshape,
326 datefield = self.datefield,
327 datetype = self.datetype,
328 initshp = study.studyshp,
329 finshp = study.finstudyshp,
330 varname = self.varname,
331 )
332
333 self.datashp = pdict['datashp']
334 self.datagrid = pdict['datagrid']
335 self.rawdatagrid = pdict['rawdatagrid']
336 self.fields = ['EVTDATE','STATE','PREVDATE','PREVSTATE']
337
338
339 self.uniquedates=num.unique(GetShpField(self.datashp,'EVTDATE'))
340 if self.adddates == True:
341 study.UpdateDates(self.uniquedates)
342
343 study.AddSource(varname,vardict)
344
345 if self.pointinterval == True:
346 PopulatePointIntervals(self.datagrid)
347
348 if self.exporttable == True:
349 ExportShapeTable(
350 inshape = self.datagrid,
351 fieldlist = self.fields,
352 outfile = self.varname,
353 sortexpr = 'POINTID A; EVTDATE A',
354 )
355
356 if self.count == True:
357 FireCountRaster(
358 rawdatagrid = self.rawdatagrid,
359 cellsize = study.cellsize
360 )
361
362 -def PrepFirePerims_X(workingdir, pointgrid, inshape, datefield, datetype, initshp= False, finshp = False):
363 """Prepare / Import the input fire perimeter history.
364
365 Inputs
366 Input variables defined in ImportFirePerims()
367
368 Procedure
369
370 Create data directory at StudyDir/FIRE
371 Copy perimeter shapefile to [datashp]
372 Copy and convert date field to EVTDATE
373 Add study area polygons at [startdate] and [enddate]
374 Intersect [datashp] * [pointgrid] -> [rawdatagrid]
375 adddates add fire occurrence dates to study dates
376
377 Outputs
378 datashape.shp Fire perimeter shapefile
379 datagrid.shp Fire occurrence point grid
380
381
382 """
383
384 MakeFolder(workingdir)
385 gp.Workspace = workingdir
386 datashp = os.path.abspath("datashape.shp")
387 datagrid = os.path.abspath("datagrid.shp")
388 rawdatagrid = os.path.abspath("rawdatagrid.shp")
389
390
391 gp.copy(inshape,datashp)
392 CopyField(datashp,'EVTDATE',datefield)
393 ConvertDateField(datashp,'EVTDATE',datetype)
394 CopyField(datashp,'STATE','FID')
395 FilterFields(datashp,['EVTDATE','STATE'])
396
397
398 gp.Intersect(study.pointgrid+';'+ datashp, rawdatagrid)
399
400
401 if initshp != False:
402 gp.Append_management(study.studyshp,datashp,"NO_TEST")
403 if finshp != False
404 gp.Append_management(study.finstudyshp,datashp,"NO_TEST")
405
406
407 gp.Intersect(study.pointgrid+';'+ datashp, datagrid)
408
409
410 gp.AddField_management(datagrid,'PREVDATE',"DOUBLE")
411 gp.AddField_management(datagrid,'PREVSTATE',"DOUBLE")
412
413
414 vardict = {
415 "datashape" : datashp,
416 "datagrid" : datagrid,
417 "rawdatagrid" : rawdatagrid,
418 }
419
420 return vardict
421