Example #1
0
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea['bbox_wgs84'])

# Find all GHCN stations within bounding box
stations = findStationsWithinBoundingBox(context.config, bbox)
print("Found %d stations in bounding box, downloading data..." %
      (len(stations)))
# Get data for each station
for station in stations:
    outFile = os.path.join(outDir, station[0])
    returnCode = getClimateDataForStation(context.config, context.projectDir,
                                          outFile, station[0])
    assert (returnCode)

    # Write metadata
    newStation = ClimatePointStation()
    newStation.type = "GHCN"
    newStation.id = station[0]
    newStation.longitude = station[1]
    newStation.latitude = station[2]
    newStation.elevation = station[3]
    newStation.name = station[4]
    newStation.data = outFile
    #newStation.startDate = datetime.strptime("200001", "%Y%m")
    #newStation.endDate = datetime.strptime("200101", "%Y%m")
    #newStation.variables = [ClimatePointStation.VAR_TMIN, \
    #                    ClimatePointStation.VAR_TMAX]
    newStation.writeToMetadata(context)

# Write processing history
GenericMetadata.appendProcessingHistoryItem(context, cmdline)
# Get study area parameters
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea["bbox_wgs84"])

# Get centroid of bounding box
(longitude, latitude) = calculateBoundingBoxCenter(bbox)
# Find nearest GHCN station
nearest = findStationNearestToCoordinates(context.config, longitude, latitude)
# Get data for station
outFile = os.path.join(outDir, nearest[0])
returnCode = getClimateDataForStation(context.config, context.projectDir, outFile, nearest[0])
assert returnCode

# Write metadata
station = ClimatePointStation()
station.type = "GHCN"
station.id = nearest[0]
station.longitude = nearest[1]
station.latitude = nearest[2]
station.elevation = nearest[3]
station.name = nearest[4]
station.data = outFile
# station.startDate = datetime.strptime("200001", "%Y%m")
# station.endDate = datetime.strptime("200101", "%Y%m")
# station.variables = [ClimatePointStation.VAR_TMIN, \
#                    ClimatePointStation.VAR_TMAX]
station.writeToMetadata(context)

# Write processing history
GenericMetadata.appendProcessingHistoryItem(context, cmdline)
Example #3
0
 def test_write_climate_point2(self):
     """ Test case where there are separate data files for each variable and there are two climate stations """
     station = ClimatePointStation()
     station.type = "GHCN"
     station.id = "US1MDBL0027"
     station.longitude = -76.716
     station.latitude = 39.317
     station.elevation = 128.0
     station.name = "My station name"
     station.startDate = datetime.strptime("201007", "%Y%m")
     station.endDate = datetime.strptime("201110", "%Y%m")
     station.variables = [ClimatePointStation.VAR_PRECIP, \
                          ClimatePointStation.VAR_SNOW]
     station.variablesData[ClimatePointStation.VAR_PRECIP] = ClimatePointStation.VAR_PRECIP + '.txt'
     station.variablesData[ClimatePointStation.VAR_SNOW] = ClimatePointStation.VAR_SNOW + '.txt'
     station.writeToMetadata(self.context)
     
     station2 = ClimatePointStation()
     station2.type = "GHCN"
     station2.id = "US1MDBL4242"
     station2.longitude = -42.716
     station2.latitude = 42.317
     station2.elevation = 42.0
     station2.name = "My 42 station"
     station2.startDate = datetime.strptime("199907", "%Y%m")
     station2.endDate = datetime.strptime("200110", "%Y%m")
     station2.variables = [ClimatePointStation.VAR_PRECIP, \
                          ClimatePointStation.VAR_SNOW]
     station2.variablesData[ClimatePointStation.VAR_PRECIP] = ClimatePointStation.VAR_PRECIP + '.txt'
     station2.variablesData[ClimatePointStation.VAR_SNOW] = ClimatePointStation.VAR_SNOW + '.txt'
     station2.writeToMetadata(self.context)
     
     climatePointStation = GenericMetadata.readClimatePointStations(self.context)[0]        
     self.assertTrue(station.type.lower() == climatePointStation.type)
     self.assertTrue(station.id.lower() == climatePointStation.id)
     self.assertTrue(station.longitude == climatePointStation.longitude)
     self.assertTrue(station.latitude == climatePointStation.latitude)
     self.assertTrue(station.elevation == climatePointStation.elevation)
     self.assertTrue(station.name == climatePointStation.name)
     self.assertTrue(station.startDate == climatePointStation.startDate)
     self.assertTrue(station.endDate == climatePointStation.endDate)
     self.assertTrue(station.variables == climatePointStation.variables)
     self.assertTrue(station.variablesData[ClimatePointStation.VAR_PRECIP] == climatePointStation.variablesData[ClimatePointStation.VAR_PRECIP])
     self.assertTrue(station.variablesData[ClimatePointStation.VAR_SNOW] == climatePointStation.variablesData[ClimatePointStation.VAR_SNOW])
Example #4
0
 def test_write_climate_point1_overwrite(self):
     """ Test case where there is a single data file the station, the entry is overwritten """
     station = ClimatePointStation()
     station.type = "GHCN"
     station.id = "US1MDBL0027"
     station.longitude = -76.716
     station.latitude = 39.317
     station.elevation = 128.0
     station.name = "My station name"
     station.data = "clim.txt"
     station.startDate = datetime.strptime("201007", "%Y%m")
     station.endDate = datetime.strptime("201110", "%Y%m")
     station.variables = [ClimatePointStation.VAR_PRECIP, \
                          ClimatePointStation.VAR_SNOW]
     station.writeToMetadata(self.context)
     
     climatePointStation = GenericMetadata.readClimatePointStations(self.context)[0]  
     self.assertTrue(station.type.lower() == climatePointStation.type)
     self.assertTrue(station.id.lower() == climatePointStation.id)
     self.assertTrue(station.longitude == climatePointStation.longitude)
     self.assertTrue(station.latitude == climatePointStation.latitude)
     self.assertTrue(station.elevation == climatePointStation.elevation)
     self.assertTrue(station.name == climatePointStation.name)
     self.assertTrue(station.data == climatePointStation.data)
     self.assertTrue(station.startDate == climatePointStation.startDate)
     self.assertTrue(station.endDate == climatePointStation.endDate)
     self.assertTrue(station.variables == climatePointStation.variables)
     
     station.longitude = -76.716
     station.latitude = 39.317
     station.elevation = 128.0
     station.name = "My (longer) station name"
     station.data = "clim.dat"
     station.startDate = datetime.strptime("201006", "%Y%m")
     station.endDate = datetime.strptime("201310", "%Y%m")
     station.variables = [ClimatePointStation.VAR_PRECIP, \
                          ClimatePointStation.VAR_SNOW]
     station.writeToMetadata(self.context)
     
     climatePointStation = GenericMetadata.readClimatePointStations(self.context)[0]  
     self.assertTrue(station.type.lower() == climatePointStation.type)
     self.assertTrue(station.id.lower() == climatePointStation.id)
     self.assertTrue(station.longitude == climatePointStation.longitude)
     self.assertTrue(station.latitude == climatePointStation.latitude)
     self.assertTrue(station.elevation == climatePointStation.elevation)
     self.assertTrue(station.name == climatePointStation.name)
     self.assertTrue(station.data == climatePointStation.data)
     self.assertTrue(station.startDate == climatePointStation.startDate)
     self.assertTrue(station.endDate == climatePointStation.endDate)
     self.assertTrue(station.variables == climatePointStation.variables)
# Get study area parameters
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea['bbox_wgs84'])

# Find all GHCN stations within bounding box
stations = findStationsWithinBoundingBox(context.config, bbox)
print "Found %d stations in bounding box, downloading data..." % (len(stations))
# Get data for each station
for station in stations:
    outFile = os.path.join(outDir, station[0])
    returnCode = getClimateDataForStation(context.config, context.projectDir, outFile, station[0])
    assert(returnCode)
    
    # Write metadata
    newStation = ClimatePointStation()
    newStation.type = "GHCN"
    newStation.id = station[0]
    newStation.longitude = station[1]
    newStation.latitude = station[2]
    newStation.elevation = station[3]
    newStation.name = station[4]
    newStation.data = outFile
    #newStation.startDate = datetime.strptime("200001", "%Y%m")
    #newStation.endDate = datetime.strptime("200101", "%Y%m")
    #newStation.variables = [ClimatePointStation.VAR_TMIN, \
    #                    ClimatePointStation.VAR_TMAX]
    newStation.writeToMetadata(context)
    
# Write processing history
GenericMetadata.appendProcessingHistoryItem(context, cmdline)
# Get study area parameters
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea['bbox_wgs84'])

# Get centroid of bounding box
(longitude, latitude) = calculateBoundingBoxCenter(bbox)
# Find nearest GHCN station
nearest = findStationNearestToCoordinates(context.config, longitude, latitude)
# Get data for station
outFile = os.path.join(outDir, nearest[0])
returnCode = getClimateDataForStation(context.config, context.projectDir,
                                      outFile, nearest[0])
assert (returnCode)

# Write metadata
station = ClimatePointStation()
station.type = "GHCN"
station.id = nearest[0]
station.longitude = nearest[1]
station.latitude = nearest[2]
station.elevation = nearest[3]
station.name = nearest[4]
station.data = outFile
#station.startDate = datetime.strptime("200001", "%Y%m")
#station.endDate = datetime.strptime("200101", "%Y%m")
#station.variables = [ClimatePointStation.VAR_TMIN, \
#                    ClimatePointStation.VAR_TMAX]
station.writeToMetadata(context)

# Write processing history
GenericMetadata.appendProcessingHistoryItem(context, cmdline)