def prepare_smb(dry=False):
    logger.info("processing variable SMB")
    SMB_files = [
        "DMI-HIRHAM5_GL2_ERAI_2001_2010_gld_DM.nc.gz",
        "DMI-HIRHAM5_GL2_ERAI_2011_2014_gld_DM.nc.gz"
    ]
    smb_files = []
    for smb_file in SMB_files:
        logger.info("extracting {}".format(smb_file))
        cmd = ["gunzip", smb_file]
        sub.call(cmd)
        smb_files.append(smb_file[:-3])

    smb_merged_file_daily_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_SMB_{}.nc".format(
        daily_mean)
    smb_merged_file_time_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_SMB_{}.nc".format(
        time_mean)

    if dry == False:

        logger.info("merge files {} to {}".format(smb_files,
                                                  smb_merged_file_daily_mean))
        cdo.mergetime(input=" ".join(smb_files),
                      output=smb_merged_file_daily_mean)

        # Convert mm to m and then to kg m-2
        logger.info("calculate time mean")
        cdo.ydaymean(
            input="-mulc," + str(ice_density / 1000) + " -del29feb " +
            smb_merged_file_daily_mean,
            output=smb_merged_file_time_mean,
        )

        logger.info("adjusting time axis")
        adjust_time_axis(smb_merged_file_time_mean)

        logger.info("renaming variable")
        rDict = {"gld": "climatic_mass_balance"}
        nco.ncrename(input=smb_merged_file_time_mean,
                     options=[c.Rename("variable", rDict)])
        opt = [
            c.Atted(mode="o",
                    att_name="units",
                    var_name="climatic_mass_balance",
                    value="kg m-2 day-1"),
            c.Atted(
                mode="o",
                att_name="standard_name",
                var_name="climatic_mass_balance",
                value="land_ice_surface_specific_mass_balance_flux",
            ),
        ]
        nco.ncatted(input=smb_merged_file_time_mean, options=opt)

    return smb_merged_file_time_mean
def prepare_pr(dry=False):
    logger.info("processing variable PR")
    PR_files = [
        "DMI-HIRHAM5_GL2_ERAI_2001_2010_PR_DM.nc.gz",
        "DMI-HIRHAM5_GL2_ERAI_2011_2014_PR_DM.nc.gz"
    ]
    pr_files = []
    for pr_file in PR_files:
        logger.info("extracting {}".format(pr_file))
        cmd = ["gunzip", pr_file]
        sub.call(cmd)
        pr_files.append(pr_file[:-3])

    pr_merged_file_daily_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_PR_{}.nc".format(
        daily_mean)
    pr_merged_file_time_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_PR_{}.nc".format(
        time_mean)

    if dry == False:
        logger.info("merge files {} to {}".format(pr_files,
                                                  pr_merged_file_daily_mean))
        cdo.mergetime(input=" ".join(pr_files),
                      output=pr_merged_file_daily_mean)

        logger.info("calculate time mean")
        tmpfile = cdo.ydaymean(input="-del29feb " + pr_merged_file_daily_mean)

        logger.info("removing height dimension")
        nco.ncwa(input=tmpfile,
                 output=pr_merged_file_time_mean,
                 average="height")

        logger.info("adjusting time axis")
        adjust_time_axis(pr_merged_file_time_mean)

        logger.info("renaming variable")
        rDict = {"pr": "precipitation"}
        nco.ncrename(input=pr_merged_file_time_mean,
                     options=[c.Rename("variable", rDict)])
        opt = [
            c.Atted(mode="o",
                    att_name="units",
                    var_name="precipitation",
                    value="kg m-2 day-1"),
            c.Atted(mode="o",
                    att_name="standard_name",
                    var_name="precipitation",
                    value="precipitation_flux"),
        ]
        nco.ncatted(input=pr_merged_file_time_mean, options=opt)

    return pr_merged_file_time_mean
示例#3
0
    'ey': 'vvelsurf_error'
}

basedir = 'measures'
basename = 'greenland_vel_mosaic250'
version = 'v1'
gdal_nc_options = gdal.TranslateOptions(format='netCDF')

ifile = '.'.join(['_'.join([basename, version]), 'tif'])
ofile_merged = '.'.join(['_'.join([basename, version]), 'nc'])
ifile_wp = os.path.join(basedir, ifile)
ofile_merged_wp = os.path.join(basedir, ofile_merged)
logger.info('Converting {} to {}'.format(ifile_wp, ofile_merged_wp))
gdal.Translate(ofile_merged_wp, ifile_wp, options=gdal_nc_options)
rDict = {'Band1': 'velsurf_mag'}
opt = [c.Rename('variable', rDict)]
nco.ncrename(input=ofile_merged_wp, options=opt)
opt = [
    c.Atted(mode="o",
            att_name="proj4",
            var_name='global',
            value="+init=epsg:3413",
            stype='c'),
    c.Atted(mode="o",
            att_name="units",
            var_name='velsurf_mag',
            value="m year-1")
]
nco.ncatted(input=ofile_merged_wp, options=opt)
for mvar in ('vx', 'vy', 'ex', 'ey'):
    ifile = '.'.join(['_'.join([basename, mvar, version]), 'tif'])
def prepare_tas(dry=False):
    logger.info("processing variable TAS")
    TAS_files = [
        "DMI-HIRHAM5_GL2_ERAI_2001_2010_TAS_DM.nc.gz",
        "DMI-HIRHAM5_GL2_ERAI_2011_2014_TAS_DM.nc.gz"
    ]
    tas_files = []
    for tas_file in TAS_files:
        logger.info("extracting {}".format(tas_file))
        cmd = ["gunzip", tas_file]
        # sub.call(cmd)
        tas_files.append(tas_file[:-3])

    tas_merged_file_daily_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_TAS_{}.nc".format(
        daily_mean)
    tas_merged_file_time_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_TAS_{}.nc".format(
        time_mean)

    if dry == False:

        logger.info("merge files {} to {}".format(tas_files,
                                                  tas_merged_file_daily_mean))
        tmpfile = cdo.mergetime(input=" ".join(tas_files))

        logger.info("removing height dimension")
        nco.ncwa(input=tmpfile,
                 output=tas_merged_file_daily_mean,
                 average="height")

        logger.info("adjusting time axis")
        adjust_time_axis(tas_merged_file_daily_mean, "2001-1-1", "2015-1-1",
                         "days", "1980-1-1", "daily")

        opt = [
            c.Atted(mode="o",
                    att_name="units",
                    var_name="time",
                    value="days since 1980-01-01 03:00:00")
        ]
        nco.ncatted(input=tas_merged_file_daily_mean, options=opt)

        logger.info("calculate time mean")
        cdo.timmean(input=tas_merged_file_daily_mean,
                    output=tas_merged_file_time_mean)

        logger.info("renaming variable")
        rDict = {"tas": "air_temp"}
        nco.ncrename(input=tas_merged_file_time_mean,
                     options=[c.Rename("variable", rDict)])

        logger.info("calculate time mean")
        tmpfile = cdo.timmean(input=tas_merged_file_daily_mean)

        logger.info("renaming variable")
        rDict = {"tas": "air_temp_mean_annual"}
        nco.ncrename(input=tmpfile, options=[c.Rename("variable", rDict)])
        nco.ncks(input=tmpfile, output=tas_merged_file_time_mean, append=True)

        logger.info("calculate standard deviation")
        tmpfile = cdo.timstd(input=tas_merged_file_daily_mean)
        logger.info("renaming variable")
        rDict = {"tas": "air_temp_sd"}
        nco.ncrename(input=tmpfile, options=[c.Rename("variable", rDict)])
        nco.ncks(input=tmpfile, output=tas_merged_file_time_mean, append=True)

        logger.info("calculate mean July temp")
        tmpfile = cdo.timmean(input="-selmonth,7 " +
                              tas_merged_file_daily_mean)
        logger.info("renaming variable")
        rDict = {"tas": "air_temp_mean_july"}
        nco.ncrename(input=tmpfile, options=[c.Rename("variable", rDict)])
        nco.ncks(input=tmpfile, output=tas_merged_file_time_mean, append=True)

        opt = [
            c.Atted(mode="d",
                    att_name="standard_name",
                    var_name="air_temp_mean_annual"),
            c.Atted(mode="d",
                    att_name="standard_name",
                    var_name="air_temp_mean_july"),
            c.Atted(mode="d", att_name="standard_name",
                    var_name="air_temp_sd"),
        ]
        nco.ncatted(input=tas_merged_file_time_mean, options=opt)

    return tas_merged_file_time_mean
nco.ncks(input=tas_merged_file_time_mean,
         output=merged_file_time_mean,
         append=True)

# add topo file
logger.info("removing height dimension of topo file")
topo_file_tmp_1 = "tmp1_topo_geog.nc"
topo_file_tmp_2 = "tmp2_topo_geog.nc"
nco.ncwa(input=topo_file, output=topo_file_tmp_1, average="height")
nco.ncwa(input=topo_file_tmp_1, output=topo_file_tmp_2, average="time")
logger.info("renaming variables and dimensions")
rDict = {"var6": "usurf"}
dDict = {"x": "rlon", "y": "rlat"}
nco.ncrename(
    input=topo_file_tmp_2,
    options=[c.Rename("variable", rDict),
             c.Rename("dimension", dDict)])

logger.info("add topo file {} to {}".format(topo_file, merged_file_time_mean))
nco.ncks(input=topo_file_tmp_2,
         output=merged_file_time_mean,
         append=True,
         variable="usurf")
opt = [
    c.Atted(mode="o", att_name="units", var_name="usurf", value="m"),
    c.Atted(mode="o",
            att_name="standard_name",
            var_name="usurf",
            value="surface_altitude"),
]
nco.ncatted(input=merged_file_time_mean, options=opt)
pr_merged_file_time_mean = "DMI-HIRHAM5_GL2_ERAI_1980_2014_PR_{}.nc".format(time_mean)
logger.info("calculate time mean")
cdo.timmean(input=pr_merged_file_daily_mean, output=pr_merged_file_time_mean)
# # add topo file
# logger.info('add topo file {} to {}'.format(topo_file, pr_merged_file_time_mean))
# nco.ncks(input=topo_file, output=pr_merged_file_time_mean, append=True)

for grid_spacing in (18000, 9000, 4500, 3600, 3000, 2400, 1800, 1500, 1200, 900, 600, 450, 300):
    grid_file = "epsg3413_griddes_{}m.nc".format(grid_spacing)
    logger.info("generating grid description {}".format(grid_file))
    create_epsg3413_grid(grid_file, grid_spacing)
    epsg3414_pr_merged_file_time_mean = "DMI-HIRHAM5_GL2_ERAI_1980_2014_PR_{}_EPSG3413_{}m.nc".format(
        time_mean, grid_spacing
    )
    logger.info("Remapping {} to {}".format(pr_merged_file_time_mean, epsg3414_pr_merged_file_time_mean))
    tmpfile = cdo.remapycon(
        "{} -selvar,pr -setgrid,{}".format(grid_file, rotated_grid_file),
        input=pr_merged_file_time_mean,
        options="-f nc4",
    )
    cdo.setmisstoc(0, input=tmpfile, output=epsg3414_pr_merged_file_time_mean)
    nco.ncks(input=grid_file, output=epsg3414_pr_merged_file_time_mean, append=True)
    opt = [
        c.Atted(mode="d", att_name="_FillValue", var_name="pr"),
        c.Atted(mode="d", att_name="missing_value", var_name="pr"),
        c.Atted(mode="o", att_name="grid_mapping", var_name="pr", value="mapping"),
    ]
    nco.ncatted(input=epsg3414_pr_merged_file_time_mean, options=opt)
    rDict = {"pr": "precipitation"}
    nco.ncrename(input=epsg3414_pr_merged_file_time_mean, options=[c.Rename("variable", rDict)])
def prepare_tas(dry=False):
    logger.info("processing variable TAS")
    TAS_files = [
        "DMI-HIRHAM5_GL2_ERAI_2001_2010_TAS_DM.nc.gz",
        "DMI-HIRHAM5_GL2_ERAI_2011_2014_TAS_DM.nc.gz"
    ]
    tas_files = []
    for tas_file in TAS_files:
        logger.info("extracting {}".format(tas_file))
        cmd = ["gunzip", tas_file]
        sub.call(cmd)
        tas_files.append(tas_file[:-3])

    tas_merged_file_daily_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_TAS_{}.nc".format(
        daily_mean)
    tas_merged_file_time_mean = "DMI-HIRHAM5_GL2_ERAI_2001_2014_TAS_{}.nc".format(
        time_mean)

    if dry == False:

        logger.info("merge files {} to {}".format(tas_files,
                                                  tas_merged_file_daily_mean))
        cdo.mergetime(input=" ".join(tas_files),
                      output=tas_merged_file_daily_mean)

        logger.info("calculate time mean")
        tmpfile = cdo.ydaymean(input="-del29feb " + tas_merged_file_daily_mean)

        logger.info("removing height dimension")
        nco.ncwa(input=tmpfile,
                 output=tas_merged_file_time_mean,
                 average="height")

        logger.info("adjusting time axis")
        adjust_time_axis(tas_merged_file_time_mean)

        logger.info("renaming variable")
        rDict = {"tas": "air_temp"}
        nco.ncrename(input=tas_merged_file_time_mean,
                     options=[c.Rename("variable", rDict)])

        logger.info("calculate standard deviation")
        tmpfile = cdo.ydaystd(input="-del29feb " + tas_merged_file_daily_mean)

        logger.info("removing height dimension")
        tasstdfile = "tmp_tasstd.nc"
        nco.ncwa(input=tmpfile, output=tasstdfile, average="height")

        logger.info("adjusting time axis")
        adjust_time_axis(tasstdfile)

        logger.info("renaming variable")
        rDict = {"tas": "air_temp_sd"}
        nco.ncrename(input=tasstdfile, options=[c.Rename("variable", rDict)])
        nco.ncks(input=tasstdfile,
                 output=tas_merged_file_time_mean,
                 append=True)

        opt = [
            c.Atted(mode="d", att_name="standard_name", var_name="air_temp_sd")
        ]
        nco.ncatted(input=tas_merged_file_time_mean, options=opt)

    return tas_merged_file_time_mean