Changeset c49aa73 in flex_extract.git


Ignore:
Timestamp:
Jul 29, 2019, 10:18:33 AM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
6431a70
Parents:
4ef5ba9
Message:

python2 downgrade/ corrected maxl/maxb calculation for Fortran prog. / rm .fp conversion calculation / bug fix for new disaggregation call / bug fix of loops over types and params (sorted) / bugfix flux selection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/python/classes/EcFlexpart.py

    rd727af2 rc49aa73  
    1 #!/usr/bin/env python3
     1#!/usr/bin/env python
    22# -*- coding: utf-8 -*-
    33#*******************************************************************************
     
    2424#          short term forecast data
    2525#        - added conversion into GRIB2
    26 #        - added conversion into .fp format for faster execution of FLEXPART
    27 #          (see https://www.flexpart.eu/wiki/FpCtbtoWo4FpFormat)
    2826#
    2927#    February 2018 - Anne Philipp (University of Vienna):
     
    5654# MODULES
    5755# ------------------------------------------------------------------------------
     56from __future__ import print_function
     57
    5858import os
    5959import sys
     
    6262import subprocess
    6363from datetime import datetime, timedelta
    64 import numpy as np
    65 
    66 from eccodes import (codes_index_select, codes_new_from_index, codes_get,
    67                      codes_get_values, codes_set_values, codes_set,
    68                      codes_write, codes_release, codes_new_from_index,
    69                      codes_index_release, codes_index_get, codes_get_array,
    70                      codes_set_array, codes_grib_new_from_file)
    7164
    7265# software specific classes and modules from flex_extract
     
    252245            self._create_params(c.gauss, c.eta, c.omega, c.cwc, c.wrf)
    253246
    254         if fluxes and not c.purefc:
     247        if fluxes:# and not c.purefc:
    255248            self._create_field_types_fluxes()
    256249        else:
     
    574567            index_vals[2]: ('0', '12', '3', '6', '9') ; stepRange
    575568        '''
     569        from eccodes import codes_index_get
     570
    576571        iid = None
    577572        index_keys = keys
     
    666661                           'param':None}
    667662
    668         for ftype in self.types:
     663        for ftype in sorted(self.types):
    669664            # ftype contains field types such as
    670665            #     [AN, FC, PF, CV]
    671             for pk, pv in self.params.items():
     666            for pk, pv in sorted(self.params.items()):
    672667                # pk contains one of these keys of params
    673668                #     [SH__ML, SH__SL, GG__ML, GG__SL, OG__ML, OG__SL,
     
    838833        from genshi.template import  TemplateLoader
    839834        from genshi.template.eval import  UndefinedError
     835        import numpy as np
    840836
    841837        try:
     
    850846            if area[1] > area[3]:
    851847                area[1] -= 360
    852             maxl = int((area[3] - area[1]) / grid[1]) + 1
    853             maxb = int((area[0] - area[2]) / grid[0]) + 1
     848            maxl = round((area[3] - area[1]) / grid[1]) + 1
     849            maxb = round((area[0] - area[2]) / grid[0]) + 1
    854850
    855851            stream = namelist_template.generate(
     
    921917
    922918        '''
     919        import numpy as np
     920        from eccodes import (codes_index_select, codes_new_from_index, codes_get,
     921                             codes_get_values, codes_set_values, codes_set,
     922                             codes_write, codes_release, codes_new_from_index,
     923                             codes_index_release)
    923924
    924925        table128 = init128(_config.PATH_GRIBTABLE)
     
    13011302
    13021303        '''
     1304        import numpy as np
     1305
    13031306        print('... disaggregation of precipitation with new method.')
    13041307
     
    13231326        else:
    13241327            for ix in range(ni*nj):
    1325                 lsp_new_np[ix,:] = disaggregation.IA3(lsp_np[0,ix,:])[:-1]
    1326                 cp_new_np[ix,:] = disaggregation.IA3(cp_np[0,ix,:])[:-1]
     1328                lsp_new_np[0,ix,:] = disaggregation.IA3(lsp_np[ix,:])[:-1]
     1329                cp_new_np[0,ix,:] = disaggregation.IA3(cp_np[ix,:])[:-1]
    13271330
    13281331        # write to grib files (full/orig times to flux file and inbetween
    1329         # times into seperate end files)
     1332        # times with step 1 and 2, respectively)
    13301333        print('... write disaggregated precipitation to files.')
    13311334
     
    14881491
    14891492        '''
     1493        from eccodes import (codes_index_select, codes_new_from_index, codes_get,
     1494                             codes_get_values, codes_set_values, codes_set,
     1495                             codes_write, codes_release, codes_new_from_index,
     1496                             codes_index_release)
    14901497
    14911498        # generate start and end timestamp of the retrieval period
     
    15051512        # for the Fortran program input
    15061513        # 10: U,V | 11: T | 12: lnsp | 13: D | 16: sfc fields
    1507         # 17: Q | 18: Q , gaussian| 19: w | 21: etadot | 22: clwc+ciwc
     1514        # 17: Q | 18: Q, SL, GG| 19: omega | 21: etadot | 22: clwc+ciwc
    15081515        fdict = {'10':None, '11':None, '12':None, '13':None, '16':None,
    15091516                 '17':None, '18':None, '19':None, '21':None, '22':None}
     
    17401747
    17411748        '''
     1749        from eccodes import (codes_grib_new_from_file, codes_get_array,
     1750                             codes_set_array, codes_release, codes_set_values,
     1751                             codes_set, codes_write, codes_release)
     1752
    17421753        # max number
    17431754        maxnum = int(self.number.split('/')[-1])
     
    18551866        return
    18561867
    1857 
    1858     def prepare_fp_files(self, c):
    1859         '''Conversion of GRIB files to FLEXPART binary format.
    1860 
    1861         Parameters
    1862         ----------
    1863         c : ControlFile
    1864             Contains all the parameters of CONTROL file and
    1865             command line.
    1866 
    1867         Return
    1868         ------
    1869 
    1870         '''
    1871         # generate AVAILABLE file
    1872         # Example of AVAILABLE file data:
    1873         # 20131107 000000      EN13110700              ON DISC
    1874         clist = []
    1875         for ofile in self.outputfilelist:
    1876             fname = ofile.split('/')
    1877             if '.' in fname[-1]:
    1878                 l = fname[-1].split('.')
    1879                 timestamp = datetime.strptime(l[0][-6:] + l[1],
    1880                                               '%y%m%d%H')
    1881                 timestamp += timedelta(hours=int(l[2]))
    1882                 cdate = datetime.strftime(timestamp, '%Y%m%d')
    1883                 chms = datetime.strftime(timestamp, '%H%M%S')
    1884             else:
    1885                 cdate = '20' + fname[-1][-8:-2]
    1886                 chms = fname[-1][-2:] + '0000'
    1887             clist.append(cdate + ' ' + chms + ' '*6 +
    1888                          fname[-1] + ' '*14 + 'ON DISC')
    1889         clist.sort()
    1890         with open(c.outputdir + '/' + 'AVAILABLE', 'w') as f:
    1891             f.write('\n'.join(clist) + '\n')
    1892 
    1893         # generate pathnames file
    1894         pwd = os.path.abspath(c.outputdir)
    1895         with open(pwd + '/pathnames', 'w') as f:
    1896             f.write(pwd + '/Options/\n')
    1897             f.write(pwd + '/\n')
    1898             f.write(pwd + '/\n')
    1899             f.write(pwd + '/AVAILABLE\n')
    1900             f.write(' = == = == = == = == = == ==  = \n')
    1901 
    1902         # create Options dir if necessary
    1903         if not os.path.exists(pwd + '/Options'):
    1904             make_dir(pwd+'/Options')
    1905 
    1906         # read template COMMAND file
    1907         with open(os.path.expandvars(os.path.expanduser(
    1908             c.flexpartdir)) + '/../Options/COMMAND', 'r') as f:
    1909             lflist = f.read().split('\n')
    1910 
    1911         # find index of list where to put in the
    1912         # date and time information
    1913         # usually after the LDIRECT parameter
    1914         i = 0
    1915         for l in lflist:
    1916             if 'LDIRECT' in l.upper():
    1917                 break
    1918             i += 1
    1919 
    1920         # insert the date and time information of run start and end
    1921         # into the list of lines of COMMAND file
    1922         lflist = lflist[:i+1] + \
    1923                  [clist[0][:16], clist[-1][:16]] + \
    1924                  lflist[i+3:]
    1925 
    1926         # write the new COMMAND file
    1927         with open(pwd + '/Options/COMMAND', 'w') as g:
    1928             g.write('\n'.join(lflist) + '\n')
    1929 
    1930         # change to outputdir and start the grib2flexpart run
    1931         # afterwards switch back to the working dir
    1932         os.chdir(c.outputdir)
    1933         cmd = [os.path.expandvars(os.path.expanduser(c.flexpartdir)) +
    1934          '/../FLEXPART_PROGRAM/grib2flexpart', 'useAvailable', '.']
    1935         execute_subprocess(cmd)
    1936         os.chdir(pwd)
    1937 
    1938         return
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG