Search code examples
pythonflopy

PyEMU - add_parameters() from a MODFLOW6 2D cross section model


I am trying to setup parameters in a PyEMU setup, using external array files for a MODLFOW 6 model generated using Flopy. The model is a 2D cross section (50 layers, 1 row and 200 columns).

Using this code:

filenames = 'gwf.npf_k_layer1.txt'
par_name_base = 'k_layer_1'
pargrp = 'k_layer_1'

pf.add_parameters(filenames=filenames,par_type="grid",
                par_name_base=par_name_base,pargp=pargrp,
                upper_bound=10.,lower_bound=0.1,ult_ubound=100,ult_lbound=0.01)

I get the following error message:

2020-11-03 11:11:37.616421 starting: adding grid type multiplier style parameters for file(s) ['gwf.npf_k_layer1.txt']
2020-11-03 11:11:37.616421 starting: loading array p1_template\gwf.npf_k_layer1.txt
2020-11-03 11:11:37.618413 finished: loading array p1_template\gwf.npf_k_layer1.txt took: 0:00:00.001992
2020-11-03 11:11:37.618413 loaded array 'gwf.npf_k_layer1.txt' of shape (10, 20)
2020-11-03 11:11:37.620407 starting: writing array-based template file 'k_layer_1_inst0_grid.csv.tpl'
2020-11-03 11:11:37.621406 starting: writing template file k_layer_1_inst0_grid.csv.tpl for ['k_layer_1_inst:0']
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-10-d946b6aed7e4> in <module>
      6 pf.add_parameters(filenames=filenames,par_type="grid",
      7                 par_name_base=par_name_base,pargp=pargrp,
----> 8                 upper_bound=10.,lower_bound=0.1,ult_ubound=100,ult_lbound=0.01)

C:\ProgramData\Anaconda3\lib\site-packages\pyemu\utils\pst_from.py in add_parameters(self, filenames, par_type, zone_array, dist_type, sigma_range, upper_bound, lower_bound, transform, par_name_base, index_cols, use_cols, pargp, pp_space, use_pp_zones, num_eig_kl, spatial_reference, geostruct, datetime, mfile_fmt, mfile_skip, ult_ubound, ult_lbound, rebuild_pst, alt_inst_str, comment_char, par_style)
   1504                     gpname=pargp,
   1505                     input_filename=in_fileabs,
-> 1506                     par_style=par_style,
   1507                 )
   1508                 self.logger.log(

C:\ProgramData\Anaconda3\lib\site-packages\pyemu\utils\pst_from.py in write_array_tpl(name, tpl_filename, suffix, par_type, zone_array, gpname, shape, longnames, fill_value, get_xy, input_filename, par_style)
   2775                 else:
   2776                     if get_xy is not None:
-> 2777                         x, y = get_xy([i, j])
   2778                         xx.append(x)
   2779                         yy.append(y)

C:\ProgramData\Anaconda3\lib\site-packages\pyemu\utils\pst_from.py in _flopy_sr_get_xy(self, args, **kwargs)
    205         else:
    206             return (
--> 207                 self._spatial_reference.xcentergrid[i, j],
    208                 self._spatial_reference.ycentergrid[i, j],
    209             )

IndexError: index 1 is out of bounds for axis 0 with size 1

From the error message, I am assuming that PyEMU is trying to get the spatial reference for a cell in model row 2 (which doesn't exist).

I note that FloPy records the external file for a single model layer in a format of 20 columns and 10 rows (for a total of 200 model columns). PyEMU manages to write the first row of the corresponding .tpl file (i.e. the first 20 elements), but then throws the error message.

Is this to do with something incorrect in my Flopy\MF6 file setup? Or does PyEMU have an issue with reading models with a single row? Or am I making some stupid mistake somewhere?

UPDATE

After JDub's response below, attempted with reshaped external arrays in a 1x200 format (nrow X ncol).

Returns the following exception:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-13-242b6d0c98f3> in <module>
      6 pf.add_parameters(filenames=filenames,par_type="grid",
      7                 par_name_base=par_name_base,pargp=pargrp,
----> 8                 upper_bound=10.,lower_bound=0.1,ult_ubound=100,ult_lbound=0.01)

C:\ProgramData\Anaconda3\lib\site-packages\pyemu\utils\pst_from.py in add_parameters(self, filenames, par_type, zone_array, dist_type, sigma_range, upper_bound, lower_bound, transform, par_name_base, index_cols, use_cols, pargp, pp_space, use_pp_zones, num_eig_kl, spatial_reference, geostruct, datetime, mfile_fmt, mfile_skip, ult_ubound, ult_lbound, rebuild_pst, alt_inst_str, comment_char, par_style)
   1504                     gpname=pargp,
   1505                     input_filename=in_fileabs,
-> 1506                     par_style=par_style,
   1507                 )
   1508                 self.logger.log(

C:\ProgramData\Anaconda3\lib\site-packages\pyemu\utils\pst_from.py in write_array_tpl(name, tpl_filename, suffix, par_type, zone_array, gpname, shape, longnames, fill_value, get_xy, input_filename, par_style)
   2679     if len(shape) != 2:
   2680         raise Exception(
-> 2681             "write_array_tpl() error: shape '{0}' not 2D" "".format(str(shape))
   2682         )
   2683 

Exception: write_array_tpl() error: shape '(200,)' not 2D

Solution

  • I think the issue is the wrapped format "feature". The PstFrom.add_parameters() method is expecting the arrays to be in nrow X ncol and PstFrom doesn't know about this wrapped format "feature". So if you reshape that array to be 1 X 200, that should do it.