def swat_prepare_pest_instruction_file(oPest_in, oModel_in): """ prepare pest instruction file """ sWorkspace_scratch = oModel_in.sWorkspace_scratch sWorkspace_data = oModel_in.sWorkspace_data sWorkspace_project = oModel_in.sWorkspace_project sRegion = oModel_in.sRegion sModel = oModel_in.sModel sWorkspace_data_project = sWorkspace_data + slash + sWorkspace_project sWorkspace_calibration_case = oModel_in.sWorkspace_calibration_case sWorkspace_pest_model = sWorkspace_calibration_case sWorkspace_simulation_copy = oModel_in.sWorkspace_simulation_copy iYear_start = oModel_in.iYear_start iYear_end = oModel_in.iYear_end #nsegment = oModel_in.nsegment nstress = oModel_in.nstress sFilename_observation = sWorkspace_data_project + slash + 'auxiliary' + slash \ + 'usgs' + slash + 'discharge' + slash + 'discharge_observation.txt' if os.path.isfile(sFilename_observation): pass else: print(sFilename_observation + ' is missing!') return aData = text_reader_string(sFilename_observation) aDischarge_observation = array(aData).astype(float) nobs_with_missing_value = len(aDischarge_observation) aDischarge_observation = np.reshape(aDischarge_observation, nobs_with_missing_value) nan_index = np.where(aDischarge_observation == missing_value) #write instruction sFilename_instruction = sWorkspace_pest_model + slash + oPest_in.sFilename_instruction ofs = open(sFilename_instruction, 'w') ofs.write('pif $\n') #we need to consider that there is missing value in the observations for i in range(0, nstress): dDummy = aDischarge_observation[i] if (dDummy != missing_value): sLine = 'l1' + ' !discharge' + "{:04d}".format(i + 1) + '!\n' else: sLine = 'l1' + ' !dum' + '!\n' ofs.write(sLine) ofs.close() print('The instruction file is prepared successfully!')
def maces_convert_omac_parameter_file(oPest_in, oModel_in): sWorkspace_pest_model = oPest_in.sWorkspace_pest sWorkspace_calibration_case = oModel_in.sWorkspace_calibration_case iFlag_debug = 0 if(iFlag_debug == 1 ): sPath_current = sWorkspace_calibration_case + slash + 'child1' else: sPath_current = os.getcwd() sFilename_parameter = sPath_current + slash + oModel_in.sFilename_parameter_omac sFilename_config = sPath_current + slash + os.path.basename(oModel_in.sFilename_config_omac) if os.path.isfile(sFilename_parameter): pass else: print('The file does not exist!'+ sFilename_parameter) return aData_all = text_reader_string(sFilename_parameter, cDelimiter_in = ',') aDummy = aData_all[:,0] nParameter = len(aDummy) aParameter_list = aDummy ngrid = 1 aParameter_value = (aData_all[:, 1]).astype(float) aParameter_value = np.asarray(aParameter_value) #second we will call the existing python function to convert #construct the command string #./xmlchange.py -f optpar_minac.xml -g M12MOD -p rhoSed -v 2650.0 #aParameter = ['d50'] for p in range(0, nParameter): sValue = "{:16.2f}".format( aParameter_value[p] ) #sCommand = '-f' + sFilename + ' -g M12MOD -p rhoSed -v ' + sValue xmlchange(filename=sFilename_config, group='M12MOD',parameter=aParameter_list[p],value=sValue.strip()) return
def swat_prepare_pest_hru_template_file(oPest_in, oModel_in): """ #prepare the pest template file """ #strings sWorkspace_data = oModel_in.sWorkspace_data sWorkspace_scratch = oModel_in.sWorkspace_scratch sWorkspace_project = oModel_in.sWorkspace_project sWorkspace_data_project = sWorkspace_data + slash + sWorkspace_project sWorkspace_calibration_case = oModel_in.sWorkspace_calibration_case sWorkspace_pest_model = sWorkspace_calibration_case #read hru type sFilename_hru_combination = sWorkspace_data_project + slash + 'auxiliary' + slash\ + 'hru' +slash + 'hru_combination.txt' aData_all = text_reader_string(sFilename_hru_combination) nhru_type = len(aData_all) sFilename_hru_template = sWorkspace_pest_model + slash + 'hru.tpl' ofs = open(sFilename_hru_template, 'w') sLine = 'ptf $\n' ofs.write(sLine) #right now we only have one parameter, we can add more later following this format sLine = 'hru, cn2\n' ofs.write(sLine) for iHru_type in range(0, nhru_type): sHru_type = "{:03d}".format(iHru_type + 1) sLine = 'hru' + sHru_type + ', ' + '$cn2' + sHru_type + '$\n' ofs.write(sLine) ofs.close() print('hru template is ready!') return
def modflow_prepare_pest_control_file(sFilename_configuration_in, sModel): """ #prepare the pest control file """ #strings sWorkspace_home = config['sWorkspace_home'] sWorkspace_scratch = config['sWorkspace_scratch'] sWorkspace_data_relative = config['sWorkspace_data'] sWorkspace_project_ralative = config['sWorkspace_project'] sWorkspace_simulation_relative = config['sWorkspace_simulation'] sWorkspace_calibration_relative = config['sWorkspace_calibration'] pest_mode = config['pest_mode'] sRegion = config['sRegion'] sWorkspace_pest_model = sWorkspace_calibration + slash + sModel sWorkspace_simulation_copy = sWorkspace_simulation + slash + 'copy' + slash + 'TxtInOut' if not os.path.exists(sWorkspace_simulation_copy): print("The simulation folder is missing") return else: pass #number iYear_start = int(config['iYear_start'] ) #the end year of spinup iYear_spinup_end = int(config['iYear_spinup_end'] ) iYear_end = int( config['iYear_end'] ) nsegment = int( config['nsegment'] ) npargp = int(config['npargp'] ) nprior = int(config['nprior'] ) nobsgp = int(config['nobsgp'] ) ntplfile = int(config['ntplfile'] ) ninsfle = int(config['ninsfle'] ) sFilename_watershed_configuration = sWorkspace_data_project + slash \ + 'auxiliary' + slash + 'subbasin' + slash + 'watershed_configuration.txt' if os.path.isfile(sFilename_watershed_configuration): pass else: print(sFilename_watershed_configuration + ' is missing!') return aData_all = text_reader_string(sFilename_watershed_configuration, delimiter_in=',') aSubbasin= aData_all[:,0].astype(int) aHru = aData_all[:,1].astype(int) nsubbasin = len(aSubbasin) sFilename_hru_combination = sWorkspace_data_project + slash \ + 'auxiliary' + slash + 'hru' + slash + 'hru_combination.txt' if os.path.isfile(sFilename_hru_combination): pass else: print(sFilename_hru_combination + ' is missing!') return aData_all=text_reader_string(sFilename_hru_combination, delimiter_in=',') nhru = len(aData_all) npar = nhru sFilename = sWorkspace_data_project + slash + 'auxiliary' + slash \ + 'usgs'+slash+ 'discharge' + slash + 'discharge_observation.txt' if os.path.isfile(sFilename): pass else: print(sFilename + ' is missing!') return aData_all = text_reader_string(sFilename, delimiter_in=',') obs= array( aData_all).astype(float) good_index = np.where(obs != missing_value) nobs_with_missing_value = len(obs) nobs = len(good_index[0]) svd = 1 if svd == 1: rlambda1 = 10 numlam = 9 else: rlambda1 = 0 numlam = 1 rlamfac = 3.0 #the rlambda1 change factor phiratsuf = 0.3 #the iteration criteria phiredlam = 0.01 relparmax = 3 facparmax = 3 facorig = 0.0001 phiredswh = 0.1 noptmax = 20 #temination criteria phiredstp = 0.005 nphistp = 5 nphinored = 4 relparstp = 0.01 nrelpar = 4 icov = 1 icor = 1 ieig = 1 derinc = 0.01 derinclb = 0.1 derincmul = 1.5 inctyp = 'relative' forcen = 'switch' dermthd = 'parabolic' partrans ='none' cn2_init = 60 cn2_min = 10 cn2_max = 100 #we need define the input within the configuration file sFilename_control = sWorkspace_pest_model + slash + sRegion + '_swat.pst' ofs = open(sFilename_control, 'w') ofs.write('pcf\n') ofs.write('* control data\n') ofs.write('restart ' + pest_mode + '\n' ) #third line sLine = "{:0d}".format(npar) + ' ' \ + "{:0d}".format(nobs) + ' ' \ + "{:0d}".format(npargp) + ' ' \ + "{:0d}".format(nprior) + ' ' \ + "{:0d}".format(nobsgp) + '\n' ofs.write(sLine) #fourth line sLine = "{:0d}".format(ntplfile) + ' ' \ + "{:0d}".format(ninsfle) + ' '\ + ' double point ' \ + '1 0 0 \n' ofs.write(sLine) #fifth line sLine = "{:0.3f}".format(rlambda1) + ' ' \ + "{:0.3f}".format(rlamfac) + ' ' \ + "{:0.3f}".format(phiratsuf) + ' ' \ + "{:0.3f}".format(phiredlam) + ' ' \ + "{:0d}".format(numlam) + '\n' ofs.write(sLine) #sixth line sLine = "{:0d}".format(relparmax) + ' ' \ + "{:0d}".format(facparmax) + ' ' \ + "{:0.4f}".format(facorig) + '\n' ofs.write(sLine) #seventh line sLine = "{:0.3f}".format(phiredswh) + '\n' ofs.write(sLine) sLine = "{:02d}".format(noptmax) + ' ' \ + "{:0.3f}".format(phiredstp) + ' ' \ + "{:0d}".format(nphistp) + ' ' \ + "{:0d}".format(nphinored) + ' ' \ + "{:0.3f}".format(relparstp) + ' ' \ + "{:0d}".format(nrelpar) + '\n' ofs.write(sLine) sLine = "{:0d}".format(icov) + ' ' \ + "{:0d}".format(icor) + ' ' \ + "{:0d}".format(ieig) + '\n' ofs.write(sLine) ofs.write('* singular value decomposition\n') ofs.write('1\n') sLine = "{:0d}".format(npar) + ' ' \ + "{:0.4f}".format(1E-4) + '\n' ofs.write(sLine) ofs.write('1\n') ofs.write( '* parameter groups\n' ) sLine = 'para_gp1 ' \ + inctyp + ' '\ + "{:0.3f}".format(derinc) + ' ' \ + "{:0.3f}".format(derinclb) + ' ' \ + forcen + ' ' \ + "{:0.3f}".format(derincmul) + ' ' \ + dermthd + '\n' ofs.write(sLine) parchglim = 'relative' ofs.write('* parameter data\n') for ihru_type in range(0, nhru): sLine = 'cn2' + "{:03d}".format(ihru_type+1) + ' ' \ + partrans + ' ' \ + parchglim + ' '\ + "{:0.3f}".format(cn2_init) + ' ' \ + "{:0.3f}".format(cn2_min) + ' ' \ +"{:0.3f}".format(cn2_max) + ' ' \ + ' para_gp1 1.0 0.0 1\n' ofs.write(sLine) ofs.write('* observation groups\n') ofs.write( 'discharge\n') ofs.write( '* observation data\n') obs = np.reshape(obs, nobs_with_missing_value) for i in range(0, nobs_with_missing_value): if obs[i] != missing_value: sLine = 'discharge' + "{:04d}".format(i+1) + ' ' \ + "{:0.4f}".format(obs[i]) + ' 1.0 ' + ' discharge\n' ofs.write(sLine) else: pass ofs.write('* model command line\n') #run the model sLine = sWorkspace_pest_model + slash + 'run_swat_model\n' ofs.write(sLine) ofs.write('* model input/output\n') #sLine1 = sWorkspace_pest_model + slash + 'watershed.tpl' #sLine2 = sWorkspace_pest_model + slash + 'watershed.para\n' #sLine = sLine1 + ' ' + sLine2 #ofs.write(sLine) #sLine1 = sWorkspace_pest_model + slash + 'subbasin.tpl' #sLine2 = sWorkspace_pest_model + slash + 'subbasin.para\n' #sLine = sLine1 + ' ' + sLine2 #ofs.write(sLine) sLine1 = sWorkspace_pest_model + slash + 'hru.tpl' sLine2 = 'hru.para\n' sLine = sLine1 + ' ' + sLine2 ofs.write(sLine) #result sFilename_instruction = config['sFilename_instruction'] sFilename_instruction = sWorkspace_pest_model + slash + sFilename_instruction sFilename_result = config['sFilename_result'] sLine = sFilename_instruction + ' ' + sFilename_result + '\n' ofs.write(sLine) phimlim = 1.0 phimaccept = 1.05 * phimlim wfinit = 0.5 wfmin = 0.1 wfmax = 10 wffac = 1.5 wftol = 0.1 iregadj = 0 if pest_mode == 'estimation' : pass else: ofs.write(' * regularisation\n') sLine = "{:0.3f}".format(phimlim) + ' ' \ + "{:0.3f}".format(phimaccept) + ' ' + '\n' ofs.write(sLine) sLine = "{:0.3f}".format(wfinit) + ' ' \ + "{:0.3f}".format(wfmin) + ' ' \ + "{:0.3f}".format(wfmax) + ' ' + '\n' ofs.write(sLine) sLine = "{:0.3f}".format(wffac) + ' ' \ + "{:0.3f}".format(wftol) + ' ' \ + "{:0.3f}".format(iregadj) + ' ' + '\n' ofs.write(sLine) pass ofs.close() print('The PEST control file is prepared successfully at: ' + sFilename_control)