예제 #1
0
파일: wdmutil.py 프로젝트: MachineAi/PyHSPF
    def open(self, wdmpath, mode):
        """Opens a WDM file for read/write, read-only, or overwrite. Returns 
        the FORTRAN file number."""

        options = {'rw': 0, 'r': 1, 'w': 2}

        ronwfg = options[mode]

        choice = {0: 'for modification', 1: 'as read-only', 2: 'as a new file'}

        if os.path.exists(wdmpath) and mode == 'w':
            if self.verbose:
                print('warning: file %s exists, overwriting\n' % wdmpath)
            os.remove(wdmpath)

        if wdmpath not in self.openfiles:
            if len(wdmpath) > 64:
                print('error, the file path must be 64 characters or less\n')
                raise

            retcode = hspf.wdbopnpy(len(self.openfiles) + 11, wdmpath, ronwfg)

            # this is a hack fix i've run into with some simulations

            if retcode == -5004:

                if self.verbose:

                    print('warning, file is already open')
                    print('trying to close open files\n')

                for i in range(100):
                    hspf.seqclose(i)

                retcode = hspf.wdbopnpy(
                    len(self.openfiles) + 11, wdmpath, ronwfg)

            if retcode == 0 and self.verbose:
                print('opened %s %s' % (wdmpath, choice[ronwfg]))
            if retcode == 1 and self.verbose:
                print('opened file %s' % wdmpath + ' but invalid filename')
            if retcode < 0:
                print('error: unable to open file {}\n'.format(wdmpath))
                raise
            self.openfiles[wdmpath] = len(self.openfiles) + 11

            # make a list of the dsns in file

            if ronwfg == 0 or ronwfg == 1:
                n = hspf.wddsnxpy(self.openfiles[wdmpath], 1)
                dsns = []
                while n != -1:
                    dsns.append(n)
                    n = hspf.wddsnxpy(self.openfiles[wdmpath], n + 1)
            else:
                dsns = []
            self.dsns[len(self.openfiles) + 10] = dsns

        return self.openfiles[wdmpath]
예제 #2
0
파일: wdmutil.py 프로젝트: eotp/PyHSPF
    def open(self, wdmpath, mode):
        """Opens a WDM file for read/write, read-only, or overwrite. Returns 
        the FORTRAN file number."""

        options = {'rw': 0, 'r': 1, 'w': 2}

        ronwfg  = options[mode]

        choice  = {0: 'for modification', 
                   1: 'as read-only', 
                   2: 'as a new file'}

        if os.path.exists(wdmpath) and mode == 'w':
            if self.verbose: 
                print('warning: file %s exists, overwriting\n' % wdmpath)
            os.remove(wdmpath)

        if wdmpath not in self.openfiles:
            if len(wdmpath) > 64: 
                print('error, the file path must be 64 characters or less\n')
                raise

            retcode = hspf.wdbopnpy(len(self.openfiles) + 11, wdmpath, ronwfg)

            # this is a hack fix i've run into with some simulations

            if retcode == -5004: 

                if self.verbose: 

                    print('warning, file is already open')
                    print('trying to close open files\n')

                for i in range(100): hspf.seqclose(i)

                retcode = hspf.wdbopnpy(len(self.openfiles) + 11, wdmpath, 
                                        ronwfg)

            if retcode == 0 and self.verbose: 
                print('opened %s %s' % (wdmpath, choice[ronwfg]))
            if retcode == 1 and self.verbose: 
                print('opened file %s' % wdmpath + ' but invalid filename')
            if retcode  < 0:
                print('error: unable to open file {}\n'.format(wdmpath))
                raise
            self.openfiles[wdmpath] = len(self.openfiles) + 11

            # make a list of the dsns in file

            if ronwfg == 0 or ronwfg == 1: 
                n = hspf.wddsnxpy(self.openfiles[wdmpath], 1)
                dsns = []
                while n != -1:
                    dsns.append(n)
                    n = hspf.wddsnxpy(self.openfiles[wdmpath], n + 1)
            else: dsns = []
            self.dsns[len(self.openfiles) + 10] = dsns

        return self.openfiles[wdmpath]