Exemplo n.º 1
0
def _compute_M2_grids(ui):
    logging.info('Computation of M2 grids started.')
    # paths
    project_name = str(ui.le_id001.text())
    project_dir = str(ui.le_id002.text())
    grace_path = os.path.join(project_dir, project_name, 'grace',
                              'single_solution')
    grids_path = os.path.join(grace_path, 'grids')
    internal_validation_path = os.path.join(project_dir, project_name,
                                            'validation', 'internal')
    # constant density
    density = 1025.0
    # which grid
    if (ui.rb_id057.isChecked() or ui.rb_id100.isChecked()):
        lon, lat, _, _ = np.genfromtxt(grid_file, skip_header=2, unpack=True)
        _compute_buffer_grid(ui, lon, lat)
    # loop over all dates
    mjd_start, mjd_end = compute_grace._date_settings(ui)
    mjd = mjd_start
    while mjd <= mjd_end:
        mjd1, mjd2 = date_functions.mjd2mjdmrange(mjd)
        mjd = (mjd2 + 1)
        ymstr = date_functions.mjd2ymstring(mjd1, '%Y-%m')[0].decode("utf-8")
        grid_file_pm = os.path.join(grids_path, '%s-grid.txt' % (ymstr))
        pointmass_file = os.path.join(internal_validation_path, 'grids',
                                      'm2_%s-pointmass.txt' % (ymstr))
        temp_dir = tempfile.mkdtemp()
        tmp_grid = os.path.join(temp_dir, 'tmp.grid')
        _compute_gfc2grid(ui, groops_bin, buffer_grid_file, tmp_grid, mjd1,
                          min_degree, max_degree, gauss)
        try:
            if ui.rb_id058.isChecked():
                lon, lat, _, _ = np.genfromtxt(grid_file_pm,
                                               skip_header=2,
                                               unpack=True)
                _compute_buffer_grid(ui, lon, lat)
            _compute_gfc2grid(ui, groops_bin, buffer_grid_file, tmp_grid, mjd1,
                              min_degree, max_degree, gauss)
        except:
            continue
        if not os.path.isfile(tmp_grid):
            continue
        lon, lat, h, area_i, tws = np.genfromtxt(tmp_grid,
                                                 skip_header=2,
                                                 unpack=True)
        area = (area_i * defaults.EARTH_EQUATORIAL_RADIUS() *
                defaults.EARTH_EQUATORIAL_RADIUS()) * 1e-6
        kg = tws * (area * 1e6) * density
        groops_interface.make_grid_file(groops_bin, pointmass_file, lon, lat,
                                        h, area, *(kg, np.array(0)))
        shutil.rmtree(temp_dir)
        logging.info('Computing grid: %s', pointmass_file)
    logging.info('Computation of M2 grids finished.')
Exemplo n.º 2
0
def _time_series(ui):
    logging.info('Creating time series files from point mass grids...')
    # paths
    project_name = str(ui.le_id001.text())
    project_dir = str(ui.le_id002.text())
    corrections_path = os.path.join(project_dir, project_name, 'corrections')
    # loop over correction types
    for correction in ('a', 'gldas', 'ice5', 'ice6', 'ice6_grace', 'klemann',
                       'lsdm', 'promet', 'wghm', 'goco'):
        # time series files
        time_series_path = os.path.join(corrections_path, correction,
                                        'time_series')
        time_series_file = os.path.join(time_series_path,
                                        '%s.txt' % correction)
        with open(time_series_file, 'w') as f:
            f.write('%25s %25s %25s\n' %
                    ('time (mjd)', 'mass (kg)', 'sigma (kg)'))
        # loop over all dates
        mjd_start, mjd_end = compute_grace._date_settings(ui)
        mjd = mjd_start
        while mjd <= mjd_end:
            mjd1, mjd2 = date_functions.mjd2mjdmrange(mjd)
            mjd = (mjd2 + 1)
            ymstr = date_functions.mjd2ymstring(mjd1,
                                                '%Y-%m')[0].decode("utf-8")
            pointmass_file = os.path.join(
                corrections_path, correction, 'grids',
                '%s_%s-pointmass.txt' % (correction, ymstr))
            try:
                lon, lat, h, area, x, sigma_x = np.genfromtxt(pointmass_file,
                                                              skip_header=2,
                                                              unpack=True)
                kg = np.sum(x)
                sigma_kg = np.sqrt(np.sum(sigma_x**2.0) / sigma_x.size)
                with open(time_series_file, 'a+') as f:
                    f.write('%+25.16e %+25.16e %+25.16e\n' %
                            (mjd1, kg, sigma_kg))
            except:
                continue
            logging.info('Creating time series file: %s_%s', correction, ymstr)
    logging.info('Time series files created.')
Exemplo n.º 3
0
def _time_series(ui):
    logging.info('Creating time series files from point mass grids...')
    # paths
    project_name = str(ui.le_id001.text())
    project_dir = str(ui.le_id002.text())
    internal_validation_path = os.path.join(project_dir, project_name,
                                            'validation', 'internal')
    # loop over element types
    for element in ('m1', 'm2', 'm1_star', 'm2_syn', 'm1_syn'):
        # time series files
        time_series_path = os.path.join(internal_validation_path,
                                        'time_series')
        time_series_file = os.path.join(time_series_path, '%s.txt' % element)
        with open(time_series_file, 'w') as f:
            f.write('%25s %25s %25s\n' %
                    ('time (mjd)', 'mass (kg)', 'sigma (kg)'))
        # loop over all dates
        mjd_start, mjd_end = compute_grace._date_settings(ui)
        mjd = mjd_start
        while mjd <= mjd_end:
            mjd1, mjd2 = date_functions.mjd2mjdmrange(mjd)
            mjd = (mjd2 + 1)
            ymstr = date_functions.mjd2ymstring(mjd1,
                                                '%Y-%m')[0].decode("utf-8")
            pointmass_file = os.path.join(
                internal_validation_path, 'grids',
                '%s_%s-pointmass.txt' % (element, ymstr))
            try:
                lon, lat, h, area, x, sigma_x = np.genfromtxt(pointmass_file,
                                                              skip_header=2,
                                                              unpack=True)
                kg = np.sum(x)
                sigma_kg = np.sqrt(np.sum(sigma_x**2.0) / sigma_x.size)
                with open(time_series_file, 'a+') as f:
                    f.write('%+25.16e %+25.16e %+25.16e\n' %
                            (mjd1, kg, sigma_kg))
            except:
                continue
            logging.info('Creating time series file: %s_%s', element, ymstr)
    mjd, m1, _ = np.genfromtxt(os.path.join(time_series_path, 'm1.txt'),
                               skip_header=1,
                               unpack=True)
    _, m2, _ = np.genfromtxt(os.path.join(time_series_path, 'm2.txt'),
                             skip_header=1,
                             unpack=True)
    _, m2_syn, _ = np.genfromtxt(os.path.join(time_series_path, 'm2_syn.txt'),
                                 skip_header=1,
                                 unpack=True)
    data = {}
    data['iaf'] = m2 / m1
    data['signal_loss'] = 100.0 * (1.0 - m2_syn / m2)
    data['m2_syn_star'] = m2 * (1.0 + (1.0 - m2_syn / m2))
    data['faf'] = data['m2_syn_star'] / m1
    for key in ('iaf', 'signal_loss', 'm2_syn_star', 'faf'):
        time_series_file = os.path.join(time_series_path, '%s.txt' % key)
        with open(time_series_file, 'w') as f:
            f.write('%25s %25s %25s\n' % ('time (mjd)', 'value', 'dummy'))
        for jx in list(range(np.size(mjd))):
            with open(time_series_file, 'a+') as f:
                f.write('%+25.16e %+25.16e %+25.16e\n' %
                        (mjd[jx], data[key][jx], 0))
    logging.info('Time series files created.')
Exemplo n.º 4
0
def _compute_M1_syn_grids(ui):
    logging.info('Computation of M1_syn grids started.')
    # paths
    project_name = str(ui.le_id001.text())
    project_dir = str(ui.le_id002.text())
    internal_validation_path = os.path.join(project_dir, project_name,
                                            'validation', 'internal')
    # global grid
    grid_file_global = pkg_resources.resource_filename(
        'picasso.data', 'grids/geographical_30-30.grid')
    grid0 = np.array(np.genfromtxt(grid_file_global, skip_header=2), ndmin=2)
    # indices
    if (ui.rb_id057.isChecked() or ui.rb_id100.isChecked()):
        lon, lat, _, _ = np.genfromtxt(grid_file, skip_header=2, unpack=True)
        ix = []
        for jx in list(range(np.size(lon))):
            ix.append(
                _return_index_closest_point(np.array([lon[jx], lat[jx]]),
                                            grid0[:, 0:2]))
    # constant density
    density = 1025.0
    # loop over all dates
    mjd_start, mjd_end = compute_grace._date_settings(ui)
    mjd = mjd_start
    while mjd <= mjd_end:
        mjd1, mjd2 = date_functions.mjd2mjdmrange(mjd)
        mjd = (mjd2 + 1)
        ymstr = date_functions.mjd2ymstring(mjd1, '%Y-%m')[0].decode("utf-8")
        m1_file = os.path.join(internal_validation_path, 'grids',
                               'm1_%s-pointmass.txt' % (ymstr))
        m2_file = os.path.join(internal_validation_path, 'grids',
                               'm2_%s-pointmass.txt' % (ymstr))
        m1_star_file = os.path.join(internal_validation_path, 'grids',
                                    'm1_star_%s-pointmass.txt' % (ymstr))
        m2_syn_file = os.path.join(internal_validation_path, 'grids',
                                   'm2_syn_%s-pointmass.txt' % (ymstr))
        m1_syn_file = os.path.join(internal_validation_path, 'grids',
                                   'm1_syn_%s-pointmass.txt' % (ymstr))
        try:
            lon1, lat1, h1, area1, x1, sigma_x1 = np.genfromtxt(m1_file,
                                                                skip_header=2,
                                                                unpack=True)
            lon2, lat2, h2, area2, x2, sigma_x2 = np.genfromtxt(m2_file,
                                                                skip_header=2,
                                                                unpack=True)
        except:
            continue
        kg1 = np.sum(x1)
        kg2 = np.sum(x2)
        IAF = kg2 / kg1
        m1_star = kg1 * IAF
        groops_interface.make_grid_file(groops_bin, m1_star_file, lon1, lat1,
                                        h1, area1, *(x1 * IAF, np.array(0)))
        if (ui.rb_id057.isChecked() or ui.rb_id100.isChecked()):
            m1_star_i = np.zeros(grid0.shape[0])
            m1_star_i[ix] = x1 * IAF / ((area1 * 1e6) * density)
        else:
            m1_star_i = griddata((lon1, lat1),
                                 x1 * IAF / ((area1 * 1e6) * density),
                                 (grid0[:, 0], grid0[:, 1]),
                                 method='cubic')
        temp_dir = tempfile.mkdtemp()
        tmp_grid1 = os.path.join(temp_dir, 'tmp1.grid')
        tmp_grid2 = os.path.join(temp_dir, 'tmp2.grid')
        groops_interface.make_grid_file(groops_bin, tmp_grid1, grid0[:, 0],
                                        grid0[:, 1], grid0[:, 2], grid0[:, 3],
                                        *(m1_star_i, np.array(0)))
        groops_interface.compute_grid_to_gfc_to_grid(groops_bin,
                                                     tmp_grid1,
                                                     tmp_grid2,
                                                     buffer_grid_file,
                                                     gauss=gauss)
        if not os.path.isfile(tmp_grid2):
            continue
        lon, lat, h, area_i, tws = np.genfromtxt(tmp_grid2,
                                                 skip_header=2,
                                                 unpack=True)
        area = (area_i * defaults.EARTH_EQUATORIAL_RADIUS() *
                defaults.EARTH_EQUATORIAL_RADIUS()) * 1e-6
        kg = tws * (area * 1e6) * density
        kg2_syn = np.sum(kg)
        groops_interface.make_grid_file(groops_bin, m2_syn_file, lon, lat, h,
                                        area, *(kg, np.array(0)))
        signal_loss = 100.0 * (1.0 - kg2_syn / kg2)
        kg2_syn_star = kg2 * (1.0 + (1.0 - kg2_syn / kg2))
        FAF = kg2_syn_star / kg1
        kg1_syn = FAF * kg1
        groops_interface.make_grid_file(groops_bin, m1_syn_file, lon1, lat1,
                                        h1, area1, *(x1 * FAF, np.array(0)))
        shutil.rmtree(temp_dir)
        logging.info('Computing grid: %s', m1_syn_file)
    logging.info('Computation of M1_syn grids finished.')
Exemplo n.º 5
0
def _compute_hydrology_grids(ui):
    logging.info('Computing hydrological grids...')
    # paths
    project_name = str(ui.le_id001.text())
    project_dir = str(ui.le_id002.text())
    grace_path = os.path.join(project_dir, project_name, 'grace',
                              'single_solution')
    grids_path = os.path.join(grace_path, 'grids')
    corrections_path = os.path.join(project_dir, project_name, 'corrections')
    # model names
    models = ('gldas', 'lsdm', 'wghm')
    if ui.rb_id109.isChecked():
        models += ('goco', )
    # loop over all dates
    mjd_start, mjd_end = compute_grace._date_settings(ui)
    mjd = mjd_start
    while mjd <= mjd_end:
        mjd1, mjd2 = date_functions.mjd2mjdmrange(mjd)
        mjd = (mjd2 + 1)
        ymstr = date_functions.mjd2ymstring(mjd1, '%Y-%m')[0].decode("utf-8")
        grid_file = os.path.join(grids_path, '%s-grid.txt' % (ymstr))
        for name in models:
            hydro_grid = os.path.join(data_path, name.upper(),
                                      '%s_TWS_%s.txt' % (name, ymstr))
            if name is 'goco':
                hydro_grid = os.path.join(data_path, 'GOCO', 'grids',
                                          'GOCO-%s.txt' % (ymstr))
            pointmass_file = os.path.join(
                corrections_path, name, 'grids',
                '%s_%s-pointmass.txt' % (name, ymstr))
            # positions
            if ui.rb_id050.isChecked():
                try:
                    grid = np.array(np.genfromtxt(hydro_grid, skip_header=2),
                                    ndmin=2)
                    grid = grid[pip_ix, :]
                    lon, lat, h, area_i, tws_i = grid[:,
                                                      0], grid[:,
                                                               1], grid[:,
                                                                        2], grid[:,
                                                                                 3], grid[:,
                                                                                          4]
                except:
                    continue
            elif ui.rb_id051.isChecked():
                try:
                    lon0, lat0, h0, area0, tws0 = np.genfromtxt(hydro_grid,
                                                                skip_header=2,
                                                                unpack=True)
                    lon, lat, h, area = np.genfromtxt(grid_file,
                                                      skip_header=2,
                                                      unpack=True)
                    tws_i = griddata((lon0, lat0), (tws0), (lon, lat),
                                     method=ui.cob_id003.currentText())
                    area_i = griddata((lon0, lat0), (area0), (lon, lat),
                                      method=ui.cob_id003.currentText())
                except:
                    continue
            point_count = np.size(lon)
            # area
            if ui.rb_id052.isChecked():
                area = ui.dsb_id001.value() / point_count
            elif ui.rb_id053.isChecked():
                area = (area_i * defaults.EARTH_EQUATORIAL_RADIUS() *
                        defaults.EARTH_EQUATORIAL_RADIUS()) * 1e-6
            elif ui.rb_id054.isChecked():
                area = ui.dsb_id012.value() / point_count
            # density
            density = 1025.0
            if ui.rb_id056.isChecked():
                density = ui.dsb_id013.value()
            # conversion to pointmass
            kg_i = tws_i * (area * 1e6) * density
            # write file
            groops_interface.make_grid_file(groops_bin, pointmass_file, lon,
                                            lat, h, area, *(kg_i, np.array(0)))
            logging.info('Current file: %s', pointmass_file)
    logging.info('Computing hydrological grids finished.')
Exemplo n.º 6
0
def _compute_goco_grids_spline(ui):
    logging.info('Computing GOCO grids (splines)...')
    # paths
    project_name = str(ui.le_id001.text())
    project_dir = str(ui.le_id002.text())
    grace_path = os.path.join(project_dir, project_name, 'grace',
                              'single_solution')
    grids_path = os.path.join(grace_path, 'grids')
    corrections_path = os.path.join(project_dir, project_name, 'corrections',
                                    'goco')
    # loop over all dates
    mjd_start, mjd_end = compute_grace._date_settings(ui)
    mjd = mjd_start
    while mjd <= mjd_end:
        mjd1, mjd2 = date_functions.mjd2mjdmrange(mjd)
        mjd = (mjd2 + 1)
        ymstr = date_functions.mjd2ymstring(mjd1, '%Y-%m')[0].decode("utf-8")
        grid_file = os.path.join(grids_path, '%s-grid.txt' % (ymstr))
        goco_grid = os.path.join(data_path, 'GOCO', 'grids',
                                 'GOCO-%s.txt' % (ymstr))
        pointmass_file = os.path.join(corrections_path, 'grids',
                                      'goco_%s-pointmass.txt' % (ymstr))
        # positions
        try:
            temp_dir = tempfile.mkdtemp()
            tmp_input_grid_file = os.path.join(temp_dir, 'grid_in.txt')
            tmp_output_grid_file = os.path.join(temp_dir, 'grid_out.txt')
            lon, lat, h, area = np.genfromtxt(grid_file,
                                              skip_header=2,
                                              unpack=True)
            groops_interface.make_grid_file(groops_bin, tmp_input_grid_file,
                                            lon, lat, h, area)
            groops_interface.compute_goco_grid(groops_bin, tmp_input_grid_file,
                                               tmp_output_grid_file, mjd1)
            lon, lat, h, area_i, tws_i = np.genfromtxt(tmp_output_grid_file,
                                                       skip_header=2,
                                                       unpack=True)
            shutil.rmtree(temp_dir)
            lon0, lat0, h0, area0, tws0 = np.genfromtxt(goco_grid,
                                                        skip_header=2,
                                                        unpack=True)
            area_i = griddata((lon0, lat0), (area0), (lon, lat),
                              method=ui.cob_id003.currentText())
        except:
            continue
        point_count = np.size(lon)
        # area
        if ui.rb_id052.isChecked():
            area = ui.dsb_id001.value() / point_count
        elif ui.rb_id053.isChecked():
            area = (area_i * defaults.EARTH_EQUATORIAL_RADIUS() *
                    defaults.EARTH_EQUATORIAL_RADIUS()) * 1e-6
        elif ui.rb_id054.isChecked():
            area = ui.dsb_id012.value() / point_count
        # density
        density = 1025.0
        if ui.rb_id056.isChecked():
            density = ui.dsb_id013.value()
        # conversion to pointmass
        kg_i = tws_i * (area * 1e6) * density
        # write file
        groops_interface.make_grid_file(groops_bin, pointmass_file, lon, lat,
                                        h, area, *(kg_i, np.array(0)))
        logging.info('Current file: %s', pointmass_file)
    logging.info('Computing GOCO grids (splines) done.')
Exemplo n.º 7
0
def _compute_danubia_grid(ui):
    logging.info('Computing PROMET/DANUBIA grids...')
    # paths
    project_name = str(ui.le_id001.text())
    project_dir = str(ui.le_id002.text())
    grace_path = os.path.join(project_dir, project_name, 'grace',
                              'single_solution')
    grids_path = os.path.join(grace_path, 'grids')
    corrections_path = os.path.join(project_dir, project_name, 'corrections')
    fh = Dataset(os.path.join(data_path, 'DANUBIA', 'Data-Danubia_update',
                              'Danubia.nc'),
                 mode='r')
    fh.set_auto_mask(False)
    lon0 = fh.variables['longitude'][:]
    lat0 = fh.variables['latitude'][:]
    LON0, LAT0 = np.meshgrid(lon0, lat0)
    mjd0 = fh.variables['time'][:]
    # loop over all dates
    mjd_start, mjd_end = compute_grace._date_settings(ui)
    mjd = mjd_start
    while mjd <= mjd_end:
        mjd1, mjd2 = date_functions.mjd2mjdmrange(mjd)
        mjd = (mjd2 + 1)
        ymstr = date_functions.mjd2ymstring(mjd1, '%Y-%m')[0].decode("utf-8")
        grid_file = os.path.join(grids_path, '%s-grid.txt' % (ymstr))
        pointmass_file = os.path.join(corrections_path, 'promet', 'grids',
                                      'promet_%s-pointmass.txt' % (ymstr))
        ix = np.argwhere(mjd0 == mjd)
        if not np.any(ix):
            continue
        ix = int(np.asscalar(ix))
        cr = fh.variables['channel_runoff'][ix, :]
        et = fh.variables['evapotranspiration'][ix, :]
        gs = fh.variables['glacier_snow'][ix, :]
        im = fh.variables['ice_melt'][ix, :]
        pc = fh.variables['precipitation'][ix, :]
        sm = fh.variables['soil_moisture'][ix, :]
        tr = fh.variables['total_runoff'][ix, :]
        ts = fh.variables['total_snow'][ix, :]
        tws0 = eval(ui.le_id225.text()) * 1e-3
        # mkae grid
        grid = np.zeros((np.size(tws0), 5))
        grid[:, 0] = LON0.flatten()
        grid[:, 1] = LAT0.flatten()
        grid[:, 4] = tws0.flatten()
        # positions
        if ui.rb_id050.isChecked():
            try:
                if (polygon is not None) and (pip is not None):
                    grid = grid[pip, :]
                elif (polygon is not None) and (pip is None):
                    ix = list(range(grid.shape[0]))
                    p = Pool(None)
                    pip = p.map(partial(compute_grace._in_polygon_aux,
                                        polygon=polygon,
                                        grid=grid),
                                ix,
                                chunksize=25)
                    pip = np.nonzero(pip)[0]
                    grid = grid[pip, :]
                lon, lat, h, area_i, tws_i = grid[:,
                                                  0], grid[:,
                                                           1], grid[:,
                                                                    2], grid[:,
                                                                             3], grid[:,
                                                                                      4]
            except:
                continue
        elif ui.rb_id051.isChecked():
            try:
                lon0, lat0, h0, area0, tws0 = grid[:,
                                                   0], grid[:,
                                                            1], grid[:,
                                                                     2], grid[:,
                                                                              3], grid[:,
                                                                                       4]
                lon, lat, h, area = np.genfromtxt(grid_file,
                                                  skip_header=2,
                                                  unpack=True)
                tws_i = griddata((lon0, lat0), (tws0), (lon, lat),
                                 method=ui.cob_id003.currentText())
                area_i = griddata((lon0, lat0), (area0), (lon, lat),
                                  method=ui.cob_id003.currentText())
            except:
                continue
        point_count = np.size(lon)
        # area
        if ui.rb_id052.isChecked():
            area = ui.dsb_id001.value() / point_count
        elif ui.rb_id053.isChecked():
            area = 1.0  # 1 km^2 per pixel
        elif ui.rb_id054.isChecked():
            area = ui.dsb_id012.value() / point_count
        # density
        density = 1025.0
        if ui.rb_id056.isChecked():
            density = ui.dsb_id013.value()
        # conversion to pointmass
        kg_i = tws_i * (area * 1e6) * density
        # write file
        groops_interface.make_grid_file(groops_bin, pointmass_file, lon, lat,
                                        h, area, *(kg_i, np.array(0)))
        logging.info('Current file: %s', pointmass_file)
    fh.close()
    logging.info('Computing PROMET/DANUBIA grids finished.')