Пример #1
0
def write_rest(MOM,SIS,FLAGS):
    # Load new restart files
    # Out: Restart files ice_model.res.nc and MOM.res.nc; bathymetry file topog
    #    Change all ocean/ ice fields that need to be changed

    # Save new fields to netCDF
    # Write change mask to netCDF
    if FLAGS.test:
        chg_msk = CDF('/p/projects/climber3/huiskamp/POEM/work/slr_tool/test_data/change_mask.nc', 'w')
    else:
        if os.path.isfile(str(path)+'history/change_mask.nc') == False: # Does the change mask file exist yet? If not, make one
            chg_msk = CDF(str(path)+'history/change_mask', 'w')
            # Create dimensions for vars.
            chg_msk.createDimension('lonh', lon.shape[1])
            chg_msk.createDimension('lath', lat.shape[0])
            chg_msk.createDimension('time', None)
            chg_msk.createVariable('lonh', 'f8', ('lonh'));
            chg_msk.variables['lonh'].units = 'degrees_east'
            chg_msk.variables['lonh'].cartesian_axis = 'X'
            chg_msk.variables['lonh'].long_name = 'T-cell longitude'
            chg_msk.variables['lonh'][:] = grid.variables['lonh'][:]
            chg_msk.createVariable('lath', 'f8', ('lath'));
            chg_msk.variables['lath'].units = 'degrees_north'
            chg_msk.variables['lath'].cartesian_axis = 'Y'
            chg_msk.variables['lath'].long_name = 'T-cell latitude'
            chg_msk.variables['lath'][:] = grid.variables['lath'][:]
            chg_msk.createVariable('time')
            # Define variables and fill data
            # Cell change mask
            chg_msk.createVariable('chng_mask', 'f8', ('lath','lonh'))
            chg_msk.variables['chng_mask'].units = 'none'
            chg_msk.variables['chng_mask'].long_name = 'Mask indicating changing ocean/ land cells'
            chg_msk.variables['chng_mask'][:] = chng_mask[:]
            # New ocean mask
            chg_msk.createVariable('o_mask_new', 'f8', ('lath','lonh'))
            chg_msk.variables['o_mask_new'].units = 'none'
            chg_msk.variables['o_mask_new'].long_name = 'Updated ocean mask'
            chg_msk.variables['o_mask_new'][:] = o_mask_new[:]
    
            chg_msk.description = "This file contains a mask which lists cells that should change from \
                        ocean to land (-1) or land to ocean (1) as well as an updated land-sea mask"
            chg_msk.history = "Created " + time.ctime(time.time())
            chg_msk.source = "created using /p/projects/climber3/huiskamp/POEM/work/slr_tool/5_prep_restarts/prep_restarts.py"
            chg_msk.close()
        else:
            chg_msk = CDF(str(path)+'history/change_mask.nc','r+');
# Write appropriate diagnostics to restart's metadata
            
            
            
            
            
            
            
            
            
            
            
            
            
Пример #2
0
def process(input, output, direction, collapse):
    """Process the file 'input', expanding or collapsing data according to
    'action' and 'direction'. Saves result in 'output'."""
    try:
        nc = CDF(input)
    except:
        print "ERROR: Can't open %s" % input
        exit(1)

    try:
        out = CDF(output, 'w', format="NETCDF3_CLASSIC")
    except:
        print "ERROR: Can't open %s" % output
        exit(1)

    copy_attributes(nc, out)

    for name in nc.dimensions.keys():
        copy_dim(nc, out, name, direction)

    if collapse:
        for name in nc.variables.keys():
            if name == direction:
                continue
            collapse_var(nc, out, name, direction)
        message = "Collapsed using flowline.py"
    else:
        out.createDimension(direction, 3)

        if direction == 'x':
            dim = 'y'
        else:
            dim = 'x'

        var1 = nc.variables[dim]
        delta = np.diff(var1[:])[0]

        var2 = out.createVariable(direction, 'f8', (direction, ))
        var2.axis = "%s" % direction.upper()
        var2.long_name = "%s-coordinate in Cartesian system" % direction.upper(
        )
        var2.standard_name = "projection_%s_coordinate" % direction
        var2.units = var1.units
        var2[:] = [-delta, 0, delta]

        for name in nc.variables.keys():
            expand_var(nc, out, name, direction)

    message = asctime() + ': ' + ' '.join(argv) + '\n'
    if 'history' in out.ncattrs():
        out.history = message + out.history  # prepend to history string
    else:
        out.history = message
    out.close()
Пример #3
0
def make_scalar_vars_ismip6_conforming(filename, ismip6_vars_dict):
    '''
    Make file ISMIP6 conforming
    '''

    # Open file
    nc = CDF(filename, 'a')

    pism_to_ismip6_dict = dict((v.pism_name, k) for k, v in ismip6_vars_dict.iteritems())

    for pism_var in nc.variables:
        nc_var = nc.variables[pism_var]
        if pism_var in pism_to_ismip6_dict.keys():
            ismip6_var = pism_to_ismip6_dict[pism_var]
            print('Processing {} / {}'.format(pism_var, ismip6_var))
            if not pism_var == ismip6_var:
                print('  Renaming {pism_var} to {ismip6_var}'.format(pism_var=pism_var, ismip6_var=ismip6_var))
                nc.renameVariable(pism_var, ismip6_var)
                nc.sync()
            if not nc_var.units == ismip6_vars_dict[ismip6_var].units:
                o_units = ismip6_vars_dict[ismip6_var].units
                i_units = nc_var.units
                print('  Converting {pism_var} from {i_units} to {o_units}'.format(pism_var=pism_var, i_units=i_units, o_units=o_units))
                i_f = cf_units.Unit(i_units)
                o_f = cf_units.Unit(o_units)
                nc_var[:] = i_f.convert(nc_var[:], o_f)
                nc_var.units = o_units
                nc_var.standard_name = ismip6_vars_dict[ismip6_var].standard_name
    nc.close()
Пример #4
0
def adjust_time_axis(icesheet,filename):
    '''
    Adjusts the time axis
    '''

    nc = CDF(filename, 'a')
    time = nc.variables['time']
    time_bnds_var = time.bounds
    time_bnds = nc.variables[time_bnds_var]

    if icesheet=="GIS":
      nt = len(time[:])
      new_timeline = np.linspace(0, 100, nt, endpoint=True)
      time[:] = new_timeline
      time_bnds[:,0] = new_timeline - 5
      time_bnds[:,1] = new_timeline
      time.units = 'years since 2008-1-1'

    elif icesheet=="AIS":
      #time.units = 'years since 2000-1-1'
      #time_bnds.units = 'years since 2000-1-1'
      time.units = 'seconds since 2000-01-01 00:00:00'
      time_bnds.units = 'seconds since 2000-01-01 00:00:00'

    nc.close()
Пример #5
0
def open_ncfile(infile):

    try:
        nc = CDF(infile, 'r')
        try:
            'time' in nc.variables.keys()
        except:
            print('Variable t not found in file %s' % infile)
    except IOError:
        pass

    return nc
def adjust_time_axis(ifile):

    nc = CDF(ifile, "a")

    # create list with dates from start_date until end_date with
    # periodicity prule.
    bnds_datelist = np.arange(0, 366)
    # calculate the days since refdate, including refdate, with time being the
    # mid-point value:
    # time[n] = (bnds[n] + bnds[n+1]) / 2
    bnds_interval_since_refdate = bnds_datelist
    time_interval_since_refdate = bnds_interval_since_refdate[0:-1] + np.diff(
        bnds_interval_since_refdate) / 2

    # create a new dimension for bounds only if it does not yet exist
    time_dim = "time"
    if time_dim not in list(nc.dimensions.keys()):
        nc.createDimension(time_dim)

    # create a new dimension for bounds only if it does not yet exist
    bnds_dim = "nb2"
    if bnds_dim not in list(nc.dimensions.keys()):
        nc.createDimension(bnds_dim, 2)

    # variable names consistent with PISM
    time_var_name = "time"
    bnds_var_name = "time_bnds"

    # create time variable
    if time_var_name not in nc.variables:
        time_var = nc.createVariable(time_var_name, "d", dimensions=(time_dim))
    else:
        time_var = nc.variables[time_var_name]
    time_var[:] = time_interval_since_refdate
    time_var.bounds = bnds_var_name
    time_var.units = "days since 2007-1-1"
    time_var.calendar = "365_day"
    time_var.standard_name = time_var_name
    time_var.axis = "T"

    # create time bounds variable
    if bnds_var_name not in nc.variables:
        time_bnds_var = nc.createVariable(bnds_var_name,
                                          "d",
                                          dimensions=(time_dim, bnds_dim))
    else:
        time_bnds_var = nc.variables[bnds_var_name]

    time_bnds_var[:, 0] = bnds_interval_since_refdate[0:-1]
    time_bnds_var[:, 1] = bnds_interval_since_refdate[1::]

    nc.close()
Пример #7
0
def create_config():
    print("  creating PISM-readable config override file gumparams.nc ...")
    nc = CDF("gumparams.nc", 'w')
    config = nc.createVariable("pism_overrides", 'i4')

    attrs = {
        "constants.standard_gravity": 9.81,
        "constants.standard_gravity_doc": "m s-2; = g",

        "constants.ice.density": 1000.0,
        "constants.ice.density_doc": "kg m-3; 1% Xanthan gum in water has same density as water",

        "stress_balance.sia.bed_smoother.range": -1.0,
        "stress_balance.sia.bed_smoother.range_doc": "m; negative value de-activates bed smoother",

        "bootstrapping.defaults.geothermal_flux": 0.0,
        "bootstrapping.defaults.geothermal_flux_doc": "W m-2; no geothermal",

        "output.runtime.time_unit_name": "second",
        "output.runtime.time_unit_name_doc": "stdout uses seconds (not years) to show model time",

        "output.runtime.time_use_calendar": "no",
        "output.runtime.time_use_calendar_doc": "stdout does not use a calendar to show model time",

        "output.runtime.volume_scale_factor_log10": -15,
        "output.runtime.volume_scale_factor_log10_doc": "; an integer; log base 10 of scale factor to use for volume in summary line to stdout; -15 gives volume in cm^3",

        "output.runtime.area_scale_factor_log10": -10,
        "output.runtime.area_scale_factor_log10_doc": "; an integer; log base 10 of scale factor to use for area in summary line to stdout; -10 gives area in cm^2",

        "geometry.ice_free_thickness_standard": 1e-8,
        "geometry.ice_free_thickness_standard_doc": "m; only if the fluid is less than this is a cell marked as ice free",

        "output.ice_free_thickness_standard": 1e-8,
        "output.ice_free_thickness_standard_doc": "fluid layer exceeding this thickness is included in computations of areas and volumes",

        "time_stepping.adaptive_ratio": 0.08,
        "time_stepping.adaptive_ratio_doc": "; compare default 0.12; needs to be smaller because gum suspension is more shear-thinning than ice?",

        "stress_balance.sia.Glen_exponent": 5.9,
        "stress_balance.sia.Glen_exponent_doc": "; : n;  Sayag & Worster (2013) give n = 5.9 +- 0.2",

        "flow_law.isothermal_Glen.ice_softness": 9.7316e-09,  # vs (e.g.) 4e-25 Pa-3 s-1 for ice
        "ice_softness_doc": "Pa-n s-1; = A_0 = B_0^(-n) = (2 x 11.4 Pa s^(1/n))^(-n);  Sayag & Worster (2013) give B_0/2 = tilde mu = 11.4 +- 0.25 Pa s^(1/n)"
    }

    keys = list(attrs.keys())
    keys.sort()
    for k in keys:
        config.setncattr(k, attrs[k])

    nc.close()
Пример #8
0
def add_projection_info(ncfile):

    # if 'mapping' not in pism_vars_av:
    print "  Add 'mapping' information"
    nc = CDF(ncfile, 'a')
    mapping = nc.createVariable("mapping", 'c')
    mapping.ellipsoid = "WGS84"
    mapping.false_easting = 0.
    mapping.false_northing = 0.
    mapping.grid_mapping_name = "polar_stereographic"
    mapping.latitude_of_projection_origin = -90.
    mapping.standard_parallel = -71.
    mapping.straight_vertical_longitude_from_pole = 0.
    nc.close()
Пример #9
0
def create_config():
    print "  creating PISM-readable config override file gumparams.nc ..."
    nc = CDF("gumparams.nc", 'w')
    config = nc.createVariable("pism_overrides", 'i4')

    config.standard_gravity = 9.81
    config.standard_gravity_doc = "m s-2; = g"

    config.ice_density = 1000.0
    config.ice_density_doc = "kg m-3; 1% Xanthan gum in water has same density as water"

    config.bed_smoother_range = -1.0
    config.bed_smoother_range_doc = "m; negative value de-activates bed smoother"

    config.bootstrapping_geothermal_flux_value_no_var = 0.0
    config.bootstrapping_geothermal_flux_value_no_var_doc = "W m-2; no geothermal"

    config.summary_time_unit_name = "second"
    config.summary_time_unit_name_doc = "stdout uses seconds (not years) to show model time"

    config.summary_time_use_calendar = "no"
    config.summary_time_use_calendar_doc = "stdout does not use a calendar to show model time"

    config.summary_vol_scale_factor_log10 = -15
    config.summary_vol_scale_factor_log10_doc = "; an integer; log base 10 of scale factor to use for volume in summary line to stdout; -15 gives volume in cm^3"

    config.summary_area_scale_factor_log10 = -10
    config.summary_area_scale_factor_log10_doc = "; an integer; log base 10 of scale factor to use for area in summary line to stdout; -10 gives area in cm^2"

    config.mask_icefree_thickness_standard = 1e-8
    config.mask_icefree_thickness_standard_doc = "m; only if the fluid is less than this is a cell marked as ice free"

    config.mask_is_floating_thickness_standard = 1e-8
    config.mask_is_floating_thickness_standard_doc = "m; should not matter since all grounded"

    config.adaptive_timestepping_ratio = 0.08
    config.adaptive_timestepping_ratio_doc = "; compare default 0.12; needs to be smaller because gum suspension is more shear-thinning than ice?"

    config.sia_Glen_exponent = 5.9
    config.sia_Glen_exponent_doc = "; = n;  Sayag & Worster (2013) give n = 5.9 +- 0.2"

    config.ice_softness = 9.7316e-09  # vs (e.g.) 4e-25 Pa-3 s-1 for ice
    config.ice_softness_doc = "Pa-n s-1; = A_0 = B_0^(-n) = (2 x 11.4 Pa s^(1/n))^(-n);  Sayag & Worster (2013) give B_0/2 = tilde mu = 11.4 +- 0.25 Pa s^(1/n)"

    nc.close()
Пример #10
0
(opts, args) = parser.parse_args()

if len(args) != 1:
    print("ERROR: File argument is missing.")
    exit(1)


if (opts.expand and opts.collapse) or ((not opts.expand) and (not opts.collapse)):
    print("ERROR: exactly one of -e and -c is required.")
    exit(1)

if not opts.direction:
    if opts.collapse or (not opts.expand):
        opts.direction = 'y'
    else:
        nc = CDF(args[0])
        try:
            x = nc.variables['x']
            opts.direction = 'y'
        except:
            opts.direction = 'x'
        nc.close()
elif opts.direction not in ['x', 'y']:
    print("ERROR: Please specify direction using the -d option. (Choose one of x,y.)")
    exit(1)

if (not opts.output_filename):
    print("ERROR: Please specify the output file name using the -o option.")
    exit(1)

if opts.collapse:
Пример #11
0
    '#984EA3',  # violet
    '#FF7F00',  # orange
    '#FF7F00',  # orange
    '#FF7F00',  # orange
    '#377EB8',  # light blue
    '#377EB8',  # light blue
    '#377EB8',  # light blue
    '#4DAF4A'
]  # green

dashes = ['-', '--', '-.', '-', '--', '-.', '-', '--', '-.', '-']

print "control run name is " + NCNAMES[0]

n = len(NCNAMES)
nc0 = CDF(NCNAMES[0], 'r')
try:
    t_units = nc0.variables['tseries'].units
    t = nc0.variables['tseries'][t_a:t_e]
except:
    t_units = nc0.variables['time'].units
    t = nc0.variables['time'][t_a:t_e]
nc0.close()

# convert to years if t is in seconds
if (t_units.split()[0] == ('seconds' or 's')):
    t /= 3.15569259747e7

ivol = zeros((len(t), n))
ivolshift = zeros((len(t), n - 1))
Пример #12
0
    "-t",
    "--target_resolution",
    dest="target_resolution",
    type=int,
    choices=[1000, 5000],
    help="Horizontal grid resolution",
    default=5000,
)

options = parser.parse_args()
experiment = options.experiment
infile = options.EXP_FILE[0]
target_resolution = options.target_resolution

# Need to get grid resolution from file
nc = CDF(infile, "r")
pism_grid_dx = int(round(nc.variables["run_stats"].grid_dx_meters))
nc.close()
PISM_GRID_RES_ID = str(pism_grid_dx / 100)
TARGET_GRID_RES_ID = str(target_resolution / 1000)
ID = options.id

IS = "GIS"
GROUP = "UAF"
MODEL = "PISM" + PISM_GRID_RES_ID + ID
EXP = experiment
TYPE = "_".join([EXP, "0" + TARGET_GRID_RES_ID])
INIT = "_".join(["init", "0" + TARGET_GRID_RES_ID])
project = "{IS}_{GROUP}_{MODEL}".format(IS=IS, GROUP=GROUP, MODEL=MODEL)
pism_stats_vars = ["pism_config", "run_stats"]
Пример #13
0
                         lat_ts=var_mapping.standard_parallel,
                         lat_0=var_mapping.latitude_of_projection_origin,
                         lon_0=var_mapping.straight_vertical_longitude_from_pole,
                         x_0=var_mapping.false_easting,
                         y_0=var_mapping.false_northing)
            except:
                print('No mapping information found, exiting.')
                sys.exit(1)

    return p


if __name__ == "__main__":

    # open netCDF file in 'append' mode
    nc = CDF(nc_outfile, 'a')

    # a list of possible x-dimensions names
    xdims = ['x', 'x1']
    # a list of possible y-dimensions names
    ydims = ['y', 'y1']

    # assign x dimension
    for dim in xdims:
        if dim in list(nc.dimensions.keys()):
            xdim = dim
    # assign y dimension
    for dim in ydims:
        if dim in list(nc.dimensions.keys()):
            ydim = dim
Пример #14
0
                     lat_ts=var_mapping.standard_parallel,
                     lat_0=var_mapping.latitude_of_projection_origin,
                     lon_0=var_mapping.straight_vertical_longitude_from_pole,
                     x_0=var_mapping.false_easting,
                     y_0=var_mapping.false_northing)
        except:
            print('No mapping information found, exiting.')
            exit(1)

    return p


if __name__ == "__main__":

    ## open netCDF file in 'append' mode
    nc = CDF(nc_outfile, 'a', format='NETCDF3_CLASSIC')

    ## a list of possible x-dimensions names
    xdims = ['x', 'x1']
    ## a list of possible y-dimensions names
    ydims = ['y', 'y1']

    ## assign x dimension
    for dim in xdims:
        if dim in nc.dimensions.keys():
            xdim = dim
    ## assign y dimension
    for dim in ydims:
        if dim in nc.dimensions.keys():
            ydim = dim
Пример #15
0
]
#sub.call(cmd_ncre)

#add step forcing
ca_cmd = [
    'python', 'create_anomalies.py', '--force_file', IM_filename,
    '--background_file', PD_pism_climate, '{}'.format(final_filename)
]
#print ca_cmd
sub.call(ca_cmd)

#add x and y and mapping
ncks_cmd = ['ncks', '-A', '-v', 'x,y', IM_filename, final_filename]
sub.call(ncks_cmd)

nc = CDF(final_filename, 'a')
try:
    mapping = nc.createVariable("mapping", 'c')
    mapping.ellipsoid = "WGS84"
    mapping.false_easting = 0.
    mapping.false_northing = 0.
    mapping.grid_mapping_name = "polar_stereographic"
    mapping.latitude_of_projection_origin = -90.
    mapping.standard_parallel = -71.
    mapping.straight_vertical_longitude_from_pole = 0.
except:
    print "  Mapping seems to exist!"
nc.close()

#change units
ncatted_cmd = [
Пример #16
0
ice_thickness_threshold = 10.0

# PISM mask values:
pism_ice_free_bedrock = 0
pism_grounded = 2
pism_floating = 3
pism_ocean = 4

# SeaRISE mask values:
searise_ocean = 1
searise_ice_free_land = 2
searise_grounded_ice = 3
searise_floating_ice = 4

nc = CDF(input, 'a')

# number of records to process:
try:
    N = len(nc.variables['t'][:])
except:
    N = len(nc.variables['time'][:])

thk = nc.variables['thk']
mask = nc.variables['mask']

mask.flag_meanings = "ice_free_ocean ice_free_land grounded_ice floating_ice"
mask.flag_values = [1, 2, 3, 4]
mask.long_name = "integer mask specifying cell type"

for j in range(N):
Пример #17
0
def create_epsg3413_grid(ofile, grid_spacing):

    xdim = "x"
    ydim = "y"

    # define output grid, these are the extents of Mathieu's domain (cell
    # corners)
    e0 = -638000
    n0 = -3349600
    e1 = 864700
    n1 = -657600

    # Add a buffer on each side such that we get nice grids up to a grid spacing
    # of 36 km.

    buffer_e = 148650
    buffer_n = 130000
    e0 -= buffer_e + 468000
    n0 -= buffer_n
    e1 += buffer_e
    n1 += buffer_n

    # Shift to cell centers
    e0 += grid_spacing / 2
    n0 += grid_spacing / 2
    e1 -= grid_spacing / 2
    n1 -= grid_spacing / 2

    de = dn = grid_spacing  # m
    M = int((e1 - e0) / de) + 1
    N = int((n1 - n0) / dn) + 1

    easting = np.linspace(e0, e1, M)
    northing = np.linspace(n0, n1, N)
    ee, nn = np.meshgrid(easting, northing)

    # Set up EPSG 3413 (NSIDC north polar stereo) projection
    projection = "+init=epsg:3413"
    proj = Proj(projection)

    lon, lat = proj(ee, nn, inverse=True)

    # number of grid corners
    grid_corners = 4
    # grid corner dimension name
    grid_corner_dim_name = "nv4"

    # array holding x-component of grid corners
    gc_easting = np.zeros((M, grid_corners))
    # array holding y-component of grid corners
    gc_northing = np.zeros((N, grid_corners))
    # array holding the offsets from the cell centers
    # in x-direction (counter-clockwise)
    de_vec = np.array([-de / 2, de / 2, de / 2, -de / 2])
    # array holding the offsets from the cell centers
    # in y-direction (counter-clockwise)
    dn_vec = np.array([-dn / 2, -dn / 2, dn / 2, dn / 2])
    # array holding lat-component of grid corners
    gc_lat = np.zeros((N, M, grid_corners))
    # array holding lon-component of grid corners
    gc_lon = np.zeros((N, M, grid_corners))

    for corner in range(0, grid_corners):
        ## grid_corners in x-direction
        gc_easting[:, corner] = easting + de_vec[corner]
        # grid corners in y-direction
        gc_northing[:, corner] = northing + dn_vec[corner]
        # meshgrid of grid corners in x-y space
        gc_ee, gc_nn = np.meshgrid(gc_easting[:, corner], gc_northing[:,
                                                                      corner])
        # project grid corners from x-y to lat-lon space
        gc_lon[:, :, corner], gc_lat[:, :, corner] = proj(gc_ee,
                                                          gc_nn,
                                                          inverse=True)

    nc = CDF(ofile, "w")

    nc.createDimension(xdim, size=easting.shape[0])
    nc.createDimension(ydim, size=northing.shape[0])

    var = xdim
    var_out = nc.createVariable(var, "f", dimensions=(xdim))
    var_out.axis = xdim
    var_out.long_name = "X-coordinate in Cartesian system"
    var_out.standard_name = "projection_x_coordinate"
    var_out.units = "meters"
    var_out[:] = easting

    var = ydim
    var_out = nc.createVariable(var, "f", dimensions=(ydim))
    var_out.axis = ydim
    var_out.long_name = "Y-coordinate in Cartesian system"
    var_out.standard_name = "projection_y_coordinate"
    var_out.units = "meters"
    var_out[:] = northing

    var = "lon"
    var_out = nc.createVariable(var, "f", dimensions=(ydim, xdim))
    var_out.units = "degrees_east"
    var_out.valid_range = -180.0, 180.0
    var_out.standard_name = "longitude"
    var_out.bounds = "lon_bnds"
    var_out[:] = lon

    var = "lat"
    var_out = nc.createVariable(var, "f", dimensions=(ydim, xdim))
    var_out.units = "degrees_north"
    var_out.valid_range = -90.0, 90.0
    var_out.standard_name = "latitude"
    var_out.bounds = "lat_bnds"
    var_out[:] = lat

    nc.createDimension(grid_corner_dim_name, size=grid_corners)

    var = "lon_bnds"
    # Create variable 'lon_bnds'
    var_out = nc.createVariable(var,
                                "f",
                                dimensions=(ydim, xdim, grid_corner_dim_name))
    # Assign units to variable 'lon_bnds'
    var_out.units = "degreesE"
    # Assign values to variable 'lon_nds'
    var_out[:] = gc_lon

    var = "lat_bnds"
    # Create variable 'lat_bnds'
    var_out = nc.createVariable(var,
                                "f",
                                dimensions=(ydim, xdim, grid_corner_dim_name))
    # Assign units to variable 'lat_bnds'
    var_out.units = "degreesN"
    # Assign values to variable 'lat_bnds'
    var_out[:] = gc_lat

    var = "dummy"
    var_out = nc.createVariable(var,
                                "f",
                                dimensions=("y", "x"),
                                fill_value=-2e9)
    var_out.units = "meters"
    var_out.long_name = "Just A Dummy"
    var_out.comment = "This is just a dummy variable for CDO."
    var_out.grid_mapping = "mapping"
    var_out.coordinates = "lon lat"
    var_out[:] = 0.0

    mapping = nc.createVariable("mapping", "c")
    mapping.ellipsoid = "WGS84"
    mapping.false_easting = 0.0
    mapping.false_northing = 0.0
    mapping.grid_mapping_name = "polar_stereographic"
    mapping.latitude_of_projection_origin = 90.0
    mapping.standard_parallel = 70.0
    mapping.straight_vertical_longitude_from_pole = -45.0

    from time import asctime

    historystr = "Created " + asctime() + "\n"
    nc.history = historystr
    nc.proj4 = projection
    nc.Conventions = "CF-1.5"
    nc.close()
Пример #18
0
def adjust_time_axis(ifile, start_date, end_date, ref_unit, ref_date,
                     periodicity):

    nc = CDF(ifile, "a")

    time_units = "%s since %s" % (ref_unit, ref_date)
    time_calendar = "standard"

    cdftime = utime(time_units, time_calendar)

    # create a dictionary so that we can supply the periodicity as a
    # command-line argument.
    pdict = {}
    pdict["SECONDLY"] = rrule.SECONDLY
    pdict["MINUTELY"] = rrule.MINUTELY
    pdict["HOURLY"] = rrule.HOURLY
    pdict["DAILY"] = rrule.DAILY
    pdict["WEEKLY"] = rrule.WEEKLY
    pdict["MONTHLY"] = rrule.MONTHLY
    pdict["YEARLY"] = rrule.YEARLY
    prule = pdict[periodicity.upper()]

    # reference date from command-line argument
    r = time_units.split(" ")[2].split("-")
    refdate = datetime(int(r[0]), int(r[1]), int(r[2]))

    # create list with dates from start_date until end_date with
    # periodicity prule.
    bnds_datelist = list(
        rrule.rrule(prule, dtstart=parse(start_date), until=parse(end_date)))

    # calculate the days since refdate, including refdate, with time being the
    # mid-point value:
    # time[n] = (bnds[n] + bnds[n+1]) / 2
    bnds_interval_since_refdate = cdftime.date2num(bnds_datelist)
    time_interval_since_refdate = bnds_interval_since_refdate[0:-1] + np.diff(
        bnds_interval_since_refdate) / 2

    # create a new dimension for bounds only if it does not yet exist
    time_dim = "time"
    if time_dim not in list(nc.dimensions.keys()):
        nc.createDimension(time_dim)

    # create a new dimension for bounds only if it does not yet exist
    bnds_dim = "nb2"
    if bnds_dim not in list(nc.dimensions.keys()):
        nc.createDimension(bnds_dim, 2)

    # variable names consistent with PISM
    time_var_name = "time"
    bnds_var_name = "time_bnds"

    # create time variable
    if time_var_name not in nc.variables:
        time_var = nc.createVariable(time_var_name, "d", dimensions=(time_dim))
    else:
        time_var = nc.variables[time_var_name]
    time_var[:] = time_interval_since_refdate
    time_var.bounds = bnds_var_name
    time_var.units = time_units
    time_var.calendar = time_calendar
    time_var.standard_name = time_var_name
    time_var.axis = "T"

    # create time bounds variable
    if bnds_var_name not in nc.variables:
        time_bnds_var = nc.createVariable(bnds_var_name,
                                          "d",
                                          dimensions=(time_dim, bnds_dim))
    else:
        time_bnds_var = nc.variables[bnds_var_name]

    time_bnds_var[:, 0] = bnds_interval_since_refdate[0:-1]
    time_bnds_var[:, 1] = bnds_interval_since_refdate[1::]

    nc.close()
    print "Prepare PISM output for ismip6 compatibility and save as '{}'".format(
        infile_ismip6)
    ncks_cmd = ['ncks', '-O', '-4', '-L', '3', infile, infile_ismip6]
    sub.call(ncks_cmd)

    #"total over ice domain of top surface ice mass flux = sub_shelf_ice_flux + grounded_basal_ice_flux" ;
    print "  Add variable 'tendlibmassbf'"
    ncap2_cmd = [
        'ncap2', '-O', '-s',
        'tendlibmassbf = sub_shelf_ice_flux + grounded_basal_ice_flux',
        infile_ismip6, infile_ismip6
    ]
    sub.call(ncap2_cmd)

    # Check if request variables are present
    nc = CDF(infile_ismip6, 'r')
    for m_var in pism_copy_vars:
        if m_var not in nc.variables:
            print("Requested variable '{}' missing".format(m_var))
    nc.close()

    print(' Transfer pism_copy_vars to {}'.format(out_file))
    cmd = 'ncks -O -v ' + ','.join(
        pism_copy_vars) + " " + infile_ismip6 + " " + initmipvars_file
    sub.check_call(cmd, shell=True)

    # adjust time axis: shift by two years (PISM issue: wrong time setting), and select
    # the INITMIP 100 yr forcing period,
    cmd = "module load cdo && cdo -O setreftime,20000101,000000,seconds -selyear,2000/2100 -shifttime,-2year "+\
            initmipvars_file+" "+out_file
    sub.check_call(cmd, shell=True)
Пример #20
0
import numpy as np

try:
    from netCDF4 import Dataset as CDF
except:
    print "netCDF4 is not installed!"
    sys.exit(1)

x, topg, thk = np.loadtxt('sg_35m_flowline.txt', unpack=True)

output = 'storglaciaren_flowline.nc'

# Write the data:
print("Writing the data to '%s'... " % output)
nc = CDF(output, "w")
nc.createDimension("x", size=len(x))

x_var = nc.createVariable("x", 'f', dimensions=("x", ))
x_var.units = "m"
x_var[:] = x

topg_var = nc.createVariable("topg", 'f', dimensions=("x", ))
topg_var.units = "m"
topg_var.standard_name = "bedrock_altitude"
topg_var[:] = topg

thk_var = nc.createVariable("thk", 'f', dimensions=("x", ))
thk_var.units = "m"
thk_var.standard_name = "land_ice_thickness"
thk_var[:] = thk
Пример #21
0
def prepare_ncfile_for_cdo(nc_outfile):
    """ This is a copy of the PISM nc2cdo.py code. """
    print("Preparing {} for cdo...".format(nc_outfile))

    # open netCDF file in 'append' mode
    nc = CDF(nc_outfile, 'a')

    # a list of possible x-dimensions names
    xdims = ['x', 'x1']
    # a list of possible y-dimensions names
    ydims = ['y', 'y1']

    # assign x dimension
    for dim in xdims:
        if dim in nc.dimensions.keys():
            xdim = dim
    # assign y dimension
    for dim in ydims:
        if dim in nc.dimensions.keys():
            ydim = dim

    # coordinate variable in x-direction
    x_var = nc.variables[xdim]
    # coordinate variable in y-direction
    y_var = nc.variables[ydim]

    # values of the coordinate variable in x-direction
    easting = x_var[:]
    # values of the coordinate variable in y-direction
    northing = y_var[:]

    # grid spacing in x-direction
    de = np.diff(easting)[0]
    # grid spacing in y-direction
    dn = np.diff(northing)[0]

    # number of grid points in x-direction
    M = easting.shape[0]
    # number of grid points in y-direction
    N = northing.shape[0]

    # number of grid corners
    grid_corners = 4
    # grid corner dimension name
    grid_corner_dim_name = "nv4"

    # array holding x-component of grid corners
    gc_easting = np.zeros((M, grid_corners))
    # array holding y-component of grid corners
    gc_northing = np.zeros((N, grid_corners))
    # array holding the offsets from the cell centers
    # in x-direction (counter-clockwise)
    de_vec = np.array([-de / 2, de / 2, de / 2, -de / 2])
    # array holding the offsets from the cell centers
    # in y-direction (counter-clockwise)
    dn_vec = np.array([-dn / 2, -dn / 2, dn / 2, dn / 2])
    # array holding lat-component of grid corners
    gc_lat = np.zeros((N, M, grid_corners))
    # array holding lon-component of grid corners
    gc_lon = np.zeros((N, M, grid_corners))

    proj = get_projection_from_file(nc)

    # If it does not yet exist, create dimension 'grid_corner_dim_name'
    if grid_corner_dim_name not in nc.dimensions.keys():
        for corner in range(0, grid_corners):
            ## grid_corners in x-direction
            gc_easting[:, corner] = easting + de_vec[corner]
            # grid corners in y-direction
            gc_northing[:, corner] = northing + dn_vec[corner]
            # meshgrid of grid corners in x-y space
            gc_ee, gc_nn = np.meshgrid(gc_easting[:, corner],
                                       gc_northing[:, corner])
            # project grid corners from x-y to lat-lon space
            gc_lon[:, :, corner], gc_lat[:, :, corner] = proj(gc_ee,
                                                              gc_nn,
                                                              inverse=True)

        nc.createDimension(grid_corner_dim_name, size=grid_corners)

        var = 'lon_bnds'
        # Create variable 'lon_bnds'
        var_out = nc.createVariable(var,
                                    'float64',
                                    dimensions=(ydim, xdim,
                                                grid_corner_dim_name))
        # Assign units to variable 'lon_bnds'
        var_out.units = "degreesE"
        # Assign values to variable 'lon_nds'
        var_out[:] = gc_lon

        var = 'lat_bnds'
        # Create variable 'lat_bnds'
        var_out = nc.createVariable(var,
                                    'float64',
                                    dimensions=(ydim, xdim,
                                                grid_corner_dim_name))
        # Assign units to variable 'lat_bnds'
        var_out.units = "degreesN"
        # Assign values to variable 'lat_bnds'
        var_out[:] = gc_lat

    if (not 'lon' in nc.variables.keys()) or (not 'lat'
                                              in nc.variables.keys()):
        print("No lat/lon coordinates found, creating them")
        ee, nn = np.meshgrid(easting, northing)
        lon, lat = proj(ee, nn, inverse=True)

    var = 'lon'
    # If it does not yet exist, create variable 'lon'
    if not var in nc.variables.keys():
        var_out = nc.createVariable(var, 'f', dimensions=(ydim, xdim))
        # Assign values to variable 'lon'
        var_out[:] = lon
    else:
        var_out = nc.variables[var]
    # Assign units to variable 'lon'
    var_out.units = "degreesE"
    # Assign long name to variable 'lon'
    var_out.long_name = "Longitude"
    # Assign standard name to variable 'lon'
    var_out.standard_name = "longitude"
    # Assign bounds to variable 'lon'
    var_out.bounds = "lon_bnds"

    var = 'lat'
    # If it does not yet exist, create variable 'lat'
    if not var in nc.variables.keys():
        var_out = nc.createVariable(var, 'f', dimensions=(ydim, xdim))
        var_out[:] = lat
    else:
        var_out = nc.variables[var]
    # Assign units to variable 'lat'
    var_out.units = "degreesN"
    # Assign long name to variable 'lat'
    var_out.long_name = "Latitude"
    # Assign standard name to variable 'lat'
    var_out.standard_name = "latitude"
    # Assign bounds to variable 'lat'
    var_out.bounds = "lat_bnds"

    # Make sure variables have 'coordinates' attribute
    for var in nc.variables.keys():
        if (nc.variables[var].ndim >= 2):
            nc.variables[var].coordinates = "lon lat"

    # lat/lon coordinates must not have mapping and coordinate attributes
    # if they exist, delete them
    for var in ['lat', 'lon', 'lat_bnds', 'lon_bnds']:
        if hasattr(nc.variables[var], 'grid_mapping'):
            delattr(nc.variables[var], 'grid_mapping')
        if hasattr(nc.variables[var], 'coordinates'):
            delattr(nc.variables[var], 'coordinates')

    # If present prepend history history attribute, otherwise create it
    from time import asctime
    histstr = asctime() + \
        ' : grid info for CDO added by nc2cdo.py, a PISM utility\n'
    if 'History' in nc.ncattrs():
        nc.History = histstr + str(nc.History)
    elif 'history' in nc.ncattrs():
        nc.history = histstr + str(nc.history)
    else:
        nc.history = histstr

    for attr in ("projection", "proj4"):
        if hasattr(nc, attr):
            delattr(nc, attr)
    # Write projection attribute
    nc.proj4 = proj.srs
    # Close file
    nc.close()

    print "Prepared file", nc_outfile, "for cdo."
Пример #22
0
                    lat_0=var_mapping.latitude_of_projection_origin,
                    lon_0=var_mapping.straight_vertical_longitude_from_pole,
                    x_0=var_mapping.false_easting,
                    y_0=var_mapping.false_northing,
                )
            except:
                print("No mapping information found, exiting.")
                sys.exit(1)

    return p


if __name__ == "__main__":

    # open netCDF file in 'append' mode
    nc = CDF(nc_outfile, "a")

    # a list of possible x-dimensions names
    xdims = ["x", "x1"]
    # a list of possible y-dimensions names
    ydims = ["y", "y1"]

    # assign x dimension
    for dim in xdims:
        if dim in list(nc.dimensions.keys()):
            xdim = dim
    # assign y dimension
    for dim in ydims:
        if dim in list(nc.dimensions.keys()):
            ydim = dim
Пример #23
0
    # -------------------- general setup --------------------
    t_main_start = t.time()

    if args.verbose:
        print("Running", sys.argv[0])
        print(" -> verbose output = True")
        print()

    # --------------- read PISM restart file ----------------
    #   -> read PISM restart file to extract timestamp
    if args.verbose:
        print("... reading PISM restart file '" + args.pism_restart_file + "'")
    t_read_pismrestartfile_start = t.time()

    try:
        nc_pism = CDF(args.pism_restart_file, 'r')
    except:
        print("pism_restart_file '" + args.pism_restart_file + \
                "' can't be found! Exiting.")
        sys.exit(1)

    # read field array
    try:
        nc_pism.variables['time'].set_auto_mask(False)
        pism_time_raw = np.squeeze(nc_pism.variables['time'][:])
        #pism_time__sec = pism_time_raw.data
        pism_time__sec = pism_time_raw
        print("pism_time__sec: ", pism_time__sec)
        print("pism_time_raw: ", pism_time_raw)
        print("type(pism_time_raw): ", type(pism_time_raw))
    except:
Пример #24
0
    action="store_true",
    help="Override weights file",
    default=False,
)

options = parser.parse_args()
experiment = options.experiment
infile = options.EXP_FILE[0]
n_procs = options.n_procs
override_weights_file = options.override_weights_file
remap_method = options.remap_method
target_resolution = options.target_resolution
target_grid_filename = "searise_grid_{}m.nc".format(target_resolution)

# Need to get grid resolution from file
nc = CDF(infile, "r")
pism_grid_dx = int(round(nc.variables["run_stats"].grid_dx_meters))
nc.close()
PISM_GRID_RES_ID = str(pism_grid_dx / 100)
TARGET_GRID_RES_ID = str(target_resolution / 1000)
ID = options.id

IS = "GIS"
GROUP = "UAF"
MODEL = "PISM" + PISM_GRID_RES_ID + ID
EXP = experiment
TYPE = "_".join([EXP, "0" + TARGET_GRID_RES_ID])
INIT = "_".join(["init", "0" + TARGET_GRID_RES_ID])
project = "{IS}_{GROUP}_{MODEL}".format(IS=IS, GROUP=GROUP, MODEL=MODEL)

pism_stats_vars = ["pism_config", "run_stats"]
Пример #25
0
# create dummy fields
[xx, yy] = np.meshgrid(x, y)
acab = np.zeros((Mx, My))
artm = np.zeros((Mx, My)) + 273.15 + 10.0
# 10 degrees Celsius
topg = 1000.0 + 200.0 * (xx + yy) / max(Lx, Ly)
# change "1000.0" to "0.0" to test
# flotation criterion, etc.
thk = 3000.0 * (1.0 - 3.0 * (xx**2 + yy**2) / Lx**2)
thk[thk < 0.0] = 0.0

# Output filename
ncfile = 'foo.nc'

# Write the data:
nc = CDF(ncfile, "w", format='NETCDF3_CLASSIC')  # for netCDF4 module

# Create dimensions x and y
nc.createDimension("x", size=Mx)
nc.createDimension("y", size=My)

x_var = nc.createVariable("x", 'f4', dimensions=("x", ))
x_var.units = "m"
x_var.long_name = "easting"
x_var.standard_name = "projection_x_coordinate"
x_var[:] = x

y_var = nc.createVariable("y", 'f4', dimensions=("y", ))
y_var.units = "m"
y_var.long_name = "northing"
y_var.standard_name = "projection_y_coordinate"
Пример #26
0
        output_dir, config.initmip_naming[exp] + "_" + config.resolution)

    if not os.path.exists(single_variable_dir):
        os.makedirs(single_variable_dir)

    tmp_file1 = os.path.join(output_dir, 'tmp1.nc')
    tmp_file2 = os.path.join(output_dir, 'tmp2.nc')

    for f in [tmp_file1, tmp_file2]:
        try:
            os.remove(f)
        except OSError:
            pass

    # Check if request variables are present
    nc = CDF(infile, 'r')
    for m_var in pism_copy_vars:
        if m_var not in nc.variables:
            print("Requested variable '{}' missing".format(m_var))
    nc.close()

    print('Extract initmip variables from {} to {}'.format(infile, tmp_file1))
    # also delete last two and first time step.
    cmd = 'ncks -O -4 -L3 -d time,1,-3 -v '+'{}'.format(','.join(pism_copy_vars))+' '+\
           infile+" "+tmp_file1
    print cmd
    sub.check_call(cmd, shell=True)

    # adjust time axis: shift by two years (PISM issue: wrong time setting)
    cmd = "module load cdo && cdo setreftime,20000101,000000,seconds -shifttime,-2year " + tmp_file1 + " " + tmp_file2
    sub.check_call(cmd, shell=True)
Пример #27
0
thk = np.zeros((Mx, My))  # no fluid on table at start
artm = np.zeros((Mx, My)) + 273.15 + temp  # 20 degrees Celsius

# smb = flux as m s-1, but scaled so that the total is correct even on a coarse grid
smb = np.zeros((Mx, My))
smb[xx ** 2 + yy ** 2 <= pipeR ** 2 + 1.0e-10] = 1.0
smbpos = sum(sum(smb))
if smbpos == 0:
    print "gridding ERROR: no cells have positive input flux ... ending now"
    sys.exit(1)
else:
    print "  input flux > 0 at %d cells ..." % smbpos
smb = (flux / (smbpos * dx * dy)) * smb  # [flux] = kg s-1  so now  [smb] = kg m-2 s-1

# Write the data:
nc = CDF(args.o, "w", format='NETCDF3_CLASSIC')  # for netCDF4 module

# Create dimensions x and y
nc.createDimension("x", size=Mx)
nc.createDimension("y", size=My)

x_var = nc.createVariable("x", 'f4', dimensions=("x",))
x_var.units = "m"
x_var.long_name = "easting"
x_var.standard_name = "projection_x_coordinate"
x_var[:] = x

y_var = nc.createVariable("y", 'f4', dimensions=("y",))
y_var.units = "m"
y_var.long_name = "northing"
y_var.standard_name = "projection_y_coordinate"
Пример #28
0
        print(" -> verbose output = True")
        print()

    # a list of possible x,y-dimensions names
    xdims = ['x', 'x1']
    ydims = ['y', 'y1']

    # -------------------- read input file --------------------
    #   -> read file with regridded variables to be processed
    if args.verbose:
        print("... reading input file '" + args.infile + "'")

    t_read_infile_start = t.time()

    try:
        nc_src = CDF(args.infile, 'r')
    except:
        print("INFILE '" + args.infile + "' can't be found! Exiting.")
        sys.exit(1)

    # assign x,y dimension
    for dim in xdims:
        if dim in list(nc_src.dimensions.keys()):
            xdim = dim
    for dim in ydims:
        if dim in list(nc_src.dimensions.keys()):
            ydim = dim

    # read field array
    try:
        field = np.squeeze(nc_src.variables[args.field][:])
Пример #29
0
    de = dn = grid_spacing  # m
    M = int((e1 - e0) / de) + 1
    N = int((n1 - n0) / dn) + 1

    easting = np.linspace(e0, e1, M)
    northing = np.linspace(n0, n1, N)
    ee, nn = np.meshgrid(easting, northing)

    # Set up EPSG 3413 (NSIDC north polar stereo) projection
    projection = "+init=epsg:3413"
    proj = Proj(projection)

    lon, lat = proj(ee, nn, inverse=True)

    nc = CDF(nc_outfile, "w", format=fileformat)

    nc.createDimension("x", size=easting.shape[0])
    nc.createDimension("y", size=northing.shape[0])

    var = "x"
    var_out = nc.createVariable(var, "d", dimensions=("x"))
    var_out.axis = "X"
    var_out.long_name = "X-coordinate in Cartesian system"
    var_out.standard_name = "projection_x_coordinate"
    var_out.units = "meters"
    var_out[:] = easting

    var = "y"
    var_out = nc.createVariable(var, "d", dimensions=("y"))
    var_out.axis = "Y"
Пример #30
0
# This script produces a figure depicting the first standard deviation of
# correlation coefficient variance over the 10 calibration windows for both
# surface air temperature and precipitation.
"""
Created on Mon Feb 10 16:31:38 2020

@author: huiskamp
"""

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
from netCDF4 import Dataset as CDF
import copy as cp

SAM_corrs = CDF('SAM_corrs.nc', 'r')
std = CDF('corr_std.nc', 'r')

sam_sat_corr = SAM_corrs.variables['SAM_SAT_land_model']
sam_pre_corr = SAM_corrs.variables['SAM_pre_land_model']
std_sat = std.variables['std_sat_wdw']
std_pre = std.variables['std_precip_wdw']

lat = std.variables['latitude']
lon = std.variables['longitude']

xgrid, ygrid = np.meshgrid(lon, lat)

minval = 0
maxval = 0.2
rng = 21