Ejemplo n.º 1
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)
Ejemplo n.º 2
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])
Ejemplo n.º 3
0
parser = argparse.ArgumentParser(
    description=
    'Dump point climate station information from EcohydroLib metadata to standard output'
)
parser.add_argument('-p',
                    '--projectDir',
                    dest='projectDir',
                    required=True,
                    help='The directory from which metadata should be read')
parser.add_argument('-s',
                    '--separator',
                    dest='separator',
                    required=False,
                    default=',',
                    help='Field separator for output')
args = parser.parse_args()

context = Context(args.projectDir, None)

s = args.separator

sys.stderr.write("Getting stations from metadata... ")
stations = GenericMetadata.readClimatePointStations(context)
sys.stderr.write("done\n")

for station in stations:
    output = station.id.upper() + s + str(station.latitude) + s + str(
        station.longitude) + s + str(
            station.elevation) + s + station.name + os.linesep
    sys.stdout.write(output)
Usage:
@code
DumpClimateStationInfo.py -p /path/to/project_dir > file.csv
@endcode
"""
import sys
import os
import argparse

from ecohydrolib.context import Context
from ecohydrolib.metadata import GenericMetadata

parser = argparse.ArgumentParser(description='Dump point climate station information from EcohydroLib metadata to standard output')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory from which metadata should be read')
parser.add_argument('-s', '--separator', dest='separator', required=False, default=',', help='Field separator for output')
args = parser.parse_args()

context = Context(args.projectDir, None) 

s = args.separator

sys.stderr.write("Getting stations from metadata... ")
stations = GenericMetadata.readClimatePointStations(context)
sys.stderr.write("done\n")

for station in stations:
    output = station.id.upper() + s + str(station.latitude) + s + str(station.longitude) + s + str(station.elevation) + s + station.name + os.linesep
    sys.stdout.write(output)