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

Class for text file IO. It contains codes for reading ascii table file 
 
Authors: L. Feng, Edinburgh University
History: v0.9, 2012.11.28
History: v0.95, 2013.01.22
 
 
Classes:
=============================================================
1. file_desc_cl: class for ascii 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
Scientific.IO.NetCDF

 
Classes
       
file_desc_cl

 
class file_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. dtable:<recarray>: recarray read from files 
7. data:<array>: array read from files 
8. attr_dict:<dict>: attribute dictionarys
9. grd:<gp_grid_cl>: grid of the data 
10.gdata:<array>: gridded data 
 
    
 
Functions
==================================
1.  __init__: initialization
2. is_comment_line: check whether a line is for comments  
3. read_table_from_file: read columns of a text file  into a table (recarray)
4. read_table_from_file_byvar: read columns named by a var_lst 
---from a text file  into a table (recarray)
5. read_table_from_lines_byvar: decode lines into a table 
---(recarray) defined by a var_lst
 
6. read_table_from_lines: decode lines into a table (recarray) (wrapper for functions in line_process_m)
 
7. read_matrix_from_lines: decode lines into matrix (array). (wrapper for functions in line_process_m)
 
 
8. find_colno: get column number 
9. get_colno_list: find the column numbers for a list of names
10. get_colno_list_byvar: find the column numbers for a list of var 
11. get_colnm_list_byvar: find the column names for a list of var 
12. get_coltype_list_byvar: find the types for a list of var 
 
13. get_lon: get longitude if existing 
14. get_lat: get latitude if existing 
15. get_time: get time if existing 
16. get_column_data: get data for a given column 
17. convert_matrix_to_grd: convert matrix to grid data
 
18. read_header_from_lines:read header from given lines
19. locate_section: find the beginning (or end) of section 
 
 
 
# file 
20. set_filename:  set the current filename and the current reference ID. 
21. set_file_path: set the file path 
22.  set_filename_format: set template for filename
23. construct_filename: use template and keywords
 
 
# attributes
24. set_attr: set attributes
25. 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, header_start, data_start, comment_begin='#', delim=',', mask_val=-999.0, colnm_lst=[], **keywords)
Inputs:
----------------------------------------------------------
1. header_start:<integer>: header starting line Number 
2. data_start:<integer>: header starting line Number
3. comment_begin:<str>:  comment starting letter 
4. delim:<str>: separator between items 
5. mask_val: <float>: fillings for bad or missing values
6. colnm_lst:<list, t:int>: list of columns to be read
7. keywords:<dict>: attributes
construct_filename(self, **keywords)
construction file name use the keyword
convert_matrix_to_grid(self, colnm_lst, colno_lst, zname=None, zval=None)
wrapper for build_grid_from_matrix in gp_grid_m.py 
 
Inputs:
-----------------------------------------------------
1. colnm_lst:<list, t:str>: axis names
2. colno_lst:<list, t:integer>: columns of the axis
 
3. data:<array, (nrow, ncol)>: data to be gridded' 
--- if (yname and yval) are given,   data is supposed to be in the column of 
--- [ax_1, ax_2, ax_3,.., ax_n, y1, y2, y3, ...]
---if (yname and yval) are not given, data is supposed to be in the form of 
--- [ax_1, ax_2, ax_3, ...,ax_n, val]
 
4. zname:<str>: name of  z (vertical) axis
5. zval:<array>:  z (vertical) axis 
 
 
Returns:
----------------------------
1. grd:<gp_grid_cl>: grid 
2. data:<array>: gridded data
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_colnm_list_byvar(self, var_lst)
find column names for a list of ot_io_var
 
Inputs:
----------------------------------------------------
1. var_lst:<list, t:io_var_cl_>:list of var to be read
 
 
Returns:
-----------------------------------------------------
1. colnm_lst:<list, t:str>: names for each var
get_colno_list(self, colnm_lst)
find column number for given name 
 
 
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_colno_list_byvar(self, var_lst)
find column number for a list of ot_io_var
 
Inputs:
----------------------------------------------------
1. var_lst:<list, t:io_var_cl_>:list of var to be read
 
 
Returns:
-----------------------------------------------------
1. colno_lst:<list, t:integer>: column number of each var
get_coltype_list_byvar(self, var_lst)
find column type for a list of ot_io_var
 
Inputs:
----------------------------------------------------
1. var_lst:<list, t:io_var_cl_>:list of var to be read
 
 
Returns:
-----------------------------------------------------
1. coltype_lst:<list, t:str>: type for each var
get_column_data(self, colnm)
get value
get_lat(self, colnm='lat')
get latitude
get_lon(self, colnm='lon')
get longitude
get_time(self, colnm='time')
get time
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)
find the first line starting with the given 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_matrix_from_lines(self, lines, colno_lst=None, lstart=None, lend=None, **replaces)
read txt lines into  a record table
 
Inputs:
-----------------------------------------
1. lines:<list, t:str>: lines from file 
2. colno_lst:<list, t:str_>:list of column name to be read
3. lstar:<int>: start row
4. lend:<inte>: end row
5. replaces: <dict>: the words need to be replace before conversion. 
 
Returns:
--------------------------------
1.data:<array>: data matrix
read_table_from_file(self, flnm, colnm_lst, colno_lst)
read txt table files into var and return a record table
 
Inputs:
-----------------------------------------
1. flnm:<str>: file name 
2. var_lst:<list, t:io_var_cl_>:list of var to be read
 
 
Returns:
----------------------------
1. dtable:<recarray>: data
read_table_from_file_byvar(self, flnm, var_lst)
read txt table files into var and return a record table
 
Inputs:
-----------------------------------------
1. flnm:<str>: file name 
2. var_lst:<list, t:io_var_cl_>:list of var to be read
 
 
Returns:
----------------------------
1. dtable:<recarray>: data
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 column type to be read
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.
read_table_from_lines_byvar(self, lines, var_lst, lstart=None, lend=None, **replaces)
read txt table files into var and return a record table
 
Inputs:
-----------------------------------------
1. lines:<list, t:str>: lines from file 
2. var_lst:<list, t:io_var_cl_>:list of var to be read
3. lstar:<int>: start row
4. lend:<inte>: end row
5. replaces: <dict>: the words need to be replace before conversion. 
 
Returns:
--------------------------------
1.dtable:<recarray>: data
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
set_filename_format(self, flnm_format)
set template for filename