Changeset 25b14be in flex_extract.git for source/pythontest/TestInstallTar/flex_extract_v7.1_ecgate/source/python/classes/EcFlexpart.py
- Timestamp:
- Sep 23, 2018, 11:40:28 AM (6 years ago)
- Branches:
- master, ctbto, dev
- Children:
- 4971f63
- Parents:
- 5d42acd
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
source/pythontest/TestInstallTar/flex_extract_v7.1_ecgate/source/python/classes/EcFlexpart.py
r2fb99de r25b14be 61 61 # 62 62 # @Class Attributes: 63 # - dtime 64 # - basetime 65 # - server 66 # - marsclass 67 # - stream 68 # - resol 69 # - accuracy 70 # - number 71 # - expver 72 # - glevelist 73 # - area 74 # - grid 75 # - level 76 # - levelist 77 # - types 78 # - dates 79 # - area 80 # - gaussian 81 # - params 82 # - inputdir 83 # - outputfilelist 63 # 64 # TODO 84 65 # 85 66 #******************************************************************************* 86 67 #pylint: disable=unsupported-assignment-operation 87 # this is disabled because its an error in pylint for this specific case68 # this is disabled because for this specific case its an error in pylint 88 69 #pylint: disable=consider-using-enumerate 89 70 # this is not useful in this case … … 104 85 import _config 105 86 from GribTools import GribTools 106 from tools import init128, to_param_id, silent_remove, product, my_error87 from mods.tools import init128, to_param_id, silent_remove, product, my_error 107 88 from MarsRetrieval import MarsRetrieval 108 import disaggregation89 import mods.disaggregation 109 90 110 91 # ------------------------------------------------------------------------------ … … 205 186 self.accuracy = c.accuracy 206 187 self.level = c.level 207 208 if c.levelist: 209 self.levelist = c.levelist 210 else: 211 self.levelist = '1/to/' + c.level 212 188 self.expver = c.expver 189 self.levelist = c.levelist 213 190 # for gaussian grid retrieval 214 191 self.glevelist = '1/to/' + c.level … … 218 195 else: 219 196 self.gaussian = '' 220 221 if hasattr(c, 'expver') and c.expver:222 self.expver = c.expver223 else:224 self.expver = '1'225 226 if hasattr(c, 'number') and c.number:227 self.number = c.number228 else:229 self.number = '0'230 197 231 198 if 'N' in c.grid: # Gaussian output grid … … 251 218 # 4) Download also data for WRF 252 219 253 254 220 # Different grids need different retrievals 255 221 # SH = Spherical Harmonics, GG = Gaussian Grid, … … 275 241 self.params['OG__ML'] = ['T/Q', 'ML', self.levelist, self.grid] 276 242 277 if c.gauss == '0' and c.eta == '1': 243 #if c.gauss == '0' and c.eta == '1': 244 if not c.gauss and c.eta: 278 245 # the simplest case 279 246 self.params['OG__ML'][0] += '/U/V/77' 280 elif c.gauss == '0' and c.eta == '0': 247 #elif c.gauss == '0' and c.eta == '0': 248 elif not c.gauss and not c.eta: 281 249 # this is not recommended (inaccurate) 282 250 self.params['OG__ML'][0] += '/U/V' 283 elif c.gauss == '1' and c.eta == '0': 251 #elif c.gauss == '1' and c.eta == '0': 252 elif c.gauss and not c.eta: 284 253 # this is needed for data before 2008, or for reanalysis data 285 254 self.params['GG__SL'] = ['Q', 'ML', '1', \ … … 287 256 self.params['SH__ML'] = ['U/V/D', 'ML', self.glevelist, 'OFF'] 288 257 else: 289 print('Warning: This is a very costly parameter combination, '290 'use only for debugging!')258 print('Warning: This is a very costly parameter combination, \ 259 use only for debugging!') 291 260 self.params['GG__SL'] = ['Q', 'ML', '1', \ 292 261 '{}'.format((int(self.resol) + 1) / 2)] … … 294 263 '{}'.format((int(self.resol) + 1) / 2)] 295 264 296 if hasattr(c, 'omega') and c.omega == '1':265 if c.omega: 297 266 self.params['OG__ML'][0] += '/W' 298 267 299 268 # add cloud water content if necessary 300 if hasattr(c, 'cwc') and c.cwc == '1':269 if c.cwc: 301 270 self.params['OG__ML'][0] += '/CLWC/CIWC' 302 271 303 272 # add vorticity and geopotential height for WRF if necessary 304 if hasattr(c, 'wrf') and c.wrf == '1':273 if c.wrf: 305 274 self.params['OG__ML'][0] += '/Z/VO' 306 275 if '/D' not in self.params['OG__ML'][0]: … … 439 408 pass 440 409 if pk == 'OG_OROLSM__SL': 441 if oro is False:410 if not oro: 442 411 mfstream = 'OPER' 443 412 mftype = 'AN' … … 459 428 460 429 # ------ on demand path -------------------------------------------------- 461 if self.basetime is None:430 if not self.basetime: 462 431 MR = MarsRetrieval(self.server, 463 432 marsclass=self.marsclass, … … 483 452 MR.data_retrieve() 484 453 elif request == 1: 485 MR.print_info( )454 MR.print_info(self.inputdir) 486 455 elif request == 2: 487 MR.print_info( )456 MR.print_info(self.inputdir) 488 457 MR.display_info() 489 458 MR.data_retrieve() 490 459 else: 491 print 'Failure'460 print('Failure') 492 461 # ------ operational path ------------------------------------------------ 493 462 else: … … 717 686 ''' 718 687 719 print '\n\nPostprocessing:\n Format: {}\n'.format(c.format)720 721 if c.ecapi is False:688 print('\n\nPostprocessing:\n Format: {}\n'.format(c.format)) 689 690 if not c.ecapi: 722 691 print('ecstorage: {}\n ecfsdir: {}\n'. 723 692 format(c.ecstorage, c.ecfsdir)) 724 if not hasattr(c, 'gateway'):725 c.gateway = os.getenv('GATEWAY')726 if not hasattr(c, 'destination'):727 c.destination = os.getenv('DESTINATION')693 #if not hasattr(c, 'gateway'): 694 # c.gateway = os.getenv('GATEWAY') 695 #if not hasattr(c, 'destination'): 696 # c.destination = os.getenv('DESTINATION') 728 697 print('ectrans: {}\n gateway: {}\n destination: {}\n ' 729 698 .format(c.ectrans, c.gateway, c.destination)) 730 699 731 print 'Output filelist: \n'732 print self.outputfilelist700 print('Output filelist: \n') 701 print(self.outputfilelist) 733 702 734 703 if c.format.lower() == 'grib2': … … 739 708 p = subprocess.check_call(['mv', ofile + '_2', ofile]) 740 709 741 if int(c.ectrans) == 1 and c.ecapi is False:710 if c.ectrans and not c.ecapi: 742 711 for ofile in self.outputfilelist: 743 712 p = subprocess.check_call(['ectrans', '-overwrite', '-gateway', … … 746 715 #print('ectrans:', p) 747 716 748 if int(c.ecstorage) == 1 and c.ecapi is False:717 if c.ecstorage and not c.ecapi: 749 718 for ofile in self.outputfilelist: 750 719 p = subprocess.check_call(['ecp', '-o', ofile, … … 757 726 # prepare environment for the grib2flexpart run 758 727 # to convert grib to flexpart binary 759 if c.grib2flexpart == '1':728 if c.grib2flexpart: 760 729 761 730 # generate AVAILABLE file … … 873 842 874 843 index_keys = ["date", "time", "step"] 875 indexfile = c.inputdir + "/date_time_stepRange.idx"844 indexfile = os.path.join(c.inputdir, _config.FILE_GRIB_INDEX) 876 845 silent_remove(indexfile) 877 846 grib = GribTools(inputfiles.files) … … 883 852 for key in index_keys: 884 853 index_vals.append(grib_index_get(iid, key)) 885 print index_vals[-1]854 print(index_vals[-1]) 886 855 # index_vals looks for example like: 887 856 # index_vals[0]: ('20171106', '20171107', '20171108') ; date … … 893 862 894 863 for prod in product(*index_vals): 864 # e.g. prod = ('20170505', '0', '12') 865 # ( date ,time, step) 866 # per date e.g. time = 0, 1200 867 # per time e.g. step = 3, 6, 9, 12 895 868 # flag for Fortran program CONVERT2 and file merging 896 869 convertFlag = False 897 print 'current prod: ', prod870 print('current prod: ', prod) 898 871 # e.g. prod = ('20170505', '0', '12') 899 872 # ( date ,time, step) … … 917 890 # they are just valid for a single product 918 891 for k, f in fdict.iteritems(): 919 silent_remove(c.inputdir + "/fort." + k) 920 fdict[k] = open(c.inputdir + '/fort.' + k, 'w') 892 fortfile = os.path.join(c.inputdir, 'fort.' + k) 893 silent_remove(fortfile) 894 fdict[k] = open(fortfile, 'w') 921 895 922 896 cdate = str(grib_get(gid, 'date')) … … 946 920 if timestamp < slimit or timestamp > elimit: 947 921 continue 922 else: 923 pass 948 924 949 925 try: 950 if c.wrf == '1': 951 if 'olddate' not in locals(): 952 fwrf = open(c.outputdir + '/WRF' + cdate + 953 '.{:0>2}'.format(time) + '.000.grb2', 'w') 926 if c.wrf: 927 if 'olddate' not in locals() or cdate != olddate: 928 fwrf = open(os.path.join(c.outputdir, 929 'WRF' + cdate + '.{:0>2}'.format(time) + 930 '.000.grb2'), 'w') 954 931 olddate = cdate[:] 955 else:956 if cdate != olddate:957 fwrf = open(c.outputdir + '/WRF' + cdate +958 '.{:0>2}'.format(time) + '.000.grb2',959 'w')960 olddate = cdate[:]961 932 except AttributeError: 962 933 pass 963 934 964 # helper variable to remember which fields are already used.935 # helper variable to remember which fields were already used. 965 936 savedfields = [] 966 937 while 1: … … 973 944 # Specific humidity (Q.grb) is used as a template only 974 945 # so we need the first we "meet" 975 with open( c.inputdir + '/fort.18', 'w') as fout:946 with open(os.path.join(c.inputdir, 'fort.18'), 'w') as fout: 976 947 grib_write(gid, fout) 977 948 elif paramId == 131 or paramId == 132: … … 986 957 grib_write(gid, fdict['13']) 987 958 elif paramId in [129, 138, 155] and levtype == 'hybrid' \ 988 and c.wrf == '1':959 and c.wrf: 989 960 pass 990 961 elif paramId == 246 or paramId == 247: … … 1006 977 savedfields.append(paramId) 1007 978 else: 1008 print 'duplicate ' + str(paramId) + ' not written'979 print('duplicate ' + str(paramId) + ' not written') 1009 980 1010 981 try: 1011 if c.wrf == '1': 1012 if levtype == 'hybrid': # model layer 1013 if paramId in [129, 130, 131, 132, 133, 138, 155]: 1014 grib_write(gid, fwrf) 1015 else: # sfc layer 1016 if paramId in wrfpars: 1017 grib_write(gid, fwrf) 982 if c.wrf: 983 # model layer 984 if levtype == 'hybrid' and \ 985 paramId in [129, 130, 131, 132, 133, 138, 155]: 986 grib_write(gid, fwrf) 987 # sfc layer 988 elif paramId in wrfpars: 989 grib_write(gid, fwrf) 1018 990 except AttributeError: 1019 991 pass … … 1029 1001 pwd = os.getcwd() 1030 1002 os.chdir(c.inputdir) 1031 if os.stat('fort.21').st_size == 0 and int(c.eta) == 1:1032 print 1033 not available for this type or date/time\n' 1034 print 'Check parameters CLASS, TYPE, STREAM, START_DATE\n'1003 if os.stat('fort.21').st_size == 0 and c.eta: 1004 print('Parameter 77 (etadot) is missing, most likely it is \ 1005 not available for this type or date/time\n') 1006 print('Check parameters CLASS, TYPE, STREAM, START_DATE\n') 1035 1007 my_error(c.mailfail, 'fort.21 is empty while parameter eta \ 1036 1008 is set to 1 in CONTROL file') … … 1038 1010 # create the corresponding output file fort.15 1039 1011 # (generated by CONVERT2) + fort.16 (paramId 167 and 168) 1040 p = subprocess.check_call( 1041 [os.path.expandvars(os.path.expanduser(c.exedir)) + 1042 '/CONVERT2'], shell=True) 1012 p = subprocess.check_call([os.path.join(c.exedir, 'CONVERT2')], 1013 shell=True) 1043 1014 os.chdir(pwd) 1044 1015 1045 1016 # create final output filename, e.g. EN13040500 (ENYYMMDDHH) 1046 fnout = c.inputdir + '/' + c.prefix1017 fnout = os.path.join(c.inputdir, c.prefix) 1047 1018 if c.maxstep > 12: 1048 1019 suffix = cdate[2:8] + '.{:0>2}'.format(time/100) + \ … … 1051 1022 suffix = cdateH[2:10] 1052 1023 fnout += suffix 1053 print "outputfile = " + fnout1024 print("outputfile = " + fnout) 1054 1025 self.outputfilelist.append(fnout) # needed for final processing 1055 1026 … … 1059 1030 c.inputdir + '/OG_OROLSM__SL.*.' + c.ppid + '*')[0]) 1060 1031 fluxfile = 'flux' + cdate[0:2] + suffix 1061 if c.cwc != '1':1032 if not c.cwc: 1062 1033 flist = ['fort.15', fluxfile, 'fort.16', orolsm] 1063 1034 else: … … 1066 1037 with open(fnout, 'wb') as fout: 1067 1038 for f in flist: 1039 shutil.copyfileobj(open(os.path.join(c.inputdir, f), 1040 'rb'), fout) 1041 1042 if c.omega: 1043 with open(os.path.join(c.outputdir, 'OMEGA'), 'wb') as fout: 1068 1044 shutil.copyfileobj( 1069 open(c.inputdir + '/' + f, 'rb'), fout) 1070 1071 if c.omega == '1': 1072 with open(c.outputdir + '/OMEGA', 'wb') as fout: 1073 shutil.copyfileobj( 1074 open(c.inputdir + '/fort.25', 'rb'), fout) 1075 1076 if hasattr(c, 'wrf') and c.wrf == '1': 1045 open(os.path.join(c.inputdir, 'fort.25'), 1046 'rb'), fout) 1047 else: 1048 pass 1049 1050 if c.wrf: 1077 1051 fwrf.close() 1078 1052 … … 1117 1091 pars = to_param_id(self.params['OG_acc_SL'][0], table128) 1118 1092 index_keys = ["date", "time", "step"] 1119 indexfile = c.inputdir + "/date_time_stepRange.idx"1093 indexfile = os.path.join(c.inputdir, _config.FILE_GRIB_INDEX) 1120 1094 silent_remove(indexfile) 1121 1095 grib = GribTools(inputfiles.files) … … 1127 1101 for key in index_keys: 1128 1102 key_vals = grib_index_get(iid, key) 1129 print key_vals1103 print(key_vals) 1130 1104 # have to sort the steps for disaggregation, 1131 1105 # therefore convert to int first … … 1148 1122 stepsdict[str(p)] = [] 1149 1123 1150 print 'maxstep: ', c.maxstep1124 print('maxstep: ', c.maxstep) 1151 1125 1152 1126 for prod in product(*index_vals): … … 1155 1129 # per date e.g. time = 0, 1200 1156 1130 # per time e.g. step = 3, 6, 9, 12 1131 print('current prod: ', prod) 1157 1132 for i in range(len(index_keys)): 1158 1133 grib_index_select(iid, index_keys[i], prod[i]) 1159 1134 1135 # get first id from current product 1160 1136 gid = grib_new_from_index(iid) 1137 1138 # if there is data for this product combination 1139 # prepare some date and time parameter before reading the data 1161 1140 if gid is not None: 1162 1141 cdate = grib_get(gid, 'date') … … 1176 1155 1177 1156 if c.maxstep > 12: 1178 fnout = c.inputdir + '/flux' + \ 1179 sdate.strftime('%Y%m%d') + '.{:0>2}'.format(time/100) + \ 1180 '.{:0>3}'.format(step-2*int(c.dtime)) 1181 gnout = c.inputdir + '/flux' + \ 1182 sdate.strftime('%Y%m%d') + '.{:0>2}'.format(time/100) + \ 1183 '.{:0>3}'.format(step-int(c.dtime)) 1184 hnout = c.inputdir + '/flux' + \ 1185 sdate.strftime('%Y%m%d') + '.{:0>2}'.format(time/100) + \ 1186 '.{:0>3}'.format(step) 1187 g = open(gnout, 'w') 1188 h = open(hnout, 'w') 1157 fnout = os.path.join(c.inputdir, 'flux' + 1158 sdate.strftime('%Y%m%d') + 1159 '.{:0>2}'.format(time/100) + 1160 '.{:0>3}'.format(step-2*int(c.dtime))) 1161 gnout = os.path.join(c.inputdir, 'flux' + 1162 sdate.strftime('%Y%m%d') + 1163 '.{:0>2}'.format(time/100) + 1164 '.{:0>3}'.format(step-int(c.dtime))) 1165 hnout = os.path.join(c.inputdir, 'flux' + 1166 sdate.strftime('%Y%m%d') + 1167 '.{:0>2}'.format(time/100) + 1168 '.{:0>3}'.format(step)) 1189 1169 else: 1190 fnout = c.inputdir + '/flux' + fdate.strftime('%Y%m%d%H') 1191 gnout = c.inputdir + '/flux' + (fdate + 1192 timedelta(hours=int(c.dtime)) 1193 ).strftime('%Y%m%d%H') 1194 hnout = c.inputdir + '/flux' + sdates.strftime('%Y%m%d%H') 1195 g = open(gnout, 'w') 1196 h = open(hnout, 'w') 1197 1198 print "outputfile = " + fnout 1199 f = open(fnout, 'w') 1170 fnout = os.path.join(c.inputdir, 'flux' + 1171 fdate.strftime('%Y%m%d%H')) 1172 gnout = os.path.join(c.inputdir, 'flux' + 1173 (fdate + timedelta(hours=int(c.dtime))). 1174 strftime('%Y%m%d%H')) 1175 hnout = os.path.join(c.inputdir, 'flux' + 1176 sdates.strftime('%Y%m%d%H')) 1177 1178 print("outputfile = " + fnout) 1179 f_handle = open(fnout, 'w') 1180 g_handle = open(gnout, 'w') 1181 h_handle = open(hnout, 'w') 1200 1182 1201 1183 # read message for message and store relevant data fields … … 1258 1240 grib_set(gid, 'date', fdate.year*10000 + 1259 1241 fdate.month*100+fdate.day) 1260 grib_write(gid, f )1242 grib_write(gid, f_handle) 1261 1243 1262 1244 if c.basetime is not None: … … 1285 1267 truedatetime.month * 100 + 1286 1268 truedatetime.day) 1287 grib_write(gid, h )1269 grib_write(gid, h_handle) 1288 1270 1289 1271 #values = (svdp[1]+svdp[2])/2. … … 1300 1282 truedatetime.day) 1301 1283 grib_set_values(gid, values) 1302 grib_write(gid, g )1284 grib_write(gid, g_handle) 1303 1285 1304 1286 grib_release(gid) … … 1306 1288 gid = grib_new_from_index(iid) 1307 1289 1308 f .close()1309 g .close()1310 h .close()1290 f_handle.close() 1291 g_handle.close() 1292 h_handle.close() 1311 1293 1312 1294 grib_index_release(iid)
Note: See TracChangeset
for help on using the changeset viewer.