NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

Changeset 489

Show
Ignore:
Timestamp:
04/27/12 09:56:48
Author:
haines
Message:

added new 'spin' function in raw2proc()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • raw2proc/trunk/raw2proc/jpier_config_20050425.py

    r213 r489  
    1111            'config_start_date' : '2005-04-25 00:00:00', 
    1212            'config_end_date' : '2008-04-11 00:00:00', # None or yyyy-mm-dd HH:MM:SS 
    13             'packages' : ('met'), 
     13            'packages' : ('met',), 
    1414            } 
    1515sensor_info = { 
  • raw2proc/trunk/raw2proc/raw2proc.py

    r488 r489  
    11#!/usr/bin/env python 
    2 # Last modified:  Time-stamp: <2012-04-17 13:22:05 haines> 
     2# Last modified:  Time-stamp: <2012-04-27 09:09:11 haines> 
    33"""Process raw data to monthly netCDF data files 
    44 
     
    8080    return attr 
    8181 
     82def get_config_dates(pi): 
     83    """ Get datetime of both start and end setting within config file 
     84 
     85    Example 
     86    ------- 
     87        >>> pi = get_config(cn+'.platform_info') 
     88        >>> (config_start_dt, config_end_dt) = get_config_dates(pi) 
     89 
     90    """ 
     91    now_dt = datetime.utcnow() 
     92    now_dt.replace(microsecond=0) 
     93    if pi['config_start_date']: 
     94        config_start_dt = filt_datetime(pi['config_start_date']) 
     95    elif pi['config_start_date'] == None: 
     96        config_start_dt = now_dt 
     97    if pi['config_end_date']: 
     98        config_end_dt = filt_datetime(pi['config_end_date']) 
     99    elif pi['config_end_date'] == None: 
     100        config_end_dt = now_dt 
     101    return (config_start_dt, config_end_dt) 
     102 
    82103def find_configs(platform, yyyy_mm, config_dir=''): 
    83104    """Find which configuration files for specified platform and month 
     
    98119    configs = glob.glob(os.path.join(config_dir, platform + '_config_*.py')) 
    99120    configs.sort() 
    100     now_dt = datetime.utcnow() 
    101     now_dt.replace(microsecond=0) 
    102121    # determine when month starts and ends 
    103122    (prev_month, this_month, next_month) = find_months(yyyy_mm) 
     
    108127    cns = [] 
    109128    for config in configs: 
    110         # datetime from filename  
    111129        cn = os.path.splitext(os.path.basename(config))[0] 
    112         cndt = filt_datetime(os.path.basename(config)) 
    113130        pi = get_config(cn+'.platform_info') 
    114         if pi['config_start_date']: 
    115             config_start_dt = filt_datetime(pi['config_start_date']) 
    116         elif pi['config_start_date'] == None: 
    117             config_start_dt = now_dt 
    118         if pi['config_end_date']: 
    119             config_end_dt = filt_datetime(pi['config_end_date']) 
    120         elif pi['config_end_date'] == None: 
    121             config_end_dt = now_dt 
    122         #  
     131        (config_start_dt, config_end_dt) = get_config_dates(pi) 
    123132        if (config_start_dt <= month_start_dt or config_start_dt <= month_end_dt) and \ 
    124133               (config_end_dt >= month_start_dt or config_end_dt >= month_end_dt): 
     
    127136 
    128137 
    129 def find_active_configs(config_dir=''): 
     138def find_active_configs(config_dir=defconfigs): 
    130139    """Find which configuration files are active 
    131140 
     
    138147    # list of all config files  
    139148    configs = glob.glob(os.path.join(config_dir, '*_config_*.py')) 
    140     now_dt = datetime.utcnow() 
    141     now_dt.replace(microsecond=0) 
    142     #  
    143149    cns = [] 
    144150    for config in configs: 
    145151        # datetime from filename  
    146152        cn = os.path.splitext(os.path.basename(config))[0] 
    147         cndt = filt_datetime(os.path.basename(config)) 
    148153        pi = get_config(cn+'.platform_info') 
    149154        if pi['config_end_date'] == None: 
     
    152157 
    153158 
     159def uniqify(seq): 
     160    seen = {} 
     161    result = [] 
     162    for item in seq: 
     163        # in old Python versions: 
     164        # if seen.has_key(item) 
     165        # but in new ones: 
     166        if item in seen: continue 
     167        seen[item] = 1 
     168        result.append(item) 
     169    return result 
     170                                                        
     171 
     172def get_all_platforms(config_dir=defconfigs): 
     173    """Get all platform ids 
     174 
     175    :Returns: 
     176       pids : list of str 
     177           Sorted list of all the platforms 
     178    """ 
     179    import glob 
     180    # list of all config files  
     181    configs = glob.glob(os.path.join(config_dir, '*_config_*.py')) 
     182    configs.sort() 
     183    pids = [] 
     184    for config in configs: 
     185        # datetime from filename  
     186        cn = os.path.splitext(os.path.basename(config))[0] 
     187        pi = get_config(cn+'.platform_info') 
     188        if pi['id']: 
     189            pids.append(pi['id']) 
     190    pids = uniqify(pids) 
     191    pids.sort() 
     192    return pids 
     193 
     194def get_all_packages(platform, config_dir=defconfigs): 
     195    """Get all package ids -- all defined packages in sensor_info{} from all configs for the platform 
     196 
     197    :Returns: 
     198       sids : list of str 
     199           Sorted list of all the sensor ids for package 
     200    """ 
     201    import glob 
     202    # list of all config files  
     203    configs = glob.glob(os.path.join(config_dir, platform + '_config_*.py')) 
     204    configs.sort() 
     205    # 
     206    sids = [] 
     207    for config in configs: 
     208        cn = os.path.splitext(os.path.basename(config))[0] 
     209        pi = get_config(cn+'.platform_info') 
     210        sids.extend(list(pi['packages'])) 
     211    sids = uniqify(sids) 
     212    sids.sort() 
     213    return sids 
     214 
     215def get_all_platform_configs(platform, config_dir=defconfigs): 
     216    """Get all the config files for a platform 
     217 
     218    :Returns: 
     219       cns : list of config names 
     220           Sorted list of all the sensor ids for package 
     221    """ 
     222    import glob 
     223    # list of all config files  
     224    configs = glob.glob(os.path.join(config_dir, platform + '_config_*.py')) 
     225    configs.sort() 
     226    # 
     227    cns = [] 
     228    for config in configs: 
     229        cn = os.path.splitext(os.path.basename(config))[0] 
     230        cns.append(cn) 
     231    return cns 
     232 
     233def get_config_packages(cn): 
     234    """ Get active packages set in platform_info{} from specific config file 
     235 
     236    :Returns: 
     237       sids : list of str 
     238           Sorted (default) or unsorted list of all the sensor ids for package 
     239           If empty [], no platform ids were found 
     240    """ 
     241    pi = get_config(cn+'.platform_info') 
     242    sids = list(pi['packages']) 
     243    return sids 
     244 
     245def list_months(dts, dte): 
     246    """ list of datetimes for all months inclusively within given date range  
     247     
     248    """ 
     249    lom = [] 
     250    if type(dts) == type(dte) == type(datetime.utcnow()) and dts <= dte: 
     251        years = range(dts.year,dte.year+1) 
     252        for yyyy in years: 
     253            if yyyy > dts.year: 
     254                a = 1 
     255            else: 
     256                a = dts.month 
     257            if yyyy < dte.year: 
     258                b = 12 
     259            else: 
     260                b = dte.month 
     261            months = range(a, b+1) 
     262            for mm in months: 
     263                lom.append(datetime(yyyy, mm, 1).strftime('%Y_%m')) 
     264    else: 
     265        print "list_months requires two inputs type datetime.datetime and dts<dte" 
     266    return lom 
     267     
     268 
     269def create_spin_list(plats, packs, dates, config_dir=defconfigs): 
     270    """ create list of params needed to run manual() mutiple ways 
     271 
     272    :Returns: 
     273       spin_list : list of three-tuple each tuple with form (platform, package, yyyy_mm) 
     274 
     275    Notes 
     276    ----- 
     277 
     278    1. plats -- 'ALL' or ['b1', 'b2'] 
     279    2. packs -- 'ALL' or ['ctd1', 'ctd2'] 
     280    3. dates -- 'ALL' or ['2011_11', '2011_12'] or [dt.datetime(2006,1,1), dt.nowutc()] 
     281 
     282    For each platform determin packages for given dates 
     283    also a good way to get listing platforms and packages for specified dates 
     284 
     285    """ 
     286    result = [] 
     287    platforms = [] 
     288    if type(plats) == str: 
     289        if plats.upper() == 'ALL': 
     290            platforms = get_all_platforms() 
     291        else: 
     292            platforms = [plats] # make one platform iterable 
     293         
     294    print ' Expanded lists for creating spin_list:' 
     295    print ' ...  platform ids : %s' % platforms 
     296 
     297    for platform in platforms: 
     298        if len(platforms)>1: 
     299            print '------------------------------------' 
     300            print ' ... ... platform : %s ' % platform 
     301        packages = [] 
     302        if type(packs) == str: 
     303            if packs.upper() == 'ALL': 
     304                packages = get_all_packages(platform) 
     305            else: 
     306                packages = [packs] # make one package iterable 
     307 
     308        print ' ... ... packages : %s' % packages 
     309        for package in packages: 
     310            # dates is a string 'ALL' or format 'YYYY_MM' 
     311            months = [] 
     312            if type(dates) == str: 
     313                if dates.upper() == 'ALL': 
     314                    cns = get_all_platform_configs(platform) 
     315                    months = [] 
     316                    for cn in cns: 
     317                        pi = get_config(cn+'.platform_info') 
     318                        (dts, dte) = get_config_dates(pi) 
     319                        if package in pi['packages']: 
     320                            months.extend(list_months(dts, dte)) 
     321                else: 
     322                    months = [dates] # make on date iterable 
     323            # dates is a list 
     324            if type(dates) == type([]): 
     325                # if dates has two datetime types 
     326                if type(dates[0]) == type(dates[1]) == type(datetime.utcnow()): 
     327                    dt1, dt2 = dates 
     328                    cns = get_all_platform_configs(platform) 
     329                    months = [] 
     330                    for cn in cns: 
     331                        pi = get_config(cn+'.platform_info') 
     332                        (dts, dte) = get_config_dates(pi) 
     333 
     334                        if dts<=dt1 and dt1<=dte: a = dt1 
     335                        elif dt1<=dts and dt1<=dte: a = dts 
     336 
     337                        if dts<dt2 and dt2<=dte: b = dt2 
     338                        elif dts<dt2 and dte<=dt2: b = dte 
     339 
     340                        if dte<dt1 or dt2<dts: 
     341                            continue 
     342                        # list only months that are in configs for wide date range 
     343                        if package in pi['packages']: 
     344                            months.extend(list_months(a,b)) 
     345                # else if string in list 
     346                elif type(dates[0]) == str: 
     347                    months = dates 
     348            print ' ... ...   months : %s' % months 
     349            for month in months: 
     350                # print '... ... %s %s %s' % (platform, package, month) 
     351                result.append((platform, package, month)) 
     352                 
     353    return result 
     354                 
    154355def find_raw(si, yyyy_mm): 
    155356    """Determine which list of raw files to process for month """ 
     
    197398def which_raw(pi, raw_files, dts): 
    198399    """Further limit file names based on configuration file timeframe """ 
    199  
    200     now_dt = datetime.utcnow() 
    201     now_dt.replace(microsecond=0) 
    202     if pi['config_start_date']: 
    203         config_start_dt = filt_datetime(pi['config_start_date']) 
    204     elif pi['config_start_date'] == None: 
    205         config_start_dt = now_dt 
    206  
    207     if pi['config_end_date']: 
    208         config_end_dt = filt_datetime(pi['config_end_date']) 
    209     elif pi['config_end_date'] == None: 
    210         config_end_dt = now_dt 
     400    (config_start_dt, config_end_dt) = get_config_dates(pi) 
    211401 
    212402    for idx, fn in enumerate(raw_files): 
     
    238428    :Parameters: 
    239429       proctype : string 
    240            'auto' or 'manual' 
     430           'auto' or 'manual' or 'spin' 
    241431 
    242432       platform : string 
     
    251441    >>> raw2proc(proctype='manual', platform='bogue', package='adcp', yyyy_mm='2007_06') 
    252442    >>> raw2proc('manual', 'bogue', 'adcp', '2007_06') 
     443 
     444    Spin 
     445    ---- 
     446    platform can be list of platforms or 'ALL' 
     447    package can be list packages or 'ALL' 
     448    yyyy_mm can be list of months, or datetime range 
     449     
     450    >>> raw2proc('spin', ['b1','b2'], ['ctd1', 'ctd2'], ['2011_11']) 
     451    >>> raw2proc('spin', ['b1','b2'], ['ctd1', 'ctd2'], 'ALL') 
     452    >>> raw2proc('spin', ['b1','b2'], ['ctd1', 'ctd2'], [datetime(2011,11,1), datetime(2012,4,1)]) 
     453    >>> raw2proc('spin', ['b1','b2'], 'ALL', 'ALL') 
     454     
     455    Not a good idea but this will reprocess all the data from level0  
     456    >>> raw2proc('spin', 'ALL', 'ALL', 'ALL') 
    253457           
    254458    """ 
     
    260464    elif proctype == 'manual': 
    261465        if platform and package and yyyy_mm: 
    262             print 'Processing in manually ...' 
     466            print 'Processing in manual-mode ...' 
    263467            print ' ...  platform id : %s' % platform 
    264468            print ' ... package name : %s' % package 
     
    269473            print 'raw2proc: Manual operation requires platform, package, and month' 
    270474            print "   >>> raw2proc(proctype='manual', platform='bogue', package='adcp', yyyy_mm='2007_07')" 
     475    elif proctype == 'spin': 
     476        if platform and package and yyyy_mm: 
     477            print 'Processing in spin-mode ...' 
     478            print ' ...  platform ids : %s' % platform 
     479            print ' ... package names : %s' % package 
     480            print ' ...        months : %s' % yyyy_mm 
     481            print ' ...   starting at : %s' % start_dt.strftime("%Y-%m-%d %H:%M:%S UTC") 
     482            spin_list = create_spin_list(platform, package, yyyy_mm) 
     483            spin(spin_list) 
     484        else: 
     485            print "raw2proc: Spin operation requires platform(s), package(s), and month(s)" 
     486            print "   >>> raw2proc(proctype='spin', platform='b1', package='ALL', yyyy_mm='ALL')" 
     487            print "   >>> raw2proc(proctype='spin', platform='ALL', package='met', yyyy_mm='2011_11')" 
     488            print "   >>> raw2proc('spin', ['b1','b2'], ['ctd1', 'ctd2'], [datetime(2011,11,1), datetime(2012,4,1)])" 
     489 
    271490    else: 
    272491        print 'raw2proc: requires either auto or manual operation' 
     
    291510            
    292511    """ 
    293     now_dt = datetime.utcnow() 
    294     now_dt.replace(microsecond=0) 
    295  
    296512    yyyy_mm = this_month() 
    297513    months = find_months(yyyy_mm) 
     
    307523            asi = get_config(cn+'.sensor_info') 
    308524            platform = pi['id'] 
    309             if pi['config_start_date']: 
    310                 pi['config_start_dt'] = filt_datetime(pi['config_start_date']) 
    311             elif pi['config_start_date'] == None: 
    312                 pi['config_start_dt'] = now_dt 
    313             if pi['config_end_date']: 
    314                 pi['config_end_dt'] = filt_datetime(pi['config_end_date']) 
    315             elif pi['config_end_date'] == None: 
    316                 pi['config_end_dt'] = now_dt 
     525            (pi['config_start_dt'], pi['config_end_dt']) = get_config_dates(pi) 
     526 
    317527            # for each sensor package 
    318528            for package in asi.keys(): 
     
    352562        print ' ... ... NOTE: No active platforms' 
    353563 
     564def spin(spin_list): 
     565    """ wrapper to run manual() for multiple months""" 
     566    for item in spin_list: 
     567        platform, package, yyyy_mm = item 
     568        raw2proc('manual',platform, package, yyyy_mm) 
     569 
    354570def manual(platform, package, yyyy_mm): 
    355571    """Process data for specified platform, sensor package, and month 
     
    363579               which raw files 
    364580    """ 
    365     now_dt = datetime.utcnow() 
    366     now_dt.replace(microsecond=0) 
    367  
    368581    months = find_months(yyyy_mm) 
    369582    month_start_dt = months[1] 
     
    378591            print ' ... config file : %s' % cn 
    379592            pi = get_config(cn+'.platform_info') 
    380             if pi['config_start_date']: 
    381                 pi['config_start_dt'] = filt_datetime(pi['config_start_date']) 
    382             elif pi['config_start_date'] == None: 
    383                 pi['config_start_dt'] = now_dt 
    384             if pi['config_end_date']: 
    385                 pi['config_end_dt'] = filt_datetime(pi['config_end_date']) 
    386             elif pi['config_end_date'] == None: 
    387                 pi['config_end_dt'] = now_dt 
     593            (pi['config_start_dt'], pi['config_end_dt']) = get_config_dates(pi) 
    388594            # month start and end dt to pi info 
    389595            asi = get_config(cn+'.sensor_info') 
     
    424630        print ' ... ... ... NOTE: %s not operational for %s' % (platform, yyyy_mm) 
    425631     
     632 
    426633def process(pi, si, raw_files, yyyy_mm): 
    427634    # tailored data processing for different input file formats and control over output 
     
    439646 
    440647            for index, val in enumerate(data['dt']): 
    441                 if val>=pi['config_start_dt'] and val>=si['proc_start_dt'] and val<=si['proc_end_dt'] and val<=pi['config_end_dt']: 
     648                if val>=pi['config_start_dt'] and \ 
     649                       val>=si['proc_start_dt'] and \ 
     650                       val<=si['proc_end_dt'] and \ 
     651                       val<=pi['config_end_dt']: 
    442652                    data['in'][index] = True 
    443653