Ejemplo n.º 1
0
def createGstatRealizations(setOfRealizations, nameCommandFile, nameOutMapList):
  # number of realizations required
  nSim=len(setOfRealizations)
  # open template gstat script and replace
  #print nameCommandFile
  gstatTemplate=file(nameCommandFile + '.gst','r')
  gstatTemplateString=gstatTemplate.read()
  gstatTemplate.close()
  gstatString=string.replace(gstatTemplateString,'NSIM',str(nSim))
  gstatFile=file('tmpGstat.gst','w')
  gstatFile.write(gstatString)
  gstatFile.close()
  # run gstat
  os.system('gstat tmpGstat.gst')
  os.remove('tmpGstat.gst')
  # rename files
  i=1
  for realization in setOfRealizations:
    #print realization
    item=0
    for name in nameOutMapList:
      gstatOutputFileName=generateNameT('g_' + name,i)
      #print gstatOutputFileName, realization[item]
      shutil.move(gstatOutputFileName,realization[item])
      item=item+1
    i = i + 1
Ejemplo n.º 2
0
def createtimeseries(names, nameExtension, locations,sampleNumbers,timeSteps):
  if deterministicInput(sampleNumbers):
    for name in names:
      tssFile = file(name + nameExtension + '.tss', "w")
      tssFile.write("timeseries scalar\n")
      tssFile.write("2\n")
      tssFile.write("timestep\n")
      tssFile.write("%s\n" % (name))
      for step in timeSteps:
        timeseriesValue=mapmaximum(ifthen(locations,generateNameT(name,step)))
        value, valid = cellvalue(timeseriesValue, 1, 1); assert valid
        tssFile.write("%d %g\n" % (step, value))
      tssFile.close()
  else:
    for name in names:
      for sample in sampleNumbers:
        tssFile = file(generateNameS("%s%s.tss" % (name,nameExtension), sample), "w")
        tssFile.write("timeseries scalar\n")
        tssFile.write("2\n")
        tssFile.write("timestep\n")
        tssFile.write("%s\n" % (name))
        for step in timeSteps:
          timeseriesValue=mapmaximum(ifthen(locations,generateNameST(name,sample,step)))
          value, valid = cellvalue(timeseriesValue, 1, 1); assert valid
          tssFile.write("%d %g\n" % (step, value))
        tssFile.close()
Ejemplo n.º 3
0
def createtimeseries(names, nameExtension, locations,sampleNumbers,timeSteps):
  if deterministicInput(sampleNumbers):
    for name in names:
      tssFile = file(name + nameExtension + '.tss', "w")
      tssFile.write("timeseries scalar\n")
      tssFile.write("2\n")
      tssFile.write("timestep\n")
      tssFile.write("%s\n" % (name))
      for step in timeSteps:
        timeseriesValue=mapmaximum(ifthen(locations,generateNameT(name,step)))
        value, valid = cellvalue(timeseriesValue, 1, 1); assert valid
        tssFile.write("%d %g\n" % (step, value))
      tssFile.close()
  else:
    for name in names:
      for sample in sampleNumbers:
        tssFile = file(generateNameS("%s%s.tss" % (name,nameExtension), sample), "w")
        tssFile.write("timeseries scalar\n")
        tssFile.write("2\n")
        tssFile.write("timestep\n")
        tssFile.write("%s\n" % (name))
        for step in timeSteps:
          timeseriesValue=mapmaximum(ifthen(locations,generateNameST(name,sample,step)))
          value, valid = cellvalue(timeseriesValue, 1, 1); assert valid
          tssFile.write("%d %g\n" % (step, value))
        tssFile.close()
Ejemplo n.º 4
0
def createGstatRealizations(setOfRealizations, nameCommandFile, nameOutMapList):
  # number of realizations required
  nSim=len(setOfRealizations)
  # open template gstat script and replace
  #print nameCommandFile
  gstatTemplate=file(nameCommandFile + '.gst','r')
  gstatTemplateString=gstatTemplate.read()
  gstatTemplate.close()
  gstatString=string.replace(gstatTemplateString,'NSIM',str(nSim))
  gstatFile=file('tmpGstat.gst','w')
  gstatFile.write(gstatString)
  gstatFile.close()
  # run gstat
  os.system('gstat tmpGstat.gst')
  os.remove('tmpGstat.gst')
  # rename files
  i=1
  for realization in setOfRealizations:
    #print realization
    item=0
    for name in nameOutMapList:
      gstatOutputFileName=generateNameT('g_' + name,i)
      #print gstatOutputFileName, realization[item]
      shutil.move(gstatOutputFileName,realization[item])
      item=item+1
    i = i + 1
Ejemplo n.º 5
0
  def readDeterministic(self, name):
    if self._userModel()._inPremc() or self._userModel()._inPostmc() or self._userModel()._inInitial():
      newName = name + ".map"
    else:
      newName = generateNameT(name, self._userModel().currentTimeStep())

    import pcraster
    return pcraster.readmap(newName)
    def readDeterministic(self, name):
        if self._userModel()._inPremc() or self._userModel()._inPostmc(
        ) or self._userModel()._inInitial():
            newName = name + ".map"
        else:
            newName = generateNameT(name, self._userModel().currentTimeStep())

        import pcraster
        return pcraster.readmap(newName)
Ejemplo n.º 7
0
 def reportState(self, variable, variableName):
     """
 Report a map into the state variable directory.
 """
     sample = str(self.currentSampleNumber())
     if re.search(".map", variableName):
         filename = variableName
     else:
         filename = frameworkBase.generateNameT(variableName, self.currentTimeStep())
     name = os.path.join(sample, "stateVar", filename)
     pcraster.report(variable, name)
Ejemplo n.º 8
0
  def readDeterministic(self, name):
    """
    Read deterministic data from disk.

    Returns the map of the current time step from the current working directory.
    """
    if self._userModel()._inPremc() or self._inPostmc() or self._inInitial():
      newName = name + ".map"
    else:
      newName = frameworkBase.generateNameT(name, self._userModel().currentTimeStep())
    import pcraster
    return pcraster.readmap(newName)
Ejemplo n.º 9
0
 def readState(self, variableName):
     """
 Read a state variable map.
 """
     sample = str(self.currentSampleNumber())
     if re.search(".map", variableName):
         filename = variableName
     else:
         timestep = self.firstTimeStep() - 1
         filename = frameworkBase.generateNameT(variableName, timestep)
     name = os.path.join(sample, "stateVar", filename)
     return pcraster.readmap(name)
 def reportState(self, variable, variableName):
     """
 Report a map into the state variable directory.
 """
     sample = str(self.currentSampleNumber())
     if re.search(".map", variableName):
         filename = variableName
     else:
         filename = frameworkBase.generateNameT(variableName,
                                                self.currentTimeStep())
     name = os.path.join(sample, "stateVar", filename)
     pcraster.report(variable, name)
 def readState(self, variableName):
     """
 Read a state variable map.
 """
     sample = str(self.currentSampleNumber())
     if re.search(".map", variableName):
         filename = variableName
     else:
         timestep = self.firstTimeStep() - 1
         filename = frameworkBase.generateNameT(variableName, timestep)
     name = os.path.join(sample, "stateVar", filename)
     return pcraster.readmap(name)
Ejemplo n.º 12
0
def mcaveragevariance(names,sampleNumbers, timeSteps):
  if staticInput(timeSteps):
    for name in names:
      mean=average(name + '.map', sampleNumbers)
      var=variance(name + '.map', sampleNumbers)
      minimum=sampleMin(name + '.map', sampleNumbers)
      maximum=sampleMax(name + '.map', sampleNumbers)
      #std=stddev(name + '.map', sampleNumbers)
      report(mean, name + '-ave.map')
      report(var, name + '-var.map')
      report(minimum, name + '-min.map')
      report(maximum, name + '-max.map')
      report(sqrt(var)/mean, name + '-err.map')
  else:
    nrSamples=scalar(len(sampleNumbers))
    for name in names:
      for step in timeSteps:
        var=variance(generateNameT(name,step),sampleNumbers)
        mean=average(generateNameT(name,step), sampleNumbers)
        report(mean, generateNameT(name + '-ave', step))
        report(var, generateNameT(name + '-var', step))
        report(sqrt(var)/mean, generateNameT(name + '-err', step))
Ejemplo n.º 13
0
def timeseries(name, timeSteps, row, col):
  mask = numpy.zeros(len(timeSteps)).astype(numpy.bool_)
  steps = numpy.zeros(len(timeSteps)).astype(numpy.int32)
  array = numpy.zeros(len(timeSteps)).astype(numpy.float32)
  i = 0
  while i < len(timeSteps):
    filename = generateNameT(name, timeSteps[i])
    steps[i] = timeSteps[i]
    array[i], mask[i] = readFieldCell(filename, row, col)
    i += 1
  steps = numpy.compress(numpy.logical_not(mask), steps)
  array = numpy.compress(numpy.logical_not(mask), array)
  return steps, array
    def readDeterministic(self, name):
        """
    Read deterministic data from disk.

    Returns the map of the file with current time step, from the current
    working directory.
    """
        if self._inPremc() or self._inPostmc() or self._inInitial():
            newName = name + ".map"
        else:
            newName = frameworkBase.generateNameT(name, self.currentTimeStep())

        return pcraster.readmap(newName)
Ejemplo n.º 15
0
def timeseries(name, timeSteps, row, col):
  mask = numpy.zeros(len(timeSteps)).astype(numpy.bool_)
  steps = numpy.zeros(len(timeSteps)).astype(numpy.int32)
  array = numpy.zeros(len(timeSteps)).astype(numpy.float32)
  i = 0
  while i < len(timeSteps):
    filename = generateNameT(name, timeSteps[i])
    steps[i] = timeSteps[i]
    array[i], mask[i] = readFieldCell(filename, row, col)
    i += 1
  steps = numpy.compress(numpy.logical_not(mask), steps)
  array = numpy.compress(numpy.logical_not(mask), array)
  return steps, array
Ejemplo n.º 16
0
def mcaveragevariance(names,sampleNumbers, timeSteps):
  if staticInput(timeSteps):
    for name in names:
      mean=average(name + '.map', sampleNumbers)
      var=variance(name + '.map', sampleNumbers)
      minimum=sampleMin(name + '.map', sampleNumbers)
      maximum=sampleMax(name + '.map', sampleNumbers)
      #std=stddev(name + '.map', sampleNumbers)
      report(mean, name + '-ave.map')
      report(var, name + '-var.map')
      report(minimum, name + '-min.map')
      report(maximum, name + '-max.map')
      report(sqrt(var)/mean, name + '-err.map')
  else:
    nrSamples=scalar(len(sampleNumbers))
    for name in names:
      for step in timeSteps:
        var=variance(generateNameT(name,step),sampleNumbers)
        mean=average(generateNameT(name,step), sampleNumbers)
        report(mean, generateNameT(name + '-ave', step))
        report(var, generateNameT(name + '-var', step))
        report(sqrt(var)/mean, generateNameT(name + '-err', step))
def mcpercentiles(names, percentiles, sampleNumbers, timeSteps):
    if staticInput(timeSteps):
        for name in names:
            results = percentile(name + ".map", sampleNumbers, percentiles)
            for i in range(len(percentiles)):
                report(results[i], "%s_%s.map" % (name, percentiles[i]))
    else:
        for name in names:
            for step in timeSteps:
                results = percentile(generateNameT(name, step), sampleNumbers,
                                     percentiles)
                assert len(results) == len(percentiles)
                for i in range(len(percentiles)):
                    report(results[i],
                           "%s_%d_%s.map" % (name, step, percentiles[i]))
Ejemplo n.º 18
0
def mcpercentiles(
  names,
  percentiles,
  sampleNumbers,
  timeSteps):
  if staticInput(timeSteps):
    for name in names:
      results = percentile(name + ".map", sampleNumbers, percentiles)
      for i in range(len(percentiles)):
        report(results[i], "%s_%s.map" % (name, percentiles[i]))
  else:
    for name in names:
      for step in timeSteps:
        results = percentile(generateNameT(name, step), sampleNumbers,
          percentiles)
        assert len(results) == len(percentiles)
        for i in range(len(percentiles)):
          report(results[i], "%s_%d_%s.map" % (name, step, percentiles[i]))