source: cows_wps/trunk/process_modules/lib_extract_uk_station_data/utils.py @ 7002

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/process_modules/lib_extract_uk_station_data/utils.py@7002
Revision 7002, 2.1 KB checked in by astephen, 10 years ago (diff)

got bbox validation working

Line 
1"""
2utils.py
3========
4
5Util functions for process extract_uk_station_data that holds the ExtractUKStationData class.
6
7"""
8
9import os, stat, time, sys
10import urllib
11
12from cows_wps.utils.duration_splitter import *
13
14# Import local modules
15sys.path.append("/home/badc/software/datasets/ukmo-midas/scripts/extract")
16# MIDAS Station search code
17import midasSubsetter
18
19
20def extractStationDataByTimeChunk(obs_tables, startTime, endTime, src_ids, time_chunk, 
21                    output_file_base, ext, tempDir):
22    """
23    Loops through time chunks extracting data to files in required time chunks.
24    Returns a list of output file paths produced.
25    """ 
26    start_hr_min = startTime[8:12]
27    end_hr_min = endTime[8:12]
28
29    # Split the time chunks appropriately
30    ds = DurationSplitter()
31    time_splits = ds.splitDuration(startTime[:8], endTime[:8], time_chunk)
32    first_date = True
33
34    # Create list to return
35    output_file_paths = []
36
37    for (start_date, end_date) in time_splits: 
38
39        # Add appropriate hours and minutes to date strings to make 12 character times
40        if first_date == True:
41            start = "%s%s" % (start_date.date, start_hr_min)
42            first_date = False
43        else:
44            start = "%s0000" % (start_date.date)
45       
46        if end_date == time_splits[-1][-1]:
47            end = "%s%s" % (end_date.date, end_hr_min)
48        else:
49            end = "%s2359" % (end_date.date)
50
51        # Decide the output file path
52        output_file_path = "%s-%s-%s.%s" % (output_file_base, start, end, ext)
53        output_file_paths.append(output_file_path)
54 
55        # Call subsetter to extract and write the data   
56        midasSubsetter.MIDASSubsetter(obs_tables, output_file_path, startTime = start,
57                       endTime = end, src_ids = src_ids, tempDir = tempDir)
58       
59    return output_file_paths
60
61
62def readFileAndExtractStations(url):
63    """
64    Downloads file and extracts stations to a list.
65    """
66    #f = urllib.urlopen(url)
67    #data = f.read()
68    #f.close()
69
70    local_path = generateLocalFilePath(url)
71    station_ids = data.split() 
72    return station_ids
Note: See TracBrowser for help on using the repository browser.