CONFIG.py 5.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""This module holds all directory/path/URL information for metobs.ceilo
packages.  This file should be the only place that holds this static information.

This file should not do any file manipulation at all, only return strings or pass
other static information such as what plot number goes with what data.

This module will check for environment variables to create constants of PATHS
and URL bases.  It also has functions for getting file locations for any file
for a specified day.
"""
import os
import re
from datetime import datetime, timedelta
from metobs.util import RODict, CONFIG as c

CEILO_INCOMING_DIR = os.environ.get( 'CEILO_INCOMING_DIR', '/beach/incoming/Instrument_Data/METOBS/RIG/Ceilo/raw')
CEILO_PRAW_DIR =     os.environ.get( 'CEILO_PRAW_DIR', '/beach/raw/aoss/ceilo')
CEILO_CACHE_DIR =    os.environ.get( 'CEILO_CACHE_DIR', '/beach/cache/aoss/ceilo')
CEILO_LATEST_DIR =   os.environ.get( 'CEILO_LATEST_DIR', '/beach/cache/aoss/ceilo')
CEILO_DIR_FORMAT =   os.environ.get( 'CEILO_DIR_FORMAT', '%Y/%m')
CEILO_ASCII_LOC =    os.environ.get( 'CEILO_ASCII_LOC', '/beach/cache/aoss/ceilo')
CEILO_NC_LOC =       os.environ.get( 'CEILO_NC_LOC', '/beach/cache/aoss/ceilo')
CEILO_IMG_LOC =      os.environ.get( 'CEILO_IMG_LOC', 'http://metobs.ssec.wisc.edu/pub/cache/aoss/ceilo')
inst = 'ceilo'
RE_DIGITS = re.compile(r'\d+')


def get_incoming_dir():
    "Return incoming directory for specified date"
    return os.path.join( CEILO_INCOMING_DIR )

def get_praw_dir(when = None):
    "Return raw directory for specified date and data_type"
    when = when or datetime.now()
    return os.path.join( CEILO_PRAW_DIR, when.strftime( CEILO_DIR_FORMAT ) )

def get_sraw_dir(when = None):
    "Return raw directory for specified date and data_type"
    raise NotImplementedError("This function is not used anymore, there should only be one primary storage location")
    #when = when or datetime.now()
    #return os.path.join( CEILO_SRAW_DIR, when.strftime( CEILO_DIR_FORMAT ) )

def get_cache_dir(data_type, when = None):
    "Return cache directory for specified date and data_type"
    when = when or datetime.now()
    return os.path.join( CEILO_CACHE_DIR, data_type, when.strftime( CEILO_DIR_FORMAT ) )

def get_latest_dir():
    "Return latest directory"
    return os.path.join( CEILO_LATEST_DIR )

def get_ascii_filename(when = None, site="rig", description=""):
    "Return the standard filename of the ascii file for the specified date"
    when = when or datetime.now()
    return c.get_filename(site, inst, when, ext='ascii', description=description)

def get_ascii_url(when = None, site="rig", description=""):
    "Return the standard url of the ascii file for the specified date"
    when = when or datetime.now()
    return os.path.join(CEILO_ASCII_LOC, 'ascii', when.strftime(CEILO_DIR_FORMAT),
            get_ascii_filename(when, site=site, description=description))

def get_nc_filename(when = None, site="rig", description=""):
    "Return the standard filename of the netCDF file for the specified date"
    when = when or datetime.now()
    return c.get_filename(site, inst, when, ext='nc', description=description)

def get_nc_url(when = None, site="rig", description=""):
    "Return the standard url of the netCDF file for the specified date"
    when = when or datetime.now()
    return os.path.join(CEILO_NC_LOC, 'nc', when.strftime(CEILO_DIR_FORMAT),
            get_nc_filename(when, site=site, description=description))

def get_img_filename(begin, end, ptype=1, tag='', site="rig", description=""):
    "Return the standard filename of the image that goes from begin to end"
    pname = _handle_plot_type(ptype)
    return c.get_filename(site, inst, begin, end=end, ext="png", plotname=pname, description=description, tag=tag)

def get_quicklook_filename(begin, end, ptype=1, site='rig', description=''):
    return get_img_filename(begin, end, ptype, tag='', site=site, description=description)

def get_thumbnail_filename(begin, end, ptype=1, site='rig', description=''):
    return get_img_filename(begin, end, ptype, tag='tn', site=site, description=description)

def get_img_url(begin, end, ptype=1, tag='', site="rig", description=""):
    "Return the standard url of the image that goes from begin to end"
    return os.path.join(CEILO_IMG_LOC, 'img', begin.strftime(CEILO_DIR_FORMAT),
            get_img_filename(begin,end,ptype,tag=tag, site=site, description=description))

def get_quicklook_url(begin, end, ptype=1, site='rig', description=''):
    return get_img_url(begin, end, ptype, tag='', site=site, description=description)

def get_thumbnail_url(begin, end, ptype=1, site='rig', description=''):
    return get_img_url(begin, end, ptype, tag='tn', site=site, description=description)

def rename_incoming(incoming_file, site='rig', description=''):
    file_date = datetime(*tuple( [ int(x) for x in RE_DIGITS.findall(incoming_file) ] ))
    present_date = datetime.now()
    praw = get_praw_dir(when = file_date)
    cache = get_cache_dir('ascii', when = file_date)
    rn = get_ascii_filename(when = file_date, site=site, description=description)
    remove = file_date.date() < (present_date - timedelta(days=30)).date()
    return praw,cache,rn,remove

def get_type_name():
    return RODict({1:'Backscatter',
            2:'Cloud Base Height',
            3:'Vertical Visibility'})

def _handle_plot_type(plottype=1):
    if plottype == 1:
        return ''
    elif plottype == 2:
        return ''
    elif plottype == 3:
        return ''
    else:
        raise ValueError("Plot type must be between 1-3")