#!/usr/bin/env python ############################################################### # < next few lines under version control, D O N O T E D I T > # $Date: 2016-10-11 08:58:01 -0400 (Tue, 11 Oct 2016) $ # $Revision: 82928 $ # $Author: Michael.Lueken@noaa.gov $ # $Id: lib_GSI.py 82928 2016-10-11 12:58:01Z Michael.Lueken@noaa.gov $ ############################################################### ''' lib_GSI.py contains utility functions for GSI ''' __author__ = "Rahul Mahajan" __email__ = "rahul.mahajan@nasa.gov" __copyright__ = "Copyright 2016, NOAA / NCEP / EMC" __license__ = "GPL" __status__ = "Prototype" __all__ = ['get_convdiag_list', 'get_convdiag_indices', 'get_convdiag_data', 'get_raddiag_list', 'get_raddiag_indices', 'get_raddiag_data', 'GSIstat'] import numpy as _np import pandas as _pd import re as _re import read_diag as _rd from datetime import datetime as _datetime def _read_diag_conv(fname,endian='big'): ''' Helper function to read a conventional diagnostic file ''' try: diag = _rd.diag_conv(fname,endian=endian) diag.read_obs() except: raise Exception('Error handling %s' % fname) return diag def _read_diag_rad(fname,endian='big'): ''' Helper function to read a radiance diagnostic file ''' try: diag = _rd.diag_rad(fname,endian=endian) diag.read_obs() except: raise Exception('Error handling %s' % fname) return diag def _print_diag_info(diag): print '%s contains...' % diag.filename print keys = diag.__dict__.keys() print ', '.join(str(x) for x in keys) print return def _get_diag_data(diag,indx,qty): ''' Helper function to get data from a diagnostic file. ''' ndim = None exec('ndim = len(diag.%s.shape)' % qty) if ndim == 1: exec('val = diag.%s[indx]' % qty) elif ndim > 1: exec('val = diag.%s[:,indx]' % qty) else: val = None return val def get_convdiag_list(fname,endian='big'): ''' Given the conventional diagnostic file, print contents INPUT: fname : name of the conventional diagnostic file OUTPUT: None : contents of diagnostic file to stdout ''' diag = _read_diag_conv(fname,endian=endian) _print_diag_info(diag) return def get_convdiag_indices(fname,obtype,code=None,iused=1,endian='big'): ''' Given parameters, get the indicies of observation locations from a conventional diagnostic file INPUT: fname : name of the conventional diagnostic file obtype : observation type e.g. 'ps', 'u', 'v', 't' etc code : KX (default: None) iused : qc flag (default: 1) endian : filetype (default: 'big') OUTPUT: index : indices of the requested data in the file ''' diag = _read_diag_conv(fname,endian=endian) indx = diag.obtype == obtype.rjust(3) if code is not None: indx = _np.logical_and(indx,diag.code==code) indx = _np.logical_and(indx,diag.used==iused) return indx def get_convdiag_data(fname,indx,qty,endian='big'): ''' Given indices, get a specific quantity from the radiance diagnostic file. For searching through the quantities, do a dir(diag) INPUT: fname : name of the conventional diagnostic file index : indices of the requested data in the file qty : quantity to retrieve e.g. 'hx', 'ob', 'oberr', etc ... OUTPUT: data : requested data ''' diag = _read_diag_conv(fname,endian=endian) val = _get_diag_data(diag,indx,qty) return val def get_raddiag_list(fname,endian='big'): ''' Given the radiance diagnostic file, print contents INPUT: fname : name of the radiance diagnostic file OUTPUT: None : contents of diagnostic file to stdout ''' diag = _read_diag_rad(fname,endian=endian) _print_diag_info(diag) return def get_raddiag_indices(fname,ichan,iused=1,oberr=1.e9,water=False,land=False,ice=False,snow=False,snowice=False,endian='big'): ''' Given parameters, get the indicies of observation locations from a radiance diagnostic file INPUT: fname : name of the conventional diagnostic file ichan : channel number iused : qc flag (default: 1) oberr : filter through observation error (default: 1.e9) water : filter observations over water (default: False) land : filter observations over land (default: False) ice : filter observations over ice (default: False) snow : filter observations over snow (default: False) snowice: filter observations over snowice (default: False) endian : filetype (default: 'big') OUTPUT: index : indices of the requested data in the file ''' diag = _read_diag_rad(fname,endian=endian) indx = _np.logical_and(diag.channel==ichan,diag.used==iused) indx = _np.logical_and(indx,diag.oberr