#!/usr/bin/perl -w

use strict;
use lib '/pm/grflib/perl','/pm/grflib/lib/perl5';
use Spreadsheet::ParseXLSX;
use Shape;
use Polygon;

my $parser = Spreadsheet::ParseXLSX->new();
my ($xls,$wks);

#
#  Get cell contents
#
sub get
{
   my ($wks,$row,$col) = @_;
   my $cell = $wks->get_cell($row,$col);
   return defined($cell) ? $cell->value() : undef;
}

#
#  Meter data
#
open(DAT , '>../data/co/2025.meter') || die "Cannot open file ../data/co/2025.meter\n";
print DAT "Use,County,WDID,AF_Pumped,Type,Acres,UTMx,UTMy\n";
$xls = $parser->parse('Repub_Irrig_2025.xlsx') || die "Cannot open file Repub_Irrig_2025.xlsx\n";
#  Irrigation and M&I wells
$wks = $xls->worksheet('Repub_Irrig_2025') || die "Cannot open sheet Repub_Irrig_2025\n";
my $vol=0;
for (my $row=1;;$row++)
{
   my $wdid = get($wks,$row,0);
   defined($wdid) || last;
   my $src  = get($wks,$row,1);
   my $use  = get($wks,$row,2);
   my $af   = get($wks,$row,5);
   #  Groundwater only
   $src && $use || next;
   ($src ne 'S:3') && next;
   #  Skip no pumping
   $af || next;
   my $acre = get($wks,$row,6);
   my $cty  = get($wks,$row,7);
   my $Xutm = get($wks,$row,8);
   my $Yutm = get($wks,$row,9);
   #  Set acres  to 0 for M&I
#  ($acre =~ /N\/A/) && ($acre = 0);
   #  Omit Storage  and M&I<50af
   ($use ne 'U:1' && $af<50) && next;
   #  Save
   $use =~ s/U://;
   print DAT "$use,$cty,$wdid,$af,SPRINKLER,$acre,$Xutm,$Yutm\n";
   $vol += $af;
}
warn "VOL=$vol af\n";
