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

Class for gridded text files.  
    
    Authors: L. Feng, Edinburgh University
    History: v0.9, 2012.12.04
    History: v0.95, 2013.02.19
    
 
Classes:
=============================================================
1. grdfile_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

 
Classes
       
grdfile_desc_cl

 
class grdfile_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. data_lst:<list, t:array>: lits of raw data read from files 
7. attr_dict:<dict>: attribute dictionarys
8. grd_lst:<list, t:gp_grid_cl>: list of grids extracted from raw data 
9. gdata:<list, t:array>:  list of gridded data. 
10.flnm_lst:<list, t:str>: file names of the stored data set 
11.ref_lst:<list, t:str/numeric>: values used to identify the data set
12.cur_flnm:<str>: the name of the file to be read 
13.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 
3. find_colno: get column number 
4. get_colno_list:  find column number for given column name list 
5. read_matrix_from_lines
6. convert_matrix_to_grid: convert matrix to grid data
7. read_header_from_lines:  Read header from lines 
 
# file 
8. set_filename:  set the current filename and the current reference ID. 
9. set_file_path: set the file path 
10.  set_filename_format: set template for filename
11. construct_filename: use template and keywords
 
 
# member manager
 
12.  get_index:   find the index in the file list for the given reference values
13. append_data:   add the data set to data list
14. del_data:  delete data set from the 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=[], **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. keywords:<dict>: attributes
append_data(self, flnm, data, grd, gdata, ref)
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. grd: <ctm_grd>: grid extracted from raw data
4. gdata:<array>:  data gridded over grd 
5. 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
convert_matrix_to_grid(self, data, 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>: (horizontal) axis names
2. colno_lst:<list, t:integer>: columns of the (horizontal) axis
 
3. data:<array, (nrow, ncol)>: data to be gridded' 
--- if (zname and zval) are given,   each row in data is supposed to be in form of 
--->:  [ax_1, ax_2, ax_3,.., ax_n, y1, y2, y3, ...]
---if (zname and zval) are not given, each row 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
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 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_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)
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 data matrix 
 
 
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
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