Beispiel #1
0
    def __init__(self, samples_dir="", start_year=-np.Inf, end_year=np.Inf):
        self.samples_dir = Path(samples_dir)

        self.year_month_date_to_file_list = get_monthyeardate_to_paths_map(file_prefix="pm", start_year=start_year,
                                                                           end_year=end_year,
                                                                           samples_dir_path=self.samples_dir)

        self.varname = "LC"
        self.level = -1
        self.level_kind = level_kinds.ARBITRARY

        self.cached_data = {}
Beispiel #2
0
    def __init__(self, samples_dir="", start_year=-np.Inf, end_year=np.Inf):
        self.samples_dir = Path(samples_dir)

        self.year_month_date_to_file_list = get_monthyeardate_to_paths_map(
            file_prefix="pm", start_year=start_year, end_year=end_year, samples_dir_path=self.samples_dir
        )

        self.varname = "LC"
        self.level = -1
        self.level_kind = level_kinds.ARBITRARY

        self.cached_data = {}
Beispiel #3
0
def main():
    start_year = 1979
    end_year = 1981

    HL_LABEL = "CRCM5_HL"
    NEMO_LABEL = "CRCM5_NEMO"

    file_prefix = "pm"
    PR_level = -1
    PR_level_type = level_kinds.ARBITRARY

    tprecip_vname = "PR"
    sprecip_vname = "SN"

    TT_level = 1
    TT_level_type = level_kinds.HYBRID

    sim_label_to_path = OrderedDict([
        (HL_LABEL,
         "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"
         ),
        (NEMO_LABEL,
         "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples"
         )
    ])

    # get a coord file ...
    coord_file = ""
    found_coord_file = False
    for mdir in os.listdir(sim_label_to_path[HL_LABEL]):

        mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir)
        if not os.path.isdir(mdir_path):
            continue

        for fn in os.listdir(mdir_path):
            print(fn)
            if fn[:2] not in ["pm", "dm", "pp", "dp"]:
                continue

            coord_file = os.path.join(mdir_path, fn)
            found_coord_file = True

        if found_coord_file:
            break

    bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(
        path=coord_file)
    xx, yy = bmp(lons, lats)

    # read necessary input and calculate snowfall and save to the file
    for sim_label, samples_dir in sim_label_to_path.items():
        samples_dir_path = Path(samples_dir)

        TT_monthdate_to_paths = get_monthyeardate_to_paths_map(
            file_prefix="dm",
            start_year=start_year,
            end_year=end_year,
            samples_dir_path=samples_dir_path)
        PR_monthdate_to_paths = get_monthyeardate_to_paths_map(
            file_prefix="pm",
            start_year=start_year,
            end_year=end_year,
            samples_dir_path=samples_dir_path)

        with Dataset(
                str(samples_dir_path.parent / "{}_snow_fall_{}-{}.nc".format(
                    sim_label, start_year, end_year)), "w") as ds:

            assert isinstance(ds, Dataset)

            ds.createDimension("time", None)
            ds.createDimension("lon", lons.shape[0])
            ds.createDimension("lat", lons.shape[1])

            # create the schema of the output file
            snow_fall = ds.createVariable(sprecip_vname,
                                          "f4",
                                          dimensions=("time", "lon", "lat"))
            lons_var = ds.createVariable("lon",
                                         "f4",
                                         dimensions=("lon", "lat"))
            lats_var = ds.createVariable("lat",
                                         "f4",
                                         dimensions=("lon", "lat"))
            time_var = ds.createVariable("time", "i8", dimensions=("time", ))
            time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format(
                datetime(start_year, 1, 1))

            lons_var[:] = lons
            lats_var[:] = lats

            # use sorted dates
            record_count = 0
            for month_date in sorted(TT_monthdate_to_paths):

                tt = MultiRPN(path=TT_monthdate_to_paths[month_date]
                              ).get_all_time_records_for_name_and_level(
                                  varname="TT",
                                  level=TT_level,
                                  level_kind=TT_level_type)

                pr = MultiRPN(path=PR_monthdate_to_paths[month_date]
                              ).get_all_time_records_for_name_and_level(
                                  varname="PR",
                                  level=PR_level,
                                  level_kind=PR_level_type)

                print("Processing {}".format(month_date))
                for d in sorted(tt):
                    t_field = tt[d]
                    pr_field = pr[d]

                    sn = get_snow_fall_m_per_s(precip_m_per_s=pr_field,
                                               tair_deg_c=t_field)

                    snow_fall[record_count, :, :] = sn
                    time_var[record_count] = date2num(d, time_var.units)
                    record_count += 1
def main():
    start_year = 1979
    end_year = 1981

    HL_LABEL = "CRCM5_HL"
    NEMO_LABEL = "CRCM5_NEMO"

    file_prefix = "pm"
    PR_level = -1
    PR_level_type = level_kinds.ARBITRARY

    tprecip_vname = "PR"
    sprecip_vname = "SN"


    TT_level = 1
    TT_level_type = level_kinds.HYBRID



    sim_label_to_path = OrderedDict(
        [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"),
         (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")]
    )

    # get a coord file ...
    coord_file = ""
    found_coord_file = False
    for mdir in os.listdir(sim_label_to_path[HL_LABEL]):

        mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir)
        if not os.path.isdir(mdir_path):
            continue

        for fn in os.listdir(mdir_path):
            print(fn)
            if fn[:2] not in ["pm", "dm", "pp", "dp"]:
                continue

            coord_file = os.path.join(mdir_path, fn)
            found_coord_file = True

        if found_coord_file:
            break

    bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file)
    xx, yy = bmp(lons, lats)



    # read necessary input and calculate snowfall and save to the file
    for sim_label, samples_dir in sim_label_to_path.items():
        samples_dir_path = Path(samples_dir)

        TT_monthdate_to_paths = get_monthyeardate_to_paths_map(file_prefix="dm", start_year=start_year, end_year=end_year, samples_dir_path=samples_dir_path)
        PR_monthdate_to_paths = get_monthyeardate_to_paths_map(file_prefix="pm", start_year=start_year, end_year=end_year, samples_dir_path=samples_dir_path)



        with Dataset(str(samples_dir_path.parent / "{}_snow_fall_{}-{}.nc".format(sim_label, start_year, end_year)), "w") as ds:


            assert isinstance(ds, Dataset)


            ds.createDimension("time", None)
            ds.createDimension("lon", lons.shape[0])
            ds.createDimension("lat", lons.shape[1])




            # create the schema of the output file
            snow_fall = ds.createVariable(sprecip_vname, "f4", dimensions=("time", "lon", "lat"))
            lons_var = ds.createVariable("lon", "f4", dimensions=("lon", "lat"))
            lats_var = ds.createVariable("lat", "f4", dimensions=("lon", "lat"))
            time_var = ds.createVariable("time", "i8", dimensions=("time", ))
            time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format(datetime(start_year, 1, 1))


            lons_var[:] = lons
            lats_var[:] = lats



            # use sorted dates
            record_count = 0
            for month_date in sorted(TT_monthdate_to_paths):

                tt = MultiRPN(path=TT_monthdate_to_paths[month_date]).get_all_time_records_for_name_and_level(varname="TT", level=TT_level, level_kind=TT_level_type)

                pr = MultiRPN(path=PR_monthdate_to_paths[month_date]).get_all_time_records_for_name_and_level(varname="PR", level=PR_level, level_kind=PR_level_type)

                print("Processing {}".format(month_date))
                for d in sorted(tt):
                    t_field = tt[d]
                    pr_field = pr[d]

                    sn = get_snow_fall_m_per_s(precip_m_per_s=pr_field, tair_deg_c=t_field)

                    snow_fall[record_count, :, :] = sn
                    time_var[record_count] = date2num(d, time_var.units)
                    record_count += 1
def main():
    start_year = 1979
    end_year = 1981

    HL_LABEL = "CRCM5_HL"
    NEMO_LABEL = "CRCM5_NEMO"

    file_prefix = "dm"
    level = 1
    level_type = level_kinds.HYBRID

    wind_comp_names = ["UU", "VV"]

    sim_label_to_path = OrderedDict([
        (HL_LABEL,
         "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"
         ),
        (NEMO_LABEL,
         "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples"
         )
    ])

    # get a coord file ...
    coord_file = ""
    found_coord_file = False
    for mdir in os.listdir(sim_label_to_path[HL_LABEL]):

        mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir)
        if not os.path.isdir(mdir_path):
            continue

        for fn in os.listdir(mdir_path):
            print(fn)
            if fn[:2] not in ["pm", "dm", "pp", "dp"]:
                continue

            coord_file = os.path.join(mdir_path, fn)
            found_coord_file = True

        if found_coord_file:
            break

    bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(
        path=coord_file)
    xx, yy = bmp(lons, lats)
    lons[lons > 180] -= 360

    # loop through all files rotate vaectors and save to netcdf
    for sim_label, samples_dir in sim_label_to_path.items():

        samples = Path(samples_dir)
        po = samples.parent

        monthdate_to_path_list = nemo_hl_util.get_monthyeardate_to_paths_map(
            file_prefix=file_prefix,
            start_year=start_year,
            end_year=end_year,
            samples_dir_path=samples)

        # Netcdf output file to put rotated winds
        po /= "rotated_wind_{}.nc".format(sim_label)

        with Dataset(str(po), "w") as ds:

            ds.createDimension("time", None)
            ds.createDimension("lon", lons.shape[0])
            ds.createDimension("lat", lons.shape[1])

            # create the schema of the output file
            vname_to_ncvar = {}
            for vname in wind_comp_names:
                vname_to_ncvar[vname] = ds.createVariable(vname,
                                                          "f4",
                                                          dimensions=("time",
                                                                      "lon",
                                                                      "lat"))
                vname_to_ncvar[vname].units = "knots"

            lons_var = ds.createVariable("lon",
                                         "f4",
                                         dimensions=("lon", "lat"))
            lats_var = ds.createVariable("lat",
                                         "f4",
                                         dimensions=("lon", "lat"))
            time_var = ds.createVariable("time", "i8", dimensions=("time", ))
            time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format(
                datetime(start_year, 1, 1))

            lons_var[:] = lons
            lats_var[:] = lats

            # use sorted dates
            record_count = 0

            for month_date in sorted(monthdate_to_path_list):
                # select only dm files
                mr = MultiRPN(path=monthdate_to_path_list[month_date])

                vname_to_fields = {}
                for vname in wind_comp_names:
                    vname_to_fields[
                        vname] = mr.get_all_time_records_for_name_and_level(
                            varname=vname, level=level, level_kind=level_type)

                for ti, t in enumerate(
                        sorted(vname_to_fields[wind_comp_names[0]])):
                    time_var[record_count] = date2num(t, time_var.units)

                    uu = vname_to_fields[wind_comp_names[0]][t]
                    vv = vname_to_fields[wind_comp_names[1]][t]

                    uu_rot, vv_rot = rotate_vecs_from_geo_to_rotpole(uu,
                                                                     vv,
                                                                     lons,
                                                                     lats,
                                                                     bmp=bmp)

                    # in knots not in m/s
                    vname_to_ncvar[wind_comp_names[0]][
                        record_count, :, :] = uu_rot
                    vname_to_ncvar[wind_comp_names[1]][
                        record_count, :, :] = vv_rot
                    record_count += 1
Beispiel #6
0
def main():
    start_year = 1979
    end_year = 1981

    HL_LABEL = "CRCM5_HL"
    NEMO_LABEL = "CRCM5_NEMO"

    file_prefix = "dm"
    level = 1
    level_type = level_kinds.HYBRID

    wind_comp_names = ["UU", "VV"]


    sim_label_to_path = OrderedDict(
        [(HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples"),
         (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples")]
    )

    # get a coord file ...
    coord_file = ""
    found_coord_file = False
    for mdir in os.listdir(sim_label_to_path[HL_LABEL]):

        mdir_path = os.path.join(sim_label_to_path[HL_LABEL], mdir)
        if not os.path.isdir(mdir_path):
            continue

        for fn in os.listdir(mdir_path):
            print(fn)
            if fn[:2] not in ["pm", "dm", "pp", "dp"]:
                continue

            coord_file = os.path.join(mdir_path, fn)
            found_coord_file = True

        if found_coord_file:
            break

    bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file(path=coord_file)
    xx, yy = bmp(lons, lats)
    lons[lons > 180] -= 360


    # loop through all files rotate vaectors and save to netcdf
    for sim_label, samples_dir in sim_label_to_path.items():

        samples = Path(samples_dir)
        po = samples.parent

        monthdate_to_path_list = nemo_hl_util.get_monthyeardate_to_paths_map(file_prefix=file_prefix,
                                                                             start_year=start_year, end_year=end_year,
                                                                             samples_dir_path=samples)

        # Netcdf output file to put rotated winds
        po /= "rotated_wind_{}.nc".format(sim_label)

        with Dataset(str(po), "w") as ds:

            ds.createDimension("time", None)
            ds.createDimension("lon", lons.shape[0])
            ds.createDimension("lat", lons.shape[1])

            # create the schema of the output file
            vname_to_ncvar = {}
            for vname in wind_comp_names:
                vname_to_ncvar[vname] = ds.createVariable(vname, "f4", dimensions=("time", "lon", "lat"))
                vname_to_ncvar[vname].units = "knots"

            lons_var = ds.createVariable("lon", "f4", dimensions=("lon", "lat"))
            lats_var = ds.createVariable("lat", "f4", dimensions=("lon", "lat"))
            time_var = ds.createVariable("time", "i8", dimensions=("time",))
            time_var.units = "hours since {:%Y-%m-%d %H:%M:%S}".format(datetime(start_year, 1, 1))

            lons_var[:] = lons
            lats_var[:] = lats


            # use sorted dates
            record_count = 0


            for month_date in sorted(monthdate_to_path_list):
                # select only dm files
                mr = MultiRPN(path=monthdate_to_path_list[month_date])


                vname_to_fields = {}
                for vname in wind_comp_names:
                    vname_to_fields[vname] = mr.get_all_time_records_for_name_and_level(varname=vname, level=level, level_kind=level_type)

                for ti, t in enumerate(sorted(vname_to_fields[wind_comp_names[0]])):
                    time_var[record_count] = date2num(t, time_var.units)

                    uu = vname_to_fields[wind_comp_names[0]][t]
                    vv = vname_to_fields[wind_comp_names[1]][t]

                    uu_rot, vv_rot = rotate_vecs_from_geo_to_rotpole(uu, vv, lons, lats, bmp=bmp)


                    # in knots not in m/s
                    vname_to_ncvar[wind_comp_names[0]][record_count, :, :] = uu_rot
                    vname_to_ncvar[wind_comp_names[1]][record_count, :, :] = vv_rot
                    record_count += 1