Sahand commited on
Commit
4727dd8
1 Parent(s): 2f9be84

Data lookup and crop fix

Browse files
Files changed (2) hide show
  1. app.py +5 -0
  2. libs/dashapp.py +85 -85
app.py CHANGED
@@ -25,6 +25,7 @@ CONFIG = {}
25
  V = 1
26
  V_IGNORE = [] # Debug, Warning, Error
27
  MODEL_PATH = ""
 
28
 
29
  # ===============================================================================
30
  # The Main App
@@ -128,6 +129,7 @@ def find_model_path(real_path):
128
  )
129
  def update_session(farm_name, session):
130
  global MODEL_PATH
 
131
  session = farm_name
132
  if farm_name is None or farm_name == "":
133
  session = ""
@@ -149,6 +151,7 @@ def update_session(farm_name, session):
149
 
150
  if os.path.exists(real_path):
151
  session = farm_name
 
152
  image = SUCCESS_IMAGE
153
  else:
154
  session = ""
@@ -213,6 +216,7 @@ def get_analysis(
213
  list of dicts of analysis files
214
  """
215
  global MODEL_PATH
 
216
  print("\nAnalysis callback triggered")
217
 
218
  if n_clicks == 0 or n_clicks is None:
@@ -233,6 +237,7 @@ def get_analysis(
233
  comparison="diff",
234
  output=None,
235
  match_raster=None,
 
236
  )
237
 
238
  print(MODEL_PATH)
 
25
  V = 1
26
  V_IGNORE = [] # Debug, Warning, Error
27
  MODEL_PATH = ""
28
+ FARM_NAME = ""
29
 
30
  # ===============================================================================
31
  # The Main App
 
129
  )
130
  def update_session(farm_name, session):
131
  global MODEL_PATH
132
+ global FARM_NAME
133
  session = farm_name
134
  if farm_name is None or farm_name == "":
135
  session = ""
 
151
 
152
  if os.path.exists(real_path):
153
  session = farm_name
154
+ FARM_NAME = farm_name
155
  image = SUCCESS_IMAGE
156
  else:
157
  session = ""
 
216
  list of dicts of analysis files
217
  """
218
  global MODEL_PATH
219
+ global FARM_NAME
220
  print("\nAnalysis callback triggered")
221
 
222
  if n_clicks == 0 or n_clicks is None:
 
237
  comparison="diff",
238
  output=None,
239
  match_raster=None,
240
+ farm_name=FARM_NAME,
241
  )
242
 
243
  print(MODEL_PATH)
libs/dashapp.py CHANGED
@@ -72,11 +72,11 @@ def read_shape_file(
72
  return geo_df
73
 
74
 
75
- def find_match_raster(model_path):
76
  # navigate one directory up in model_path
77
- root = os.path.dirname(model_path)
78
- root = os.path.dirname(root)
79
- root = os.path.dirname(root)
80
  print("Looking for a matching raster in:", root)
81
  for root, dirs, _ in os.walk(root):
82
  # print(root,dirs,"\n\n")
@@ -91,11 +91,12 @@ def find_match_raster(model_path):
91
  return None
92
 
93
 
94
- def find_shape_file(model_path):
95
- # navigate one directory up in model_path
96
- root = os.path.dirname(model_path)
97
- root = os.path.dirname(root)
98
- root = os.path.dirname(root)
 
99
  print("Looking for a shape file in:", root)
100
  for root, dirs, files in os.walk(root):
101
  # print(root,dirs,"\n\n")
@@ -327,6 +328,7 @@ def analyse(
327
  output: str = None,
328
  agg_history: str = "mean",
329
  agg_window: str = "mean",
 
330
  **kwargs,
331
  ) -> Dict[str, str]:
332
  """Main function to run the script.
@@ -351,6 +353,8 @@ def analyse(
351
  Aggregation method to use for the historic years. Possible values: 'mean', 'median', 'max', 'min', 'std', None. Default: 'mean'.
352
  agg_window : str
353
  Aggregation method to use for the window. Possible values: 'mean', 'median', 'max', 'min', 'std', None. Default: 'mean'.
 
 
354
 
355
  Returns
356
  -------
@@ -366,7 +370,7 @@ def analyse(
366
  os.makedirs(output)
367
 
368
  if match_raster is None:
369
- match_raster = find_match_raster(input)
370
  print("match_raster is:", match_raster)
371
 
372
  if match_raster is not None:
@@ -471,7 +475,7 @@ def analyse(
471
  )
472
 
473
  # Search for a shape file ending with .shp
474
- shape_file = find_shape_file(input)
475
  print("Shape file:", shape_file)
476
 
477
  # Save the rasters
@@ -510,86 +514,80 @@ def analyse(
510
  try:
511
  with fiona.open(shape_file, "r") as shapefile:
512
  shapes = [feature["geometry"] for feature in shapefile]
 
 
513
 
514
- try:
515
- with rasterio.open(historic_raster) as src:
516
- out_image, transformed = rasterio.mask.mask(
517
- src, shapes, crop=True, filled=True
518
- )
519
- out_profile = src.profile.copy()
520
- out_profile.update(
521
- {
522
- "width": out_image.shape[2],
523
- "height": out_image.shape[1],
524
- "transform": transformed,
525
- }
526
- )
527
- with rasterio.open(historic_raster, "w", **out_profile) as dst:
528
- dst.write(out_image)
529
- except Exception as e:
530
- print("Failed to crop historic raster", historic_raster)
531
- print(e)
532
-
533
- try:
534
- with rasterio.open(current_raster) as src:
535
- out_image, transformed = rasterio.mask.mask(
536
- src, shapes, crop=True, filled=True
537
- )
538
- out_profile = src.profile.copy()
539
- out_profile.update(
540
- {
541
- "width": out_image.shape[2],
542
- "height": out_image.shape[1],
543
- "transform": transformed,
544
- }
545
  )
546
- with rasterio.open(current_raster, "w", **out_profile) as dst:
547
- dst.write(out_image)
548
- except Exception as e:
549
- print("Failed to crop current raster", current_raster)
550
- print(e)
551
-
552
- try:
553
- with rasterio.open(delta_raster) as src:
554
- out_image, transformed = rasterio.mask.mask(
555
- src, shapes, crop=True, filled=True
556
- )
557
- out_profile = src.profile.copy()
558
- out_profile.update(
559
- {
560
- "width": out_image.shape[2],
561
- "height": out_image.shape[1],
562
- "transform": transformed,
563
- }
564
  )
565
- with rasterio.open(delta_raster, "w", **out_profile) as dst:
566
- dst.write(out_image)
567
- except Exception as e:
568
- print("Failed to crop delta raster", delta_raster)
569
- print(e)
570
-
571
- try:
572
- with rasterio.open(quant_raster) as src:
573
- out_image, transformed = rasterio.mask.mask(
574
- src, shapes, crop=True, filled=True
575
- )
576
- out_profile = src.profile.copy()
577
- out_profile.update(
578
- {
579
- "width": out_image.shape[2],
580
- "height": out_image.shape[1],
581
- "transform": transformed,
582
- }
583
  )
584
- with rasterio.open(quant_raster, "w", **out_profile) as dst:
585
- dst.write(out_image)
586
- except Exception as e:
587
- print("Failed to crop quantile raster", quant_raster)
588
- print(e)
 
 
 
 
 
 
 
589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
590
  except Exception as e:
591
- print("Could not open shape file:", shape_file)
592
- print(e)
593
 
594
  # current_data, _ = rasterio.mask.mask(current_data, shapes, crop=True)
595
  # historic_data, _ = rasterio.mask.mask(historic_data, shapes, crop=True)
@@ -663,6 +661,7 @@ def perform_analysis(
663
  agg_history: str = "mean",
664
  agg_window: str = "mean",
665
  comparison: str = "diff",
 
666
  **args,
667
  ) -> Dict[str, str]:
668
  """Perform the analysis.
@@ -708,6 +707,7 @@ def perform_analysis(
708
  layer=layer,
709
  output=output,
710
  match_raster=match_raster,
 
711
  )
712
  return files
713
 
 
72
  return geo_df
73
 
74
 
75
+ def find_match_raster(model_path, farm_name):
76
  # navigate one directory up in model_path
77
+ while not os.path.basename(model_path) == farm_name:
78
+ model_path = os.path.dirname(model_path)
79
+ root = model_path
80
  print("Looking for a matching raster in:", root)
81
  for root, dirs, _ in os.walk(root):
82
  # print(root,dirs,"\n\n")
 
91
  return None
92
 
93
 
94
+ def find_shape_file(model_path, farm_name):
95
+ # navigate one directory up in model_path until you reach the farm_name directory
96
+ while not os.path.basename(model_path) == farm_name:
97
+ model_path = os.path.dirname(model_path)
98
+ print("Looking for a shape file in:", model_path)
99
+ root = model_path
100
  print("Looking for a shape file in:", root)
101
  for root, dirs, files in os.walk(root):
102
  # print(root,dirs,"\n\n")
 
328
  output: str = None,
329
  agg_history: str = "mean",
330
  agg_window: str = "mean",
331
+ farm_name: str = None,
332
  **kwargs,
333
  ) -> Dict[str, str]:
334
  """Main function to run the script.
 
353
  Aggregation method to use for the historic years. Possible values: 'mean', 'median', 'max', 'min', 'std', None. Default: 'mean'.
354
  agg_window : str
355
  Aggregation method to use for the window. Possible values: 'mean', 'median', 'max', 'min', 'std', None. Default: 'mean'.
356
+ farm_name : str
357
+ Name of the farm. Should be provided. Default: None.
358
 
359
  Returns
360
  -------
 
370
  os.makedirs(output)
371
 
372
  if match_raster is None:
373
+ match_raster = find_match_raster(input, farm_name)
374
  print("match_raster is:", match_raster)
375
 
376
  if match_raster is not None:
 
475
  )
476
 
477
  # Search for a shape file ending with .shp
478
+ shape_file = find_shape_file(input, farm_name)
479
  print("Shape file:", shape_file)
480
 
481
  # Save the rasters
 
514
  try:
515
  with fiona.open(shape_file, "r") as shapefile:
516
  shapes = [feature["geometry"] for feature in shapefile]
517
+ except Exception as e:
518
+ print("Error reading shape file:", e)
519
 
520
+ try:
521
+ with rasterio.open(historic_raster) as src:
522
+ out_image, transformed = rasterio.mask.mask(
523
+ src, shapes, crop=True, filled=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524
  )
525
+ out_profile = src.profile.copy()
526
+ out_profile.update(
527
+ {
528
+ "width": out_image.shape[2],
529
+ "height": out_image.shape[1],
530
+ "transform": transformed,
531
+ }
532
+ )
533
+ with rasterio.open(historic_raster, "w", **out_profile) as dst:
534
+ dst.write(out_image)
535
+ except Exception as e:
536
+ print("Error clipping historic raster:", e)
537
+
538
+ try:
539
+ with rasterio.open(current_raster) as src:
540
+ out_image, transformed = rasterio.mask.mask(
541
+ src, shapes, crop=True, filled=True
 
542
  )
543
+ out_profile = src.profile.copy()
544
+ out_profile.update(
545
+ {
546
+ "width": out_image.shape[2],
547
+ "height": out_image.shape[1],
548
+ "transform": transformed,
549
+ }
550
+ )
551
+ with rasterio.open(current_raster, "w", **out_profile) as dst:
552
+ dst.write(out_image)
553
+ except Exception as e:
554
+ print("Error clipping current raster:", e)
555
+
556
+ try:
557
+ with rasterio.open(delta_raster) as src:
558
+ out_image, transformed = rasterio.mask.mask(
559
+ src, shapes, crop=True, filled=True
 
560
  )
561
+ out_profile = src.profile.copy()
562
+ out_profile.update(
563
+ {
564
+ "width": out_image.shape[2],
565
+ "height": out_image.shape[1],
566
+ "transform": transformed,
567
+ }
568
+ )
569
+ with rasterio.open(delta_raster, "w", **out_profile) as dst:
570
+ dst.write(out_image)
571
+ except Exception as e:
572
+ print("Error clipping delta raster:", e)
573
 
574
+ try:
575
+ with rasterio.open(quant_raster) as src:
576
+ out_image, transformed = rasterio.mask.mask(
577
+ src, shapes, crop=True, filled=True
578
+ )
579
+ out_profile = src.profile.copy()
580
+ out_profile.update(
581
+ {
582
+ "width": out_image.shape[2],
583
+ "height": out_image.shape[1],
584
+ "transform": transformed,
585
+ }
586
+ )
587
+ with rasterio.open(quant_raster, "w", **out_profile) as dst:
588
+ dst.write(out_image)
589
  except Exception as e:
590
+ print("Error clipping quantile raster:", e)
 
591
 
592
  # current_data, _ = rasterio.mask.mask(current_data, shapes, crop=True)
593
  # historic_data, _ = rasterio.mask.mask(historic_data, shapes, crop=True)
 
661
  agg_history: str = "mean",
662
  agg_window: str = "mean",
663
  comparison: str = "diff",
664
+ farm_name: str = None,
665
  **args,
666
  ) -> Dict[str, str]:
667
  """Perform the analysis.
 
707
  layer=layer,
708
  output=output,
709
  match_raster=match_raster,
710
+ farm_name=farm_name,
711
  )
712
  return files
713