Changeset 0f89116 in flex_extract.git for Source/Python/Classes/EcFlexpart.py
- Timestamp:
- Dec 19, 2019, 8:07:12 PM (4 years ago)
- Branches:
- master, ctbto, dev
- Children:
- a73c0f6
- Parents:
- 0d99607
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Source/Python/Classes/EcFlexpart.py
rc77630a r0f89116 53 53 #pylint: disable=consider-using-enumerate 54 54 # this is not useful in this case 55 #pylint: disable=unsubscriptable-object 56 # this error is a bug 57 #pylint: disable=ungrouped-imports 58 # not necessary that we group the imports 55 59 # ------------------------------------------------------------------------------ 56 60 # MODULES … … 62 66 import glob 63 67 import shutil 64 import subprocess65 68 from datetime import datetime, timedelta 66 69 67 70 # software specific classes and modules from flex_extract 71 #pylint: disable=wrong-import-position 68 72 sys.path.append('../') 69 73 import _config 70 74 from Classes.GribUtil import GribUtil 71 75 from Mods.tools import (init128, to_param_id, silent_remove, product, 72 my_error, make_dir,get_informations, get_dimensions,76 my_error, get_informations, get_dimensions, 73 77 execute_subprocess, to_param_id_with_tablenumber, 74 78 generate_retrieval_period_boundary) … … 76 80 from Classes.UioFiles import UioFiles 77 81 import Mods.disaggregation as disaggregation 78 82 #pylint: enable=wrong-import-position 79 83 # ------------------------------------------------------------------------------ 80 84 # CLASS … … 401 405 else: # GAUSS and ETA 402 406 print('Warning: Collecting etadot and parameters for gaussian grid ' 403 404 407 'is a very costly parameter combination, ' 408 'use this combination only for debugging!') 405 409 self.params['GG__SL'] = ['Q', 'ML', '1', 406 410 '{}'.format((int(self.resol) + 1) // 2)] … … 415 419 416 420 # ADDITIONAL FIELDS FOR FLEXPART-WRF MODEL (IF QUESTIONED) 417 # ---------------------------------------------------------------------- -421 # ---------------------------------------------------------------------- 418 422 if wrf: 419 423 # @WRF … … 710 714 retr_param_dict['date'] = self.dates.split('/')[0] 711 715 retr_param_dict['target'] = self._mk_targetname('', 712 pk, retr_param_dict['date']) 716 pk, 717 retr_param_dict['date']) 713 718 elif pk == 'OG_OROLSM__SL' and oro: 714 719 continue … … 752 757 if 'acc' in pk: 753 758 startdate = retr_param_dict['date'].split('/')[0] 754 enddate = datetime.strftime(elimit - t24h, '%Y%m%d')759 enddate = datetime.strftime(elimit - t24h, '%Y%m%d') 755 760 retr_param_dict['date'] = '/'.join([startdate, 756 761 'to', … … 781 786 timesave = ''.join(retr_param_dict['time']) 782 787 783 if ('/' in retr_param_dict['time'] and784 pk != 'OG_OROLSM__SL' and785 'acc' not in pk ):788 if all(['/' in retr_param_dict['time'], 789 pk != 'OG_OROLSM__SL', 790 'acc' not in pk]): 786 791 times = retr_param_dict['time'].split('/') 787 792 steps = retr_param_dict['step'].split('/') … … 794 799 retr_param_dict['time'] = times[0] 795 800 796 if (pk != 'OG_OROLSM__SL' and797 int(retr_param_dict['step'].split('/')[0]) == 0 and798 int(timesave.split('/')[0]) == 0):801 if all([pk != 'OG_OROLSM__SL', 802 int(retr_param_dict['step'].split('/')[0]) == 0, 803 int(timesave.split('/')[0]) == 0]): 799 804 800 805 retr_param_dict['date'] = \ … … 810 815 else: 811 816 raise ValueError('ERROR: Basetime has an invalid value ' 812 '-> {}'.format(str(basetime)))817 '-> {}'.format(str(self.basetime))) 813 818 814 819 if request == 0 or request == 2: … … 860 865 861 866 stream = namelist_template.generate( 862 maxl =str(maxl),863 maxb =str(maxb),864 mlevel =str(self.level),865 mlevelist =str(self.levelist),866 mnauf =str(self.resol),867 metapar ='77',868 rlo0 =str(area[1]),869 rlo1 =str(area[3]),870 rla0 =str(area[2]),871 rla1 =str(area[0]),872 momega =str(c.omega),873 momegadiff =str(c.omegadiff),874 mgauss =str(c.gauss),875 msmooth =str(c.smooth),876 meta =str(c.eta),877 metadiff =str(c.etadiff),878 mdpdeta =str(c.dpdeta)867 maxl=str(maxl), 868 maxb=str(maxb), 869 mlevel=str(self.level), 870 mlevelist=str(self.levelist), 871 mnauf=str(self.resol), 872 metapar='77', 873 rlo0=str(area[1]), 874 rlo1=str(area[3]), 875 rla0=str(area[2]), 876 rla1=str(area[0]), 877 momega=str(c.omega), 878 momegadiff=str(c.omegadiff), 879 mgauss=str(c.gauss), 880 msmooth=str(c.smooth), 881 meta=str(c.eta), 882 metadiff=str(c.etadiff), 883 mdpdeta=str(c.dpdeta) 879 884 ) 880 885 except UndefinedError as e: … … 928 933 ''' 929 934 import numpy as np 930 from eccodes import (codes_index_select, codes_ new_from_index, codes_get,935 from eccodes import (codes_index_select, codes_get, 931 936 codes_get_values, codes_set_values, codes_set, 932 937 codes_write, codes_release, codes_new_from_index, … … 1053 1058 step = codes_get(gid, 'step') # integer 1054 1059 ctime = '{:0>2}'.format(time) 1055 cstep = '{:0>3}'.format(step)1056 1060 1057 1061 t_date = datetime.strptime(cdate + ctime, '%Y%m%d%H') … … 1132 1136 deac_vals[parId].append( 1133 1137 (orig_vals[parId][-1] - orig_vals[parId][-2]) / 1134 1138 int(c.dtime)) 1135 1139 1136 1140 # store precipitation if new disaggregation method is selected … … 1332 1336 for inum in range(maxnum): 1333 1337 for ix in range(ni*nj): 1334 lsp_new_np[inum, ix,:] = disaggregation.IA3(lsp_np[inum,ix,:])[:-1]1335 cp_new_np[inum, ix,:] = disaggregation.IA3(cp_np[inum,ix,:])[:-1]1338 lsp_new_np[inum, ix, :] = disaggregation.IA3(lsp_np[inum, ix, :])[:-1] 1339 cp_new_np[inum, ix, :] = disaggregation.IA3(cp_np[inum, ix, :])[:-1] 1336 1340 else: 1337 1341 for ix in range(ni*nj): 1338 lsp_new_np[0, ix,:] = disaggregation.IA3(lsp_np[ix,:])[:-1]1339 cp_new_np[0, ix,:] = disaggregation.IA3(cp_np[ix,:])[:-1]1342 lsp_new_np[0, ix, :] = disaggregation.IA3(lsp_np[ix, :])[:-1] 1343 cp_new_np[0, ix, :] = disaggregation.IA3(cp_np[ix, :])[:-1] 1340 1344 1341 1345 # write to grib files (full/orig times to flux file and inbetween … … 1402 1406 # write original time step to flux file as usual 1403 1407 fluxfile = GribUtil(os.path.join(c.inputdir, fluxfilename)) 1404 fluxfile.set_keys(tmpfile, filemode='ab', strict=True,1408 fluxfile.set_keys(tmpfile, filemode='ab', 1405 1409 wherekeynames=['paramId'], wherekeyvalues=[142], 1406 keynames=['perturbationNumber','date','time','stepRange','values'], 1410 keynames=['perturbationNumber', 'date', 'time', 1411 'stepRange', 'values'], 1407 1412 keyvalues=[inumb, int(date.strftime('%Y%m%d')), 1408 date.hour*100, 0, lsp_new_np[inumb, :,it]],1413 date.hour*100, 0, lsp_new_np[inumb, :, it]], 1409 1414 ) 1410 fluxfile.set_keys(tmpfile, filemode='ab', strict=True,1415 fluxfile.set_keys(tmpfile, filemode='ab', 1411 1416 wherekeynames=['paramId'], wherekeyvalues=[143], 1412 keynames=['perturbationNumber','date','time','stepRange','values'], 1413 keyvalues=[inumb,int(date.strftime('%Y%m%d')), 1414 date.hour*100, 0, cp_new_np[inumb,:,it]] 1417 keynames=['perturbationNumber', 'date', 'time', 1418 'stepRange', 'values'], 1419 keyvalues=[inumb, int(date.strftime('%Y%m%d')), 1420 date.hour*100, 0, cp_new_np[inumb, :, it]] 1415 1421 ) 1416 1422 1417 1423 # rr for first subgrid point is identified by step = 1 1418 fluxfile.set_keys(tmpfile, filemode='ab', strict=True,1424 fluxfile.set_keys(tmpfile, filemode='ab', 1419 1425 wherekeynames=['paramId'], wherekeyvalues=[142], 1420 keynames=['perturbationNumber','date','time','stepRange','values'], 1421 keyvalues=[inumb,int(date.strftime('%Y%m%d')), 1422 date.hour*100, '1', lsp_new_np[inumb,:,it+1]] 1423 ) 1424 fluxfile.set_keys(tmpfile, filemode='ab', strict=True, 1426 keynames=['perturbationNumber', 'date', 'time', 1427 'stepRange', 'values'], 1428 keyvalues=[inumb, int(date.strftime('%Y%m%d')), 1429 date.hour*100, '1', lsp_new_np[inumb, :, it+1]] 1430 ) 1431 fluxfile.set_keys(tmpfile, filemode='ab', 1425 1432 wherekeynames=['paramId'], wherekeyvalues=[143], 1426 keynames=['perturbationNumber','date','time','stepRange','values'], 1427 keyvalues=[inumb,int(date.strftime('%Y%m%d')), 1428 date.hour*100, '1', cp_new_np[inumb,:,it+1]] 1429 ) 1433 keynames=['perturbationNumber', 'date', 'time', 1434 'stepRange', 'values'], 1435 keyvalues=[inumb, int(date.strftime('%Y%m%d')), 1436 date.hour*100, '1', cp_new_np[inumb, :, it+1]] 1437 ) 1430 1438 1431 1439 # rr for second subgrid point is identified by step = 2 1432 fluxfile.set_keys(tmpfile, filemode='ab', strict=True,1440 fluxfile.set_keys(tmpfile, filemode='ab', 1433 1441 wherekeynames=['paramId'], wherekeyvalues=[142], 1434 keynames=['perturbationNumber','date','time','stepRange','values'], 1435 keyvalues=[inumb,int(date.strftime('%Y%m%d')), 1436 date.hour*100, '2', lsp_new_np[inumb,:,it+2]] 1437 ) 1438 fluxfile.set_keys(tmpfile, filemode='ab', strict=True, 1442 keynames=['perturbationNumber', 'date', 'time', 1443 'stepRange', 'values'], 1444 keyvalues=[inumb, int(date.strftime('%Y%m%d')), 1445 date.hour*100, '2', lsp_new_np[inumb, :, it+2]] 1446 ) 1447 fluxfile.set_keys(tmpfile, filemode='ab', 1439 1448 wherekeynames=['paramId'], wherekeyvalues=[143], 1440 keynames=['perturbationNumber','date','time','stepRange','values'], 1441 keyvalues=[inumb,int(date.strftime('%Y%m%d')), 1442 date.hour*100, '2', cp_new_np[inumb,:,it+2]] 1443 ) 1449 keynames=['perturbationNumber', 'date', 'time', 1450 'stepRange', 'values'], 1451 keyvalues=[inumb, int(date.strftime('%Y%m%d')), 1452 date.hour*100, '2', cp_new_np[inumb, :, it+2]] 1453 ) 1444 1454 1445 1455 it = it + 3 # jump to next original time step in rr fields … … 1463 1473 ''' 1464 1474 1465 gribfile = GribUtil(os.path.join(inputdir, 'rr_grib_dummy.grb'))1475 gribfile = GribUtil(os.path.join(inputdir, 'rr_grib_dummy.grb')) 1466 1476 1467 1477 gribfile.copy_dummy_msg(ifile, keynames=['paramId'], 1468 keyvalues=[142], filemode='wb')1478 keyvalues=[142], filemode='wb') 1469 1479 1470 1480 gribfile.copy_dummy_msg(ifile, keynames=['paramId'], 1471 keyvalues=[143], filemode='ab')1481 keyvalues=[143], filemode='ab') 1472 1482 1473 1483 return … … 1501 1511 1502 1512 ''' 1503 from eccodes import (codes_index_select, codes_ new_from_index, codes_get,1513 from eccodes import (codes_index_select, codes_get, 1504 1514 codes_get_values, codes_set_values, codes_set, 1505 1515 codes_write, codes_release, codes_new_from_index, … … 1583 1593 cdate_hour = datetime.strftime(timestamp, '%Y%m%d%H') 1584 1594 1595 # if basetime is used, adapt start/end date period 1596 if c.basetime is not None: 1597 time_delta = timedelta(hours=12-int(c.dtime)) 1598 start_period = datetime.strptime(c.end_date + str(c.basetime), 1599 '%Y%m%d%H') - time_delta 1600 end_period = datetime.strptime(c.end_date + str(c.basetime), 1601 '%Y%m%d%H') 1602 1585 1603 # skip all temporary times 1586 # which are outside the retrieval period 1604 # which are outside the retrieval period 1587 1605 if timestamp < start_period or \ 1588 1606 timestamp > end_period: 1589 1607 continue 1590 1591 # if the timestamp is out of basetime start/end date period, 1592 # skip this specific product 1593 if c.basetime is not None: 1594 time_delta = timedelta(hours=12-int(c.dtime)) 1595 start_time = datetime.strptime(c.end_date + str(c.basetime), 1596 '%Y%m%d%H') - time_delta 1597 end_time = datetime.strptime(c.end_date + str(c.basetime), 1598 '%Y%m%d%H') 1599 if timestamp < start_time or timestamp > end_time: 1600 continue 1608 1601 1609 1602 1610 # @WRF … … 1620 1628 paramId = codes_get(gid, 'paramId') 1621 1629 gridtype = codes_get(gid, 'gridType') 1622 levtype = codes_get(gid, 'typeOfLevel')1623 1630 if paramId == 77: # ETADOT 1624 1631 codes_write(gid, fdict['21']) … … 1704 1711 # Fortran program creates file fort.15 (with u,v,etadot,t,sp,q) 1705 1712 execute_subprocess([os.path.join(c.exedir, 1706 _config.FORTRAN_EXECUTABLE)],1713 _config.FORTRAN_EXECUTABLE)], 1707 1714 error_msg='FORTRAN PROGRAM FAILED!')#shell=True) 1708 1715 … … 1733 1740 # to the outputfile (final GRIB input files for FLEXPART) 1734 1741 orolsm = os.path.basename(glob.glob(c.inputdir + 1735 '/OG_OROLSM__SL.*.' + c.ppid + '*')[0]) 1742 '/OG_OROLSM__SL.*.' + 1743 c.ppid + 1744 '*')[0]) 1736 1745 fluxfile = 'flux' + cdate[0:2] + suffix 1737 1746 if not c.cwc: … … 1783 1792 ''' 1784 1793 from eccodes import (codes_grib_new_from_file, codes_get_array, 1785 codes_set_array, codes_release, codes_set_values,1786 codes_set, codes_write , codes_release)1794 codes_set_array, codes_release, 1795 codes_set, codes_write) 1787 1796 1788 1797 # max number … … 1790 1799 1791 1800 # get a list of all prepared output files with control forecast (CF) 1792 CF_filelist = UioFiles(path, prefix + '*.N000')1793 CF_filelist.files = sorted(CF_filelist.files)1794 1795 for cffile in CF_filelist.files:1801 cf_filelist = UioFiles(path, prefix + '*.N000') 1802 cf_filelist.files = sorted(cf_filelist.files) 1803 1804 for cffile in cf_filelist.files: 1796 1805 with open(cffile, 'rb') as f: 1797 cfvalues =[]1806 cfvalues = [] 1798 1807 while True: 1799 1808 fid = codes_grib_new_from_file(f) … … 1900 1909 1901 1910 return 1902
Note: See TracChangeset
for help on using the changeset viewer.