Пример #1
0
    def test_getScores(self):
        input = Input.Text("tests/example.txt")
        obs = input.getScores("obs")
        fcst = input.getScores("fcst")
        self.assertEqual((1, 4, 4), obs.shape)
        self.assertEqual((1, 4, 4), fcst.shape)
        stations = input.getStations()
        I0 = -1
        for i in range(0, len(stations)):
            if (stations[i] == Station.Station(0, 1, 1, 1)):
                I0 = i
            if (stations[i] == Station.Station(0, 0, 0, 2)):
                I1 = i
        self.assertEqual(12, obs[0, 0, I0])
        self.assertTrue(np.isnan(obs[0, 1, I0]))
        self.assertTrue(np.isnan(obs[0, 2, I0]))
        self.assertEqual(5, obs[0, 3, I0])
        self.assertEqual(13, fcst[0, 0, I0])
        self.assertTrue(np.isnan(fcst[0, 1, I0]))
        self.assertTrue(np.isnan(fcst[0, 2, I0]))
        self.assertEqual(4, fcst[0, 3, I0])

        for i in range(0, obs.shape[1]):
            self.assertTrue(np.isnan(obs[0, i, I1]))
        self.assertEqual(11, fcst[0, 0, I1])
        for i in range(1, obs.shape[1]):
            self.assertTrue(np.isnan(obs[0, i, I1]))
Пример #2
0
 def test_getOffsets(self):
     input = Input.Text("tests/example.txt")
     offsets = input.getOffsets()
     self.assertEqual(4, offsets.shape[0])
     self.assertEqual(0, offsets[0])
     self.assertEqual(1, offsets[1])
     self.assertEqual(3, offsets[2])
     self.assertEqual(22, offsets[3])
Пример #3
0
 def test_getStations(self):
     input = Input.Text("tests/example.txt")
     stations = input.getStations()
     stations = np.sort(stations)
     self.assertEqual(4, len(stations))
     self.assertTrue(Station.Station(0, 1, 1, 1) in stations)
     self.assertTrue(Station.Station(0, 0, 0, 1) in stations)
     self.assertTrue(Station.Station(0, 0, 0, 2) in stations)
     self.assertTrue(Station.Station(0, 2, 2, 1) in stations)
Пример #4
0
 def test_getDates(self):
     input = Input.Text("tests/example.txt")
     dates = input.getDates()
     self.assertEqual(1, dates.shape[0])
     self.assertEqual(20120101, dates[0])
Пример #5
0
    def __init__(self,
                 filenames,
                 dates=None,
                 offsets=None,
                 locations=None,
                 latlonRange=None,
                 elevRange=None,
                 clim=None,
                 climType="subtract",
                 training=None,
                 legend=None,
                 removeMissingAcrossAll=True):
        if (not isinstance(filenames, list)):
            filenames = [filenames]
        self._axis = "date"
        self._index = 0
        self._removeMissingAcrossAll = removeMissingAcrossAll

        if (legend is not None and len(filenames) is not len(legend)):
            Util.error("Need one legend entry for each filename")
        self._legend = legend

        # Organize files
        self._files = list()
        self._cache = list()
        self._clim = None
        for filename in filenames:
            if (not os.path.exists(filename)):
                Util.error("File '" + filename + "' does not exist")
            if (Input.NetcdfCf.isValid(filename)):
                file = Input.NetcdfCf(filename)
            elif (Input.Comps.isValid(filename)):
                file = Input.Comps(filename)
            elif (Input.Text.isValid(filename)):
                file = Input.Text(filename)
            else:
                Util.error("File '" + filename + "' is not a valid input file")
            self._files.append(file)
            self._cache.append(dict())
        if (clim is not None):
            if (not os.path.exists(clim)):
                Util.error("File '" + clim + "' does not exist")
            if (Input.NetcdfCf.isValid(clim)):
                self._clim = Input.NetcdfCf(clim)
            elif (Input.Comps.isValid(clim)):
                self._clim = Input.Comps(clim)
            elif (Input.Text.isValid(clim)):
                self._clim = Input.Text(clim)
            else:
                Util.error("File '" + clim +
                           "' is not a valid climatology file")
            self._cache.append(dict())
            if (not (climType == "subtract" or climType == "divide")):
                Util.error("Data: climType must be 'subtract' or 'divide")
            self._climType = climType

            # Climatology file
            self._files = self._files + [self._clim]

        # Latitude-Longitude range
        if (latlonRange is not None):
            lat = self._files[0].getLats()
            lon = self._files[0].getLons()
            locId = self._files[0].getStationIds()
            latlonLocations = list()
            minLon = latlonRange[0]
            maxLon = latlonRange[1]
            minLat = latlonRange[2]
            maxLat = latlonRange[3]
            for i in range(0, len(lat)):
                currLat = float(lat[i])
                currLon = float(lon[i])
                if (currLat >= minLat and currLat <= maxLat
                        and currLon >= minLon and currLon <= maxLon):
                    latlonLocations.append(locId[i])
            useLocations = list()
            if (locations is not None):
                for i in range(0, len(locations)):
                    currLocation = locations[i]
                    if (currLocation in latlonLocations):
                        useLocations.append(currLocation)
            else:
                useLocations = latlonLocations
            if (len(useLocations) == 0):
                Util.error("No available locations within lat/lon range")
        elif locations is not None:
            useLocations = locations
        else:
            useLocations = self._files[0].getStationIds()

        # Elevation range
        if (elevRange is not None):
            stations = self._files[0].getStations()
            minElev = elevRange[0]
            maxElev = elevRange[1]
            elevLocations = list()
            for i in range(0, len(stations)):
                currElev = float(stations[i].elev())
                id = stations[i].id()
                if (currElev >= minElev and currElev <= maxElev):
                    elevLocations.append(id)
            useLocations = Util.intersect(useLocations, elevLocations)
            if (len(useLocations) == 0):
                Util.error("No available locations within elevation range")

        # Find common indicies
        self._datesI = Data._getUtilIndices(self._files, "Date", dates)
        self._offsetsI = Data._getUtilIndices(self._files, "Offset", offsets)
        self._locationsI = Data._getUtilIndices(self._files, "Location",
                                                useLocations)
        if (len(self._datesI[0]) == 0):
            Util.error("No valid dates selected")
        if (len(self._offsetsI[0]) == 0):
            Util.error("No valid offsets selected")
        if (len(self._locationsI[0]) == 0):
            Util.error("No valid locations selected")

        # Training
        if (training is not None):
            for f in range(0, len(self._datesI)):
                if (len(self._datesI[f]) <= training):
                    Util.error("Training period too long for " +
                               self.getFilenames()[f] +
                               ". Max training period is " +
                               str(len(self._datesI[f]) - 1) + ".")
                self._datesI[f] = self._datesI[f][training:]

        self._findex = 0