Ejemplo n.º 1
1
    def test(self):
        from netCDF4 import Dataset as NetCDF

        timeidx = 0
        in_wrfnc = NetCDF(wrf_in)

        if (varname == "interplevel"):
            ref_ht_850 = _get_refvals(referent, "interplevel", repeat, multi)
            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)
            hts_850 = interplevel(hts, p, 850)

            nt.assert_allclose(to_np(hts_850), ref_ht_850)

        elif (varname == "vertcross"):
            ref_ht_cross = _get_refvals(referent, "vertcross", repeat, multi)

            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)

            pivot_point = CoordPair(hts.shape[-1] // 2, hts.shape[-2] // 2)
            ht_cross = vertcross(hts, p, pivot_point=pivot_point, angle=90.)

            nt.assert_allclose(to_np(ht_cross), ref_ht_cross, rtol=.01)

        elif (varname == "interpline"):

            ref_t2_line = _get_refvals(referent, "interpline", repeat, multi)

            t2 = getvar(in_wrfnc, "T2", timeidx=timeidx)
            pivot_point = CoordPair(t2.shape[-1] // 2, t2.shape[-2] // 2)

            t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(t2_line1), ref_t2_line)

        elif (varname == "vinterp"):
            # Tk to theta
            fld_tk_theta = _get_refvals(referent, "vinterp", repeat, multi)
            fld_tk_theta = np.squeeze(fld_tk_theta)

            tk = getvar(in_wrfnc, "temp", timeidx=timeidx, units="k")

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 5 / 100.
            atol = 0.0001

            field = np.squeeze(field)

            nt.assert_allclose(to_np(field), fld_tk_theta, tol, atol)
Ejemplo n.º 2
0
def fix_ice_categories(storage_path,
                       log_path='../coarse_1975-1980.log',
                       cpu_count=4):
    ice_files = ice_cat_errors_files(log_path)

    for file in tqdm(ice_files):
        print(f'File to fix: {file}')
        date_raw = extracted_date_by_pattern(file)
        date = datetime.datetime.strptime(date_raw, '%Y%m%d')

        file_path = f'{storage_path}/{date.year}/{file}'

        tmp_path = f'../tmp_fixed/{date.year}'

        if not os.path.exists(tmp_path):
            os.mkdir(tmp_path)

        copyfile(f'{storage_path}/1976/{correct_ice_file}',
                 f'{tmp_path}/{file}')

        fixed_file = NetCDF(f'{tmp_path}/{file}', mode='r+')
        file_with_errors = NetCDF(file_path, mode='r')

        for var_name in file_with_errors.variables:
            if var_name not in ['siconcat', 'sithicat', 'ncatice']:
                fixed_file[var_name][:] = file_with_errors[var_name][:]
                print(f'Variable: {var_name} was copied')
        file_with_errors.close()

        conc_fixed, thic_fixed = ice_with_8_categories(file_path=file_path,
                                                       cpu_count=cpu_count)

        fixed_file['siconcat'][:] = conc_fixed
        fixed_file['sithicat'][:] = thic_fixed
        fixed_file.close()
Ejemplo n.º 3
0
    def test(self):
        from netCDF4 import Dataset as NetCDF

        timeidx = 0
        in_wrfnc = NetCDF(wrf_in)

        refnc = NetCDF(referent)

        if testid == "xy":
            # Since this domain is not moving, the reference values are the
            # same whether there are multiple or single files
            ref_vals = refnc.variables["xy"][:]
            # Lats/Lons taken from NCL script, just hard-coding for now
            lats = [22.0, 25.0, 27.0]
            lons = [-90.0, -87.5, -83.75]

            xy = ll_to_xy(in_wrfnc, lats[0], lons[0])

            nt.assert_allclose(to_np(xy), ref_vals)

        else:
            # Since this domain is not moving, the reference values are the
            # same whether there are multiple or single files
            ref_vals = refnc.variables["ll"][:]

            # i_s, j_s taken from NCL script, just hard-coding for now
            # NCL uses 1-based indexing for this, so need to subtract 1
            x_s = np.asarray([10, 50, 90], int)
            y_s = np.asarray([10, 50, 90], int)

            ll = xy_to_ll(in_wrfnc, x_s[0], y_s[0])

            nt.assert_allclose(to_np(ll), ref_vals)
Ejemplo n.º 4
0
def test_files_time_dif_correct(directory, first_file, second_file):
    first = NetCDF(f'{directory}/{first_file}')
    second = NetCDF(f'{directory}/{second_file}')

    time_var = 'time_counter'

    print(second[time_var][:][0] - first[time_var][:][0])
Ejemplo n.º 5
0
    def test(self):

        from netCDF4 import Dataset as NetCDF
        timeidx = 0
        in_wrfnc = NetCDF(wrf_in)
        refnc = NetCDF(referent)

        # These have a left index that defines the product type
        multiproduct = varname in ("uvmet", "uvmet10", "cape_2d", "cape_3d",
                                   "cfrac")

        ref_vals = refnc.variables[varname][:]

        if (varname == "tc"):
            my_vals = getvar(in_wrfnc, "temp", timeidx=timeidx, units="c")
            tol = 1 / 100.
            atol = .1  # Note:  NCL uses 273.16 as conversion for some reason
            nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
        elif (varname == "pw"):
            my_vals = getvar(in_wrfnc, "pw", timeidx=timeidx)
            tol = .5 / 100.0
            atol = 0  # NCL uses different constants and doesn't use same
            # handrolled virtual temp in method
            nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
        elif (varname == "cape_2d"):
            cape_2d = getvar(in_wrfnc, varname, timeidx=timeidx)
            tol = 0 / 100.
            atol = 200.0
            # Let's only compare CAPE values until the F90 changes are
            # merged back in to NCL.  The modifications to the R and CP
            # changes TK enough that non-lifting parcels could lift, thus
            # causing wildly different values in LCL
            nt.assert_allclose(to_np(cape_2d[0, :]), ref_vals[0, :], tol, atol)
        elif (varname == "cape_3d"):
            cape_3d = getvar(in_wrfnc, varname, timeidx=timeidx)
            # Changing the R and CP constants, while keeping TK within
            # 2%, can lead to some big changes in CAPE.  Tolerances
            # have been set wide when comparing the with the original
            # NCL.  Change back when the F90 code is merged back with
            # NCL
            tol = 0 / 100.
            atol = 200.0

            #print np.amax(np.abs(to_np(cape_3d[0,:]) - ref_vals[0,:]))
            nt.assert_allclose(to_np(cape_3d), ref_vals, tol, atol)
        else:
            my_vals = getvar(in_wrfnc, varname, timeidx=timeidx)
            tol = 2 / 100.
            atol = 0.1
            #print (np.amax(np.abs(to_np(my_vals) - ref_vals)))
            nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
Ejemplo n.º 6
0
def test_era_misses():
    nc_file = NetCDF('../reanalysis/era_2016-2017/era-prec-y2016.nc')
    station_coords = [171, 75]

    prec_at_point = nc_file.variables['var228'][:][75, 171, :]
    print(prec_at_point)
    nc_file.close()
Ejemplo n.º 7
0
    def check_variables(self, file_format):
        errors = []

        if self.path is "":
            errors.append("%s is absent, can't be opened" % self.name)
        else:
            nc_file = NetCDF(self.path)

            for correct_var in file_format.variables(self.type):
                try:
                    var = nc_file.variables[correct_var.name]
                    error = self.check_shape(var, correct_var, self.name)
                    if error is not "":
                        errors.append(error)

                    # TODO: change magic constants with more smart calculations
                    if self.check_for_nan(var, 200, 200):
                        error = f"{var.name} variable has NaN-value at (200,200) point in {self.name}"
                        errors.append(error)
                except KeyError:
                    error = "%s variable is not presented in %s" % (
                        correct_var.name, self.name)
                    errors.append(error)

        return errors
Ejemplo n.º 8
0
def _get_refvals(referent, varname, repeat, multi):
    try:
        from netCDF4 import Dataset as NetCDF
    except:
        pass

    refnc = NetCDF(referent)

    if not multi:
        ref_vals = refnc.variables[varname][:]
    else:
        data = refnc.variables[varname][:]
        new_dims = [repeat] + [x for x in data.shape]
        masked = False
        if (isinstance(data, ma.core.MaskedArray)):
            masked = True

        if not masked:
            ref_vals = np.zeros(new_dims, data.dtype)
        else:
            ref_vals = ma.asarray(np.zeros(new_dims, data.dtype))

        for i in xrange(repeat):
            ref_vals[i, :] = data[:]

            if masked:
                ref_vals.mask[i, :] = data.mask[:]

    return ref_vals
Ejemplo n.º 9
0
def _get_refvals(referent, varname, multi):
    try:
        from netCDF4 import Dataset as NetCDF
    except ImportError:
        pass

    refnc = NetCDF(referent)
    try:
        pass
    except ImportError:
        pass

    multi2d = ("uvmet10", "wspd_wdir10", "uvmet10_wspd_wdir", "cape_2d",
               "cfrac")
    multi3d = ("uvmet", "wspd_wdir", "uvmet_wspd_wdir", "cape_3d")
    interpline = ("t2_line", "t2_line2", "t2_line3")

    if not multi:
        if varname in multi2d:
            ref_vals = refnc.variables[varname][..., 0, :, :]
        elif varname in multi3d:
            ref_vals = refnc.variables[varname][..., 0, :, :, :]
        else:
            v = refnc.variables[varname][:]
            if v.ndim == 2:
                if varname in interpline:
                    ref_vals = v[0, :]
                else:
                    ref_vals = v
            else:
                ref_vals = v[0, :]
    else:
        ref_vals = refnc.variables[varname][:]

    return ref_vals
Ejemplo n.º 10
0
def _get_refvals(referent, varname, repeat, multi):
    from netCDF4 import Dataset as NetCDF

    refnc = NetCDF(referent)

    ref_vals = refnc.variables[varname][:]

    return ref_vals
Ejemplo n.º 11
0
def save_interp_precip(prev_file_path, fixed_file_path, rea_name, interp_precip, time):
    copyfile(prev_file_path, fixed_file_path)

    nc_file = NetCDF(fixed_file_path, mode='r+')
    var_name = FIELDS_BY_REA_NAME[rea_name]
    nc_file[var_name][:] = interp_precip
    nc_file['time'][:] = time
    nc_file.close()
Ejemplo n.º 12
0
def hourly_time(path_to_rea, year):
    nc_file = NetCDF(path_to_rea, mode='r')
    first_hour = int(nc_file['time'][:][0])
    last_hour = first_hour + days_in_year(year) * 24
    nc_file.close()

    time = [time_step for time_step in range(first_hour, last_hour)]

    return np.asarray(time)
Ejemplo n.º 13
0
    def check_for_integrity(self):
        error = ""
        try:
            nc_file = NetCDF(self.path)
            nc_file.close()
        except OSError:
            error = "%s can't be opened" % self.name

        return error
Ejemplo n.º 14
0
def extracted_precip(path_to_rea, rea_name):
    nc_file = NetCDF(path_to_rea, mode='r')

    var_name = FIELDS_BY_REA_NAME[rea_name]

    precip = nc_file[var_name][:]
    if rea_name is 'era':
        precip[precip < 0.0] = 0.0

    return precip
Ejemplo n.º 15
0
def fix_missed_variables(directory, file_to_fix, previous_file):
    copyfile(f'{directory}/{previous_file}',
             f'{directory}/fixed_{file_to_fix}')
    file_with_errors = NetCDF(f'{directory}/{file_to_fix}', mode='r')
    fixed_file = NetCDF(f'{directory}/fixed_{file_to_fix}', mode='r+')

    for var_name in file_with_errors.variables:
        if var_name not in fixed_file.variables:
            print(f'Variable {var_name} not in previous file')
        elif fixed_file[var_name].shape != fixed_file[var_name].shape:
            print(
                f'Variable {var_name} with incompatible shape:'
                f' expected - {fixed_file[var_name].shape}, actual - {file_with_errors[var_name].shape}'
            )
        else:
            fixed_file[var_name][:] = file_with_errors[var_name][:]

    file_with_errors.close()
    fixed_file.close()
Ejemplo n.º 16
0
def daily_average(path_to_interp_files):
    for year in range(2016, 2018):
        file_path = os.path.join(path_to_interp_files, f'precip_interp_y{year}.nc')
        nc_file = NetCDF(file_path)
        prec_at_point = nc_file.variables['precip'][:][75, 171, :]
        nc_file.close()
        avg_precip = np.average(prec_at_point)
        total_precip = np.sum(prec_at_point)

        del prec_at_point
        print(f'year: {year}; avg_precip: {avg_precip}; total: {total_precip}')
Ejemplo n.º 17
0
def fix_precip_attributes(path):
    nc_file = NetCDF(path, mode='r+')
    nc_file.history = ''
    nc_file.variables['precip'].long_name = 'Daily Precipitation Rate at surface'

    if 'Identification' in nc_file.ncattrs():
        nc_file.Identification = ''
    if 'Documentation' in nc_file.ncattrs():
        nc_file.Documentation = ''
    if 'About' in nc_file.ncattrs():
        nc_file.About = ''

    nc_file.close()
Ejemplo n.º 18
0
def fix_time_variables(directory,
                       file_to_fix,
                       previous_file,
                       time_dif=86400.0):
    copyfile(f'{directory}/{previous_file}', f'{directory}/{file_to_fix}')
    fixed_file = NetCDF(f'{directory}/{file_to_fix}', mode='r+')

    for time_var in [
            'time_counter', 'time_instant', 'time_counter_bounds',
            'time_counter_bounds'
    ]:
        if time_var not in fixed_file.variables:
            print(f'Variable {time_var} is not in {file_to_fix}')
        else:
            fixed_file[time_var][:] += time_dif

    fixed_file.close()
Ejemplo n.º 19
0
    def variable(self, name):
        nc_file = NetCDF(self.path)
        var = nc_file.variables[name][:]
        nc_file.close()

        return self.handle_sat_field(var)
Ejemplo n.º 20
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except ImportError:
            pass

        try:
            import Nio
        except ImportError:
            pass

        timeidx = 0 if not multi else None
        pat = os.path.join(dir, pattern)
        wrf_files = glob.glob(pat)
        wrf_files.sort()

        wrfin = []
        for fname in wrf_files:
            if not pynio:
                f = NetCDF(fname)
                try:
                    f.set_always_mask(False)
                except AttributeError:
                    pass
                wrfin.append(f)
            else:
                if not fname.endswith(".nc"):
                    _fname = fname + ".nc"
                else:
                    _fname = fname
                f = Nio.open_file(_fname)
                wrfin.append(f)

        refnc = NetCDF(referent)
        try:
            refnc.set_auto_mask(False)
        except AttributeError:
            pass

        # These have a left index that defines the product type
        multiproduct = varname in ("uvmet", "uvmet10", "wspd_wdir",
                                   "wspd_wdir10", "uvmet_wspd_wdir",
                                   "uvmet10_wspd_wdir", "cape_2d", "cape_3d",
                                   "cfrac")
        multi2d = ("uvmet10", "wspd_wdir10", "uvmet10_wspd_wdir", "cape_2d",
                   "cfrac")
        multi3d = ("uvmet", "wspd_wdir", "uvmet_wspd_wdir", "cape_3d")

        # These varnames don't have NCL functions to test against
        ignore_referent = ("zstag", "geopt_stag")

        if varname not in ignore_referent:
            if not multi:
                if varname in multi2d:
                    ref_vals = refnc.variables[varname][..., 0, :, :]
                elif varname in multi3d:
                    ref_vals = refnc.variables[varname][..., 0, :, :, :]
                else:
                    ref_vals = refnc.variables[varname][0, :]
            else:
                ref_vals = refnc.variables[varname][:]

        if (varname == "zstag" or varname == "geopt_stag"):
            v = getvar(wrfin, varname, timeidx=timeidx)
            # For now, only make sure it runs without crashing since no NCL
            # to compare with yet.0
        else:

            if varname == "cape_2d" or varname == "cape_3d":
                # Cape still has a small floating point issue that
                # hasn't been completely resolved, so for now check
                # that cape is within 50.
                my_vals = getvar(wrfin, varname, timeidx=timeidx)
                rtol = 0
                atol = 50.
            else:
                # All other tests should be within .001 of each other
                my_vals = getvar(wrfin, varname, timeidx=timeidx)
                rtol = 0
                atol = 1.0E-3

            try:
                nt.assert_allclose(to_np(my_vals), ref_vals, rtol, atol)
            except AssertionError:
                absdiff = np.abs(to_np(my_vals) - ref_vals)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))

                raise
Ejemplo n.º 21
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except ImportError:
            pass

        try:
            import Nio
        except ImportError:
            pass

        timeidx = 0 if not multi else None
        pat = os.path.join(dir, pattern)
        wrf_files = glob.glob(pat)
        wrf_files.sort()

        refnc = NetCDF(referent)
        try:
            refnc.set_always_mask(False)
        except AttributeError:
            pass

        wrfin = []
        for fname in wrf_files:
            if not pynio:
                f = NetCDF(fname)
                try:
                    f.set_auto_mask(False)
                except AttributeError:
                    pass
                wrfin.append(f)
            else:
                if not fname.endswith(".nc"):
                    _fname = fname + ".nc"
                else:
                    _fname = fname
                f = Nio.open_file(_fname)
                wrfin.append(f)

        if testid == "xy":

            # Lats/Lons taken from NCL script, just hard-coding for now
            lats = [22.0, 25.0, 27.0]
            lons = [-90.0, -87.5, -83.75]

            # Just call with a single lat/lon
            if single:
                timeidx = 8
                ref_vals = refnc.variables["xy2"][:]

                xy = ll_to_xy(wrfin,
                              lats[0],
                              lons[0],
                              timeidx=timeidx,
                              as_int=True)
                ref = ref_vals[:, 0]

                nt.assert_allclose(to_np(xy), ref)

                # Next make sure the 'proj' version works
                projparams = extract_proj_params(wrfin, timeidx=timeidx)
                xy_proj = ll_to_xy_proj(lats[0],
                                        lons[0],
                                        as_int=True,
                                        **projparams)

                nt.assert_allclose(to_np(xy_proj), to_np(xy))

            else:
                ref_vals = refnc.variables["xy1"][:]
                xy = ll_to_xy(wrfin, lats, lons, timeidx=None, as_int=False)

                ref = ref_vals[:]

                nt.assert_allclose(to_np(xy), ref)

                if xy.ndim > 2:
                    # Moving nest
                    is_moving = True
                    numtimes = xy.shape[-2]
                else:
                    is_moving = False
                    numtimes = 1

                for tidx in range(9):

                    # Next make sure the 'proj' version works
                    projparams = extract_proj_params(wrfin, timeidx=tidx)
                    xy_proj = ll_to_xy_proj(lats,
                                            lons,
                                            as_int=False,
                                            **projparams)

                    if is_moving:
                        idxs = (slice(None), tidx, slice(None))
                    else:
                        idxs = (slice(None), )

                    nt.assert_allclose(to_np(xy_proj), to_np(xy[idxs]))

        else:
            # i_s, j_s taken from NCL script, just hard-coding for now
            # NCL uses 1-based indexing for this, so need to subtract 1
            x_s = np.asarray([10, 50, 90], int)
            y_s = np.asarray([10, 50, 90], int)

            if single:
                timeidx = 8
                ref_vals = refnc.variables["ll2"][:]
                ll = xy_to_ll(wrfin, x_s[0], y_s[0], timeidx=timeidx)
                ref = ref_vals[::-1, 0]

                nt.assert_allclose(to_np(ll), ref)

                # Next make sure the 'proj' version works
                projparams = extract_proj_params(wrfin, timeidx=8)
                ll_proj = xy_to_ll_proj(x_s[0], y_s[0], **projparams)

                nt.assert_allclose(to_np(ll_proj), to_np(ll))

            else:
                ref_vals = refnc.variables["ll1"][:]
                ll = xy_to_ll(wrfin, x_s, y_s, timeidx=None)
                ref = ref_vals[::-1, :]

                nt.assert_allclose(to_np(ll), ref)

                if ll.ndim > 2:
                    # Moving nest
                    is_moving = True
                    numtimes = ll.shape[-2]
                else:
                    is_moving = False
                    numtimes = 1

                for tidx in range(numtimes):
                    # Next make sure the 'proj' version works
                    projparams = extract_proj_params(wrfin, timeidx=tidx)
                    ll_proj = xy_to_ll_proj(x_s, y_s, **projparams)

                    if is_moving:
                        idxs = (slice(None), tidx, slice(None))
                    else:
                        idxs = (slice(None), )

                    nt.assert_allclose(to_np(ll_proj), to_np(ll[idxs]))
Ejemplo n.º 22
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except ImportError:
            pass

        try:
            import Nio
        except ImportError:
            pass

        timeidx = 0 if not multi else None
        pat = os.path.join(dir, pattern)
        wrf_files = glob.glob(pat)
        wrf_files.sort()

        wrfin = []
        for fname in wrf_files:
            if not pynio:
                f = NetCDF(fname)
                try:
                    f.set_always_mask(False)
                except AttributeError:
                    pass
                wrfin.append(f)
            else:
                if not fname.endswith(".nc"):
                    _fname = fname + ".nc"
                else:
                    _fname = fname
                f = Nio.open_file(_fname)
                wrfin.append(f)

        if (varname == "interplevel"):
            ref_ht_500 = _get_refvals(referent, "z_500", multi)
            ref_p_5000 = _get_refvals(referent, "p_5000", multi)
            ref_ht_multi = _get_refvals(referent, "z_multi", multi)
            ref_p_multi = _get_refvals(referent, "p_multi", multi)

            ref_ht2_500 = _get_refvals(referent, "z2_500", multi)
            ref_p2_5000 = _get_refvals(referent, "p2_5000", multi)
            ref_ht2_multi = _get_refvals(referent, "z2_multi", multi)
            ref_p2_multi = _get_refvals(referent, "p2_multi", multi)

            ref_p_lev2d = _get_refvals(referent, "p_lev2d", multi)

            hts = getvar(wrfin, "z", timeidx=timeidx)
            p = getvar(wrfin, "pressure", timeidx=timeidx)
            wspd_wdir = getvar(wrfin, "wspd_wdir", timeidx=timeidx)

            # Make sure the numpy versions work first
            hts_500 = interplevel(to_np(hts), to_np(p), 500)
            hts_500 = interplevel(hts, p, 500)

            # Note: the '*2*' versions in the reference are testing
            # against the new version of interplevel in NCL
            nt.assert_allclose(to_np(hts_500), ref_ht_500)
            nt.assert_allclose(to_np(hts_500), ref_ht2_500)

            # Make sure the numpy versions work first
            p_5000 = interplevel(to_np(p), to_np(hts), 5000)
            p_5000 = interplevel(p, hts, 5000)

            nt.assert_allclose(to_np(p_5000), ref_p_5000)
            nt.assert_allclose(to_np(p_5000), ref_p2_5000)

            hts_multi = interplevel(to_np(hts), to_np(p),
                                    [1000., 850., 500., 250.])
            hts_multi = interplevel(hts, p, [1000., 850., 500., 250.])

            nt.assert_allclose(to_np(hts_multi), ref_ht_multi)
            nt.assert_allclose(to_np(hts_multi), ref_ht2_multi)

            p_multi = interplevel(to_np(p), to_np(hts),
                                  [500., 2500., 5000., 10000.])
            p_multi = interplevel(p, hts, [500., 2500., 5000., 10000.])

            nt.assert_allclose(to_np(p_multi), ref_p_multi)
            nt.assert_allclose(to_np(p_multi), ref_p2_multi)

            pblh = getvar(wrfin, "PBLH", timeidx=timeidx)
            p_lev2d = interplevel(to_np(p), to_np(hts), to_np(pblh))
            p_lev2d = interplevel(p, hts, pblh)
            nt.assert_allclose(to_np(p_lev2d), ref_p_lev2d)

            # Just make sure these run below
            wspd_wdir_500 = interplevel(to_np(wspd_wdir), to_np(p), 500)
            wspd_wdir_500 = interplevel(wspd_wdir, p, 500)

            wspd_wdir_multi = interplevel(to_np(wspd_wdir), to_np(p),
                                          [1000, 500, 250])
            wdpd_wdir_multi = interplevel(wspd_wdir, p, [1000, 500, 250])

            wspd_wdir_pblh = interplevel(to_np(wspd_wdir), to_np(hts), pblh)
            wspd_wdir_pblh = interplevel(wspd_wdir, hts, pblh)

            if multi:
                wspd_wdir_pblh_2 = interplevel(to_np(wspd_wdir), to_np(hts),
                                               pblh[0, :])
                wspd_wdir_pblh_2 = interplevel(wspd_wdir, hts, pblh[0, :])

                # Since PBLH doesn't change in this case, it should match
                # the 0 time from previous computation. Note that this
                # only works when the data has 1 time step that is repeated.
                # If you use a different case with multiple times,
                # this will probably fail.
                nt.assert_allclose(to_np(wspd_wdir_pblh_2[:, 0, :]),
                                   to_np(wspd_wdir_pblh[:, 0, :]))

                nt.assert_allclose(to_np(wspd_wdir_pblh_2[:, -1, :]),
                                   to_np(wspd_wdir_pblh[:, 0, :]))

        elif (varname == "vertcross"):
            ref_ht_cross = _get_refvals(referent, "ht_cross", multi)
            ref_p_cross = _get_refvals(referent, "p_cross", multi)
            ref_ht_vertcross1 = _get_refvals(referent, "ht_vertcross1", multi)
            ref_ht_vertcross2 = _get_refvals(referent, "ht_vertcross2", multi)
            ref_ht_vertcross3 = _get_refvals(referent, "ht_vertcross3", multi)

            hts = getvar(wrfin, "z", timeidx=timeidx)
            p = getvar(wrfin, "pressure", timeidx=timeidx)

            pivot_point = CoordPair(hts.shape[-1] / 2, hts.shape[-2] / 2)

            # Beginning in wrf-python 1.3, users can select number of levels.
            # Originally, for pressure, dz was 10, so let's back calculate
            # the number of levels.
            p_max = np.floor(np.amax(p) / 10) * 10  # bottom value
            p_min = np.ceil(np.amin(p) / 10) * 10  # top value

            p_autolevels = int((p_max - p_min) / 10)

            # Make sure the numpy versions work first

            ht_cross = vertcross(to_np(hts),
                                 to_np(p),
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 autolevels=p_autolevels)

            ht_cross = vertcross(hts,
                                 p,
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 autolevels=p_autolevels)

            try:
                nt.assert_allclose(to_np(ht_cross), ref_ht_cross, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - ref_ht_cross)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            lats = hts.coords["XLAT"]
            lons = hts.coords["XLONG"]

            # Test the manual projection method with lat/lon
            # Only do this for the non-multi case, since the domain
            # might be moving
            if not multi:
                if lats.ndim > 2:  # moving nest
                    lats = lats[0, :]
                    lons = lons[0, :]

                ll_point = ll_points(lats, lons)

                pivot = CoordPair(lat=lats[int(lats.shape[-2] / 2),
                                           int(lats.shape[-1] / 2)],
                                  lon=lons[int(lons.shape[-2] / 2),
                                           int(lons.shape[-1] / 2)])

                v1 = vertcross(hts,
                               p,
                               wrfin=wrfin,
                               pivot_point=pivot_point,
                               angle=90.0)
                v2 = vertcross(hts,
                               p,
                               projection=hts.attrs["projection"],
                               ll_point=ll_point,
                               pivot_point=pivot_point,
                               angle=90.)

                try:
                    nt.assert_allclose(to_np(v1), to_np(v2), atol=.0001)
                except AssertionError:
                    absdiff = np.abs(to_np(v1) - to_np(v2))
                    maxdiff = np.amax(absdiff)
                    print(maxdiff)
                    print(np.argwhere(absdiff == maxdiff))
                    raise

            # Test opposite

            p_cross1 = vertcross(p, hts, pivot_point=pivot_point, angle=90.0)

            try:
                nt.assert_allclose(to_np(p_cross1), ref_p_cross, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(p_cross1) - ref_p_cross)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test point to point
            start_point = CoordPair(0, hts.shape[-2] / 2)
            end_point = CoordPair(-1, hts.shape[-2] / 2)

            p_cross2 = vertcross(p,
                                 hts,
                                 start_point=start_point,
                                 end_point=end_point)

            try:
                nt.assert_allclose(to_np(p_cross1),
                                   to_np(p_cross2),
                                   atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(p_cross1) - to_np(p_cross2))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Check the new vertcross routine
            pivot_point = CoordPair(hts.shape[-1] / 2, hts.shape[-2] / 2)
            ht_cross = vertcross(hts,
                                 p,
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 latlon=True)

            try:
                nt.assert_allclose(to_np(ht_cross),
                                   to_np(ref_ht_vertcross1),
                                   atol=.005)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - to_np(ref_ht_vertcross1))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            levels = [1000., 850., 700., 500., 250.]
            ht_cross = vertcross(hts,
                                 p,
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 levels=levels,
                                 latlon=True)

            try:
                nt.assert_allclose(to_np(ht_cross),
                                   to_np(ref_ht_vertcross2),
                                   atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - to_np(ref_ht_vertcross2))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            idxs = (0, slice(None)) if lats.ndim > 2 else (slice(None), )

            start_lat = np.amin(
                lats[idxs]) + .25 * (np.amax(lats[idxs]) - np.amin(lats[idxs]))
            end_lat = np.amin(
                lats[idxs]) + .65 * (np.amax(lats[idxs]) - np.amin(lats[idxs]))

            start_lon = np.amin(
                lons[idxs]) + .25 * (np.amax(lons[idxs]) - np.amin(lons[idxs]))
            end_lon = np.amin(
                lons[idxs]) + .65 * (np.amax(lons[idxs]) - np.amin(lons[idxs]))

            start_point = CoordPair(lat=start_lat, lon=start_lon)
            end_point = CoordPair(lat=end_lat, lon=end_lon)

            ll_point = ll_points(lats[idxs], lons[idxs])

            ht_cross = vertcross(hts,
                                 p,
                                 start_point=start_point,
                                 end_point=end_point,
                                 projection=hts.attrs["projection"],
                                 ll_point=ll_point,
                                 latlon=True,
                                 autolevels=1000)

            try:
                nt.assert_allclose(to_np(ht_cross),
                                   to_np(ref_ht_vertcross3),
                                   atol=.01)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - to_np(ref_ht_vertcross3))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            if multi:
                ntimes = hts.shape[0]

                for t in range(ntimes):
                    hts = getvar(wrfin, "z", timeidx=t)
                    p = getvar(wrfin, "pressure", timeidx=t)

                    ht_cross = vertcross(hts,
                                         p,
                                         start_point=start_point,
                                         end_point=end_point,
                                         wrfin=wrfin,
                                         timeidx=t,
                                         latlon=True,
                                         autolevels=1000)

                    refname = "ht_vertcross_t{}".format(t)
                    ref_ht_vertcross = _get_refvals(referent, refname, False)

                    try:
                        nt.assert_allclose(to_np(ht_cross),
                                           to_np(ref_ht_vertcross),
                                           atol=.01)
                    except AssertionError:
                        absdiff = np.abs(
                            to_np(ht_cross) - to_np(ref_ht_vertcross))
                        maxdiff = np.amax(absdiff)
                        print(maxdiff)
                        print(np.argwhere(absdiff == maxdiff))
                        raise

        elif (varname == "interpline"):

            ref_t2_line = _get_refvals(referent, "t2_line", multi)
            ref_t2_line2 = _get_refvals(referent, "t2_line2", multi)
            ref_t2_line3 = _get_refvals(referent, "t2_line3", multi)

            t2 = getvar(wrfin, "T2", timeidx=timeidx)
            pivot_point = CoordPair(t2.shape[-1] / 2, t2.shape[-2] / 2)

            # Make sure the numpy version works
            t2_line1 = interpline(to_np(t2),
                                  pivot_point=pivot_point,
                                  angle=90.0)
            t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(t2_line1), ref_t2_line)

            # Test the new NCL wrf_user_interplevel result
            try:
                nt.assert_allclose(to_np(t2_line1), ref_t2_line2)
            except AssertionError:
                absdiff = np.abs(to_np(t2_line1) - ref_t2_line2)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test the manual projection method with lat/lon
            lats = t2.coords["XLAT"]
            lons = t2.coords["XLONG"]
            if multi:
                if lats.ndim > 2:  # moving nest
                    lats = lats[0, :]
                    lons = lons[0, :]

            ll_point = ll_points(lats, lons)

            pivot = CoordPair(lat=lats[int(lats.shape[-2] / 2),
                                       int(lats.shape[-1] / 2)],
                              lon=lons[int(lons.shape[-2] / 2),
                                       int(lons.shape[-1] / 2)])

            l1 = interpline(t2,
                            wrfin=wrfin,
                            pivot_point=pivot_point,
                            angle=90.0)

            l2 = interpline(t2,
                            projection=t2.attrs["projection"],
                            ll_point=ll_point,
                            pivot_point=pivot_point,
                            angle=90.)
            try:
                nt.assert_allclose(to_np(l1), to_np(l2))
            except AssertionError:
                absdiff = np.abs(to_np(l1) - to_np(l2))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test point to point
            start_point = CoordPair(0, t2.shape[-2] / 2)
            end_point = CoordPair(-1, t2.shape[-2] / 2)

            t2_line2 = interpline(t2,
                                  start_point=start_point,
                                  end_point=end_point)

            try:
                nt.assert_allclose(to_np(t2_line1), to_np(t2_line2))
            except AssertionError:
                absdiff = np.abs(to_np(t2_line1) - t2_line2)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Now test the start/end with lat/lon points

            start_lat = float(
                np.amin(lats) + .25 * (np.amax(lats) - np.amin(lats)))
            end_lat = float(
                np.amin(lats) + .65 * (np.amax(lats) - np.amin(lats)))

            start_lon = float(
                np.amin(lons) + .25 * (np.amax(lons) - np.amin(lons)))
            end_lon = float(
                np.amin(lons) + .65 * (np.amax(lons) - np.amin(lons)))

            start_point = CoordPair(lat=start_lat, lon=start_lon)
            end_point = CoordPair(lat=end_lat, lon=end_lon)

            t2_line3 = interpline(t2,
                                  wrfin=wrfin,
                                  timeidx=0,
                                  start_point=start_point,
                                  end_point=end_point,
                                  latlon=True)

            try:
                nt.assert_allclose(to_np(t2_line3), ref_t2_line3, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(t2_line3) - ref_t2_line3)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test all time steps
            if multi:
                refnc = NetCDF(referent)
                ntimes = t2.shape[0]

                for t in range(ntimes):
                    t2 = getvar(wrfin, "T2", timeidx=t)

                    line = interpline(t2,
                                      wrfin=wrfin,
                                      timeidx=t,
                                      start_point=start_point,
                                      end_point=end_point,
                                      latlon=True)

                    refname = "t2_line_t{}".format(t)
                    refline = refnc.variables[refname][:]

                    try:
                        nt.assert_allclose(to_np(line),
                                           to_np(refline),
                                           atol=.0001)
                    except AssertionError:
                        absdiff = np.abs(to_np(line) - refline)
                        maxdiff = np.amax(absdiff)
                        print(maxdiff)
                        print(np.argwhere(absdiff == maxdiff))
                        raise

                refnc.close()

            # Test NCLs single time case
            if not multi:
                refnc = NetCDF(referent)
                ref_t2_line4 = refnc.variables["t2_line4"][:]

                t2 = getvar(wrfin, "T2", timeidx=0)
                line = interpline(t2,
                                  wrfin=wrfin,
                                  timeidx=0,
                                  start_point=start_point,
                                  end_point=end_point,
                                  latlon=True)

                try:
                    nt.assert_allclose(to_np(line),
                                       to_np(ref_t2_line4),
                                       atol=.0001)
                except AssertionError:
                    absdiff = np.abs(to_np(line) - ref_t2_line4)
                    maxdiff = np.amax(absdiff)
                    print(maxdiff)
                    print(np.argwhere(absdiff == maxdiff))
                    raise
                finally:
                    refnc.close()

        elif (varname == "vinterp"):
            # Tk to theta
            fld_tk_theta = _get_refvals(referent, "fld_tk_theta", multi)
            fld_tk_theta = np.squeeze(fld_tk_theta)

            tk = getvar(wrfin, "temp", timeidx=timeidx, units="k")

            interp_levels = [200, 300, 500, 1000]

            # Make sure the numpy version works
            field = vinterp(wrfin,
                            field=to_np(tk),
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 0 / 100.
            atol = 0.0001

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_tk_theta)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_theta)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to theta-e
            fld_tk_theta_e = _get_refvals(referent, "fld_tk_theta_e", multi)
            fld_tk_theta_e = np.squeeze(fld_tk_theta_e)

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="theta-e",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            try:
                nt.assert_allclose(to_np(field), fld_tk_theta_e, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_theta_e)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to pressure
            fld_tk_pres = _get_refvals(referent, "fld_tk_pres", multi)
            fld_tk_pres = np.squeeze(fld_tk_pres)

            interp_levels = [850, 500]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_tk_pres, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_pres)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to geoht_msl
            fld_tk_ght_msl = _get_refvals(referent, "fld_tk_ght_msl", multi)
            fld_tk_ght_msl = np.squeeze(fld_tk_ght_msl)
            interp_levels = [1, 2]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="ght_msl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            try:
                nt.assert_allclose(to_np(field), fld_tk_ght_msl, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_ght_msl)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to geoht_agl
            fld_tk_ght_agl = _get_refvals(referent, "fld_tk_ght_agl", multi)
            fld_tk_ght_agl = np.squeeze(fld_tk_ght_agl)
            interp_levels = [1, 2]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="ght_agl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_tk_ght_agl, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_ght_agl)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Hgt to pressure
            fld_ht_pres = _get_refvals(referent, "fld_ht_pres", multi)
            fld_ht_pres = np.squeeze(fld_ht_pres)

            z = getvar(wrfin, "height", timeidx=timeidx, units="m")
            interp_levels = [500, 50]
            field = vinterp(wrfin,
                            field=z,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="ght",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_ht_pres, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_ht_pres)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Pressure to theta
            fld_pres_theta = _get_refvals(referent, "fld_pres_theta", multi)
            fld_pres_theta = np.squeeze(fld_pres_theta)

            p = getvar(wrfin, "pressure", timeidx=timeidx)
            interp_levels = [200, 300, 500, 1000]
            field = vinterp(wrfin,
                            field=p,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="pressure",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_pres_theta, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_pres_theta)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Theta-e to pres
            fld_thetae_pres = _get_refvals(referent, "fld_thetae_pres", multi)
            fld_thetae_pres = np.squeeze(fld_thetae_pres)

            eth = getvar(wrfin, "eth", timeidx=timeidx)
            interp_levels = [850, 500, 5]
            field = vinterp(wrfin,
                            field=eth,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="theta-e",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_thetae_pres, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_thetae_pres)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise
Ejemplo n.º 23
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except:
            pass

        try:
            from PyNIO import Nio
        except:
            pass

        if not multi:
            timeidx = 0
            if not pynio:
                in_wrfnc = NetCDF(wrf_in)
            else:
                # Note: Python doesn't like it if you reassign an outer scoped
                # variable (wrf_in in this case)
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                in_wrfnc = Nio.open_file(wrf_file)
        else:
            timeidx = None
            if not pynio:
                nc = NetCDF(wrf_in)
                in_wrfnc = [nc for i in xrange(repeat)]
            else:
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                nc = Nio.open_file(wrf_file)
                in_wrfnc = [nc for i in xrange(repeat)]

        if (varname == "interplevel"):
            ref_ht_500 = _get_refvals(referent, "z_500", repeat, multi)
            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)
            hts_500 = interplevel(hts, p, 500)

            nt.assert_allclose(to_np(hts_500), ref_ht_500)

        elif (varname == "vertcross"):
            ref_ht_cross = _get_refvals(referent, "ht_cross", repeat, multi)
            ref_p_cross = _get_refvals(referent, "p_cross", repeat, multi)

            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)

            pivot_point = CoordPair(hts.shape[-1] / 2, hts.shape[-2] / 2)
            ht_cross = vertcross(hts, p, pivot_point=pivot_point, angle=90.)

            nt.assert_allclose(to_np(ht_cross), ref_ht_cross, rtol=.01)

            # Test opposite
            p_cross1 = vertcross(p, hts, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(p_cross1), ref_p_cross, rtol=.01)
            # Test point to point
            start_point = CoordPair(0, hts.shape[-2] / 2)
            end_point = CoordPair(-1, hts.shape[-2] / 2)

            p_cross2 = vertcross(p,
                                 hts,
                                 start_point=start_point,
                                 end_point=end_point)

            nt.assert_allclose(to_np(p_cross1), to_np(p_cross2))

        elif (varname == "interpline"):

            ref_t2_line = _get_refvals(referent, "t2_line", repeat, multi)

            t2 = getvar(in_wrfnc, "T2", timeidx=timeidx)
            pivot_point = CoordPair(t2.shape[-1] / 2, t2.shape[-2] / 2)

            t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(t2_line1), ref_t2_line)

            # Test point to point
            start_point = CoordPair(0, t2.shape[-2] / 2)
            end_point = CoordPair(-1, t2.shape[-2] / 2)

            t2_line2 = interpline(t2,
                                  start_point=start_point,
                                  end_point=end_point)

            nt.assert_allclose(to_np(t2_line1), to_np(t2_line2))
        elif (varname == "vinterp"):
            # Tk to theta
            fld_tk_theta = _get_refvals(referent, "fld_tk_theta", repeat,
                                        multi)
            fld_tk_theta = np.squeeze(fld_tk_theta)

            tk = getvar(in_wrfnc, "temp", timeidx=timeidx, units="k")

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 5 / 100.
            atol = 0.0001

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_theta)))
            nt.assert_allclose(to_np(field), fld_tk_theta, tol, atol)

            # Tk to theta-e
            fld_tk_theta_e = _get_refvals(referent, "fld_tk_theta_e", repeat,
                                          multi)
            fld_tk_theta_e = np.squeeze(fld_tk_theta_e)

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta-e",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 3 / 100.
            atol = 50.0001

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_theta_e)/fld_tk_theta_e)*100)
            nt.assert_allclose(to_np(field), fld_tk_theta_e, tol, atol)

            # Tk to pressure
            fld_tk_pres = _get_refvals(referent, "fld_tk_pres", repeat, multi)
            fld_tk_pres = np.squeeze(fld_tk_pres)

            interp_levels = [850, 500]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            #print (np.amax(np.abs(to_np(field) - fld_tk_pres)))
            nt.assert_allclose(to_np(field), fld_tk_pres, tol, atol)

            # Tk to geoht_msl
            fld_tk_ght_msl = _get_refvals(referent, "fld_tk_ght_msl", repeat,
                                          multi)
            fld_tk_ght_msl = np.squeeze(fld_tk_ght_msl)
            interp_levels = [1, 2]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="ght_msl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_ght_msl)))
            nt.assert_allclose(to_np(field), fld_tk_ght_msl, tol, atol)

            # Tk to geoht_agl
            fld_tk_ght_agl = _get_refvals(referent, "fld_tk_ght_agl", repeat,
                                          multi)
            fld_tk_ght_agl = np.squeeze(fld_tk_ght_agl)
            interp_levels = [1, 2]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="ght_agl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_ght_agl)))
            nt.assert_allclose(to_np(field), fld_tk_ght_agl, tol, atol)

            # Hgt to pressure
            fld_ht_pres = _get_refvals(referent, "fld_ht_pres", repeat, multi)
            fld_ht_pres = np.squeeze(fld_ht_pres)

            z = getvar(in_wrfnc, "height", timeidx=timeidx, units="m")
            interp_levels = [500, 50]
            field = vinterp(in_wrfnc,
                            field=z,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="ght",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_ht_pres)))
            nt.assert_allclose(to_np(field), fld_ht_pres, tol, atol)

            # Pressure to theta
            fld_pres_theta = _get_refvals(referent, "fld_pres_theta", repeat,
                                          multi)
            fld_pres_theta = np.squeeze(fld_pres_theta)

            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)
            interp_levels = [200, 300, 500, 1000]
            field = vinterp(in_wrfnc,
                            field=p,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="pressure",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_pres_theta)))
            nt.assert_allclose(to_np(field), fld_pres_theta, tol, atol)

            # Theta-e to pres
            fld_thetae_pres = _get_refvals(referent, "fld_thetae_pres", repeat,
                                           multi)
            fld_thetae_pres = np.squeeze(fld_thetae_pres)

            eth = getvar(in_wrfnc, "eth", timeidx=timeidx)
            interp_levels = [850, 500, 5]
            field = vinterp(in_wrfnc,
                            field=eth,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="theta-e",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_thetae_pres)))
            nt.assert_allclose(to_np(field), fld_thetae_pres, tol, atol)
Ejemplo n.º 24
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except:
            pass

        try:
            from PyNIO import Nio
        except:
            pass

        if not multi:
            timeidx = 0
            if not pynio:
                in_wrfnc = NetCDF(wrf_in)
            else:
                # Note: Python doesn't like it if you reassign an outer scoped
                # variable (wrf_in in this case)
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                in_wrfnc = Nio.open_file(wrf_file)
        else:
            timeidx = None
            if not pynio:
                nc = NetCDF(wrf_in)
                in_wrfnc = [nc for i in xrange(repeat)]
            else:
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                nc = Nio.open_file(wrf_file)
                in_wrfnc = [nc for i in xrange(repeat)]

        refnc = NetCDF(referent)

        if testid == "xy":
            # Since this domain is not moving, the reference values are the
            # same whether there are multiple or single files
            ref_vals = refnc.variables["ij"][:]
            # Lats/Lons taken from NCL script, just hard-coding for now
            lats = [-55, -60, -65]
            lons = [25, 30, 35]

            # Just call with a single lat/lon
            if single:
                xy = ll_to_xy(in_wrfnc, lats[0], lons[0])
                xy = xy + 1  # NCL uses fortran indexing
                ref = ref_vals[:, 0]

                nt.assert_allclose(to_np(xy), ref)

                # Next make sure the 'proj' version works
                projparams = extract_proj_params(in_wrfnc)
                xy_proj = ll_to_xy_proj(lats[0], lons[0], **projparams)

                nt.assert_allclose(to_np(xy_proj), to_np(xy - 1))

            else:
                xy = ll_to_xy(in_wrfnc, lats, lons)
                xy = xy + 1  # NCL uses fortran indexing
                ref = ref_vals[:]

                nt.assert_allclose(to_np(xy), ref)

                # Next make sure the 'proj' version works
                projparams = extract_proj_params(in_wrfnc)
                xy_proj = ll_to_xy_proj(lats, lons, **projparams)

                nt.assert_allclose(to_np(xy_proj), to_np(xy - 1))

        else:
            # Since this domain is not moving, the reference values are the
            # same whether there are multiple or single files
            ref_vals = refnc.variables["ll"][:]

            # i_s, j_s taken from NCL script, just hard-coding for now
            # NCL uses 1-based indexing for this, so need to subtract 1
            i_s = np.asarray([10, 100, 150], int) - 1
            j_s = np.asarray([10, 100, 150], int) - 1

            if single:
                ll = xy_to_ll(in_wrfnc, i_s[0], j_s[0])
                ref = ref_vals[::-1, 0]

                nt.assert_allclose(to_np(ll), ref)

                # Next make sure the 'proj' version works
                projparams = extract_proj_params(in_wrfnc)
                ll_proj = xy_to_ll_proj(i_s[0], j_s[0], **projparams)

                nt.assert_allclose(to_np(ll_proj), to_np(ll))
            else:
                ll = xy_to_ll(in_wrfnc, i_s, j_s)
                ref = ref_vals[::-1, :]

                nt.assert_allclose(to_np(ll), ref)

                # Next make sure the 'proj' version works
                projparams = extract_proj_params(in_wrfnc)
                ll_proj = xy_to_ll_proj(i_s, j_s, **projparams)

                nt.assert_allclose(to_np(ll_proj), to_np(ll))
Ejemplo n.º 25
0
def make_test(wrf_file=None, fixed_case=None):
    if wrf_file is not None:
        ncfile = NetCDF(wrf_file)
        lats, lons, proj_params = get_proj_params(ncfile)
        proj = getproj(lats=lats, lons=lons, **proj_params)
        name_suffix = basename(wrf_file)
    elif fixed_case is not None:
        name_suffix = fixed_case
        if fixed_case == "south_rot":
            lats, lons, proj = nz_proj()
        elif fixed_case == "arg_rot":
            lats, lons, proj = argentina_proj()
        elif fixed_case == "south_polar":
            lats, lons, proj = south_polar_proj()
        elif fixed_case == "north_polar":
            lats, lons, proj = north_polar_proj()
        elif fixed_case == "dateline_rot":
            lats, lons, proj = dateline_rot_proj()

    print("wrf proj4: {}".format(proj.proj4()))
    if PYNGL:
        # PyNGL plotting
        wks_type = bytes("png")
        wks = Ngl.open_wks(wks_type, bytes("pyngl_{}".format(name_suffix)))
        mpres = proj.pyngl()
        map = Ngl.map(wks, mpres)

        Ngl.delete_wks(wks)

    if BASEMAP:
        # Basemap plotting
        fig = plt.figure(figsize=(10, 10))
        ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

        # Define and plot the meridians and parallels
        min_lat = np.amin(lats)
        max_lat = np.amax(lats)
        min_lon = np.amin(lons)
        max_lon = np.amax(lons)

        parallels = np.arange(np.floor(min_lat), np.ceil(max_lat),
                              (max_lat - min_lat) / 5.0)
        meridians = np.arange(np.floor(min_lon), np.ceil(max_lon),
                              (max_lon - min_lon) / 5.0)

        bm = proj.basemap()
        bm.drawcoastlines(linewidth=.5)
        #bm.drawparallels(parallels,labels=[1,1,1,1],fontsize=10)
        #bm.drawmeridians(meridians,labels=[1,1,1,1],fontsize=10)
        print("basemap proj4: {}".format(bm.proj4string))
        plt.savefig("basemap_{}.png".format(name_suffix))
        plt.close(fig)

    if CARTOPY:
        # Cartopy plotting
        fig = plt.figure(figsize=(10, 10))
        ax = plt.axes([0.1, 0.1, 0.8, 0.8], projection=proj.cartopy())
        print("cartopy proj4: {}".format(proj.cartopy().proj4_params))

        ax.coastlines('50m', linewidth=0.8)
        #print proj.x_extents()
        #print proj.y_extents()
        ax.set_xlim(proj.cartopy_xlim())
        ax.set_ylim(proj.cartopy_ylim())
        ax.gridlines()
        plt.savefig("cartopy_{}.png".format(name_suffix))
        plt.close(fig)
Ejemplo n.º 26
0
 def __loaded_mask(self):
     # TODO: clear this somehow
     nc_file = NetCDF('../bathy_meter_mask.nc')
     mask = nc_file.variables['Bathymetry'][:]
     nc_file.close()
     return mask
Ejemplo n.º 27
0
    def test(self):

        try:
            from netCDF4 import Dataset as NetCDF
        except:
            pass

        try:
            from PyNIO import Nio
        except:
            pass

        if not multi:
            timeidx = 0
            if not pynio:
                in_wrfnc = NetCDF(wrf_in)
            else:
                # Note: Python doesn't like it if you reassign an outer scoped
                # variable (wrf_in in this case)
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                in_wrfnc = Nio.open_file(wrf_file)
        else:
            timeidx = None
            if not pynio:
                nc = NetCDF(wrf_in)
                in_wrfnc = [nc for i in xrange(repeat)]
            else:
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                nc = Nio.open_file(wrf_file)
                in_wrfnc = [nc for i in xrange(repeat)]

        refnc = NetCDF(referent)

        # These have a left index that defines the product type
        multiproduct = varname in ("uvmet", "uvmet10", "cape_2d", "cape_3d",
                                   "cfrac")

        if not multi:
            ref_vals = refnc.variables[varname][:]
        else:
            data = refnc.variables[varname][:]
            if (varname != "uvmet" and varname != "uvmet10"
                    and varname != "cape_2d" and varname != "cape_3d"):
                new_dims = [repeat] + [x for x in data.shape]
            elif (varname == "uvmet" or varname == "uvmet10"
                  or varname == "cape_3d"):
                new_dims = [2] + [repeat] + [x for x in data.shape[1:]]
            elif (varname == "cape_2d"):
                new_dims = [4] + [repeat] + [x for x in data.shape[1:]]
            elif (varname == "cfrac"):
                new_dims = [3] + [repeat] + [x for x in data.shape[1:]]

            masked = False
            if (isinstance(data, ma.core.MaskedArray)):
                masked = True

            if not masked:
                ref_vals = np.zeros(new_dims, data.dtype)
            else:
                ref_vals = ma.asarray(np.zeros(new_dims, data.dtype))

            for i in xrange(repeat):
                if not multiproduct:
                    ref_vals[i, :] = data[:]

                    if masked:
                        ref_vals.mask[i, :] = data.mask[:]

                else:
                    for prod in xrange(ref_vals.shape[0]):
                        ref_vals[prod, i, :] = data[prod, :]

                        if masked:
                            ref_vals.mask[prod, i, :] = data.mask[prod, :]

        if (varname == "tc"):
            my_vals = getvar(in_wrfnc, "temp", timeidx=timeidx, units="c")
            tol = 1 / 100.
            atol = .1  # Note:  NCL uses 273.16 as conversion for some reason
            nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
        elif (varname == "pw"):
            my_vals = getvar(in_wrfnc, "pw", timeidx=timeidx)
            tol = .5 / 100.0
            atol = 0  # NCL uses different constants and doesn't use same
            # handrolled virtual temp in method
            nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
        elif (varname == "cape_2d"):
            cape_2d = getvar(in_wrfnc, varname, timeidx=timeidx)
            tol = 0 / 100.
            atol = 200.0
            # Let's only compare CAPE values until the F90 changes are
            # merged back in to NCL.  The modifications to the R and CP
            # changes TK enough that non-lifting parcels could lift, thus
            # causing wildly different values in LCL
            nt.assert_allclose(to_np(cape_2d[0, :]), ref_vals[0, :], tol, atol)
        elif (varname == "cape_3d"):
            cape_3d = getvar(in_wrfnc, varname, timeidx=timeidx)
            # Changing the R and CP constants, while keeping TK within
            # 2%, can lead to some big changes in CAPE.  Tolerances
            # have been set wide when comparing the with the original
            # NCL.  Change back when the F90 code is merged back with
            # NCL
            tol = 0 / 100.
            atol = 200.0

            #print np.amax(np.abs(to_np(cape_3d[0,:]) - ref_vals[0,:]))
            nt.assert_allclose(to_np(cape_3d), ref_vals, tol, atol)
        else:
            my_vals = getvar(in_wrfnc, varname, timeidx=timeidx)
            tol = 2 / 100.
            atol = 0.1
            #print (np.amax(np.abs(to_np(my_vals) - ref_vals)))
            nt.assert_allclose(to_np(my_vals), ref_vals, tol, atol)
Ejemplo n.º 28
0
    def join_csv_files(dir_list,
                       output_file_path,
                       extract_rainfall=True,
                       forecast_date=None,
                       station_data=None):
        # def join_csv_files(dir_list, output_file_path, extract_rainfall=True):
        """
        Joins a list of CSV files into one NetCDF file that's compatible with pSIMS' format.
        :param dir_list: The list of paths
        :param output_file_path: Output file path, should be absolute.
        :param extract_rainfall: Wether to extract rainfall data from weather series or not.
        :param forecast_date: Only used it extract_rainfall is True.
        :return: A dictionary if extract_rainfall is True, None otherwise.
        """
        proc_start_time = time.time()

        necdf_file_name = "%s_%s.psims.nc" % (station_data['grid_row'],
                                              station_data['grid_column'])
        nectdf_file_path = os.path.join(output_file_path, necdf_file_name)

        output_file = NetCDF(nectdf_file_path, 'w')

        csv_files = dir_list

        # If there's only one scenario, we call it '0' internally. Though the NetCDF output file won't have the
        # 'scen' dimension defined. This is needed when we extract rainfall data.
        scen_names = [0]
        if len(csv_files) > 1:
            # If there's more than one file, we extract the scenario name (the year of the climate series).
            scen_names = [
                CSVDatabaseWeatherSeries.__scen_name__(i) for i in csv_files
            ]

        expected_variables = {'fecha', 'rad', 'tmax', 'tmin', 'prcp'}
        var_units = {"rad": "MJm-2", "tmax": "C", "tmin": "C", "prcp": "mm"}

        # Define base dimensions, it's contents and units.
        dims = ['latitude', 'longitude', 'time']
        dim_var_contents = [[0], [0], []]
        dims_units = [
            'degrees_east', 'degrees_north',
            'days since %s 00:00:00' % WeatherNetCDFWriter.reference_date
        ]

        rainfall_data = None

        # Add scen dimension in case there is more than one weather file.
        if len(csv_files) > 1:
            dims = dims + ['scen']
            dim_var_contents += [scen_names]
            dims_units += ['Scenarios']

        # Create dimensions.
        for index, dim in enumerate(dims):
            output_file.createDimension(dim)
            dim_var = output_file.createVariable(dim, 'int32', (dim, ))
            dim_var.units = dims_units[index]
            dim_var[:] = dim_var_contents[index]

        variables_contents = {}
        time_var_content = []

        # Parse reference_date (str) to date.
        ref_date = datetime.strptime(WeatherNetCDFWriter.reference_date,
                                     '%Y-%m-%d')

        # Calculate forecast date as time difference (in days).
        if forecast_date:
            forecast_date = datetime.strptime(forecast_date, '%Y-%m-%d')
            forecast_date = forecast_date - ref_date
            forecast_date = forecast_date.days

        # Loop through CSV weather files.
        for scen_index, f in enumerate(csv_files):
            # Unpack dictionary entry: (key, value).
            scen_name = scen_names[scen_index]
            csv_file = csv.reader(open(f), delimiter='\t')

            csv_variables = []
            csv_content = dict()

            for r_index, row in enumerate(csv_file):
                if r_index == 0:
                    # Header
                    csv_variables = row

                    # Check that the header variables match the expected variables.
                    if len(expected_variables.intersection(
                            csv_variables)) != len(expected_variables):
                        raise RuntimeError(
                            "The variables in the CSV file \"%s\" don't match the expected ones (%s)."
                            % (csv_files[scen_index], expected_variables))

                    for column in row:
                        csv_content[column] = []

                else:
                    for i, value in enumerate(row):
                        var_name = csv_variables[i]
                        csv_content[var_name].append(value)

            csv_content['time'] = csv_content['fecha']
            del csv_content['fecha']

            # Calculate time diff in days for each date.
            for i, day in enumerate(csv_content['time']):
                day = datetime.strptime(day, '%Y-%m-%d')
                delta = day - ref_date
                csv_content['time'][i] = delta.days

            # Initialize the content of the time variable for it to be written once we finish
            # writting the other variables.
            if len(time_var_content) == 0:
                time_var_content = copy.deepcopy(csv_content['time'])
            else:
                # If it's already initialized, check that every CSV file has data for the same days.
                if time_var_content != csv_content['time']:
                    raise RuntimeError("Dates do not match between CSV files.")

            # Delete this variable to avoid trying to write it to the NetCDF file.
            del csv_content['time']

            # Loop through each variable in the CSV header.
            for var_name in list(csv_content.keys()):
                if var_name not in expected_variables:
                    continue

                if var_name in variables_contents:
                    var_array = variables_contents[var_name]
                else:
                    # Initialize this variable.
                    shape = (len(csv_content[var_name]), 1, 1)
                    if len(csv_files) > 1:
                        shape = (len(csv_files), ) + shape

                    var_array = np.empty(shape=shape)
                    var_array.fill(-99)
                    variables_contents[var_name] = var_array

                # Write variable content.
                if len(csv_files) > 1:
                    # The file index will be the scenario number.
                    var_array[scen_index, 0:len(csv_content[var_name]), 0,
                              0] = csv_content[var_name]
                else:
                    var_array[:, 0, 0] = csv_content[var_name]

        # Create the dimensions tuple to create variables in the NetCDF file.
        dims = ('time', 'latitude', 'longitude')
        if len(csv_files) > 1:
            dims = ('scen', ) + dims

        start_time = time.time()
        # Write variables to the NetCDF file.
        for var_name in variables_contents:
            netcdf_var = output_file.createVariable(var_name,
                                                    'float32',
                                                    dims,
                                                    fill_value=-99)
            netcdf_var.units = var_units[var_name]

            netcdf_var[:] = variables_contents[var_name]

        # Check if we need to extract rainfall data.
        if extract_rainfall:
            rainfall_data = dict()
            rain_variable = variables_contents['prcp']
            # Convert time variable to Numpy array, otherwise we can't use array indexes.
            time_var_content = np.array(time_var_content)

            # Again, check if there's more than one climate serie.
            if len(csv_files) > 1:
                # Extract rainfall data until the date of forecast.
                pre_forecast_time = time_var_content[
                    time_var_content <= forecast_date]
                rain = rain_variable[0, 0:len(pre_forecast_time), 0, 0]
                rainy_days = np.where(rain > 0)[0]

                rainfall_data['0'] = {
                    'dates': pre_forecast_time[rainy_days].tolist(),
                    'values': rain[rainy_days].tolist()
                }

                # Extract rainfall data for each scenario after the forecast date.
                for i, year in enumerate(scen_names):
                    post_forecast_time = time_var_content[
                        time_var_content > forecast_date]
                    post_forecast_start = len(time_var_content) - len(
                        post_forecast_time)
                    rain = rain_variable[i, post_forecast_start:, 0, 0]

                    rainy_days = np.where(rain > 0)[0]
                    rainfall_data[str(year)] = {
                        'dates': post_forecast_time[rainy_days].tolist(),
                        'values': rain[rainy_days].tolist()
                    }
            else:
                rain = rain_variable[:, 0, 0]
                rainy_days = np.where(rain > 0)[0]

                rainfall_data['0'] = {
                    'dates': time_var_content[rainy_days].tolist(),
                    'values': rain[rainy_days].tolist()
                }

        time_var = output_file.variables['time']
        time_var[:] = time_var_content

        output_file.close()
        logging.getLogger().debug('Write NetCDF file: %f.' %
                                  (time.time() - start_time))
        logging.getLogger().debug("NetCDF file created: '%s'. Time: %s." %
                                  (output_file_path,
                                   (time.time() - proc_start_time)))

        result = os.path.exists(nectdf_file_path)
        return result, rainfall_data
Ejemplo n.º 29
0
def main():
    wrfnc = NetCDF("/Users/bladwig/wrfout_d03_2003-05-07_09:00:00")
    
    # Cape NO RESULTS FOR LCL OR LFC
    cape, cin, lcl, lfc = w.getvar(wrfnc, "cape2d")
    #cape, cin = w.getvar(wrfnc, "cape3d")
    print n.amax(cape)
    print n.amax(cin)
    print n.amax(lcl)
    print n.amax(lfc)
    
    
    # DBZ 
    dbz = w.getvar(wrfnc, "dbz")
    print n.amax(dbz)
    
    # DP
    dp = w.getvar(wrfnc, "dp", units="f")
    print n.amax(dp)
    
    dp2 = w.getvar(wrfnc, "dp2m", units="f")
    print n.amax(dp2)
    
    # Height
    ht = w.getvar(wrfnc, "height", msl=False, units="m")
    print n.amax(ht)
    
    geopt = w.getvar(wrfnc, "geopt")
    print n.amax(geopt)
    
    # Helicity
    srh = w.getvar(wrfnc, "srh")
    print n.amax(srh)
    
    uhel = w.getvar(wrfnc, "uhel")
    print n.amax(uhel)
    
    # Omega (Not sure if this is correct, and units aren't C)
    omega = w.getvar(wrfnc, "omega")
    print n.amax(omega)
    
    # Precip Water (NOT SURE)
    pw = w.getvar(wrfnc, "pw")
    print n.amax(pw)
    
    # RH
    rh = w.getvar(wrfnc, "rh")
    print n.amax(rh)
    
    rh2 = w.getvar(wrfnc, "rh2m")
    print n.amax(rh2)
    
    # SLP
    slp = w.getvar(wrfnc, "slp", units="hpa")
    print n.amax(slp)
    
    # TEMP
    t = w.getvar(wrfnc, "temp", units="f")
    print n.amax(t)
    
    # ETH VALUES SEEM HIGH....
    eth = w.getvar(wrfnc, "theta_e", units="k")
    print n.amax(eth)
    
    tv = w.getvar(wrfnc, "tv", units="k")
    print n.amax(tv)
    
    # Note: NCL says this is in 'C', but appears to be 'K'
    tw = w.getvar(wrfnc, "tw", units="f")
    print n.amax(tw)
    
    # WIND
    umet,vmet = w.getvar(wrfnc, "uvmet", units="kts")
    print n.amax(umet)
    print n.amax(vmet)
    
    umet10,vmet10 = w.getvar(wrfnc, "uvmet10", units="kts")
    print n.amax(umet10)
    print n.amax(vmet10)
    
    
    
    # TERRAIN
    ter = w.getvar(wrfnc, "terrain", units="dm")
    print n.amax(ter)
    
    # VORTICITY
    avo = w.getvar(wrfnc, "avo")
    print n.amax(avo)
    
    pvo = w.getvar(wrfnc, "pvo")
    print n.amax(pvo)
    
    # LAT/LON
    lat = w.getvar(wrfnc, "lat")
    print n.amax(lat)
    print n.amin(lat)
    
    lon = w.getvar(wrfnc, "lon")
    print n.amax(lon)
    print n.amin(lon)
    
    i,j = w.get_ij(wrfnc, -97.516540, 35.467787)
    print i,j
    
    lon, lat = w.get_ll(wrfnc, 33.5, 33.5)
    print lon, lat
    
    #ETA -- Result somewhat different than geopt
    z = w.convert_eta(wrfnc, msl=False, units="m")
    print n.amax(z)
    
    diff = n.abs(z - ht)/ht * 100.0
    print n.amin(diff), n.amax(diff)
Ejemplo n.º 30
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except:
            pass

        try:
            from PyNIO import Nio
        except:
            pass

        if not multi:
            timeidx = 0
            if not pynio:
                in_wrfnc = NetCDF(wrf_in)
            else:
                # Note: Python doesn't like it if you reassign an outer scoped
                # variable (wrf_in in this case)
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                in_wrfnc = Nio.open_file(wrf_file)
        else:
            timeidx = None
            if not pynio:
                nc = NetCDF(wrf_in)
                in_wrfnc = [nc for i in xrange(repeat)]
            else:
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                nc = Nio.open_file(wrf_file)
                in_wrfnc = [nc for i in xrange(repeat)]

        if (varname == "interplevel"):
            ref_ht_500 = _get_refvals(referent, "z_500", repeat, multi)
            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)

            # Make sure the numpy versions work first
            hts_500 = interplevel(to_np(hts), to_np(p), 500)
            hts_500 = interplevel(hts, p, 500)

            nt.assert_allclose(to_np(hts_500), ref_ht_500)

        elif (varname == "vertcross"):
            ref_ht_cross = _get_refvals(referent, "ht_cross", repeat, multi)
            ref_p_cross = _get_refvals(referent, "p_cross", repeat, multi)

            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)

            pivot_point = CoordPair(hts.shape[-1] / 2, hts.shape[-2] / 2)
            #ht_cross = vertcross(to_np(hts), p, pivot_point=pivot_point,
            #                     angle=90., latlon=True)
            # Make sure the numpy versions work first
            ht_cross = vertcross(to_np(hts),
                                 to_np(p),
                                 pivot_point=pivot_point,
                                 angle=90.)
            ht_cross = vertcross(hts, p, pivot_point=pivot_point, angle=90.)

            # Note:  Until the bug is fixed in NCL, the wrf-python cross
            # sections will contain one extra point
            nt.assert_allclose(to_np(ht_cross)[..., 0:-1],
                               ref_ht_cross,
                               rtol=.01)

            # Test the manual projection method with lat/lon
            lats = hts.coords["XLAT"]
            lons = hts.coords["XLONG"]
            ll_point = ll_points(lats, lons)
            pivot = CoordPair(lat=lats[int(lats.shape[-2] / 2),
                                       int(lats.shape[-1] / 2)],
                              lon=lons[int(lons.shape[-2] / 2),
                                       int(lons.shape[-1] / 2)])
            v1 = vertcross(hts,
                           p,
                           wrfin=in_wrfnc,
                           pivot_point=pivot_point,
                           angle=90.0)
            v2 = vertcross(hts,
                           p,
                           projection=hts.attrs["projection"],
                           ll_point=ll_point,
                           pivot_point=pivot_point,
                           angle=90.)

            nt.assert_allclose(to_np(v1), to_np(v2), rtol=.01)

            # Test opposite
            p_cross1 = vertcross(p, hts, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(p_cross1)[..., 0:-1],
                               ref_p_cross,
                               rtol=.01)
            # Test point to point
            start_point = CoordPair(0, hts.shape[-2] / 2)
            end_point = CoordPair(-1, hts.shape[-2] / 2)

            p_cross2 = vertcross(p,
                                 hts,
                                 start_point=start_point,
                                 end_point=end_point)

            nt.assert_allclose(to_np(p_cross1), to_np(p_cross2))

        elif (varname == "interpline"):

            ref_t2_line = _get_refvals(referent, "t2_line", repeat, multi)

            t2 = getvar(in_wrfnc, "T2", timeidx=timeidx)
            pivot_point = CoordPair(t2.shape[-1] / 2, t2.shape[-2] / 2)

            #t2_line1 = interpline(to_np(t2), pivot_point=pivot_point,
            #                      angle=90.0, latlon=True)
            # Make sure the numpy version works
            t2_line1 = interpline(to_np(t2),
                                  pivot_point=pivot_point,
                                  angle=90.0)
            t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)

            # Note:  After NCL is fixed, remove the slice.
            nt.assert_allclose(to_np(t2_line1)[..., 0:-1], ref_t2_line)

            # Test the manual projection method with lat/lon
            lats = t2.coords["XLAT"]
            lons = t2.coords["XLONG"]
            ll_point = ll_points(lats, lons)
            pivot = CoordPair(lat=lats[int(lats.shape[-2] / 2),
                                       int(lats.shape[-1] / 2)],
                              lon=lons[int(lons.shape[-2] / 2),
                                       int(lons.shape[-1] / 2)])
            l1 = interpline(t2,
                            wrfin=in_wrfnc,
                            pivot_point=pivot_point,
                            angle=90.0)
            l2 = interpline(t2,
                            projection=t2.attrs["projection"],
                            ll_point=ll_point,
                            pivot_point=pivot_point,
                            angle=90.)
            nt.assert_allclose(to_np(l1), to_np(l2), rtol=.01)

            # Test point to point
            start_point = CoordPair(0, t2.shape[-2] / 2)
            end_point = CoordPair(-1, t2.shape[-2] / 2)

            t2_line2 = interpline(t2,
                                  start_point=start_point,
                                  end_point=end_point)

            nt.assert_allclose(to_np(t2_line1), to_np(t2_line2))
        elif (varname == "vinterp"):
            # Tk to theta
            fld_tk_theta = _get_refvals(referent, "fld_tk_theta", repeat,
                                        multi)
            fld_tk_theta = np.squeeze(fld_tk_theta)

            tk = getvar(in_wrfnc, "temp", timeidx=timeidx, units="k")

            interp_levels = [200, 300, 500, 1000]

            # Make sure the numpy version works
            field = vinterp(in_wrfnc,
                            field=to_np(tk),
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 5 / 100.
            atol = 0.0001

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_theta)))
            nt.assert_allclose(to_np(field), fld_tk_theta, tol, atol)

            # Tk to theta-e
            fld_tk_theta_e = _get_refvals(referent, "fld_tk_theta_e", repeat,
                                          multi)
            fld_tk_theta_e = np.squeeze(fld_tk_theta_e)

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta-e",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 3 / 100.
            atol = 50.0001

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_theta_e)/fld_tk_theta_e)*100)
            nt.assert_allclose(to_np(field), fld_tk_theta_e, tol, atol)

            # Tk to pressure
            fld_tk_pres = _get_refvals(referent, "fld_tk_pres", repeat, multi)
            fld_tk_pres = np.squeeze(fld_tk_pres)

            interp_levels = [850, 500]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            #print (np.amax(np.abs(to_np(field) - fld_tk_pres)))
            nt.assert_allclose(to_np(field), fld_tk_pres, tol, atol)

            # Tk to geoht_msl
            fld_tk_ght_msl = _get_refvals(referent, "fld_tk_ght_msl", repeat,
                                          multi)
            fld_tk_ght_msl = np.squeeze(fld_tk_ght_msl)
            interp_levels = [1, 2]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="ght_msl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_ght_msl)))
            nt.assert_allclose(to_np(field), fld_tk_ght_msl, tol, atol)

            # Tk to geoht_agl
            fld_tk_ght_agl = _get_refvals(referent, "fld_tk_ght_agl", repeat,
                                          multi)
            fld_tk_ght_agl = np.squeeze(fld_tk_ght_agl)
            interp_levels = [1, 2]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="ght_agl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_ght_agl)))
            nt.assert_allclose(to_np(field), fld_tk_ght_agl, tol, atol)

            # Hgt to pressure
            fld_ht_pres = _get_refvals(referent, "fld_ht_pres", repeat, multi)
            fld_ht_pres = np.squeeze(fld_ht_pres)

            z = getvar(in_wrfnc, "height", timeidx=timeidx, units="m")
            interp_levels = [500, 50]
            field = vinterp(in_wrfnc,
                            field=z,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="ght",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_ht_pres)))
            nt.assert_allclose(to_np(field), fld_ht_pres, tol, atol)

            # Pressure to theta
            fld_pres_theta = _get_refvals(referent, "fld_pres_theta", repeat,
                                          multi)
            fld_pres_theta = np.squeeze(fld_pres_theta)

            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)
            interp_levels = [200, 300, 500, 1000]
            field = vinterp(in_wrfnc,
                            field=p,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="pressure",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_pres_theta)))
            nt.assert_allclose(to_np(field), fld_pres_theta, tol, atol)

            # Theta-e to pres
            fld_thetae_pres = _get_refvals(referent, "fld_thetae_pres", repeat,
                                           multi)
            fld_thetae_pres = np.squeeze(fld_thetae_pres)

            eth = getvar(in_wrfnc, "eth", timeidx=timeidx)
            interp_levels = [850, 500, 5]
            field = vinterp(in_wrfnc,
                            field=eth,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="theta-e",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_thetae_pres)))
            nt.assert_allclose(to_np(field), fld_thetae_pres, tol, atol)