ESA.util.otool_descfile_io
index
/home/lfeng/otool/ESA/util/otool_descfile_io.py

Class for reading ASCII description file  
 
Authors: L. Feng, Edinburgh University
History: v0.9, 2012.12.04
History: v0.95, 2013.02.19
 
 
Classes:
=============================================================
1. descfile_desc_cl: class for ascii description file access.

 
Modules
       
ESA.util.gp_grid_m
ESA.util.line_process_m
ESA.util.message_m
numpy
ESA.util.otool_obj
ESA.util.otool_var_io

 
Classes
       
descfile_desc_cl

 
class descfile_desc_cl
    class for description of ascii file  
 
Members:
=====================================================
1. header_start:<int>: header starting row 
2. data_start:<int>: data starting row 
3. comment_begin:<str>: beginning character of comment lines
4. mask_val:<float>: filling for bad or missing data
5. colnm_lst:<list, t:str>: list of column names in the file
6. colnm_dict:<dict>: translation from column names in the file to variable names
 
 
7. data_lst:<list, t:array>: lits of raw data read from files 
8. attr_dict:<dict>: attribute dictionarys
9.flnm_lst:<list, t:str>: file names of the stored data set 
10.ref_lst:<list, t:str/numeric>: values used to identify the data set
11.cur_flnm:<str>: the name of the file to be read 
12.cur_ref:<str>: current reference for file 
---> the reference can be time step etc
 
 
Functions
==================================
1.  __init__: initialization
2. is_comment_line: check whether a line is for comments 
 
# data and column 
3. read_table_from_lines:  read txt lines into  a record table
4. translate_column_names:  translate column names to the names used to access data 
5. find_colno: get column number 
6.  get_colno_list:  find column number for given column name list
 
7. locate_section: find the first line starting with the given marker
8. read_header_from_lines:  Read header from lines 
 
# file 
9. set_filename:  set the current filename and the current reference ID. 
10. set_file_path: set the file path 
11.  set_filename_format: set template for filename
12. construct_filename: use template and keywords
 
# member manager
13.  get_index:   find the index in the file list for the given reference values
14. append_data:   add the data set to data list
    
# attributes
15. set_attr: set attributes
16. get_attr: get attributes
 
 
 
Attributes (reserved words)
-----------------------------------------------------------
1. flnm: <str>: full file name 
2. filepath:  <str>: path of the file name 
 
3. flnmformat:  <str>: template for filename
 
  Methods defined here:
__init__(self, filename, ref, header_start, data_start, comment_begin='#', delim=',', mask_val=-999.0, colnm_lst=[], colnm_dict={}, **keywords)
Inputs:
----------------------------------------------------------
1. filename:<str>: name the file 
2. ref:<str/numeric>: reference value 
 
3. header_start:<integer>: header starting line Number 
4. data_start:<integer>: header starting line Number
5. comment_begin:<str>:  comment starting letter 
6. delim:<str>: separator between items 
7. mask_val: <float>: fillings for bad or missing values
8. colnm_lst:<list, t:int>: list of columns to be read
9. colnm_dict:<dict>: translations from column name to variable names 
10. keywords:<dict>: attributes
append_data(self, flnm, data, ref, head=[])
add the data set to lists of the class
 
 
Inputs:
-------------------------------------------
1. flnm: <str>:   file name 
2. data: <array>:  raw data read from the file 
3. ref:<str/numeric>: id (such as time etc) for the data set
construct_filename(self, **keywords)
construction file name use the keyword 
 
Inputs:
----------------------------
 
1. keywords:<dict>: dictionary for file format and path
---expected Keynames:
--->1. flnmformat:<str>: file format
--->2. filepath:<str>: file path
--->3. words used to replace dummies in file format.
 
Returns
---------------------------------------
1. sflnm:<str>: file name
del_data(self, idx, by_ref=False)
Remove data from the list 
 
Inputs:
-----------------------------------------------------
 
1.idx :<int/obj>: index of the data set to be deleted 
2. by_ref:<T/F>: if ture , index will be treated as ref values for looking up ref_lst
 
Returns:
---------------------------------------------------
self.nset:<int>: the length of the current data lst
find_colno(self, colnm)
find the column no for a given column name 
Inputs:
-----------------------------------------
1. colnm:<str>: column name 
 
 
Returns:
---------------------------------------
1. col_idx:<integer>: the column with name given by colnm
get_attr(self, attr_name)
get one attribute
 
Inputs:
------------------------------------------------
    1. name:<str>: attribute name
Returns:
-----------------------------------------------
    1. val: <obj>: attribute value
get_colno_list(self, colnm_lst)
find column number for given names 
 
 
Inputs:
----------------------------------------------------
1. colnm_lst:<list, t:str>:list of column name  to be read
 
 
Returns:
-----------------------------------------------------
1. colno_lst:<list, t:integer>: column no for each column name
get_index(self, ref)
find the index of the given reference values
 
Inputs:
---------------------------------------------
1. ref:<str/numeric>: reference value 
 
Returns:
-------------------------------------------
1.index:<int>: if found, index of the reference value will be  returned
is_comment_line(self, line)
check a line is just for comments
 
Inputs:
----------------------------------
1. line: <str>: line 
 
Returns:
-------------------------------------
1. is_comment:<T/F>: True if the line start with comment_begin
locate_section(self, lines, marker='#C')
find the first line starting with the given section marker 
 
Inputs:
----------------------
1. lines:<list, t:str>: list of lines
2. marker:<str>: marker for the line 
 
Returns:
-----------------------------
1. pos:<int>: the location  of the line starting with marker
read_header_from_lines(self, lines, head_st=None, head_end=None, marker=None)
Read header from lines 
Inputs:
-----------------------------------
1. lines:<list, t:str>: list of lines to be read
2. head_st:<int/str>: start of the head section 
3. head_end:<int/str>: end of the head section 
4. marker:<str>: marker for header lines 
 
Returns:
-----------------------------------------
1. head_line:<list, t:str>: head lines (see Note 1)
 
Notes:
-----------------------------------------
1. if marker==None,  the section between header_start and data_start 
will be treated as header section
read_table_from_lines(self, lines, colnm_lst, colno_lst, coltype_lst=None, lstart=None, lend=None, **replaces)
read txt lines into  a record table
 
Inputs:
-----------------------------------------
1. lines:<list, t:str>: lines from file 
2. colnm_lst:<list, t:str_>:list of column name to be read
3. colno_lst:<list, t:str_>:list of column number to be read
4. coltype_lst:<list, t:str>:list of types of each selected column
5. lstar:<int>: start row
6. lend:<inte>: end row
7. replaces: <dict>: the words need to be replace before conversion. 
 
Returns:
--------------------------------
1.dtable:<recarray>: data
 
Notes:
 
--------------------------------------------
1. If one value is not able to converted the required type, it will be replaced by a filling value.  
2. If coltype is not provided, all columns will be converted to float.
set_attr(self, attr_name, value)
assign one attribute
Inputs:
-------------------------------------------------------
1. attr_name: <str>: attribute name
2. value: <obj>:attribute value
set_file_path(self, path)
set path for data file 
Inputs:
-----------------------------------
1. path:<str>: path  for file
set_filename(self, flnm, ref)
set the current filename and the current reference  
 
 
Inputs:
---------------------------------------
1. yyyy:<int>: year
2. mm:<int>: month
3. dd:<int>: dd
4. flnm:<str>: file name
set_filename_format(self, flnm_format)
set template for filename 
Inputs:
-----------------------------------
1. flnm_format:<str>: format for file name
translate_column_names(self, colnm_lst, **keywords)
translate column names to the names used to access data 
 
Inputs:
-------------------------------
1. colnm_lst:<list, t:str>: list of column names
2. keywords:<dict>: extra inputs:
---reversed keywords:
---colnm_dict:<dict>: column name dictionary
 
Returns:
--------------------
1. new_column_lst: <list, t:str>:New column names: