def sampleAtTimesofGridB(gridA, gridB, matchformat="yyyyMMddhhmm"): """ A function to programatically sample gridA at times of gridB, similar to idv gui but the format decides what type type is picked. one other application of this utility is when there are a lot of times where gui selection in IDV becomes tedious. """ from visad import RealType from visad import Gridded1DDoubleSet from visad import FieldImpl from visad import FunctionType from visad import VisADException timeSetA = GridUtil.getTimeSet(gridA) timeSetB = GridUtil.getTimeSet(gridB) timesA = getSampleTimesInFormat(gridA, str(matchformat)) timesB = getSampleTimesInFormat(gridB, str(matchformat)) indicesA = returnMatches(timesA, timesB) timevalsA = timeSetA.getSamples()[0] subsetTimeValuesA = [timevalsA[i] for i in indicesA] newTimesA = Gridded1DDoubleSet(RealType.Time, [subsetTimeValuesA], len(subsetTimeValuesA), None, timeSetA.getSetUnits(), None) subsetgridA = FieldImpl( FunctionType(RealType.Time, gridA.getSample(0).getType()), newTimesA) for i in range(len(subsetTimeValuesA)): subsetgridA.setSample(i, gridA[indicesA[i]]) return subsetgridA
def makeTimeComposite(variable, avgvariable, minvalue, maxvalue): """ Make a time composite of grid supplied(variable) between min max ranges of a 1d avg variable supplied. """ from visad import FunctionType from visad import FieldImpl from visad import RealType from visad import Gridded1DDoubleSet from visad import VisADException from ucar.unidata.util.Misc import getAverage timeSet = GridUtil.getTimeSet(avgvariable) newTimeIndexList = java.util.ArrayList() newTimeValues = [] for i in range(timeSet.getLength()): avg = getAverage(avgvariable.getSample(i).getFloats()[0]) if minvalue < avg <= maxvalue: newTimeIndexList.add(Integer(i)) newTimeValues.append(timeSet[i].getValue()) print(len(newTimeIndexList)) if (len(newTimeIndexList) < 1): raise VisADException("No Matches found to make a time composite") newTimes = Gridded1DDoubleSet(RealType.Time, [newTimeValues], len(newTimeValues), None, timeSet.getSetUnits(), None) compvariable = FieldImpl( FunctionType(RealType.Time, variable.getSample(0).getType()), newTimes) for i in range(len(newTimeValues)): compvariable.setSample(i, variable[newTimeIndexList[i]]) return compvariable
def getSamplesAtTimes(grid, year=None, season=None, mon=None, day=None, hour=None, min=None, sec=None, ms=None): """ Samples a grid at specified time periods, multiple arguments can be used in complex sampling eg.., using hour = 5 would return all samples corresponding to 5 am, further specifing year = 2008 would give samples at 5am in year 2008 """ from visad import RealType from visad import Gridded1DDoubleSet from visad import FieldImpl from visad import FunctionType from visad import VisADException if (str(mon) != "None" and str(season) != "None"): raise VisADException("One of Month or Season can be used, not both") timeSet = GridUtil.getTimeSet(grid) indices = getSampleTimeIndices(grid, year, season, mon, day, hour, min, sec, ms) timevals = timeSet.getSamples()[0] subsetTimeValues = [timevals[i] for i in indices] newTimes = Gridded1DDoubleSet(RealType.Time, [subsetTimeValues], len(subsetTimeValues), None, timeSet.getSetUnits(), None) subsetgrid = FieldImpl( FunctionType(RealType.Time, grid.getSample(0).getType()), newTimes) for i in range(len(subsetTimeValues)): subsetgrid.setSample(i, grid[indices[i]]) return subsetgrid
def makeTimeSequence(g): """ Merge a set of single time grids/images into a time sequence """ from visad import FunctionType, FieldImpl, Gridded1DDoubleSet, QuickSort from jarray import array domain = getDomainSet(g[0]) dt = getDomainType(g[0]) v=[getDomain(g[i]).indexToDouble([0,])[0][0] for i in range(len(g))] va = array(v,'d') index = QuickSort.sort(va) ft=FunctionType(dt, getRangeType(g[0])) fld=FieldImpl(ft,Gridded1DDoubleSet.create(dt,va,None,domain.getSetUnits()[0],None)) for i in range(len(g)): fld.setSample(i,g[index[i]].getSample(0),0) return fld
def subsetGridTimes(gridA, gridB): """ A function to subset gridA by times of gridB. duplicate of another function subsetAtTimesofB """ timeSet = GridUtil.getTimeSet(gridB) indices = getSampleTimeIndices(grid, year, season, mon, day, hour, min, sec, ms) timevals = timeSet.getSamples()[0] subsetTimeValues = [timevals[i] for i in indices] newTimes = Gridded1DDoubleSet(RealType.Time, [subsetTimeValues], len(subsetTimeValues), None, timeSet.getSetUnits(), None) subsetgrid = FieldImpl( FunctionType(RealType.Time, grid.getSample(0).getType()), newTimes) for i in range(len(subsetTimeValues)): subsetgrid.setSample(i, grid[indices[i]]) return subsetgrid
def stdMon(grid): """ Create monthly standard deviations from a grid of monthly data over a period of years. The number of timesteps must be a multiple of 12. """ from visad import VisADException from visad import Gridded1DDoubleSet from visad import FunctionType from visad import FieldImpl from visad import RealType timeSet = GridUtil.getTimeSet(grid) numT = timeSet.getLength() if (numT % 12 > 0): raise VisADException("Number of times must be a multiple of 12") numYears = numT / 12 days = [[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]] units = [Util.parseUnit("days since 0001-01-01 00:00")] newTimes = Gridded1DDoubleSet(RealType.Time, days, 12, None, units, None) stdev = FieldImpl(FunctionType(RealType.Time, grid.getSample(0).getType()), newTimes) for month in range(12): a = GridMath.applyFunctionOverTime(grid, GridMath.FUNC_STDEV, month, 12, 0) stdev.setSample(month, a, 0) return stdev
def ddt(grid, timegradunit): """ compute tendency (time derivative) using forward difference, units of returned grid are units of grid per timegradient unit timegradient unit can be month, day, hour, minute, seconds """ from visad import Real from visad import FunctionType from visad import FieldImpl from visad import RealType from visad import Gridded1DDoubleSet from ucar.visad.data import CalendarDateTime from visad import CommonUnit from visad import VisADException if (GridUtil.isTimeSequence(grid) == 1): newTimeValues = [] timediffs = [] ts = GridUtil.getTimeSet(grid) if (str(timegradunit).lower() in ("mon", "month", "months")): timefactor = 86400.0 * 30 timegradunit = "month" elif (str(timegradunit).lower() in ("day", "d", "days")): timefactor = 86400.0 timegradunit = "day" elif (str(timegradunit).lower() in ("hr", "hour", "hours")): timefactor = 3600.0 timegradunit = "hr" elif (str(timegradunit).lower() in ("m", "min", "minute", "minutes")): timefactor = 60.0 timegradunit = "min" elif (str(timegradunit).lower() in ("s", "sec", "second", "seconds")): timefactor = 1.0 timegradunit = "s" else: raise VisADException( "Requested time gradient unit is ambigious,use month,day,hour etc" ) for i in range(grid.getDomainSet().getLength() - 1): newTimeValues.append((ts[i].getValue() + ts[i + 1].getValue()) / 2) prevtime = float(ts[i].getValue(CommonUnit.secondsSinceTheEpoch)) nexttime = float(ts[i + 1].getValue( CommonUnit.secondsSinceTheEpoch)) timediffs.append((nexttime - prevtime) / timefactor) newTimes = Gridded1DDoubleSet(RealType.Time, [newTimeValues], len(newTimeValues), None, ts.getSetUnits(), None) ddtgrid = FieldImpl( FunctionType(RealType.Time, grid.getSample(0).getType()), newTimes) for i in range(grid.getDomainSet().getLength() - 1): diff = (grid.getSample(i + 1) - grid.getSample(i)).divide( Real(timediffs[i])) ddtgrid.setSample(i, diff) unitname = str( GridUtil.getParamType(grid).getComponent(0).getDefaultUnit()) print("[" + unitname + "]/" + str(timegradunit)) newunit = Util.parseUnit("(" + unitname + ")/" + str(timegradunit)) newType = Util.makeRealType("ddt of " + getVarName(grid), newunit) else: raise VisADException( "Well, this data is not a time series, hard to do a time derivative!" ) return GridUtil.setParamType(ddtgrid, newType, 0)
def createTimeMeans(grid, meanType="None"): """ Create time mean of a grid at periods specified by type. meanType can be yearly, monthly, daily, hourly, minutes, seconds """ from visad import Real from visad import Gridded1DDoubleSet from visad import FieldImpl from visad import FunctionType from visad import RealType from visad import VisADException if (str(meanType).lower() in ("year", "yr", "years", "yearly")): searchFormat = "yyyy" elif (str(meanType).lower() in ("mon", "month", "months", "monthly")): searchFormat = "MM" elif (str(meanType).lower() in ("day", "d", "days", "daily")): searchFormat = "dd" elif (str(meanType).lower() in ("hr", "hour", "hours", "hourly")): searchFormat = "hh" elif (str(meanType).lower() in ("m", "min", "minute", "minutes", "minutely")): searchFormat = "mm" elif (str(meanType).lower() in ("s", "sec", "second", "seconds")): searchFormat = "ss" else: raise VisADException( "Unrecognized time mean type, use yearly or monthly etc") alltimes = getSampleTimesInFormat(grid, searchFormat) timeSet = GridUtil.getTimeSet(grid) timeset = GridUtil.getTimeSet(grid).getSamples()[0] timevalues = [i for i in timeset] oldtime = alltimes[0] temptime = 0 count = 0 newtimelist = [] for currt, tv, i in zip(alltimes, timevalues, range(len(alltimes))): #values are always accumulated next time if currt == oldtime: #takes care of multiple times,first time,last time temptime = temptime + tv count = count + 1 if (i == (len(alltimes) - 1)): newtimeval = temptime / count newtimelist.append(newtimeval) else: #prev values are accumulated join to list newtimeval = temptime / count newtimelist.append(newtimeval) count = 1 temptime = tv oldtime = currt if (i == (len(alltimes) - 1)): newtimelist.append(temptime) #create new time set newTimes = Gridded1DDoubleSet(RealType.Time, [newtimelist], len(newtimelist), None, timeSet.getSetUnits(), None) newdatalist = FieldImpl( FunctionType(RealType.Time, grid.getSample(0).getType()), newTimes) timindices = range(len(newtimelist)) oldtime = alltimes[0] tempdata = grid.getSample(0).multiply(Real(0.0)) count = 0 newind = 0 for currt, i in zip(alltimes, range(len(alltimes))): #values are always accumulated next time if currt == oldtime: #takes care of multiple times,first time,last time tempdata = tempdata.add(grid.getSample(i)) count = count + 1 if (i == (len(alltimes) - 1)): newdatalist.setSample(newind, tempdata.divide(Real(count))) newind = newind + 1 else: #prev values are accumulated join to list newdatalist.setSample(newind, tempdata.divide(Real(count))) newind = newind + 1 count = 1 tempdata = grid.getSample(i) oldtime = currt if (i == (len(alltimes) - 1)): newdatalist.setSample(newind, tempdata.divide(Real(count))) newParamName = "Time Mean " + str( Util.cleanTypeName(GridUtil.getParamType(grid))) return newName(newdatalist, newParamName)