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

class for reading and writing GEOS-Chem binary punch files
history:
 
Authors: L. Feng, Edinburgh University
History: v0.9, 2012.10.22
History: v0.95, 2013.03.02
 
 
 
Classes:
=============================================
1. diag_info_cl: class for diaginfo file
2. tracer_info_cl: class for tracer info file 
3. gcfile_desc_cl: class for accessing GEOS-Chem bpch2 files
 
 
Functions:
================================================
 
1.  is_bpch_matched: check whether it is the required tracer 
2.  read_bpch_to_data_list: read 3D files from 
bpch file to list oif ctm_field
 
3.  read_bpch_to_profile_list: read 3D files from bpch file 
and sample them along latitude and logitudes.  
 
4.  open_bpch2_write: open a new bpch2 file 
 
5.  close_bpch2_file: close one bpch2 file 
 
6.  write_record_to_bpch2: write a data set into bpch2 file 
 
7.  close_bpch2_file: close one bpch2 file

 
Modules
       
ESA.util.bpch2_rw_py
ESA.atmosphere.ctm_field_m
ESA.atmosphere.ctm_profile_m
ESA.util.geo_constant
ESA.atmosphere.gc_grid_3d
ESA.util.message_m
numpy
ESA.util.otool_obj
ESA.util.otool_var_io
ESA.util.pres_m
ESA.util.pres_m
ESA.util.time_module

 
Classes
       
diag_info_cl
gcfile_desc_cl
tracer_info_cl

 
class diag_info_cl
    class for infomation of the tracers 
included in the GEOS-Chem simulation
 
Members:
-----------------------------------------------------
1. name:<list, t:str>: short name of tracers  
2. fullname:<list, t:str>: full name of tracer  
3. tracer:<list/array, t:integer>: id of the tracer  
4. scale:<list, t:float>: scaling factor 
5. unit: <unit, t:str>: unit of the tracers 
6. c: <list, t:integer>: category id of the tracer. 
7. molew:<list,t:float>: mass of tracer
 
 
Functions:
----------------------------------------------
1. __init__: initialization
2. read_info: decode lines into different fields.
3. get_offset:  get the offset for given category 
4. load_diag_info: read diag info from file
 
  Methods defined here:
__init__(self, flnm)
initialization
 
Inputs:
------------------------------------
1. flnm :<str>:  name of the diaginfo file
get_offset(self, category)
get the offset for given category 
Inputs:
-----------------------
1. category:<str>: tracer category 
 
 
Returns:
1. offset:<integer>: offset of the category
load_diag_info(self, flnm)
read diag info from file
 
Inputs:
-------------------------------
1. flnm:<str>: file name
read_info(self, lines)
read information from lines in diaginfo file  
 
Inputs:
-------------------------------
1. lines: <list, t:str>: list of lines 
--typical line includes:  offset, category and comment

 
class gcfile_desc_cl
    Members:
====================================
1. flnm:<str>: file name
2, ftracerinfo:<str>: name for tracerinfo file
3. fdiaginfo:<str>: name for diaginfo file
4. categorys:<list, t:str>: list of categorys to be read
5. tracers:<list,t:int>: list of tracers to be read 
6. taus:<list, t:float>: list of taus to be read 
7. tranames:<list, t:str>:list of tracer name
8. fio_keywords: <dict>: extra inputs for file IO
9. attr_dict:<dict>: attributes
 
10. mask_val:<float>: fillings for bad or missing values
11. data_lst:<list>: list of list of model data or profiles read from bpch2 file 
12. ref_lst:<list, t:int>:list of the references.
13. flnm_lst:<list, t:str>: list of the file names.
14. tracerinfo_flnm_lst:<list, t:str>: list of the tracerinfo file names.
15. diaginfo_flnm_lst:<list, t:str>: list of the diaginfo file names.
 
16. cur_flnm: <str>: current filename 
17. cur_tracerinfo_flnm: <str>: current tracer info file 
18. cur_diaginfo_flnm:<str>: current diag info file 
19. cur_ref:<current ref>: current reference number 
 
 
 
 
Functions:
=================================================
1.  __init__: initialization 
2. append_data: add the data set to lists of the class
3. del_data: Remove data from the list 
    
4. set_file_path: set path for data file
5. set_filename_format: set template for filename
 
6. set_tracerinfo_file_path: set path for tracerinfo  file
7. set_tracerinfo_filename_format: set template for tracerinfo filename
 
8. set_diaginfo_file_path:set path for diaginfo  file
9. set_diaginfo_filename_format:set template for diaginfo filename
10. construct_filename: construction file name using keywords 
 
11. construct_diaginfo_filename: construction name for diag info file using keywords
12. set_attr: assign one attribute
13. get_attr: get one attribute
14. read_file: Read file from GEOS-Chem runs
15. read_file_to_profile_list:Read outputs from GEOS-Chem  runs to list of profiles (ctm_profile_cl)
 
# ensemble runs 
 
16. construct_enr_filename_dict:  Form dictionary (extensions) to construct name for ensemble runs
17. read_enr_file: Read file from ensemble runs to a list of fields (gc_field_3d)
18. read_enr_file_to_profile_list:Read outputs from ensemble run to list of profiles (ctm_profile_cl)
 
19. get_data: find the data meeting criteria on traname, category and tracer id etc 
 
# pressure grid
 
20. compute_mod_pres: compute from read surface pressure 
   or directly fetch read model pressure
 
  Methods defined here:
__init__(self, flnm, ftracerinfo, fdiaginfo, yyyy, mm, dd, is_enr_file=False, categorys=None, tracers=None, taus=None, tranames=None, mask_val=-999.0, fio_keywords={}, **keywords)
initialization 
 
Inputs:
--------------------------------------------------------------
1. flnm:<str>: file name
2, ftracerinfo:<str>: name for tracerinfo file
3. fdiaginfo:<str>: name for diaginfo file
4. categorys:<list, t:str>: list of categorys to be read
5. tracers:<list,t:int>: list of tracers to be read 
6. taus:<list, t:float>: list of taus to be read 
7. tranames:<list, t:str>:list of tracer name
8. mask_val:<float>: values for bad or missing data
9. fio_keywors: <dict>: extra inputs for file IO
10. keywords:<dict>: attributes
append_data(self, flnm, tracerinfo_flnm, diaginfo_flnm, data_lst, ref)
add the data set to lists of the class
 
Inputs:
-------------------------------------------
1. flnm: <str>:   file name 
2. tracerinfo_flnm: <str>:   name for tracerinfo file 
3. diaginfo_flnm: <str>:  name for diaginfo file  
 
4. data: <array>:  raw data read from the file 
5. ref:<str/numeric>: id (such as time etc) for the data set
compute_mod_pres(self, traname='PSURF', use_sp=True, geos_ver=5, use_reduced=1, **keywords)
compute or fetch pressure at model level centres 
 
Inputs:
-------------------------------------
1. traname:<str>: traname for pressure or sp 
2. use_sp:<T/F>: True, surface pressure will be used to calculate pressures
3. geos_ver:<int>: GEOS-Chem version
4. use_reduced:<int>: 1 means reduced model vertical grid is used  
 
keywords:
=========================================
---Reserved keys:
---category:<str>: category of the surface pressure or pressure
---tracer:<int>: tracer ID of surface pressure or model pressure
---tau:<float>: time of surface pressure or model pressure
 
 
 
Returns:
----------------------------------
1. mod_res: <array,(nx, [ny],  [lx])>: pressure at model level or surface pressure (see Notes 1)
 
Notes:
-----------------------------------------------------------
1. when use_sp is set to False, the return will be the surface pressure
construct_diaginfo_filename(self, **keywords)
construction name for diag info file using keywords
 
Inputs:
----------------------------
1. keywords:<dict>: dictionary for file format and path
---expected Keynames:
--->1. diaginfo_flnmformat:<str>: file format
--->2. diaginfo_filepath:<str>: file path
--->3. words used to replace dummies in file format.
 
 
Returns
---------------------------------------
1. sflnm:<str>: file name
construct_enr_filename_dict(self, yyyy, dd, mm, enr_yyyy, enr_doy, enr_step, enr_em_st, enr_em_end)
Form dictionary (extensions) to construct name for ensemble runs
 
Input:
============================================
1. enr_yyyy:<int>: year for ensemble run start 
2. enr_doy:<int>:  doy for ensemble run start
3. enr_step:<int>: step for ensemble run 
4. enr_em_st:<int>: firt one in the ensemble 
5. enr_em_end:<int>: last one in the ensemble 
6. yyyy, dd, mm:<int>: output time 
 
Notes:
    1. the default extension will be like: 
        STXENRSTEPX.ENXEMSTX-XEMEND.XYYYYXXMMXXDDX
        for example:
         ST001.EN0001-EN0048.2009.01.01
         stands for 
         step=1
         ensemble member of 0001-0049
         yyyyy=2009, mm=1, dd=1
construct_filename(self, **keywords)
construction file name using keywords 
 
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
construct_tracerinfo_filename(self, **keywords)
construction name for tracer info file  use the keyword 
 
Inputs:
----------------------------
1. keywords:<dict>: dictionary for file format and path
---expected Keynames:
--->1. tracerinfo_flnmformat:<str>: file format
--->2. tracerfo_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
get_attr(self, attr_name)
get one attribute
 
Inputs:
------------------------------------------------
    1. name:<str>: attribute name
Returns:
-----------------------------------------------
    1. val: <obj>: attribute value
get_data(self, traname=None, category=None, tracer=None)
select the data from data_lst
 
 
Inputs:
=======================================
1. traname:<list, t:str>: tracer name
2. category:<list, t:str>: category 
3. tracerID:<list, t:int>: tracer ID 
 
 
Returns:
==========================================
1. traname_lst:<list, t:str>: trace name 
2. category_lst:<list, t:str>: category 
3. tid_lst:<list, t:int>: tracer ID 
4. sid_lst:<list, t:int>:  Index of the data in the list
5. found_data_lst:<list, t:class bpdata or profile>:
read_enr_file(self, yyyy, mm, dd, enr_yst_lst, enr_dst_lst, enr_step_lst, enr_em_st_lst, enr_em_end_lst, **keywords)
Read outputs from ensemble runs to list of ctm_data_cl
 
 
Input:
============================================================
1. yyyy, mm, dd:<int>: year month day 
2. enr_yyyy_lst:<int>: year for ensemble run start.
3. enr_doy_lst:<int>:  doy for ensemble run start.
4. enr_step_lst:<int>: step for ensemble run.
5. enr_em_st_st:<int>: firt one in the ensemble. 
6. enr_em_end_lst:<int>: last one in the ensemble. 
7. keywords:<dict>: extra inputs:
 
Returns:
==========================================================
1. self.nset:<int>: number of data set.
read_enr_file_profile_list(self, yyyy, mm, dd, olon, olat, enr_yst_lst, enr_dst_lst, enr_step_lst, enr_em_st_lst, enr_em_end_lst, **keywords)
Read outputs from ensemble runs to list of ctm_profile_cl
 
Input:
============================================================
1. yyyy, mm, dd:<int>: year month day 
2. olon:<array>: longitude
3. olat:<array>:latitude 
4. enr_yyyy_lst:<int>: year for ensemble run start.
5. enr_doy_lst:<int>:  doy for ensemble run start.
6. enr_step_lst:<int>: step for ensemble run.
7. enr_em_st_st:<int>: firt one in the ensemble. 
8. enr_em_end_lst:<int>: last one in the ensemble. 
9. keywords:<dict>: extra inputs:
 
Returns:
==========================================================
1. self.nset:<int>: number of data set.
read_file(self, yyyy, mm, dd, ext='', **keywords)
Read outputs from GEOS-Chem runs to a list of 3D fields 
 
 
Input:
============================================================
1. yyyy, mm, dd:<int>: year month day 
2. ent:<str>: string for file extension 
3. keywords:<dict>: extra inputs:
 
Returns:
==========================================================
1. bp_lst:<list, t:gc_field_3d>: list of  data sets.
read_file_to_profile_list(self, yyyy, mm, dd, olon, olat, ext='', **keywords)
Read outputs from forecast runs to a list of ctm_profile_cl
 
Input:
============================================================
1. yyyy, mm, dd:<int>: year month day 
2. olon:<array>: longitude
3. olat:<array>:latitude 
4. ext:<str>: file extension 
 
5. keywords:<dict>: extra inputs:
 
Returns:
==========================================================
1. bp_lst:<list, t:prof_lst>: list of found profiles.
set_attr(self, attr_name, value)
assign one attribute
Inputs:
-------------------------------------------------------
1. attr_name: <str>: attribute name
2. value: <obj>:attribute value
set_diaginfo_file_path(self, path)
set path for diaginfo  file 
Inputs:
-----------------------------------
1. path:<str>: path  for tracerinfo file
set_diaginfo_filename_format(self, flnm_format)
set template for diaginfo filename 
Inputs:
-----------------------------------
1. flnm_format:<str>: format for diaginfo file name
set_file_path(self, path)
set path for data file 
Inputs:
-----------------------------------
1. path:<str>: path  for file
set_filename_format(self, flnm_format)
set template for filename 
Inputs:
-----------------------------------
1. flnm_format:<str>: format for file name
set_tracerinfo_file_path(self, path)
set path for tracerinfo  file 
Inputs:
-----------------------------------
1. path:<str>: path  for tracerinfo file
set_tracerinfo_filename_format(self, flnm_format)
set template for tracerinfo filename 
Inputs:
-----------------------------------
1. flnm_format:<str>: format for tracerinfo file name

 
class tracer_info_cl
    class for infomation of the tracers 
included in the GEOS-Chem simulation
 
Members:
-----------------------------------------------------
1. name:<list, t:str>: short name of tracers  
2. fullname:<list, t:str>: full name of tracer  
3. tracer:<list/array, t:integer>: id of the tracer  
4. scale:<list, t:float>: scaling factor 
5. unit: <unit, t:str>: unit of the tracers 
6. c: <list, t:integer>: category id of the tracer. 
7. molew:<list,t:float>: mass of tracer
 
 
Functions:
----------------------------------------------
 
1. __init__: initialization
2. read_info: decode lines into different fields 
3. get_tracer_info:check information of the tracer 
4. load_tracer_info(self, flnm): Read tracer infor from file
 
  Methods defined here:
__init__(self, flnm)
get_tracer_info(self, tracer_in)
check information of the tracer 
Inputs:
----------------------------------------
1. tracer_in:<integer>: (real) tracer id 
 
Returns:
------------------------------------
1. name:<str>: name of the tracer
2. fullname:<str>: full name of the tracer 
3. molew: <float>: molecular mass
4. scale: <float>: scaling factor 
5. unit: <str>: unit 
 
Notes:
----------------------------
1. If tracer_in is not found in self.tracer, returns will be filled with "???" or with oob.fill_val
load_tracer_info(self, flnm)
Read tracer infor from file
 
Inputs:
---------------------------------------
1. flnm:<str>: file name for tracer info
read_info(self, lines)
decode lines in tracer info file into different fields
Inputs:
----------------------------------------------
1. lines:<list, str>: lines in tracerinfo file 
--- a typical line include
name, fullname, molecular mass, scaling factor, tracer_id, unit, category id

 
Functions
       
close_bpch2_file(funit)
open one bpch2 file to write 
Inputs:
-------------------------
1. funit:<int>: file unit handle 
 
Returns:
---------------------
1. stat:<int>: status of the operation
is_bpch_matched(tcat, ttracer, tname, ttau0, ttau1, categorys=None, tracers=None, taus=None, tranames=None)
Check whether a bpch set meeting the selection criteria
 
 
Inputs:
-------------------------------------------------
1. tcat:<str>: category 
2. ttracer:<integer>: tracer id 
3. tname:<str>: name of the tracer 
4. ttau0:<float>: start tau
5. ttau1:<float>: end tau 
 
6. categorys:<list, t:str>: required  category 
7. tracers:<list, t:integer>: required tracer numbers
8. taus: <list, t:float>:  required tau 
9. tranames:<list, t:float>: required names
 
Returns:
==================================================
1. matched: <True>: if the criteria are met. 
 
Notes:
=================================================
1. tranames are the short names  
2. ttracer are the ID stored in the bpch2 file, 
---instead of the ones after being shifted by category offset 
 
3. fill values are treated as switching off the criteria.
open_bpch2_write(funit, flnm, title='')
open one bpch2 file to write 
Inputs:
-------------------------
1. funit:<int>: file unit handle 
2. flnm:<str>: file name 
3. title:<str>: title of the data set to be written. 
 
 
Returns:
---------------------
1. stat:<int>: status of the operation
read_bpch_to_data_list(flnm, ftracerinfo='', fdiaginfo='', categorys=None, tracers=None, taus=None, tranames=None)
read in the bpch data which meet the selection criteria 
 
Inputs:
-------------------------------------------------------------
1. flnm:<str>: name of the bpch2 file 
2. ftracerinfo:<str>: name of the tracerinfo file 
3. fdiaginfo:<str>:name of the diag info file 
4. categorys:<list, t:str>: required  category 
5. tracers:<list, t:integer>: required tracer numbers
6. taus: <list, t:float>:  required tau 
7. tranames:<list, t:float>: required names
 
 
Returns:
-----------------------------------------------
1. data_lst:<list, t:ctm_field_cl>: list of class object of ctm_field_cl.
 
 
Notes:
-----------------------------------------
1. tracers is the ID stored in the bpch2 file,  instead of the 'real'
--- tracer ID  after being shifted by category offsets read from diaginfo file 
2. Detailed information such as traname, trafullname, tramolew, trascale and traunit 
---are read from tracerinfo file according to the real tracer ID 
 
3. fill value means  the switching off the criteria.
read_bpch_to_profile_list(olon, olat, flnm, ftracerinfo='', fdiaginfo='', categorys=None, tracers=None, taus=None, tranames=None)
read in the bpch data which meet the selection criteria 
Inputs:
-------------------------------------------------------------
1. olon:<array>: longitude
2. olat:<array>: latitude 
 
3. flnm:<str>: name of the bpch2 file 
4. ftracerinfo:<str>: name of the tracerinfo file 
5. fdiaginfo:<str>:name of the diag info file 
6. categorys:<list, t:str>: required  category 
7. tracers:<list, t:integer>: required tracer numbers (Note 1)
 
8. taus: <list, t:float>:  required tau 
9. tranames:<list, t:str>: required names
 
 
Returns:
-----------------------------------------------
1. data_lst:<list, t:ctm_profile_cl>: list of class object of ctm_field_cl.
 
 
Notes:
-----------------------------------------
1. tracers is the ID stored in the bpch2 file,  instead of the 'real'
--- tracer ID  after being shifted by category offsets read from diaginfo file 
2. Detailed information such as traname, trafullname, tramolew, trascale and traunit 
---are read from tracerinfo file according to the real tracer ID 
 
3. fill value means  the switching off the criteria.
write_record_to_bpch2(funit, ctm_data, **keywords)
write data into one open bpch2 file 
 
Inputs:
-----------------------------------------
 
1. funit:    integer- the unit number of the bpch2 file 
2. ctm_data:<ctm_field_cl/array>: gridded data with the ctm_grd 
3. keywords: Additional inputs to replace what stored in data 
--- reserved keywords 
--->'ctm_grd':<gc_grid_cl>: grid 
--->'unit':<str>: unit        
--->'modelname': <str>: model name 
--->'tau0':<float>: starting tau for the data set 
--->'tau1':<float>: end tau for the data set
---'>tracer_id:':<int>: tracer number 
--->'category': <str>: tracer category 
--->'reserved': <str>: reserved fields.
 
 
Returns:
---------------------
1. stat:<int>: status of the operation