def composite_pcp_tot_year(year1, year2, lead):
	"""Computes the total composite precipitation due to observed TCs for a particular
	TC season (July year1 - June year2)."""

	infiles = []
	for m in [7, 8, 9, 10, 11, 12]:  # 7,
		# m = str(m).zfill(2)
		filename = COMP_PCP_TOT_FILE % (lead, year1, m)
		infile = os.path.join(COMP_PCP_TOT_DIR, str(lead), str(year1), filename )
		infiles.append(infile)

	for m in [1, 2, 3, 4, 5, 6]:
		# m=str(m).zfill(2)
		# year=year2
		filename = COMP_PCP_TOT_FILE % (lead, year2, m)
		infile = os.path.join(COMP_PCP_TOT_DIR, str(lead), str(year2), filename)
		infiles.append(infile)


	print infiles

	outfile = "ukmo_nwp.comp_pcp_total.%d-days.%d-%d.nc" % (lead, year1, year2)
	outpath = os.path.join(COMP_PCP_TOT_DIR, str(lead), outfile)

	pl.add_files(infiles, outpath, cube_count=12)
Esempio n. 2
0
def track_density_year(lead, year1, year2, box_size=2.5, threeway=True):
    """Sums the TC track density for the given lead time and year (TC season)."""

    # Check lead time is in available range
    if not 0 <= lead <= 6:
        raise ValueError('lead=%s; must be 0 <= lead <= 6' % lead)

    # Get list of files
    #dens_dir = DENS_DIR_3WAY if threeway else DENS_DIR
    #m2 = 12
    infiles = []
    for m in [7, 8, 9, 10, 11, 12]:  #7,
        #m = str(m).zfill(2)
        infiles.append(DENSITY_FILENAME.format(lead, year1, str(m).zfill(2)))

    for m in [1, 2, 3, 4, 5, 6]:
        #m=str(m).zfill(2)
        #year=year2
        infiles.append(DENSITY_FILENAME.format(lead, year2, str(m).zfill(2)))

    #infiles = [DENS_FILE % (lead, year, month) for month in xrange(1, m2+1)]
    inpaths = [
        os.path.join(DENSITY_DIR, str(lead),
                     str(year1) + str(year2), ff) for ff in infiles
    ]
    print inpaths

    outfile = "ukmo_nwp.density.lt{0}_days.{1}{2}.nc".format(
        lead, year1, year2)
    outpath = os.path.join(DENSITY_DIR, str(lead),
                           str(year1) + str(year2), outfile)

    pl.add_files(inpaths, outpath, cube_count=12)
def composite_pcp_tot_year_separate_resolutions(year1,year2, lead):
	"""Computes the total precipitation at a particular forecast lead time (in
	days) for a particular year."""

	# Check whether separation of resolutions is required
	#if year not in [2010, 2014]:
		#raise ValueError('All data in %d are at the same resolution' % year)

	# Get list of infiles
	infiles = []

	infiles = []
	for m in [7, 8, 9, 10, 11, 12]:  # 7,
		if (year1, m) == (2014,7):
			res = ['n512', 'n768']
			infile = [os.path.join(COMP_PCP_TOT_DIR, str(lead), str(year1), COMP_PCP_TOT_FILE % (lead, year1, m))] * 2
			infile = [i.replace('.nc', '%s.nc' % r) for i, r in zip(infile, res)]
			for a in infile:
				infiles.append(a)
				
		elif (year1, m) == (2017,7):
			res = ['n768', 'n1280']
			infile = [os.path.join(COMP_PCP_TOT_DIR, str(lead), str(year1), COMP_PCP_TOT_FILE % (lead, year1, m))] * 2
			infile = [i.replace('.nc', '%s.nc' % r) for i, r in zip(infile, res)]
			for a in infile:
				infiles.append(a)
		else:
			# m = str(m).zfill(2)
			filename = COMP_PCP_TOT_FILE % (lead, year1, m)
			infile = os.path.join(COMP_PCP_TOT_DIR, str(lead), str(year1), filename)
			infiles.append(infile)

	for m in [1, 2, 3, 4, 5, 6]:
		if (year2,m) == (2010,3):
			res = ['n320', 'n512']
			infile = [os.path.join(COMP_PCP_TOT_DIR, str(lead), str(year2), COMP_PCP_TOT_FILE % (lead, year2, m))] * 2
			infile = [i.replace('.nc', '%s.nc' % r) for i, r in zip(infile, res)]
			for a in infile:
				infiles.append(a)
		# m=str(m).zfill(2)
		# year=year2
		else:
			filename = COMP_PCP_TOT_FILE % (lead, year2, m)
			infile = os.path.join(COMP_PCP_TOT_DIR, str(lead), str(year2), filename)
			infiles.append(infile)

	print infiles

	outfile = "ukmo_nwp.comp_pcp_total.%d-days.%d-%d.nc" % (lead, year1, year2)
	outpath = os.path.join(COMP_PCP_TOT_DIR, str(lead), outfile)

	pl.add_files(infiles, outpath, separate_resolutions=True,deal_with_masks=False, cube_count=13)
def composite_pcp_tot_year(year1, year2, lead=None):
    """Computes the total composite precipitation due to observed TCs for a particular
	TC season (July year1 - June year2)."""

    infiles = []
    for m in [11, 12]:  # 7, 8, 9, 10,
        # m = str(m).zfill(2)
        filename = COMP_PCP_TOT_FILE % (year1, m)
        infile = os.path.join(COMP_PCP_TOT_DIR, str(year1), filename)
        infiles.append(infile)

    for m in [1, 2, 3, 4]:  #, 5, 6
        # m=str(m).zfill(2)
        # year=year2
        filename = COMP_PCP_TOT_FILE % (year2, m)
        infile = os.path.join(COMP_PCP_TOT_DIR, str(year2), filename)
        infiles.append(infile)

    print infiles

    outfile = "gpm_imerg.comp_pcp_total.%d-%d.nc" % (year1, year2)
    outpath = os.path.join(COMP_PCP_TOT_DIR, outfile)

    pl.add_files(infiles, outpath, cube_count=6)  #NORMALLY 12!!!!!
#trmm_tc_outfile=trmm_tc_dir+"trmm.comp_pcp_tc.072006-122016_n320.nc"
#pl.add_files(trmm_tc_infiles,trmm_tc_outfile)

#####################################################################################################################
#THIS BIT NEEDS MODIFYING AND RUNNING FOR THE TRMM ANALYSIS TRACK COMPOSITES!
#this, again, will be used to calculate the 2006-2016 TRMM precip composites using the analysis tracks rather than ibtracs tracks
#this will also need updating when eventually update the analysis to 2018 or 2019
trmm_tc_dir = "/gws/nopw/j04/klingaman/emerton/analysis_trmm_tc_precip_composites/"
#last input file will eventually be -062019:
trmm_tc_infiles = [
    trmm_tc_dir + "trmm.comp_pcp_tc.analysis.072006-032010_regridded_n320.nc",
    trmm_tc_dir + "trmm.comp_pcp_tc.analysis.032010-072014_regridded_n320.nc",
    trmm_tc_dir + "trmm.comp_pcp_tc.analysis.072014-122016_regridded_n320.nc"
]  #, trmm_tc_dir+"trmm.comp_pcp_tc.072014-072017_regridded_n320.nc", trmm_tc_dir+"trmm.comp_pcp_tc.072017-062018_regridded_n320.nc"
trmm_tc_outfile = trmm_tc_dir + "trmm.comp_pcp_tc.072006-122016_n320.nc"
pl.add_files(trmm_tc_infiles, trmm_tc_outfile)
#####################################################################################################################

#for lead in [0,1,2,3,4,5,6]:
#ukmo_tot_dir="/gws/nopw/j04/klingaman/emerton/ukmo_nwp_analysis/ukmo_nwp_TOTAL_precip_composites/"+str(lead)+"/"
#ukmo_tot_infiles=[ukmo_tot_dir+"ukmo_nwp.comp_pcp_tot."+str(lead)+"_days.072006-032010.n320.nc",ukmo_tot_dir+"ukmo_nwp.comp_pcp_tot."+str(lead)+"_days.032010-072014.regridded_n320.nc", ukmo_tot_dir+"ukmo_nwp.comp_pcp_tot."+str(lead)+"_days.072014-072017.regridded_n320.nc", ukmo_tot_dir+"ukmo_nwp.comp_pcp_tot."+str(lead)+"_days.072017-062018.regridded_n320.nc"]
#print ukmo_tot_infiles
#ukmo_tot_outfile=ukmo_tot_dir+"ukmo_nwp.comp_pcp_tot."+str(lead)+"_days.072006-062018.n320.nc"
#pl.add_files(ukmo_tot_infiles,ukmo_tot_outfile,cube_count=4)

#for lead in [0,1,2,3,4,5,6]:
#ukmo_tc_dir="/gws/nopw/j04/klingaman/emerton/ukmo_nwp_analysis/ukmo_nwp_precip_composites/"+str(lead)+"/"
#ukmo_tc_infiles=[ukmo_tc_dir+"ukmo_nwp.comp_pcp_tc."+str(lead)+"_days.072006-032010.n320.nc",ukmo_tc_dir+"ukmo_nwp.comp_pcp_tc."+str(lead)+"_days.032010-072014.regridded_n320.nc", ukmo_tc_dir+"ukmo_nwp.comp_pcp_tc."+str(lead)+"_days.072014-072017.regridded_n320.nc", ukmo_tc_dir+"ukmo_nwp.comp_pcp_tc."+str(lead)+"_days.072017-062018.regridded_n320.nc"]
#ukmo_tc_outfile = ukmo_tc_dir + "ukmo_nwp.comp_pcp_tc." + str(lead) + "_days.072006-062018.n320.nc"
#pl.add_files(ukmo_tc_infiles,ukmo_tc_outfile,cube_count=4)
def composite_pcp_tot_seasons_res_trmm():
    """Computes the total precipitation at a particular forecast lead time (in
	days) in the given range of years for each season JJASON and DJFMAM."""

    #first_year = [2006, 2009][lead == 6]
    months_dict = {
        'NDJFMA': [11, 12, 1, 2, 3, 4],
        'MJJASO': [5, 6, 7, 8, 9, 10],
        'DJFM': [12, 1, 2, 3]
    }
    for res in ['n320', 'n512', 'n768', 'n1280']:

        if res == 'n320':
            y1 = '072006'
            y2 = '032010'
        elif res == 'n512':
            y1 = '032010'
            y2 = '072014'
        elif res == 'n768':
            y1 = '072014'
            y2 = '072017'
        elif res == 'n1280':
            y1 = '072017'
            y2 = '062018'

        for season, months in months_dict.iteritems():
            if season in ['NDJFMA', 'MJJASO']:
                cube_count = 6
            elif season == 'DJFM':
                cube_count = 4
            infile = []
            for m in months:

                if res == 'n320':
                    #if lead == 6:
                    #start_year = 2009
                    if m <= 6:
                        start_year = 2007
                    else:
                        start_year = 2006
                    if m <= 3:
                        years = range(start_year, 2011)
                    else:
                        years = range(start_year, 2010)

                elif res == 'n512':
                    #start_year = 2006
                    if m <= 2:
                        years = range(2011, 2015)
                    elif m <= 7:
                        years = range(2010, 2015)
                    else:
                        years = range(2010, 2014)

                elif res == 'n768':

                    if month <= 6:
                        years = range(2015, 2018)
                    elif month == 7:
                        years = range(2014, 2018)
                    else:
                        years = range(2014, 2017)

                elif res == 'n1280':

                    end_year = 2018  # THIS WILL BE 2019 ONCE HAVE THE 2018-2019 IBTRACS ETC (FULL DATASET_

                    if month <= 6:
                        years = range(2018, end_year + 1)
                    elif month == 7:
                        years = range(2018, end_year + 1)
                    else:
                        years = range(
                            2017, 2018
                        )  #THIS IS CURRENTLY 2017-2017 SO THAT WE JUST HAVE 072017-062018; EVENTUALLY THIS WILL ALSO BE "end_year+1" as well

                #if m <= 7:
                #last_year = 2017
                #else:
                #last_year = 2016
                tot_file = COMP_PCP_TOT_FILE_TRMM.replace(
                    '%d%02d', '%d_%d.%02d.%s')
                tot_file = tot_file % (years[0], years[-1], m, res)
                infile.append(os.path.join(COMP_PCP_TOT_DIR_TRMM, tot_file))

            outfilename = 'trmm.comp_pcp_tc.%s-%s.%s.%s.nc' % (y1, y2, res,
                                                               season)
            outfile = os.path.join(COMP_PCP_TOT_DIR_TRMM, outfilename)

            pl.add_files(infile,
                         outfile,
                         deal_with_masks=True,
                         cube_count=cube_count)
def composite_pcp_tc_all_separate_resolutions_trmm_analysis():
    """Computes the total precipitation at a particular forecast lead time (in
	days) in the given range of years."""
    """Requires monthly totals first"""

    # Iterate for each resolution
    for res in ['n320', 'n512', 'n768', 'n1280']:  #
        #for res in ['n768']:

        if res == 'n320':
            y1 = '072006'
            y2 = '032010'
        elif res == 'n512':
            y1 = '032010'
            y2 = '072014'
        elif res == 'n768':
            y1 = '072014'
            y2 = '072017'
        elif res == 'n1280':
            y1 = '072017'
            y2 = '062018'

        infile = []

        for m in xrange(1, 13):

            if res == 'n320':
                #if lead == 6:
                #start_year = 2009
                if m <= 6:
                    start_year = 2007
                else:
                    start_year = 2006
                if m <= 3:
                    years = range(start_year, 2011)
                else:
                    years = range(start_year, 2010)

            elif res == 'n512':
                #start_year = 2006
                if m <= 2:
                    years = range(2011, 2015)
                elif m <= 7:
                    years = range(2010, 2015)
                else:
                    years = range(2010, 2014)

            elif res == 'n768':

                if m <= 6:
                    years = range(2015, 2018)  #2015,2018
                elif m == 7:
                    years = range(2014, 2018)  #2014,2018
                else:
                    years = range(2014, 2017)  #2014,2017

            elif res == 'n1280':

                end_year = 2018  # THIS WILL BE 2019 ONCE HAVE THE 2018-2019 IBTRACS ETC (FULL DATASET_

                if m <= 6:
                    years = range(2018, end_year + 1)
                elif m == 7:
                    years = range(2018, end_year + 1)
                else:
                    years = range(
                        2017, 2018
                    )  #THIS IS CURRENTLY 2017-2017 SO THAT WE JUST HAVE 072017-062018; EVENTUALLY THIS WILL ALSO BE "end_year+1" as well

            #if lead is None:
            #tot_file = COMP_PCP_TOT_FILE_TRMM.replace('%d%02d', '%d_%d.%02d')
            #tot_file = tot_file % (years[0], years[-1], m)
            #else:
            tot_file = COMP_PCP_TC_FILE_TRMM_ANALYSIS.replace(
                '%d%02d', '%d_%d.%02d')
            tot_file = tot_file % (years[0], years[-1], m)
            tot_file = tot_file.replace('.nc', '.%s.nc' % res)
            infile.append(os.path.join(COMP_PCP_TC_DIR_TRMM_ANALYSIS,
                                       tot_file))

        print infile
        outfilename = 'trmm.comp_pcp_tc.analysis.%s-%s.%s.nc' % (y1, y2, res)
        outfile = os.path.join(COMP_PCP_TC_DIR_TRMM_ANALYSIS, outfilename)
        pl.add_files(infile, outfile, deal_with_masks=True, cube_count=12)
        print '################'
        print outfile
        print '################'
def composite_pcp_tc_month_separate_resolutions_trmm_analysis(month):
    """Computes the total precipitation at a particular forecast lead time (in
	days) for a particular month in the given years."""

    # Iterate for each resolution
    #y1 = [2006, 2008][lead==6]
    #y2 = [2016, 2017][month <= 7]
    #y2=2016
    #res_list = {2006: ['n320', 'n512', 'n768'], 2008: ['n320']}[y1]
    res_list = ['n320', 'n512', 'n768']  #,'n1280'
    #res_list = ['n320', 'n768']
    for res in res_list:
        print res
        # Iterate for each year
        if res == 'n320':
            if month <= 6:
                start_year = 2007
            else:
                start_year = 2006
            if month <= 3:
                years = range(start_year, 2011)
            else:
                years = range(start_year, 2010)
        elif res == 'n512':
            #start_year = 2006
            if month <= 2:
                years = range(2011, 2015)
            elif month <= 7:
                years = range(2010, 2015)
            else:
                years = range(2010, 2014)

        elif res == 'n768':

            #when precip composites are updates for ukmo nwp, the numbers in brackets should be the ones commented out after, rather than ending in 2016
            if month <= 6:
                years = range(2015, 2016)  #2015, 2018
            elif month == 7:
                years = range(2014, 2016)  #2014, 2018
            else:
                years = range(2014, 2016)  #2014, 2017

        #elif res == 'n1280':
        ###start_year = [2006, 2008][lead==6]

        #end_year=2018 # THIS WILL BE 2019 ONCE HAVE THE 2018-2019 IBTRACS ETC (FULL DATASET)

        #if month <= 6:
        #years=range(2018,end_year+1)
        #elif month == 7:
        #years=range(2018,end_year+1)
        #else:
        #years=range(2017,2018)

        print years
        infiles = []
        for y in years:
            tot_file = COMP_PCP_TC_FILE_TRMM_ANALYSIS % (y, month)
            if (y, month) in [(2010, 3), (2014, 7), (2017, 7)]:
                tot_file = tot_file.replace('.nc', '.%s.nc' % res)
            #print tot_file
            infiles.append(
                os.path.join(COMP_PCP_TC_DIR_TRMM_ANALYSIS, str(y), tot_file))

        print infiles

        outfile = infiles[0].replace('/%d' % years[0], '')
        outfile = outfile.replace('%d%02d' % (years[0], month),
                                  '%d_%d.%02d' % (years[0], years[-1], month))
        if res not in outfile:
            outfile = outfile.replace('.nc', '.%s.nc' % res)
        print outfile
        pl.add_files(infiles,
                     outfile,
                     deal_with_masks=True,
                     cube_count=len(years))
def composite_pcp_tot_month_separate_resolutions(month, lead):
    """Computes the total precipitation at a particular forecast lead time (in
	days) for a particular month in the given years."""

    # Iterate for each resolution
    #y1 = [2006, 2008][lead==6]
    #y2 = [2016, 2017][month <= 7]
    #y2=2016
    #res_list = {2006: ['n320', 'n512', 'n768'], 2008: ['n320']}[y1]
    res_list = ['n320', 'n512', 'n768', 'n1280']
    #res_list = ['n320', 'n768']
    for res in res_list:
        print res
        # Iterate for each year
        if res == 'n320':
            if lead == 6:
                start_year = 2009
            elif month <= 6:
                start_year = 2007
            else:
                start_year = 2006
            if month <= 3:
                years = range(start_year, 2011)
            else:
                years = range(start_year, 2010)
        elif res == 'n512':
            start_year = 2006
            if month <= 2:
                years = range(2011, 2015)
            elif month <= 7:
                years = range(2010, 2015)
            else:
                years = range(2010, 2014)
        elif res == 'n768':
            start_year = [2006, 2008][lead == 6]

            if month <= 6:
                years = range(2015, 2018)
            elif month == 7:
                years = range(2014, 2018)
            else:
                years = range(2014, 2017)

        elif res == 'n1280':
            start_year = [2006, 2008][lead == 6]

            end_year = 2018  # THIS WILL BE 2019 ONCE HAVE THE 2018-2019 IBTRACS ETC (FULL DATASET_

            if month <= 6:
                years = range(2018, end_year + 1)
            elif month == 7:
                years = range(2018, end_year + 1)
            else:
                years = range(2017, 2018)

        print years
        infiles = []
        for y in years:
            tot_file = COMP_PCP_TOT_FILE % (lead, y, month)
            if (y, month) in [(2010, 3), (2014, 7), (2017, 7)]:
                tot_file = tot_file.replace('.nc', '.%s.nc' % res)
            #print tot_file
            infiles.append(
                os.path.join(COMP_PCP_TOT_DIR, str(lead), str(y), tot_file))

        print infiles

        outfile = infiles[0].replace('/%d' % years[0], '')
        outfile = outfile.replace('%d%02d' % (years[0], month),
                                  '%d_%d.%02d' % (years[0], years[-1], month))
        if res not in outfile:
            outfile = outfile.replace('.nc', '.%s.nc' % res)
        print outfile
        pl.add_files(infiles,
                     outfile,
                     deal_with_masks=True,
                     cube_count=len(years))