示例#1
0
    def read_and_save_output(self, ts_names, nts_names, save_file=None,
                             timesteps=None, save=False):
        """
        Reads in output files from this fulldomain and saves to a file. 

        NOTE THIS DOES NOT CURRENTLY WORK FOR STATION DATA! ONLY USE FOR GLOBAL
        DATA i.e files that are fort.*3 or fort.*4

        NOTE THIS DOES NOT CURRENTLY WORK FOR ANY NTS DATA EXCEPT FOR MAXELE

        :param list ts_names: names of ADCIRC timeseries
            output files to be recorded from each run
        :param list nts_names: names of ADCIRC non timeseries
            output files to be recorded from each run
        :param string save_file: name of file to save comparision matricies to
        :param int timesteps: number of timesteps to read from file
        :rtype: dict
        :returns: full_dict

        """
        
        if save_file is None:
            save_file = os.path.join(self.path, 'full.mat')
        fulldict = dict()

        # Get nts_error
        for fid in nts_names:
            key = fid.replace('.', '')
            fulldict[key] = output.get_nts_sr(self.path, self, fid)

        # Get ts_data
        for fid in ts_names:
            key = fid.replace('.', '')
            fulldict[key] = output.get_ts_sr(self.path,
                                             fid, timesteps=timesteps,
                                             ihot=self.ihot)[0]

        # fix dry nodes
        if fulldict.has_key('fort63'):
            fulldict['fort63'] = np.expand_dims(fulldict['fort63'], axis=2)
            fulldict = rmn.fix_dry_nodes(fulldict, self)
            fulldict['fort63'] = np.squeeze(fulldict['fort63'])
        # fix dry data
        if fulldict.has_key('fort61'):
            fulldict['fort61'] = np.expand_dims(fulldict['fort61'], axis=1)
            fulldict = rmn.fix_dry_data(fulldict, self)
            fulldict['fort61'] = np.squeeze(fulldict['fort61'])
        # fix dry nodes nts
        if fulldict.has_key('maxele63'):
            fulldict['maxele63'] = np.expand_dims(fulldict['maxele63'], axis=1)
            fulldict = rmn.fix_dry_nodes_nts(fulldict, self)
            fulldict['maxele63'] = np.squeeze(fulldict['maxele63'])

        if save:
            sio.savemat(save_file, fulldict, do_compression=True)

        return fulldict
示例#2
0
    def compare_to_fulldomain(self,
                              ts_names,
                              nts_names,
                              save_file=None,
                              timesteps=None,
                              savefull=True,
                              readmatfull=False,
                              savesub=True,
                              readmatsub=False,
                              fulldict=None):
        """
        Reads in output files from this subdomain and from it's fulldomain and
        compares them.

        NOTE THIS DOES NOT CURRENTLY WORK FOR STATION DATA! ONLY USE FOR GLOBAL
        DATA i.e files that are fort.*3 or fort.*4

        NOTE THIS DOES NOT CURRENTLY WORK FOR ANY NTS DATA EXCEPT FOR MAXELE

        comparision_data = fulldomain_data - subdomain_data

        :param list ts_names: names of ADCIRC timeseries
            output files to be recorded from each run
        :param list nts_names: names of ADCIRC non timeseries
            output files to be recorded from each run
        :param string save_file: name of file to save comparision matricies to
        :param int timesteps: number of timesteps to read from file
        :rtype: tuple
        :returns: (ts_error, nts_error, time_obs, ts_data, nts_data)

        """

        if save_file is None:
            save_file = os.path.join(self.path, 'compare_s2f.mat')

        full_file = os.path.join(self.fulldomain.path, 'full.mat')
        sub_file = os.path.join(self.path, 'sub.mat')

        # Save matricies to *.mat file for use by MATLAB or Python
        mdict = dict()
        if readmatsub:
            subdict = sio.loadmat(sub_file)
            savesub = False
        else:
            subdict = dict()

        if fulldict is None:
            if readmatfull:
                fulldict = sio.loadmat(full_file)
                savefull = False
            else:
                fulldict = dict()
        else:
            readmatfull = True
            savefull = False

        # Pre-allocate arrays for non-timeseries data
        nts_error = {}
        ts_error = {}
        time_obs = {}

        fulldom_nodes = [v - 1 for v in self.sub2full_node.values()]

        # Get nts_data
        for fid in nts_names:
            key = fid.replace('.', '')
            if not readmatfull:
                fulldict[key] = output.get_nts_sr(self.fulldomain.path,
                                                  self.fulldomain, fid)
            if not readmatsub:
                subdict[key] = output.get_nts_sr(self.path, self, fid)

        # Get ts_data
        for fid in ts_names:
            key = fid.replace('.', '')
            if not readmatsub:
                subdict[key], time_obs[key] = output.get_ts_sr(self.path,
                                                               fid,
                                                               True,
                                                               ihot=self.ihot)
                subdict[key + '_time'] = time_obs[key]
            if not readmatfull:
                fulldict[key] = output.get_ts_sr(self.fulldomain.path,
                                                 fid,
                                                 timesteps=timesteps,
                                                 ihot=self.fulldomain.ihot)[0]

        if not readmatsub:
            # fix dry nodes
            if subdict.has_key('fort63'):
                subdict['fort63'] = np.expand_dims(subdict['fort63'], axis=2)
                subdict = rmn.fix_dry_nodes(subdict, self)
                subdict['fort63'] = np.squeeze(subdict['fort63'])
            # fix dry data
            if subdict.has_key('fort61'):
                subdict['fort61'] = np.expand_dims(subdict['fort61'], axis=1)
                subdict = rmn.fix_dry_data(subdict, self)
                subdict['fort61'] = np.squeeze(subdict['fort61'])
            # fix dry nodes nts
            if subdict.has_key('maxele63'):
                subdict['maxele63'] = np.expand_dims(subdict['maxele63'],
                                                     axis=1)
                subdict = rmn.fix_dry_nodes_nts(subdict, self)
                subdict['maxele63'] = np.squeeze(subdict['maxele63'])

        if not readmatfull:
            # fix dry nodes
            if fulldict.has_key('fort63'):
                fulldict['fort63'] = np.expand_dims(fulldict['fort63'], axis=2)
                fulldict = rmn.fix_dry_nodes(fulldict, self)
                fulldict['fort63'] = np.squeeze(fulldict['fort63'])
            # fix dry data
            if fulldict.has_key('fort61'):
                fulldict['fort61'] = np.expand_dims(fulldict['fort61'], axis=1)
                fulldict = rmn.fix_dry_data(fulldict, self)
                fulldict['fort61'] = np.squeeze(fulldict['fort61'])
            # fix dry nodes nts
            if fulldict.has_key('maxele63'):
                fulldict['maxele63'] = np.expand_dims(fulldict['maxele63'],
                                                      axis=1)
                fulldict = rmn.fix_dry_nodes_nts(fulldict, self)
                fulldict['maxele63'] = np.squeeze(fulldict['maxele63'])

        # Get ts_error
        for fid in ts_names:
            key = fid.replace('.', '')
            sub_data, time_obs[key] = subdict[key], subdict[key + '_time']
            total_obs = sub_data.shape[1]
            if timesteps and timesteps < total_obs:
                total_obs = timesteps
            full_data = fulldict[key]
            if self.recording[key][2] == 1:
                full_data = full_data[fulldom_nodes, 0:total_obs]
            else:
                full_data = full_data[fulldom_nodes, 0:total_obs, :]
            ts_error[key] = (full_data - sub_data)
            if key == 'fort63' and (subdict.has_key('maxele63') or\
                    subdict.has_key('maxele.63')):
                nts_error['maxele63'] = np.max(full_data,
                                               axis=1) - subdict['maxele63']
            if key == 'fort64' and (subdict.has_key('maxvel63') or\
                    subdict.has_key('maxvel.63')):
                nts_error['maxvel63'] = np.max(
                    np.sqrt(full_data[:, :, 0]**2 + full_data[:, :, 1]**2),
                    axis=1) - subdict['maxvel63']

        # Get nts_error
        for fid in nts_names:
            key = fid.replace('.', '')
            if key != 'maxele63' and key != 'maxevel63':
                nts_error[key] = fulldict[key][fulldom_nodes] - subdict[key]

        # Update and save
        # export nontimeseries data
        for k, v in nts_error.iteritems():
            mdict[k] = v
            print k
            #b = np.ma.fix_invalid(v, fill_value=0)
            b = v
            print np.max(abs(b)), np.argmax(abs(b))
            print "Nodes above threshold", sum(np.abs(b) > 1e-2)
        # export timeseries data
        for k, v in ts_error.iteritems():
            mdict[k] = v
            print k
            #b = np.ma.fix_invalid(v, fill_value=0)
            b = v
            print np.max(abs(b)), np.argmax(abs(b))
            print "Nodes above threshold", sum(np.abs(b) > 1e-2)

        # export time_obs data
        for k, v in time_obs.iteritems():
            mdict[k + '_time'] = v

        sio.savemat(save_file, mdict, do_compression=True)
        if savesub:
            sio.savemat(sub_file, subdict, do_compression=True)
        if savefull:
            sio.savemat(full_file, fulldict, do_compression=True)

        return (ts_error, nts_error, time_obs, None, None)
示例#3
0
    def compare_to_fulldomain(self, ts_names, nts_names, save_file=None,
                              timesteps=None, savefull=True, readmatfull=False,
                              savesub=True, readmatsub=False, fulldict=None):
        """
        Reads in output files from this subdomain and from it's fulldomain and
        compares them.

        NOTE THIS DOES NOT CURRENTLY WORK FOR STATION DATA! ONLY USE FOR GLOBAL
        DATA i.e files that are fort.*3 or fort.*4

        NOTE THIS DOES NOT CURRENTLY WORK FOR ANY NTS DATA EXCEPT FOR MAXELE

        comparision_data = fulldomain_data - subdomain_data

        :param list ts_names: names of ADCIRC timeseries
            output files to be recorded from each run
        :param list nts_names: names of ADCIRC non timeseries
            output files to be recorded from each run
        :param string save_file: name of file to save comparision matricies to
        :param int timesteps: number of timesteps to read from file
        :rtype: tuple
        :returns: (ts_error, nts_error, time_obs, ts_data, nts_data)

        """
        
        if save_file is None:
            save_file = os.path.join(self.path, 'compare_s2f.mat')

        full_file = os.path.join(self.fulldomain.path, 'full.mat')
        sub_file = os.path.join(self.path, 'sub.mat')

        # Save matricies to *.mat file for use by MATLAB or Python
        mdict = dict()
        if readmatsub:
            subdict = sio.loadmat(sub_file)
            savesub = False
        else:
            subdict = dict()

        if fulldict is None:
            if readmatfull:
                fulldict = sio.loadmat(full_file)
                savefull = False
            else:
                fulldict = dict()
        else:
            readmatfull = True
            savefull = False

        # Pre-allocate arrays for non-timeseries data
        nts_error = {}
        ts_error = {}
        time_obs = {}

        fulldom_nodes = [v-1 for v in self.sub2full_node.values()]

        # Get nts_data
        for fid in nts_names:
            key = fid.replace('.', '')
            if not readmatfull:
                fulldict[key] = output.get_nts_sr(self.fulldomain.path,
                                                  self.fulldomain, fid)
            if not readmatsub:
                subdict[key] = output.get_nts_sr(self.path, self, fid)

        # Get ts_data
        for fid in ts_names:
            key = fid.replace('.', '')
            if not readmatsub:
                subdict[key], time_obs[key] = output.get_ts_sr(self.path, fid,
                                                               True,
                                                               ihot=self.ihot) 
                subdict[key+'_time'] = time_obs[key]
            if not readmatfull:
                fulldict[key] = output.get_ts_sr(self.fulldomain.path,
                                                 fid, timesteps=timesteps,
                                                 ihot=self.fulldomain.ihot)[0]
       
        if not readmatsub:
            # fix dry nodes
            if subdict.has_key('fort63'):
                subdict['fort63'] = np.expand_dims(subdict['fort63'], axis=2)
                subdict = rmn.fix_dry_nodes(subdict, self)
                subdict['fort63'] = np.squeeze(subdict['fort63'])
            # fix dry data
            if subdict.has_key('fort61'):
                subdict['fort61'] = np.expand_dims(subdict['fort61'], axis=1)
                subdict = rmn.fix_dry_data(subdict, self)
                subdict['fort61'] = np.squeeze(subdict['fort61'])
            # fix dry nodes nts
            if subdict.has_key('maxele63'):
                subdict['maxele63'] = np.expand_dims(subdict['maxele63'],
                                                     axis=1) 
                subdict = rmn.fix_dry_nodes_nts(subdict, self)
                subdict['maxele63'] = np.squeeze(subdict['maxele63'])


        if not readmatfull:
            # fix dry nodes
            if fulldict.has_key('fort63'):
                fulldict['fort63'] = np.expand_dims(fulldict['fort63'], axis=2)
                fulldict = rmn.fix_dry_nodes(fulldict, self)
                fulldict['fort63'] = np.squeeze(fulldict['fort63'])
            # fix dry data
            if fulldict.has_key('fort61'):
                fulldict['fort61'] = np.expand_dims(fulldict['fort61'], axis=1)
                fulldict = rmn.fix_dry_data(fulldict, self)
                fulldict['fort61'] = np.squeeze(fulldict['fort61'])
            # fix dry nodes nts
            if fulldict.has_key('maxele63'):
                fulldict['maxele63'] = np.expand_dims(fulldict['maxele63'], 
                                                      axis=1)
                fulldict = rmn.fix_dry_nodes_nts(fulldict, self)
                fulldict['maxele63'] = np.squeeze(fulldict['maxele63'])
    
        # Get ts_error
        for fid in ts_names:
            key = fid.replace('.', '')
            sub_data, time_obs[key] = subdict[key], subdict[key+'_time']
            total_obs = sub_data.shape[1]
            if timesteps and timesteps < total_obs:
                total_obs = timesteps
            full_data = fulldict[key]
            if self.recording[key][2] == 1:
                full_data = full_data[fulldom_nodes, 0:total_obs] 
            else:
                full_data = full_data[fulldom_nodes, 0:total_obs, :]
            ts_error[key] = (full_data - sub_data)
            if key == 'fort63' and (subdict.has_key('maxele63') or\
                    subdict.has_key('maxele.63')):
                nts_error['maxele63'] = np.max(full_data,
                                               axis=1) - subdict['maxele63']
            if key == 'fort64' and (subdict.has_key('maxvel63') or\
                    subdict.has_key('maxvel.63')):
                nts_error['maxvel63'] = np.max(np.sqrt(full_data[:, :, 0]**2 +
                                                       full_data[:, :, 1]**2), 
                                               axis=1) - subdict['maxvel63']
        
        # Get nts_error
        for fid in nts_names:
            key = fid.replace('.', '')
            if key != 'maxele63' and key != 'maxevel63':
                nts_error[key] = fulldict[key][fulldom_nodes] - subdict[key]
        
        # Update and save
        # export nontimeseries data
        for k, v in nts_error.iteritems():
            mdict[k] = v
            print k
            #b = np.ma.fix_invalid(v, fill_value=0)
            b = v
            print np.max(abs(b)), np.argmax(abs(b))
            print "Nodes above threshold", sum(np.abs(b) > 1e-2)
        # export timeseries data
        for k, v in ts_error.iteritems():
            mdict[k] = v
            print k
            #b = np.ma.fix_invalid(v, fill_value=0)
            b = v
            print np.max(abs(b)), np.argmax(abs(b))
            print "Nodes above threshold", sum(np.abs(b) > 1e-2)

        # export time_obs data
        for k, v in time_obs.iteritems():
            mdict[k+'_time'] = v

        sio.savemat(save_file, mdict, do_compression=True)
        if savesub:
            sio.savemat(sub_file, subdict, do_compression=True)
        if savefull:
            sio.savemat(full_file, fulldict, do_compression=True)

        return (ts_error, nts_error, time_obs, None, None)