From ee6773717da3db395bbdf88d12f7794eb06daed3 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Wed, 9 Aug 2017 14:16:58 +0100 Subject: [PATCH] Python examples: code reformatting using PyCharm --- examples/python/binary_message.py | 1 + examples/python/bufr_attributes.py | 2 +- examples/python/bufr_clone.py | 4 +- examples/python/bufr_copy_data.py | 18 +-- examples/python/bufr_encode_flight.py | 9 +- examples/python/bufr_expanded.py | 2 +- examples/python/bufr_get_keys.py | 2 +- examples/python/bufr_keys_iterator.py | 5 +- examples/python/bufr_read_header.py | 2 +- examples/python/bufr_read_scatterometer.py | 2 +- examples/python/bufr_read_synop.py | 2 +- examples/python/bufr_read_temp.py | 15 +- examples/python/bufr_read_tropical_cyclone.py | 132 +++++++++--------- examples/python/bufr_set_keys.py | 2 +- examples/python/bufr_subset.py | 3 +- examples/python/ecc-448.py | 7 +- examples/python/get_product_kind.py | 1 + examples/python/grib_ccsds.py | 1 + examples/python/grib_clone.py | 1 + examples/python/grib_count_messages.py | 1 + examples/python/grib_get_keys.py | 1 + examples/python/grib_get_message_offset.py | 1 + examples/python/grib_index.py | 1 + examples/python/grib_iterator.py | 4 +- examples/python/grib_iterator_bitmap.py | 1 + examples/python/grib_keys_iterator.py | 1 + examples/python/grib_ls_JSON.py | 14 +- examples/python/grib_multi_write.py | 1 + examples/python/grib_nearest.py | 3 +- examples/python/grib_print_data.py | 1 + examples/python/grib_samples.py | 1 + examples/python/grib_set_bitmap.py | 1 + examples/python/grib_set_keys.py | 25 ++-- examples/python/grib_set_missing.py | 3 +- examples/python/grib_set_pv.py | 3 +- examples/python/gts_get_keys.py | 2 +- examples/python/high_level_api.py | 79 ++++++----- examples/python/keys_iterator_gts.py | 1 + examples/python/metar_get_keys.py | 2 +- 39 files changed, 197 insertions(+), 160 deletions(-) diff --git a/examples/python/binary_message.py b/examples/python/binary_message.py index 26d39dd5c..ef508c0fb 100644 --- a/examples/python/binary_message.py +++ b/examples/python/binary_message.py @@ -49,5 +49,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_attributes.py b/examples/python/bufr_attributes.py index 5b811d546..0b0febe05 100644 --- a/examples/python/bufr_attributes.py +++ b/examples/python/bufr_attributes.py @@ -24,7 +24,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -114,5 +113,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_clone.py b/examples/python/bufr_clone.py index 9ac93893d..1ab9c0981 100644 --- a/examples/python/bufr_clone.py +++ b/examples/python/bufr_clone.py @@ -26,8 +26,8 @@ INPUT = '../../data/bufr/syno_1.bufr' OUTPUT = 'bufr_clone_test_p.clone.bufr' VERBOSE = 1 # verbose error reporting -def example(): +def example(): # open BUFR file fin = open(INPUT) @@ -40,7 +40,6 @@ def example(): # create several clones of this message and alter them # in different ways for centre in range(0, 3): - # clone the message clone_id = codes_clone(bufr) @@ -71,5 +70,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_copy_data.py b/examples/python/bufr_copy_data.py index 98e870de0..7263e9c7a 100644 --- a/examples/python/bufr_copy_data.py +++ b/examples/python/bufr_copy_data.py @@ -20,11 +20,12 @@ from eccodes import * VERBOSE = 1 # verbose error reporting + def example(input_filename, output_filename): ibufr = codes_new_from_samples('BUFR3', CODES_PRODUCT_BUFR) f = open(input_filename) ibufrin = codes_bufr_new_from_file(f) - ivalues=( + ivalues = ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, @@ -50,15 +51,15 @@ def example(input_filename, output_filename): codes_set(ibufr, 'numberOfSubsets', 1) codes_set(ibufr, 'observedData', 1) codes_set(ibufr, 'compressedData', 0) - ivalues=( - 307011,7006,10004,222000,101023,31031,1031,1032,101023,33007, - 225000,236000,101023,31031,1031,1032,8024,101001,225255,225000, - 236000,101023,31031,1031,1032,8024,101001,225255, - 1063,2001,4001,4002,4003,4004,4005,5002, - 6002,7001,7006,11001,11016,11017,11002) + ivalues = ( + 307011, 7006, 10004, 222000, 101023, 31031, 1031, 1032, 101023, 33007, + 225000, 236000, 101023, 31031, 1031, 1032, 8024, 101001, 225255, 225000, + 236000, 101023, 31031, 1031, 1032, 8024, 101001, 225255, + 1063, 2001, 4001, 4002, 4003, 4004, 4005, 5002, + 6002, 7001, 7006, 11001, 11016, 11017, 11002) codes_set_array(ibufr, 'unexpandedDescriptors', ivalues) codes_set(ibufrin, 'unpack', 1) - codes_bufr_copy_data(ibufrin, ibufr) # Copy data across + codes_bufr_copy_data(ibufrin, ibufr) # Copy data across with open(output_filename, 'w') as outfile: codes_write(ibufr, outfile) @@ -84,5 +85,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_encode_flight.py b/examples/python/bufr_encode_flight.py index 807f2e9e6..de9e26f70 100644 --- a/examples/python/bufr_encode_flight.py +++ b/examples/python/bufr_encode_flight.py @@ -20,8 +20,8 @@ from eccodes import * VERBOSE = 1 # verbose error reporting -def example(csvfile, input_filename, output_filename): +def example(csvfile, input_filename, output_filename): fbufrin = open(input_filename, 'rb') fbufrout = open(output_filename, 'wb') @@ -63,10 +63,10 @@ def example(csvfile, input_filename, output_filename): # unexpandedDescriptors and BufrTemplate can be set alternatively # to choose the template for the BUFR message - #unexpandedDescriptors = [301051,4006,7002,10004,12001,11001,11002,11031,11032,11033,20041] - #codes_set_array(bufr, 'unexpandedDescriptors', unexpandedDescriptors) + # unexpandedDescriptors = [301051,4006,7002,10004,12001,11001,11002,11031,11032,11033,20041] + # codes_set_array(bufr, 'unexpandedDescriptors', unexpandedDescriptors) - codes_set(bufr,'BufrTemplate','aircraftReportWithSecondsAndPressure') + codes_set(bufr, 'BufrTemplate', 'aircraftReportWithSecondsAndPressure') codes_set_array(bufr, 'year', years) codes_set_array(bufr, 'month', months) @@ -107,5 +107,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_expanded.py b/examples/python/bufr_expanded.py index d6ca4172b..ab6e42e3f 100644 --- a/examples/python/bufr_expanded.py +++ b/examples/python/bufr_expanded.py @@ -27,7 +27,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -80,5 +79,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_get_keys.py b/examples/python/bufr_get_keys.py index 961042690..837f0e748 100644 --- a/examples/python/bufr_get_keys.py +++ b/examples/python/bufr_get_keys.py @@ -26,7 +26,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -124,5 +123,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_keys_iterator.py b/examples/python/bufr_keys_iterator.py index cd73f35d6..be8d00b8d 100644 --- a/examples/python/bufr_keys_iterator.py +++ b/examples/python/bufr_keys_iterator.py @@ -26,7 +26,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -43,14 +42,13 @@ def example(): # we need to instruct ecCodes to expand all the descriptors # i.e. unpack the data values - codes_set(bufr,'unpack',1) + codes_set(bufr, 'unpack', 1) # get BUFR key iterator iterid = codes_bufr_keys_iterator_new(bufr) # loop over the keys while codes_bufr_keys_iterator_next(iterid): - # print key name keyname = codes_bufr_keys_iterator_get_name(iterid) print(" %s" % keyname) @@ -78,5 +76,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_read_header.py b/examples/python/bufr_read_header.py index 52c53b344..0d1c4348d 100644 --- a/examples/python/bufr_read_header.py +++ b/examples/python/bufr_read_header.py @@ -26,7 +26,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -80,5 +79,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_read_scatterometer.py b/examples/python/bufr_read_scatterometer.py index e546ba3e0..6af11915e 100644 --- a/examples/python/bufr_read_scatterometer.py +++ b/examples/python/bufr_read_scatterometer.py @@ -31,7 +31,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -105,5 +104,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_read_synop.py b/examples/python/bufr_read_synop.py index d2bad5675..116159dd8 100644 --- a/examples/python/bufr_read_synop.py +++ b/examples/python/bufr_read_synop.py @@ -29,7 +29,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -101,5 +100,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_read_temp.py b/examples/python/bufr_read_temp.py index 07175f9ed..74c474900 100644 --- a/examples/python/bufr_read_temp.py +++ b/examples/python/bufr_read_temp.py @@ -28,6 +28,7 @@ from six.moves import range INPUT = '../../data/bufr/PraticaTemp.bufr' VERBOSE = 1 # verbose error reporting + def example(): # open bufr file f = open(INPUT) @@ -55,16 +56,19 @@ def example(): windSpeed = codes_get_array(bufr, "windSpeed") blockNumber = codes_get(bufr, "blockNumber") stationNumber = codes_get(bufr, "stationNumber") - print('station %d%d' % (blockNumber,stationNumber)) - print('timePeriod pressure geopotentialHeight latitudeDisplacement longitudeDisplacement airTemperature windDirection windSpeed significance') - for i in range(0,len(windSpeed)-1): - print(timePeriod[i],pressure[i],geopotentialHeight[i],latitudeDisplacement[i],longitudeDisplacement[i],airTemperature[i],windDirection[i],windSpeed[i],extendedVerticalSoundingSignificance[i]) + print('station %d%d' % (blockNumber, stationNumber)) + print( + 'timePeriod pressure geopotentialHeight latitudeDisplacement longitudeDisplacement airTemperature windDirection windSpeed significance') + for i in range(0, len(windSpeed) - 1): + print(timePeriod[i], pressure[i], geopotentialHeight[i], latitudeDisplacement[i], longitudeDisplacement[i], + airTemperature[i], windDirection[i], windSpeed[i], extendedVerticalSoundingSignificance[i]) cnt += 1 # delete handle codes_release(bufr) # close the file f.close() + def main(): try: example() @@ -74,6 +78,7 @@ def main(): else: sys.stderr.write(err.msg + '\n') return 1 + + if __name__ == "__main__": sys.exit(main()) - diff --git a/examples/python/bufr_read_tropical_cyclone.py b/examples/python/bufr_read_tropical_cyclone.py index c5a95a0be..caf13615b 100644 --- a/examples/python/bufr_read_tropical_cyclone.py +++ b/examples/python/bufr_read_tropical_cyclone.py @@ -22,12 +22,10 @@ from six.moves import range INPUT = '../../data/bufr/tropical_cyclone.bufr' VERBOSE = 1 # verbose error reporting - -data=collections.defaultdict(dict) +data = collections.defaultdict(dict) def example(): - # open BUFR file f = open(INPUT) @@ -40,148 +38,149 @@ def example(): if bufr is None: break - print('**************** MESSAGE: ',cnt+1,' *****************') + print('**************** MESSAGE: ', cnt + 1, ' *****************') # we need to instruct ecCodes to expand all the descriptors # i.e. unpack the data values codes_set(bufr, 'unpack', 1) - numObs= codes_get(bufr, "numberOfSubsets") - year = codes_get(bufr, "year") + numObs = codes_get(bufr, "numberOfSubsets") + year = codes_get(bufr, "year") month = codes_get(bufr, "month") - day = codes_get(bufr, "day") - hour = codes_get(bufr, "hour") - minute= codes_get(bufr, "minute") + day = codes_get(bufr, "day") + hour = codes_get(bufr, "hour") + minute = codes_get(bufr, "minute") - print('Date and time: ', day,'.',month,'.',year,' ',hour,':',minute) + print('Date and time: ', day, '.', month, '.', year, ' ', hour, ':', minute) - stormIdentifier = codes_get(bufr,"stormIdentifier") + stormIdentifier = codes_get(bufr, "stormIdentifier") print('Storm identifier: ', stormIdentifier) # How many different timePeriod in the data structure? - numberOfPeriods=0 + numberOfPeriods = 0 while True: - numberOfPeriods=numberOfPeriods+1 + numberOfPeriods = numberOfPeriods + 1 try: - codes_get_array(bufr,"#%d#timePeriod" %numberOfPeriods) + codes_get_array(bufr, "#%d#timePeriod" % numberOfPeriods) except CodesInternalError as err: break - #the numberOfPeriods includes the analysis (period=0) + # the numberOfPeriods includes the analysis (period=0) # Get ensembleMemberNumber memberNumber = codes_get_array(bufr, "ensembleMemberNumber") - memberNumberLen=len(memberNumber) + memberNumberLen = len(memberNumber) # Observed Storm Centre - significance = codes_get(bufr,'#1#meteorologicalAttributeSignificance') - latitudeCentre = codes_get(bufr,'#1#latitude') - longitudeCentre = codes_get(bufr,'#1#longitude') + significance = codes_get(bufr, '#1#meteorologicalAttributeSignificance') + latitudeCentre = codes_get(bufr, '#1#latitude') + longitudeCentre = codes_get(bufr, '#1#longitude') - if significance!=1: + if significance != 1: print('ERROR: unexpected #1#meteorologicalAttributeSignificance') return 1 - if (latitudeCentre==CODES_MISSING_DOUBLE) and (longitudeCentre==CODES_MISSING_DOUBLE): + if (latitudeCentre == CODES_MISSING_DOUBLE) and (longitudeCentre == CODES_MISSING_DOUBLE): print('Observed storm centre position missing') else: - print('Observed storm centre: latitude=',latitudeCentre,' longitude=',longitudeCentre) + print('Observed storm centre: latitude=', latitudeCentre, ' longitude=', longitudeCentre) # Location of storm in perturbed analysis - significance = codes_get(bufr,'#2#meteorologicalAttributeSignificance') + significance = codes_get(bufr, '#2#meteorologicalAttributeSignificance') - if significance!=4: + if significance != 4: print('ERROR: unexpected #2#meteorologicalAttributeSignificance') return 1 - latitudeAnalysis = codes_get_array(bufr,'#2#latitude') - longitudeAnalysis = codes_get_array(bufr,'#2#longitude') - pressureAnalysis = codes_get_array(bufr,'#1#pressureReducedToMeanSeaLevel') + latitudeAnalysis = codes_get_array(bufr, '#2#latitude') + longitudeAnalysis = codes_get_array(bufr, '#2#longitude') + pressureAnalysis = codes_get_array(bufr, '#1#pressureReducedToMeanSeaLevel') # Location of Maximum Wind - significance=codes_get(bufr,'#3#meteorologicalAttributeSignificance') + significance = codes_get(bufr, '#3#meteorologicalAttributeSignificance') - if significance!=3: + if significance != 3: print('ERROR: unexpected #3#meteorologicalAttributeSignificance=', significance) return 1 - latitudeMaxWind0=codes_get_array(bufr,'#3#latitude') - longitudeMaxWind0= codes_get_array(bufr,'#3#longitude') - windMaxWind0= codes_get_array(bufr,'#1#windSpeedAt10M') + latitudeMaxWind0 = codes_get_array(bufr, '#3#latitude') + longitudeMaxWind0 = codes_get_array(bufr, '#3#longitude') + windMaxWind0 = codes_get_array(bufr, '#1#windSpeedAt10M') - if len(latitudeAnalysis)==len(memberNumber) and len(latitudeMaxWind0)==len(memberNumber): + if len(latitudeAnalysis) == len(memberNumber) and len(latitudeMaxWind0) == len(memberNumber): for k in range(len(memberNumber)): - data[k][0]=[latitudeAnalysis[k],longitudeAnalysis[k],pressureAnalysis[k],latitudeMaxWind0[k],longitudeMaxWind0[k],windMaxWind0[k]] + data[k][0] = [latitudeAnalysis[k], longitudeAnalysis[k], pressureAnalysis[k], latitudeMaxWind0[k], + longitudeMaxWind0[k], windMaxWind0[k]] else: for k in range(len(memberNumber)): - data[k][0]=[latitudeAnalysis[0],longitudeAnalysis[0],pressureAnalysis[k],latitudeMaxWind0[0],longitudeMaxWind0[0],windMaxWind0[k]] + data[k][0] = [latitudeAnalysis[0], longitudeAnalysis[0], pressureAnalysis[k], latitudeMaxWind0[0], + longitudeMaxWind0[0], windMaxWind0[k]] - - timePeriod=[0 for x in range(numberOfPeriods)] - for i in range(1,numberOfPeriods): + timePeriod = [0 for x in range(numberOfPeriods)] + for i in range(1, numberOfPeriods): rank1 = i * 2 + 2 rank3 = i * 2 + 3 - ivalues= codes_get_array(bufr,"#%d#timePeriod" %(i)) + ivalues = codes_get_array(bufr, "#%d#timePeriod" % (i)) - if len(ivalues)==1: - timePeriod[i]=ivalues[0] + if len(ivalues) == 1: + timePeriod[i] = ivalues[0] else: - for j in range(len (ivalues)): - if ivalues[j]!=CODES_MISSING_LONG: - timePeriod[i]=ivalues[j] + for j in range(len(ivalues)): + if ivalues[j] != CODES_MISSING_LONG: + timePeriod[i] = ivalues[j] break # Location of the storm values = codes_get_array(bufr, "#%d#meteorologicalAttributeSignificance" % rank1) - if len(values)==1: - significance=values[0] + if len(values) == 1: + significance = values[0] else: - for j in range(len (values)): - if values[j]!=CODES_MISSING_LONG: - significance=values[j] + for j in range(len(values)): + if values[j] != CODES_MISSING_LONG: + significance = values[j] break - if significance==1: + if significance == 1: lat = codes_get_array(bufr, "#%d#latitude" % rank1) lon = codes_get_array(bufr, "#%d#longitude" % rank1) press = codes_get_array(bufr, "#%d#pressureReducedToMeanSeaLevel" % (i + 1)) else: - print('ERROR: unexpected meteorologicalAttributeSignificance=',significance) + print('ERROR: unexpected meteorologicalAttributeSignificance=', significance) # Location of maximum wind values = codes_get_array(bufr, "#%d#meteorologicalAttributeSignificance" % rank3) - if len(values)==1: - significanceWind=values[0] + if len(values) == 1: + significanceWind = values[0] else: - for j in range(len (values)): - if values[j]!=CODES_MISSING_LONG: - significanceWind=values[j] + for j in range(len(values)): + if values[j] != CODES_MISSING_LONG: + significanceWind = values[j] break - if significanceWind==3: + if significanceWind == 3: latWind = codes_get_array(bufr, "#%d#latitude" % rank3) lonWind = codes_get_array(bufr, "#%d#longitude" % rank3) wind10m = codes_get_array(bufr, "#%d#windSpeedAt10M" % (i + 1)) else: - print('ERROR: unexpected meteorologicalAttributeSignificance=',significanceWind) + print('ERROR: unexpected meteorologicalAttributeSignificance=', significanceWind) for k in range(len(memberNumber)): - data[k][i]=[lat[k],lon[k],press[k],latWind[k],lonWind[k],wind10m[k]] + data[k][i] = [lat[k], lon[k], press[k], latWind[k], lonWind[k], wind10m[k]] -# ---------------------------------------- Print the values ------------- + # ---------------------------------------- Print the values ------------- for m in range(len(memberNumber)): - print("== Member %d" %memberNumber[m]) + print("== Member %d" % memberNumber[m]) print("step latitude longitude pressure latitude longitude wind") for s in range(len(timePeriod)): - if data[m][s][0]!=CODES_MISSING_DOUBLE and data[m][s][1]!=CODES_MISSING_DOUBLE: - print(" {:>3d}{}{:>6.1f}{}{:>6.1f}{}{:>8.1f}{}{:>6.1f}{}{:>6.1f}{}{:>6.1f}".format(\ - timePeriod[s],' ',data[m][s][0],' ',data[m][s][1],' ',data[m][s][2],' ', - data[m][s][3],' ',data[m][s][4],' ',data[m][s][5])) + if data[m][s][0] != CODES_MISSING_DOUBLE and data[m][s][1] != CODES_MISSING_DOUBLE: + print(" {:>3d}{}{:>6.1f}{}{:>6.1f}{}{:>8.1f}{}{:>6.1f}{}{:>6.1f}{}{:>6.1f}".format( \ + timePeriod[s], ' ', data[m][s][0], ' ', data[m][s][1], ' ', data[m][s][2], ' ', + data[m][s][3], ' ', data[m][s][4], ' ', data[m][s][5])) -# ----------------------------------------------------------------------- + # ----------------------------------------------------------------------- cnt += 1 # release the BUFR message @@ -202,5 +201,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_set_keys.py b/examples/python/bufr_set_keys.py index d48c9a241..62b81df8f 100644 --- a/examples/python/bufr_set_keys.py +++ b/examples/python/bufr_set_keys.py @@ -26,7 +26,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open BUFR file fin = open(INPUT) @@ -88,5 +87,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/bufr_subset.py b/examples/python/bufr_subset.py index b927bf187..54367bd86 100644 --- a/examples/python/bufr_subset.py +++ b/examples/python/bufr_subset.py @@ -26,7 +26,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -52,7 +51,6 @@ def example(): # loop over the subsets for i in range(1, numberOfSubsets + 1): - # read and print some data values key = '/subsetNumber=%d/blockNumber' % i @@ -84,5 +82,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/ecc-448.py b/examples/python/ecc-448.py index 0db55b050..3a6ccd8fd 100644 --- a/examples/python/ecc-448.py +++ b/examples/python/ecc-448.py @@ -18,7 +18,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -32,20 +31,19 @@ def example(): break print("message: %s" % cnt) - + # ECC-448: create a new BUFR handle from the message # of the original the_message = codes_get_message(bufr) newbufr = codes_new_from_message(the_message) - codes_set(newbufr,'unpack',1) + codes_set(newbufr, 'unpack', 1) # get BUFR key iterator iterid = codes_bufr_keys_iterator_new(newbufr) # loop over the keys while codes_bufr_keys_iterator_next(iterid): - # print key name keyname = codes_bufr_keys_iterator_get_name(iterid) print(" %s" % keyname) @@ -73,5 +71,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/get_product_kind.py b/examples/python/get_product_kind.py index c2ab8fb2c..d393ab3b3 100644 --- a/examples/python/get_product_kind.py +++ b/examples/python/get_product_kind.py @@ -51,5 +51,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_ccsds.py b/examples/python/grib_ccsds.py index a275f1c5a..1bbc35ed2 100644 --- a/examples/python/grib_ccsds.py +++ b/examples/python/grib_ccsds.py @@ -53,5 +53,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_clone.py b/examples/python/grib_clone.py index 8ecf72d6a..8cc9ad2a3 100644 --- a/examples/python/grib_clone.py +++ b/examples/python/grib_clone.py @@ -60,5 +60,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_count_messages.py b/examples/python/grib_count_messages.py index afbd4c58c..53ca13be7 100644 --- a/examples/python/grib_count_messages.py +++ b/examples/python/grib_count_messages.py @@ -70,5 +70,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_get_keys.py b/examples/python/grib_get_keys.py index c84cc945d..bb35e21c8 100644 --- a/examples/python/grib_get_keys.py +++ b/examples/python/grib_get_keys.py @@ -63,5 +63,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_get_message_offset.py b/examples/python/grib_get_message_offset.py index d1b6ff686..7dd2fa497 100644 --- a/examples/python/grib_get_message_offset.py +++ b/examples/python/grib_get_message_offset.py @@ -51,5 +51,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_index.py b/examples/python/grib_index.py index 897aca147..7119f185f 100644 --- a/examples/python/grib_index.py +++ b/examples/python/grib_index.py @@ -91,5 +91,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_iterator.py b/examples/python/grib_iterator.py index 59585a8f2..7f199dabc 100644 --- a/examples/python/grib_iterator.py +++ b/examples/python/grib_iterator.py @@ -15,7 +15,8 @@ import sys from eccodes import * VERBOSE = 1 # verbose error reporting -missingValue = 1e+20 # A value out of range +missingValue = 1e+20 # A value out of range + def example(INPUT): f = open(INPUT) @@ -65,5 +66,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_iterator_bitmap.py b/examples/python/grib_iterator_bitmap.py index b7f426f32..face47ba3 100644 --- a/examples/python/grib_iterator_bitmap.py +++ b/examples/python/grib_iterator_bitmap.py @@ -74,5 +74,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_keys_iterator.py b/examples/python/grib_keys_iterator.py index 743c202b7..a6e8d6e76 100644 --- a/examples/python/grib_keys_iterator.py +++ b/examples/python/grib_keys_iterator.py @@ -59,5 +59,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_ls_JSON.py b/examples/python/grib_ls_JSON.py index 8a6072a7b..4a1f996e6 100644 --- a/examples/python/grib_ls_JSON.py +++ b/examples/python/grib_ls_JSON.py @@ -18,8 +18,9 @@ import os import getopt from eccodes import * -VERBOSE=1 # verbose error reporting -default_namespace='ls' +VERBOSE = 1 # verbose error reporting +default_namespace = 'ls' + def do_print(namespace, INPUT): f = open(INPUT) @@ -43,13 +44,13 @@ def do_print(namespace, INPUT): f1 = True while codes_keys_iterator_next(iterid): keyname = codes_keys_iterator_get_name(iterid) - keyval = codes_get_string(iterid,keyname) + keyval = codes_get_string(iterid, keyname) if not f1: print(',') else: print('') f1 = False - print(" \"%s\" : \"%s\"" % (keyname,keyval), end=' ') + print(" \"%s\" : \"%s\"" % (keyname, keyval), end=' ') print('') print(' }') @@ -60,6 +61,7 @@ def do_print(namespace, INPUT): print('}') f.close() + def usage(): progname = os.path.basename(sys.argv[0]) print("Usage: ", progname, "[options] grib_file1 grib_file2 ...") @@ -69,6 +71,7 @@ def usage(): print('\t-m Mars keys are printed.') print('') + def main(): if len(sys.argv) < 2: usage() @@ -92,7 +95,7 @@ def main(): for arg in args: do_print(namespace, arg) except getopt.GetoptError as err: - print('Error: ',err) + print('Error: ', err) usage() return 1 except GribInternalError as err: @@ -103,5 +106,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_multi_write.py b/examples/python/grib_multi_write.py index 73a8bfc19..6a87fd197 100644 --- a/examples/python/grib_multi_write.py +++ b/examples/python/grib_multi_write.py @@ -51,5 +51,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_nearest.py b/examples/python/grib_nearest.py index 9a257bf65..f08322af2 100644 --- a/examples/python/grib_nearest.py +++ b/examples/python/grib_nearest.py @@ -30,7 +30,7 @@ def example(): nearest = codes_grib_find_nearest(gid, lat, lon)[0] print(lat, lon) print(nearest.lat, nearest.lon, nearest.value, nearest.distance, \ - nearest.index) + nearest.index) four = codes_grib_find_nearest(gid, lat, lon, is_lsm=False, npoints=4) for i in range(len(four)): @@ -54,5 +54,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_print_data.py b/examples/python/grib_print_data.py index 4c7337824..d59943260 100644 --- a/examples/python/grib_print_data.py +++ b/examples/python/grib_print_data.py @@ -48,5 +48,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_samples.py b/examples/python/grib_samples.py index a6797506e..2db796d82 100644 --- a/examples/python/grib_samples.py +++ b/examples/python/grib_samples.py @@ -84,5 +84,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_set_bitmap.py b/examples/python/grib_set_bitmap.py index 5ffa08f1f..491e275e6 100644 --- a/examples/python/grib_set_bitmap.py +++ b/examples/python/grib_set_bitmap.py @@ -67,5 +67,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_set_keys.py b/examples/python/grib_set_keys.py index 31e940d6b..20ff9c6d3 100644 --- a/examples/python/grib_set_keys.py +++ b/examples/python/grib_set_keys.py @@ -33,9 +33,9 @@ def example(): centreIntVal = codes_get_array(gid, 'centre', int) centreStrVal = codes_get_array(gid, 'centre', str) dateStrVal = codes_get_array(gid, 'dataDate', str) - assert(centreIntVal[0] == 80) - assert(centreStrVal[0] == 'cnmc') - assert(dateStrVal[0] == today) + assert (centreIntVal[0] == 80) + assert (centreStrVal[0] == 'cnmc') + assert (dateStrVal[0] == today) print('get centre as an integer - centre = %d' % centreIntVal[0]) print('get centre as a string - centre = %s' % centreStrVal[0]) print('get date as a string - date = %s' % dateStrVal[0]) @@ -43,23 +43,23 @@ def example(): # Now do the same but using set_key_vals, setting keys all at once print('set keys using one long comma-separated string...') codes_set_key_vals(gid, 'level=1,centre=98') - assert(codes_get(gid, 'centre', str) == 'ecmf') - assert(codes_get(gid, 'level', int) == 1) + assert (codes_get(gid, 'centre', str) == 'ecmf') + assert (codes_get(gid, 'level', int) == 1) print('set keys using a list of strings...') codes_set_key_vals(gid, ['level=2', 'centre=kwbc']) - assert(codes_get(gid, 'centre', int) == 7) - assert(codes_get(gid, 'level', int) == 2) + assert (codes_get(gid, 'centre', int) == 7) + assert (codes_get(gid, 'level', int) == 2) print('set keys using a dictionary (order not as specified!)...') codes_set_key_vals(gid, {'level': 3, 'centre': 84}) - assert(codes_get(gid, 'centre', str) == 'lfpw') - assert(codes_get(gid, 'level', int) == 3) + assert (codes_get(gid, 'centre', str) == 'lfpw') + assert (codes_get(gid, 'level', int) == 3) print('set keys using an ordered dictionary...') - codes_set_key_vals(gid, OrderedDict( [('level', 3), ('centre', 84)] )) - assert(codes_get(gid, 'centre', str) == 'lfpw') - assert(codes_get(gid, 'level', int) == 3) + codes_set_key_vals(gid, OrderedDict([('level', 3), ('centre', 84)])) + assert (codes_get(gid, 'centre', str) == 'lfpw') + assert (codes_get(gid, 'level', int) == 3) codes_gts_header(True) codes_gts_header(False) @@ -81,5 +81,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_set_missing.py b/examples/python/grib_set_missing.py index a7cb9ef5b..d5988ed16 100644 --- a/examples/python/grib_set_missing.py +++ b/examples/python/grib_set_missing.py @@ -28,7 +28,7 @@ def example(): codes_set_long(gid, "scaledValueOfFirstFixedSurface", 15) codes_set_long(gid, "scaleFactorOfFirstFixedSurface", 1) level = codes_get_double(gid, "level") - assert(level == 1.5) + assert (level == 1.5) # set type of level to surface codes_set(gid, 'typeOfFirstFixedSurface', 'sfc') @@ -53,5 +53,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/grib_set_pv.py b/examples/python/grib_set_pv.py index 47fb6415c..783d84051 100644 --- a/examples/python/grib_set_pv.py +++ b/examples/python/grib_set_pv.py @@ -28,7 +28,7 @@ def example(): numberOfLevels = 60 numberOfCoefficients = 2 * (numberOfLevels + 1) - assert(len(pv) == numberOfCoefficients) + assert (len(pv) == numberOfCoefficients) fout = open('grib_set_pv.py.temp.grib', 'w') gid = codes_grib_new_from_samples('reduced_gg_sfc_grib1') @@ -55,5 +55,6 @@ def main(): return 1 + if __name__ == '__main__': sys.exit(main()) diff --git a/examples/python/gts_get_keys.py b/examples/python/gts_get_keys.py index dbbec612e..c33b0761d 100644 --- a/examples/python/gts_get_keys.py +++ b/examples/python/gts_get_keys.py @@ -25,7 +25,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open bufr file f = open(INPUT) @@ -71,5 +70,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/high_level_api.py b/examples/python/high_level_api.py index 262051574..cb9433bf5 100644 --- a/examples/python/high_level_api.py +++ b/examples/python/high_level_api.py @@ -137,7 +137,8 @@ KNOWN_BUFR_KEYS = ['edition', 'masterTableNumber', 'bufrHeaderSubCentre', 'bufrH 'typicalHour', 'typicalMinute', 'rdbType', 'newSubtype', 'rdbtimeDay', 'rdbtimeHour', 'rdbtimeMinute', 'rdbtimeSecond', 'rectimeDay', 'rectimeHour', 'rectimeMinute', 'rectimeSecond', 'correction1', 'correction1Part', 'correction2', 'correction2Part', 'correction3', 'correction3Part', - 'correction4', 'correction4Part', 'qualityControl', 'numberOfSubsets', 'localLatitude', 'localLongitude', + 'correction4', 'correction4Part', 'qualityControl', 'numberOfSubsets', 'localLatitude', + 'localLongitude', 'observedData', 'compressedData', 'unexpandedDescriptors', '#1#blockNumber', '#1#blockNumber->percentConfidence', '#1#stationNumber', '#1#stationNumber->percentConfidence', '#1#stationType', '#1#stationType->percentConfidence', '#1#year', '#1#year->percentConfidence', @@ -149,7 +150,8 @@ KNOWN_BUFR_KEYS = ['edition', 'masterTableNumber', 'bufrHeaderSubCentre', 'bufrH '#1#pressureReducedToMeanSeaLevel->percentConfidence', '#1#3HourPressureChange', '#1#3HourPressureChange->percentConfidence', '#1#characteristicOfPressureTendency', '#1#characteristicOfPressureTendency->percentConfidence', '#1#windDirectionAt10M', - '#1#windDirectionAt10M->percentConfidence', '#1#windSpeedAt10M', '#1#windSpeedAt10M->percentConfidence', + '#1#windDirectionAt10M->percentConfidence', '#1#windSpeedAt10M', + '#1#windSpeedAt10M->percentConfidence', '#1#airTemperatureAt2M', '#1#airTemperatureAt2M->percentConfidence', '#1#dewpointTemperatureAt2M', '#1#dewpointTemperatureAt2M->percentConfidence', '#1#relativeHumidity', '#1#relativeHumidity->percentConfidence', '#1#horizontalVisibility', @@ -161,22 +163,32 @@ KNOWN_BUFR_KEYS = ['edition', 'masterTableNumber', 'bufrHeaderSubCentre', 'bufrH '#1#cloudAmount->percentConfidence', '#1#heightOfBaseOfCloud', '#1#heightOfBaseOfCloud->percentConfidence', '#1#cloudType', '#1#cloudType->percentConfidence', '#2#cloudType', '#2#cloudType->percentConfidence', '#3#cloudType', '#3#cloudType->percentConfidence', - '#2#verticalSignificanceSurfaceObservations', '#2#verticalSignificanceSurfaceObservations->percentConfidence', + '#2#verticalSignificanceSurfaceObservations', + '#2#verticalSignificanceSurfaceObservations->percentConfidence', '#2#cloudAmount', '#2#cloudAmount->percentConfidence', '#4#cloudType', - '#4#cloudType->percentConfidence', '#2#heightOfBaseOfCloud', '#2#heightOfBaseOfCloud->percentConfidence', - '#3#verticalSignificanceSurfaceObservations', '#3#verticalSignificanceSurfaceObservations->percentConfidence', - '#3#cloudAmount', '#3#cloudAmount->percentConfidence', '#5#cloudType', '#5#cloudType->percentConfidence', - '#3#heightOfBaseOfCloud', '#3#heightOfBaseOfCloud->percentConfidence', '#4#verticalSignificanceSurfaceObservations', - '#4#verticalSignificanceSurfaceObservations->percentConfidence', '#4#cloudAmount', '#4#cloudAmount->percentConfidence', - '#6#cloudType', '#6#cloudType->percentConfidence', '#4#heightOfBaseOfCloud', '#4#heightOfBaseOfCloud->percentConfidence', - '#5#verticalSignificanceSurfaceObservations', '#5#verticalSignificanceSurfaceObservations->percentConfidence', '#5#cloudAmount', - '#5#cloudAmount->percentConfidence', '#7#cloudType', '#7#cloudType->percentConfidence', '#5#heightOfBaseOfCloud', + '#4#cloudType->percentConfidence', '#2#heightOfBaseOfCloud', + '#2#heightOfBaseOfCloud->percentConfidence', + '#3#verticalSignificanceSurfaceObservations', + '#3#verticalSignificanceSurfaceObservations->percentConfidence', + '#3#cloudAmount', '#3#cloudAmount->percentConfidence', '#5#cloudType', + '#5#cloudType->percentConfidence', + '#3#heightOfBaseOfCloud', '#3#heightOfBaseOfCloud->percentConfidence', + '#4#verticalSignificanceSurfaceObservations', + '#4#verticalSignificanceSurfaceObservations->percentConfidence', '#4#cloudAmount', + '#4#cloudAmount->percentConfidence', + '#6#cloudType', '#6#cloudType->percentConfidence', '#4#heightOfBaseOfCloud', + '#4#heightOfBaseOfCloud->percentConfidence', + '#5#verticalSignificanceSurfaceObservations', + '#5#verticalSignificanceSurfaceObservations->percentConfidence', '#5#cloudAmount', + '#5#cloudAmount->percentConfidence', '#7#cloudType', '#7#cloudType->percentConfidence', + '#5#heightOfBaseOfCloud', '#5#heightOfBaseOfCloud->percentConfidence', '#1#totalPrecipitationPast6Hours', - '#1#totalPrecipitationPast6Hours->percentConfidence', '#1#totalSnowDepth', '#1#totalSnowDepth->percentConfidence', + '#1#totalPrecipitationPast6Hours->percentConfidence', '#1#totalSnowDepth', + '#1#totalSnowDepth->percentConfidence', '#1#centre', '#1#generatingApplication'] -class TestGribFile(unittest.TestCase): +class TestGribFile(unittest.TestCase): """Test GribFile functionality.""" def test_memory_management(self): @@ -186,7 +198,7 @@ class TestGribFile(unittest.TestCase): for i in range(len(grib_file)): msg = GribMessage(grib_file) self.assertEqual(msg["shortName"], "msl") - self.assertEqual(msg['count'], i+1) + self.assertEqual(msg['count'], i + 1) self.assertEqual(len(grib_file.open_messages), 5) self.assertEqual(len(grib_file.open_messages), 0) @@ -219,7 +231,6 @@ class TestGribFile(unittest.TestCase): class TestGribMessage(unittest.TestCase): - """Test GribMessage functionality.""" def test_metadata(self): @@ -240,7 +251,7 @@ class TestGribMessage(unittest.TestCase): self.assertTrue(msg.missing("scaleFactorOfSecondFixedSurface")) msg["scaleFactorOfSecondFixedSurface"] = 5 msg.set_missing("scaleFactorOfSecondFixedSurface") - #with self.assertRaises(KeyError): + # with self.assertRaises(KeyError): # msg["scaleFactorOfSecondFixedSurface"] def test_value_setting(self): @@ -249,21 +260,21 @@ class TestGribMessage(unittest.TestCase): msg = GribMessage(grib_file) msg["scaleFactorOfSecondFixedSurface"] = 5 msg["values"] = [1, 2, 3] - self.assertEqual( msg['scaleFactorOfSecondFixedSurface'], 5 ) + self.assertEqual(msg['scaleFactorOfSecondFixedSurface'], 5) def test_multi_value_setting(self): """Multiple keys/values can be set properly.""" msg = GribMessage(sample='GRIB1') - msg[ 'paramId', 'stepType', 'edition' ] = 49, 'avg', 2 - self.assertEqual( msg['shortName'], '10fg' ) + msg['paramId', 'stepType', 'edition'] = 49, 'avg', 2 + self.assertEqual(msg['shortName'], '10fg') # Another test with GribFile(TESTGRIB) as grib_file: msg = GribMessage(grib_file) - msg[ 'setLocalDefinition', 'localDefinitionNumber' ] = 1,25 - msg[ 'typeOfFirstFixedSurface', 'typeOfSecondFixedSurface' ] = 1, 8 - msg[ ('typeOfFirstFixedSurface','typeOfSecondFixedSurface') ] = (1, 8) #Also works - self.assertEqual( msg['localDefinitionNumber'], 25 ) - self.assertEqual( msg['typeOfLevel'], 'entireAtmosphere' ) + msg['setLocalDefinition', 'localDefinitionNumber'] = 1, 25 + msg['typeOfFirstFixedSurface', 'typeOfSecondFixedSurface'] = 1, 8 + msg[('typeOfFirstFixedSurface', 'typeOfSecondFixedSurface')] = (1, 8) # Also works + self.assertEqual(msg['localDefinitionNumber'], 25) + self.assertEqual(msg['typeOfLevel'], 'entireAtmosphere') def test_serialize(self): """Message can be serialized to file.""" @@ -282,7 +293,6 @@ class TestGribMessage(unittest.TestCase): class TestGribIndex(unittest.TestCase): - """Test GribIndex functionality.""" def test_memory_management(self): @@ -316,7 +326,6 @@ class TestGribIndex(unittest.TestCase): class TestBufrFile(unittest.TestCase): - """Test BufrFile functionality.""" def test_memory_management(self): @@ -326,7 +335,7 @@ class TestBufrFile(unittest.TestCase): for i in range(len(bufr_file)): msg = BufrMessage(bufr_file) self.assertEqual(msg["bufrHeaderCentre"], 98) - self.assertEqual(msg['count'], i+1) + self.assertEqual(msg['count'], i + 1) self.assertEqual(len(bufr_file.open_messages), 3) self.assertEquals(len(bufr_file.open_messages), 0) @@ -359,7 +368,6 @@ class TestBufrFile(unittest.TestCase): class TestBufrMessage(unittest.TestCase): - """Test BufrMessage functionality""" def test_metadata(self): @@ -412,7 +420,7 @@ class TestBufrMessage(unittest.TestCase): bufr = BufrMessage(sample='BUFR3') with BufrFile('../../data/bufr/metar_with_2_bias.bufr') as bufr_file: bufrin = BufrMessage(bufr_file) - ivalues=( + ivalues = ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, @@ -438,12 +446,12 @@ class TestBufrMessage(unittest.TestCase): bufr['numberOfSubsets'] = 1 bufr['observedData'] = 1 bufr['compressedData'] = 0 - ivalues=( - 307011,7006,10004,222000,101023,31031,1031,1032,101023,33007, - 225000,236000,101023,31031,1031,1032,8024,101001,225255,225000, - 236000,101023,31031,1031,1032,8024,101001,225255, - 1063,2001,4001,4002,4003,4004,4005,5002, - 6002,7001,7006,11001,11016,11017,11002) + ivalues = ( + 307011, 7006, 10004, 222000, 101023, 31031, 1031, 1032, 101023, 33007, + 225000, 236000, 101023, 31031, 1031, 1032, 8024, 101001, 225255, 225000, + 236000, 101023, 31031, 1031, 1032, 8024, 101001, 225255, + 1063, 2001, 4001, 4002, 4003, 4004, 4005, 5002, + 6002, 7001, 7006, 11001, 11016, 11017, 11002) bufr['unexpandedDescriptors'] = ivalues bufrin.unpack() bufrin.copy_data(bufr) @@ -452,6 +460,5 @@ class TestBufrMessage(unittest.TestCase): os.unlink(TEST_OUTPUT) - if __name__ == "__main__": unittest.main() diff --git a/examples/python/keys_iterator_gts.py b/examples/python/keys_iterator_gts.py index 1a1dc3931..d849445fb 100644 --- a/examples/python/keys_iterator_gts.py +++ b/examples/python/keys_iterator_gts.py @@ -56,5 +56,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main()) diff --git a/examples/python/metar_get_keys.py b/examples/python/metar_get_keys.py index f2597fb14..751fd1ab0 100644 --- a/examples/python/metar_get_keys.py +++ b/examples/python/metar_get_keys.py @@ -25,7 +25,6 @@ VERBOSE = 1 # verbose error reporting def example(): - # open metar file f = open(INPUT) @@ -72,5 +71,6 @@ def main(): return 1 + if __name__ == "__main__": sys.exit(main())