Example #1
0
def test_adhmodel_fromnnetcdf():
    # set test project name
    project_name = 'SanDiego'
    # set project directory
    path = os.path.join(TESTDIR, 'test_files', project_name)
    # instantiate empty adh model object
    adhmod = AdhModel()
    # read model files
    adhmod.from_netcdf(path=path, project_name=project_name, crs=None)
Example #2
0
class LoadModel(param.Parameterized):
    """
    Parameterized class to load model information

    Formatted for Panel and Pipeline Page
    """
    load_sim_widget = param.ClassSelector(default=LoadSimulation(),
                                          class_=LoadSimulation)
    att_widget = param.ClassSelector(default=Attributes(), class_=Attributes)
    projection = param.ClassSelector(default=Projection(), class_=Projection)

    load_data = param.Action(lambda self: self.param.trigger('load_data'),
                             label='Load Data',
                             precedence=0.2)

    label = param.String(default='Basic Input', precedence=-1)

    def __init__(self, **params):
        super(LoadModel, self).__init__(**params)
        self.dat_files = []

        self.model = None

        self.adh_mod = AdhModel()

    # output the adh_viz object
    # @param.output(annot=AdhAnnotator)
    @param.output(adh_mod=AdhModel)
    def output(self):
        return self._load_data()

    @param.depends('load_sim_widget.load_netcdf',
                   'load_sim_widget.adh_root_filename',
                   'load_sim_widget.adh_directory',
                   watch=True)
    def available_attributes(self):
        # todo this needs a 'no files found' check
        att_list = []
        # enable the projections
        self.projection.set_constant(value=False)

        # if netcdf is selected
        if self.load_sim_widget.load_netcdf:
            filename = os.path.join(
                self.load_sim_widget.adh_directory,
                self.load_sim_widget.adh_root_filename + '.nc')

            try:
                # open the xarray dataset (does not load into memory)
                ncdf = xr.open_dataset(filename)

            except FileNotFoundError:
                print('File Not Found: {}'.format(filename))
            else:
                # look for crs in the file
                if 'crs' in ncdf.attrs.keys():
                    # set the projection in the widget
                    self.projection.set_crs(get_crs(ncdf))
                    # disable the widget
                    self.projection.set_constant(value=True)

                with ncdf:
                    # enable and add all variables in the netcdf file
                    for var in ncdf.data_vars:
                        att_name = var.lower().replace(" ", "_")

                        # if the variable has results dimensions
                        if 'times' in ncdf[var].dims and 'nodes_ids' in ncdf[
                                var].dims:
                            if att_name == 'depth-averaged_velocity':
                                att_list.append('velocity')
                            elif att_name == 'error':
                                att_list.append('error_hydro')
                            else:
                                # add to the list
                                att_list.append(att_name)
                # close the dataset
                ncdf.close()

        # otherwise read from available *.dat files
        else:
            # get the list of filenames  # todo this isn't foolproof e.g. `SanDieg` finds files
            file_names = glob.glob(
                os.path.join(self.load_sim_widget.adh_directory,
                             self.load_sim_widget.adh_root_filename +
                             '_*.dat'))

            # convert file suffixes to a list of widgets
            att_list = [
                self.att_widget.suffix_to_widget[get_variable_from_file_name(
                    x)] for x in file_names
            ]

        # # dictionary for naming inconsistencies # todo add complexity for err_con in future
        # label_to_widget = {'error': 'error_hydro',
        #                    'depth-averaged_velocity': 'velocity'}
        #
        # # loop over the inconsistencies
        # for key in label_to_widget.keys():
        #     # if this is found in the list
        #     if key in att_list:
        #         # remove the key
        #         att_list.remove(key)
        #         # add back in the corrected value
        #         att_list.append(label_to_widget[key])

        # adjust available attribute widgets based on available data
        for p in self.att_widget.param:
            # if this attribute wasn't in the list
            pobj = self.att_widget.param[p]
            if p in att_list:
                # ensure the widget is enabled
                pobj.constant = False
                # set the widget value
                setattr(self.att_widget, p, True)
            elif p != 'name':
                # ensure the widget is enabled
                pobj.constant = False
                # uncheck the attribute
                setattr(self.att_widget, p, False)
                # disable attribute
                pobj.constant = True

    @param.depends('load_data', watch=True)
    def _load_data(self):
        # if file is netcdf
        if self.load_sim_widget.load_netcdf:

            self.adh_mod.from_netcdf(
                path=self.load_sim_widget.adh_directory,
                project_name=self.load_sim_widget.adh_root_filename,
                crs=self.projection.get_crs())
        # request to load data from *dat files:
        else:
            # get a list of requested suffix strings
            slist = self.att_widget.suffix_list(value=True)

            # construct list of filenames
            fnames = []
            [
                fnames.append(
                    os.path.join(
                        self.load_sim_widget.adh_directory,
                        self.load_sim_widget.adh_root_filename + '_' + x +
                        '.dat')) for x in slist
            ]
            # read the requested files
            self.adh_mod.from_ascii(
                path=self.load_sim_widget.adh_directory,
                project_name=self.load_sim_widget.adh_root_filename,
                crs=self.projection.get_crs(),
                file_names=fnames)

        return self.adh_mod

    # visualize the page
    def panel(self):
        self.available_attributes()  # for the initial load
        return pn.Column(
            pn.Row(pn.Param(self.load_sim_widget, show_name=False),
                   pn.Param(self.att_widget, show_name=False),
                   pn.Param(self.projection, show_name=False),
                   name=self.label),
            pn.Pane(self.param, parameters=['load_data'], show_name=False))