def processAlgorithm(self, progress):
        PEST_mode = self.getParameterValue(MDWF_Calibrate_d.PEST_mode)
        SRC_FOLDER = self.getParameterValue(MDWF_Calibrate_d.SRC_FOLDER)
        CONTROL_FILE = self.getParameterValue(MDWF_Calibrate_d.CONTROL_FILE)
        CONTROL_FILE = os.path.split(CONTROL_FILE)[1]
        pestspecs = SWAT_PEST_specs()

        # Writing PEST batch file
        batname = SRC_FOLDER + os.sep + 'WOIS_PEST_run.bat'
        batfile = open(batname, 'w')
        batfile.writelines('@ECHO OFF\r\n')
        batfile.writelines('cd ' + SRC_FOLDER + '\r\n')
        if PEST_mode == 0:
            batfile.writelines('"' + pestspecs.PESTexeFolder + os.sep +
                               'pest.exe" ' + CONTROL_FILE + '\r\n')
        elif PEST_mode == 1:
            batfile.writelines('"' + pestspecs.PESTexeFolder + os.sep +
                               'sceua_p.exe" ' + CONTROL_FILE + '\r\n')
        else:
            batfile.writelines('"' + pestspecs.PESTexeFolder + os.sep +
                               'cmaes_p.exe" ' + CONTROL_FILE + '\r\n')
        batfile.writelines('@PAUSE\r\n')
        batfile.close()

        # Running PEST
        currpath = os.getcwd()
        os.chdir(SRC_FOLDER)
        runres = subprocess.call(batname)
        os.chdir(currpath)
    def processAlgorithm(self, progress):
        SRC_FOLDER = self.getParameterValue(MDWF_Sensan_c.SRC_FOLDER)
        CONTROL_FILE = self.getParameterValue(MDWF_Sensan_c.CONTROL_FILE)
        CONTROL_FILE = os.path.split(CONTROL_FILE)[1]
        pestspecs = SWAT_PEST_specs()

        # Writing PEST batch file
        batname = SRC_FOLDER + os.sep + 'WOIS_SENSAN_run.bat'
        batfile = open(batname,'w')
        batfile.writelines('@ECHO OFF\r\n')
        batfile.writelines('cd ' + SRC_FOLDER + '\r\n')
        batfile.writelines('"'+pestspecs.PESTexeFolder + os.sep + 'sensan.exe" ' + CONTROL_FILE + '\r\n')
        batfile.writelines('@PAUSE\r\n')
        batfile.close()

        # Running SENSAN
        currpath = os.getcwd()
        os.chdir(SRC_FOLDER)
        runres = subprocess.call(batname)
        os.chdir(currpath)
Esempio n. 3
0
 def processAlgorithm(self, progress):
     pestspecs = SWAT_PEST_specs()
     parspecs = SWAT_parameter_specs()
     MODEL_FILE = self.getParameterValue(MDWF_Calibrate_c.MODEL_FILE)
     SRC_FOLDER = self.getParameterValue(MDWF_Calibrate_c.SRC_FOLDER)
     OBGNME = self.getParameterValue(MDWF_Calibrate_c.OBGNME)
     SWAT_EXE = self.getParameterValue(MDWF_Calibrate_c.SWAT_EXE)
     model = ModelFile(MODEL_FILE)
     ctlfilename = SRC_FOLDER + os.sep + model.desc['ModelName'] + '.pst'
     ctlfile = open(ctlfilename, 'w')
     ctlfile.writelines(pestspecs.CFfirstline + '\n')
     ctlfile.writelines('* control data\n')
     ctlfile.writelines(pestspecs.RSTFLE + ' ' + pestspecs.PESTMODE + '\n')
     #find number of parameters and prepare the parameter block
     filelist = os.listdir(SRC_FOLDER)
     NPAR = 0
     PARBLOCK = []
     for f in filelist:
         if '.pbf' in f:
             NPAR = NPAR + 1
             PARBLOCK.append(
                 open(SRC_FOLDER + os.sep + f).readlines()[0] + '\n')
     #find number of observations and prepare the observation block
     OBSBLOCK = []
     for f in filelist:
         if 'observation_block.obf' in f:
             cobsblock = open(SRC_FOLDER + os.sep + f).readlines()
             for i in range(0, len(cobsblock)):
                 OBSBLOCK.append(cobsblock[i])
     NOBS = len(OBSBLOCK)
     if NPAR > 0 and NOBS > 0:
         ctlfile.writelines(
             str(NPAR).ljust(4) + str(NOBS).ljust(7) +
             str(pestspecs.NPARGP).ljust(4) +
             str(pestspecs.NPRIOR).ljust(4) +
             str(pestspecs.NOBSGP).ljust(4) + '\n')
     else:
         raise GeoAlgorithmExecutionException(
             'Number of observations and number of parameters must be larger than zero'
         )
     #find number of template files and prepare template block
     NTPLFLE = 0
     TPLBLOCK = []
     for f in filelist:
         if '.tpl' in f:
             TPLBLOCK.append(f + ' ' + f.split('.')[0].split('_')[0] + '.' +
                             f.split('.')[0].split('_')[1] + '\n')
             NTPLFLE = NTPLFLE + 1
     #find number of instruction files and prepare instruction block
     NINSFLE = 0
     INSBLOCK = []
     for f in filelist:
         if '.ins' in f:
             INSBLOCK.append(f + ' output.rch\n')
             NINSFLE = NINSFLE + 1
     if NTPLFLE > 0 and NINSFLE > 0:
         ctlfile.writelines(
             str(NTPLFLE).ljust(4) + str(NINSFLE).ljust(4) +
             str(pestspecs.PRECIS).ljust(7) +
             str(pestspecs.DPOINT).ljust(8) +
             str(pestspecs.NUMCOM).ljust(3) +
             str(pestspecs.JACFILE).ljust(3) +
             str(pestspecs.MESSFILE).ljust(3) + '\n')
     else:
         raise GeoAlgorithmExecutionException(
             'Number of template files and number of instruction files must be larger than zero'
         )
     ctlfile.writelines(
         str(pestspecs.RLAMBDA1).ljust(6) +
         str(pestspecs.RLAMFAC).ljust(6) +
         str(pestspecs.PHIRATSUF).ljust(6) +
         str(pestspecs.PHIREDLAM).ljust(6) + str(pestspecs.NUMLAM) + '\n')
     ctlfile.writelines(
         str(pestspecs.RELPARMAX).ljust(6) +
         str(pestspecs.FACPARMAX).ljust(6) + str(pestspecs.FACORIG) + '\n')
     ctlfile.writelines(str(pestspecs.PHIREDSWH) + '\n')
     ctlfile.writelines(
         str(pestspecs.NOPTMAX).ljust(5) +
         str(pestspecs.PHIREDSTP).ljust(7) +
         str(pestspecs.NPHISTP).ljust(4) +
         str(pestspecs.NPHINORED).ljust(4) +
         str(pestspecs.RELPARSTP).ljust(7) + str(pestspecs.NRELPAR) + '\n')
     ctlfile.writelines(
         str(pestspecs.ICOV).ljust(3) + str(pestspecs.ICOR).ljust(3) +
         str(pestspecs.IEIG) + '\n')
     ctlfile.writelines('* parameter groups\n')
     for i in range(0, pestspecs.NPARGP):
         ctlfile.writelines(parspecs.PARAMETERS[i].ljust(13) +
                            parspecs.INCTYP[i].ljust(9) +
                            str(parspecs.DERINC[i]).ljust(7) +
                            str(parspecs.DERINCLB[i]).ljust(7) +
                            parspecs.FORCEN[i].ljust(9) +
                            str(parspecs.DERINCMUL[i]).ljust(7) +
                            parspecs.DERMTHD[i] + '\n')
     ctlfile.writelines('* parameter data\n')
     ctlfile.writelines(PARBLOCK)
     ctlfile.writelines('* observation groups\n')
     ctlfile.writelines(OBGNME + '\n')
     ctlfile.writelines('* observation data\n')
     ctlfile.writelines(OBSBLOCK)
     ctlfile.writelines('* model command line\n')
     ctlfile.writelines('pest_swatrun.bat\n')
     batfile = open(SRC_FOLDER + os.sep + 'pest_swatrun.bat', 'w')
     batfile.writelines('@ECHO OFF \n')
     batfile.writelines('cd ' + SRC_FOLDER + '\n')
     batfile.writelines(SWAT_EXE + '> nul\n')
     batfile.close()
     ctlfile.writelines('* model input/output\n')
     ctlfile.writelines(TPLBLOCK)
     ctlfile.writelines(INSBLOCK)
     ctlfile.close()
def create_PEST_template(src_folder, SWATparameter, parameter_name, subid,
                         hruid, PARVAL1, PARLBND, PARUBND):
    if (SWATparameter[0:5] == 'RFINC') | (SWATparameter[0:6] == 'TMPINC'):
        if len(parameter_name) > 6:
            raise GeoAlgorithmExecutionException(
                'Parameter name for ' + SWATparameter +
                ' should have maximum 6 characters.')
    if len(parameter_name) > 12:
        raise GeoAlgorithmExecutionException(
            'Parameter name for ' + SWATparameter +
            ' should have maximum 12 characters.')

    specs = SWAT_parameter_specs()
    pestspecs = SWAT_PEST_specs()
    pars = numpy.array(specs.PARAMETERS)
    index = int(numpy.where(pars == SWATparameter)[0])
    if specs.PARLEVELS[index] == 'bsn':
        SWAT_filename = 'basins.bsn'
        TEMPLATE_filename = 'basins_bsn.tpl'
    elif specs.PARLEVELS[index] == 'sub':
        SWAT_filename = "%05d" % subid + '0000.' + specs.PARFILES[index]
        TEMPLATE_filename = "%05d" % subid + '0000_' + specs.PARFILES[
            index] + '.tpl'
    elif specs.PARLEVELS[index] == 'hru':
        SWAT_filename = "%05d" % subid + "%04d" % hruid + '.' + specs.PARFILES[
            index]
        TEMPLATE_filename = "%05d" % subid + "%04d" % hruid + '_' + specs.PARFILES[
            index] + '.tpl'

    SWAT_filename = src_folder + os.sep + SWAT_filename
    TEMPLATE_filename = src_folder + os.sep + TEMPLATE_filename
    if os.path.isfile(SWAT_filename):
        if os.path.isfile(TEMPLATE_filename):
            lines = open(TEMPLATE_filename, 'r').readlines()
            changeline = lines[specs.PARLINES[index]]
            if (SWATparameter[0:5] == 'RFINC') | (
                    SWATparameter[0:6]
                    == 'TMPINC') | (SWATparameter[0:5] == 'SOL_Z') | (
                        SWATparameter[0:7] == 'SOL_AWC') | (SWATparameter[0:5]
                                                            == 'SOL_K'):
                lines[specs.PARLINES[index]] = changeline[:specs.PARSTARTCOL[
                    index] - 1] + pestspecs.ptf + parameter_name.rjust(
                        specs.PARENDCOL[index] - specs.PARSTARTCOL[index] -
                        1) + pestspecs.ptf + changeline[
                            specs.PARENDCOL[index]:len(changeline)]
            else:
                lines[specs.
                      PARLINES[index]] = pestspecs.ptf + parameter_name.rjust(
                          specs.PARENDCOL[index] - specs.PARSTARTCOL[index]
                      ) + pestspecs.ptf + changeline[specs.PARENDCOL[index] +
                                                     1:len(changeline)]
            ntfile = open(TEMPLATE_filename, 'w')
            ntfile.writelines(lines)
            ntfile.close()
        else:
            line1 = 'ptf ' + pestspecs.ptf + '\n'
            lines = open(SWAT_filename).readlines()
            changeline = lines[specs.PARLINES[index] - 1]
            if (SWATparameter[0:5] == 'RFINC') | (
                    SWATparameter[0:6]
                    == 'TMPINC') | (SWATparameter[0:5] == 'SOL_Z') | (
                        SWATparameter[0:7] == 'SOL_AWC') | (SWATparameter[0:5]
                                                            == 'SOL_K'):
                lines[
                    specs.PARLINES[index] -
                    1] = changeline[:specs.PARSTARTCOL[index] -
                                    1] + pestspecs.ptf + parameter_name.rjust(
                                        specs.PARENDCOL[index] -
                                        specs.PARSTARTCOL[index] - 1
                                    ) + pestspecs.ptf + changeline[
                                        specs.PARENDCOL[index]:len(changeline)]
            else:
                lines[specs.PARLINES[index] -
                      1] = pestspecs.ptf + parameter_name.rjust(
                          specs.PARENDCOL[index] - specs.PARSTARTCOL[index]
                      ) + pestspecs.ptf + changeline[specs.PARENDCOL[index] +
                                                     1:len(changeline)]
            ntfile = open(TEMPLATE_filename, 'w')
            ntfile.writelines(line1)
            ntfile.writelines(lines)
            ntfile.close()
        parblockname = src_folder + os.sep + parameter_name + '.pbf'
        parblockfile = open(parblockname, 'w')
        parblockfile.writelines(
            parameter_name.ljust(13) + specs.PARTRANS[index].ljust(6) +
            specs.PARCHGLIM[index].ljust(9) + str(PARVAL1).ljust(9) +
            str(PARLBND).ljust(9) + str(PARUBND).ljust(9) +
            SWATparameter.ljust(13) + specs.SCALE[index].ljust(5) +
            specs.OFFSET[index].ljust(5) + specs.DERCOM[index])
        parblockfile.close()
    else:
        raise GeoAlgorithmExecutionException('SWAT input file ' +
                                             SWAT_filename +
                                             ' not found in source directory')
    return TEMPLATE_filename, SWAT_filename
def create_PEST_instruction(src_folder, obsfile, obsgroup, nreaches, tmpres):
    specs = SWAT_output_format_specs()
    pestspecs = SWAT_PEST_specs()
    obsdata = numpy.genfromtxt(obsfile,
                               delimiter=',',
                               skiprows=0,
                               missing_values='NaN')
    obsdata[:, 0] = obsdata[:, 0] + specs.PYEX_DATE_OFFSET
    SWAT_time_info = read_SWAT_time(src_folder)
    startyr = datetime.date(int(SWAT_time_info[1]), 1, 1)
    startdate = startyr + datetime.timedelta(days=int(SWAT_time_info[2]) - 1)
    if SWAT_time_info[4] > 0:
        startdate = datetime.date(int(SWAT_time_info[1] + SWAT_time_info[4]),
                                  1, 1)
    endyear = datetime.date(int(SWAT_time_info[1] + SWAT_time_info[0] - 1), 1,
                            1)
    enddate = endyear + datetime.timedelta(days=int(SWAT_time_info[3]) - 1)
    insname = obsfile.split('.')[0] + '_' + obsfile.split('.')[1] + '.ins'
    insfile = open(insname, 'w')
    insfile.writelines('pif ' + pestspecs.pif + '\n')
    insfile.writelines('l9\n')
    obsblockname = obsfile.split('.')[0] + '_' + obsfile.split(
        '.')[1] + '_observation_block.obf'
    obsblockfile = open(obsblockname, 'w')
    obsid = 1
    if tmpres == 0:
        tsteps = int((enddate - startdate).days) + 1
        for i in range(0, tsteps):
            currentdate = startdate + datetime.timedelta(i)
            currentdatenum = matplotlib.dates.date2num(currentdate)
            obs = obsdata[obsdata[:, 0] == currentdatenum, 1]
            obsw = obsdata[obsdata[:, 0] == currentdatenum, 2]
            obsw = 1. / numpy.power(obsw, 2)
            try:
                reachid = int(obsdata[obsdata[:, 0] == currentdatenum, 3][0])
            except:
                pass
            if ~numpy.isnan(obs):
                obsblockfile.writelines('reach_' + str(reachid) + '_' +
                                        str(obsid) + ' ' + str(obs[0]) + ' ' +
                                        str(obsw[0]) + ' ' + obsgroup + '\n')
                insfile.writelines('l' + str(reachid) + ' [' + 'reach_' +
                                   str(reachid) + '_' + str(obsid) + ']' +
                                   str(pestspecs.flowoutinicol) + ':' +
                                   str(pestspecs.flowoutendcol) + '\n')
                if reachid < nreaches:
                    insfile.writelines('l' + str(nreaches - reachid) + '\n')
                obsid = obsid + 1
            else:
                insfile.writelines('l' + str(nreaches) + '\n')
    elif tmpres == 1:
        tsteps = int((enddate - startdate).days) + 1
        currentyear = startdate
        while (currentyear <= endyear):
            if currentyear == endyear:
                for m in range(0, enddate.month):
                    currentdate = currentyear + relativedelta(months=+m)
                    currentdatenum = matplotlib.dates.date2num(currentdate)
                    obs = obsdata[obsdata[:, 0] == currentdatenum, 1]
                    obsw = obsdata[obsdata[:, 0] == currentdatenum, 2]
                    obsw = 1. / numpy.power(obsw, 2)
                    try:
                        reachid = int(obsdata[obsdata[:, 0] == currentdatenum,
                                              3][0])
                    except:
                        pass
                    if ~numpy.isnan(obs):
                        obsblockfile.writelines('reach_' + str(reachid) + '_' +
                                                str(obsid) + ' ' +
                                                str(obs[0]) + ' ' +
                                                str(obsw[0]) + ' ' + obsgroup +
                                                '\n')
                        insfile.writelines('l' + str(reachid) + ' [' +
                                           'reach_' + str(reachid) + '_' +
                                           str(obsid) + ']' + str(50) + ':' +
                                           str(61) + '\n')
                        if reachid < nreaches:
                            insfile.writelines('l' + str(nreaches - reachid) +
                                               '\n')
                        obsid = obsid + 1
                    else:
                        insfile.writelines('l' + str(nreaches) + '\n')
                insfile.writelines('l' + str(nreaches) + '\n')
                currentyear = currentyear + relativedelta(years=+1)
            else:
                for m in range(0, 12):
                    currentdate = currentyear + relativedelta(months=+m)
                    currentdatenum = matplotlib.dates.date2num(currentdate)
                    obs = obsdata[obsdata[:, 0] == currentdatenum, 1]
                    obsw = obsdata[obsdata[:, 0] == currentdatenum, 2]
                    obsw = 1. / numpy.power(obsw, 2)
                    try:
                        reachid = int(obsdata[obsdata[:, 0] == currentdatenum,
                                              3][0])
                    except:
                        pass
                    if ~numpy.isnan(obs):
                        obsblockfile.writelines('reach_' + str(reachid) + '_' +
                                                str(obsid) + ' ' +
                                                str(obs[0]) + ' ' +
                                                str(obsw[0]) + ' ' + obsgroup +
                                                '\n')
                        insfile.writelines('l' + str(reachid) + ' [' +
                                           'reach_' + str(reachid) + '_' +
                                           str(obsid) + ']' + str(50) + ':' +
                                           str(61) + '\n')
                        if reachid < nreaches:
                            insfile.writelines('l' + str(nreaches - reachid) +
                                               '\n')
                        obsid = obsid + 1
                    else:
                        insfile.writelines('l' + str(nreaches) + '\n')
                insfile.writelines('l' + str(nreaches) + '\n')
                currentyear = currentyear + relativedelta(years=+1)

    return insname, obsblockname