Example #1
0
    def test_skew(self):
        """ Test the skew of an image """

        test_dir = os.path.dirname(os.path.abspath(__file__))

        # read in the dem from the gold file as we're
        # comparing the arrays at the end and it will
        # ensure that the bit resolution is kept

        infile = os.path.join(
            test_dir, 'Lakes/gold_ipw/gold_dem.ipw')
        d = ipw.IPW(infile)
        gold_dem = d.bands[0].data

        for angle in range(-45, 45, 5):

            # Get the IPW gold skew values
            gold = ipw.IPW(
                os.path.join(
                    test_dir,
                    'Lakes/gold_ipw/skew/skew_{}.ipw'.format(angle)
                )
            )
            gold_data = gold.bands[0].data

            # skew the initial array
            sarr = skew(gold_dem, angle=angle, fill_min=True)
            self.assertTrue(np.array_equal(sarr, gold_data))

            # skew it back to original
            sbarr = skew(sarr, angle=angle, fwd=False)
            self.assertTrue(np.array_equal(sbarr, gold_dem))
Example #2
0
def get_timestep_ipw(tstep, input_list, ppt_list, myawsm):
    """
    Pull out a time step from the forcing files (IPW) and
    place that time step into a dict

    Args:
        tstep:      datetime of time step
        input_list: numpy array (1D) of integer time steps given
        ppt_list:   numpy array(1D) of integer time steps for ppt_list
        myawsm:     AWSM instance for current run

    Returns:
        inpt:       dictionary of forcing variable images

    """

    inpt = {}

    # map function from these values to the ones required by snobal
    map_val = {1: 'T_a', 5: 'S_n', 0: 'I_lw', 2: 'e_a', 3: 'u'}
    map_val_prec = {0: 'm_pp', 1: 'percent_snow', 2: 'rho_snow', 3: 'T_pp'}

    # get wy hour
    wyhr = int(utils.water_day(tstep)[0] * 24)
    # if we have inputs matching this water year hour
    if np.any(input_list == wyhr):
        i_in = ipw.IPW(os.path.join(myawsm.pathi, 'in.%04i' % (wyhr)))
        # assign soil temp
        inpt['T_g'] = myawsm.soil_temp * np.ones(
            (myawsm.topo.ny, myawsm.topo.nx))
        # myawsm._logger.info('T_g: {}'.format(myawsm.soil_temp))
        # inpt['T_g'] = -2.5*np.ones((myawsm.topo.ny, myawsm.topo.nx))
        for f, v in map_val.items():
            # if no solar data, give it zero
            if f == 5 and len(i_in.bands) < 6:
                # myawsm._logger.info('No solar data for {}'.format(tstep))
                inpt[v] = np.zeros((myawsm.topo.ny, myawsm.topo.nx))
            else:
                inpt[v] = i_in.bands[f].data
    # assign ppt data if there
    else:
        raise ValueError('No input time steps for {}'.format(tstep))

    if np.any(ppt_list == wyhr):
        i_ppt = ipw.IPW(os.path.join(myawsm.path_ppt, 'ppt.4b_%04i' % (wyhr)))
        for f, v in map_val_prec.items():
            inpt[v] = i_ppt.bands[f].data
    else:
        for f, v in map_val_prec.items():
            inpt[v] = np.zeros((myawsm.topo.ny, myawsm.topo.nx))

    # convert from C to K
    inpt['T_a'] += FREEZE
    inpt['T_pp'] += FREEZE
    inpt['T_g'] += FREEZE

    return inpt
Example #3
0
    def get_ipw(self):
        """
        Set init fields for iSnobal out as init file
        """
        i_in = ipw.IPW(self.init_file)
        self.init['z_0'] = i_in.bands[1].data * self.topo.mask  # snow depth

        self.logger.warning(
            'Using roughness from iSnobal ipw init file for initializing of model!'
        )

        self.init['z_s'] = i_in.bands[2].data * self.topo.mask  # snow depth
        self.init['rho'] = i_in.bands[3].data * self.topo.mask  # snow density

        self.init[
            'T_s_0'] = i_in.bands[4].data * self.topo.mask  # active layer temp

        # get bands depending on if there is a lower layer or not
        if len(i_in.bands) == 8:
            self.init['T_s_l'] = i_in.bands[
                5].data * self.topo.mask  # lower layer temp
            self.init['T_s'] = i_in.bands[
                6].data * self.topo.mask  # avgerage snow temp
            self.init['h2o_sat'] = i_in.bands[
                7].data * self.topo.mask  # percent saturation

        elif len(i_in.bands) == 7:
            self.init['T_s'] = i_in.bands[
                5].data * self.topo.mask  # avgerage snow temp
            self.init['h2o_sat'] = i_in.bands[
                6].data * self.topo.mask  # percent saturation
Example #4
0
    def write_init(self):
        """
        Write the iSnobal init file
        """
        # get mask
        mask = self.topo.mask
        # make ipw init file
        i_out = ipw.IPW()
        i_out.new_band(self.init['elevation'])
        i_out.new_band(self.init['z_0'])
        i_out.new_band(self.init['z_s'] * mask)  # snow depth
        i_out.new_band(self.init['rho'] * mask)  # snow density

        i_out.new_band(self.init['T_s_0'] * mask)  # active layer temp
        if self.start_wyhr > 0 or self.restart_crash:
            i_out.new_band(self.init['T_s_l'] * mask)  # lower layer temp

        i_out.new_band(self.init['T_s'] * mask)  # avgerage snow temp

        i_out.new_band(self.init['h2o_sat'] * mask)  # percent saturatio
        i_out.add_geo_hdr([self.topo.u, self.topo.v],
                          [self.topo.du, self.topo.dv], self.topo.units,
                          self.csys)

        i_out.write(self.fp_init, 16)
Example #5
0
    def setUpClass(cls):

        test_dir = os.path.dirname(os.path.abspath(__file__))

        # input DEM
        infile = os.path.join(test_dir, 'Lakes/gold_ipw/gold_dem.ipw')
        d = ipw.IPW(infile)

        # C code for hor1d is expecting double
        # the LQ headers have added some small values in the
        # linearization of the image
        cls.gold_dem = np.double(d.bands[0].data)
        cls.spacing = d.bands[0].dline

        # Horizon gold files
        cls.gold_file = os.path.join(test_dir,
                                     'Lakes/gold_ipw/horizon/horizon_{}.ipw')

        # to make a fair comparison, first convert the hcos to
        # integers then back using the 16 bit LQ
        # with horizon the float min/max are constant from 0-1
        cls.float_min = 1.0
        cls.float_max = 0.0
        cls.int_min = 0
        cls.int_max = 2**16 - 1
Example #6
0
    def calc_vis(self, min_storm_day, wy_day, tz_min_west, wyear, cosz,
                 azimuth):
        """
        Run ``stoporad`` for the visible bands

        Args:
            min_storm_day: decimal day of last storm for the entire basin, from
                :mod:`smrf.distribute.precip.ppt.last_storm_day_basin`
            wy_day: day of water year, from
                :mod:`~smrf.distirbute.solar.solar.radiation_dates`
            tz_min_west: time zone in minutes west from UTC, from
                :mod:`~smrf.distirbute.solar.solar.radiation_dates`
            wyear: water year, from
                :mod:`~smrf.distirbute.solar.solar.radiation_dates`
            cosz: cosine of the zenith angle for the basin, from
                :mod:`smrf.envphys.radiation.sunang`
            azimuth: azimuth to the sun for the basin, from
                :mod:`smrf.envphys.radiation.sunang`
        """
        self._logger.debug('Calculating clear sky radiation, visible')

        vis_cmd = 'stoporad -z %i -t %s -w %s -g %s -x 0.28,0.7 -s %s'\
            ' -d %s -f %i -y %i -A %f,%f -a %i -m %i -c %i -D %s > %s' \
            % (self.config['clear_opt_depth'],
               str(self.config['clear_tau']),
               str(self.config['clear_omega']),
               str(self.config['clear_gamma']),
               str(min_storm_day),
               str(wy_day),
               tz_min_west,
               wyear,
               cosz,
               azimuth,
               self.albedoConfig['grain_size'],
               self.albedoConfig['max_grain'],
               self.albedoConfig['dirt'],
               self.stoporad_in,
               self.vis_file)
        #         self._logger.debug(vis_cmd)

        visp = sp.Popen(vis_cmd,
                        shell=True,
                        env={
                            "PATH": os.environ['PATH'],
                            "WORKDIR": os.environ['WORKDIR']
                        })

        stdoutdata, stderrdata = visp.communicate()

        if visp.returncode != 0:
            raise Exception('Clear sky for visible failed')

        # load clear sky files back in
        vis = ipw.IPW(self.vis_file)
        clear_vis_beam = vis.bands[0].data
        clear_vis_diffuse = vis.bands[1].data

        return clear_vis_beam, clear_vis_diffuse
Example #7
0
    def get_ipw_out(self):
        """
        Set init fields for iSnobal out as init file
        """
        i_in = ipw.IPW(self.init_file)
        self.init['z_s'] = i_in.bands[0].data * self.topo.mask  # snow depth
        self.init['rho'] = i_in.bands[1].data * self.topo.mask  # snow density

        self.init[
            'T_s_0'] = i_in.bands[4].data * self.topo.mask  # active layer temp
        self.init[
            'T_s_l'] = i_in.bands[5].data * self.topo.mask  # lower layer temp
        self.init[
            'T_s'] = i_in.bands[6].data * self.topo.mask  # avgerage snow temp

        self.init['h2o_sat'] = i_in.bands[
            8].data * self.topo.mask  # percent saturation
Example #8
0
    def run_horizon(self, azimuth):
        """Run the horizon function for a given azimuth and
        compare with the gold file

        Arguments:
            azimuth {float} -- azimuth to test
        """

        hcos = horizon(azimuth, self.gold_dem, self.spacing)

        # convert the numpy array from a float, to int and back
        h_int = ipw.map_fn(hcos, self.float_min, self.float_max, self.int_min,
                           self.int_max)
        h_float = self.int_to_float(h_int)

        gold = ipw.IPW(self.gold_file.format(azimuth))
        gold_data = gold.bands[0].data

        return gold_data, h_float
Example #9
0
    def stoporadInput(self):
        """
        Calculate the necessary input file for stoporad
        The IPW and WORKDIR environment variables must be set
        """
        # if self.topoConfig['type'] != 'ipw':
        #
        f = os.path.abspath(
            os.path.expanduser(os.path.join(self.tempDir, 'dem.ipw')))
        i = ipw.IPW()
        i.new_band(self.dem)

        i.add_geo_hdr([self.x[0], self.y[0]],
                      [np.mean(np.diff(self.x)),
                       np.mean(np.diff(self.y))], 'm', 'UTM')
        i.write(f, 16)

        self.topoConfig['dem'] = f

        # calculate the skyview
        svfile = os.path.abspath(
            os.path.expanduser(os.path.join(self.tempDir, 'sky_view.ipw')))
        self._logger.debug('sky view file - %s' % svfile)

        # calculate the gradient
        gfile = os.path.abspath(
            os.path.expanduser(os.path.join(self.tempDir, 'gradient.ipw')))
        self._logger.debug('gradient file - %s' % gfile)

        if self.threaded:

            ts = Process(target=self._viewf,
                         args=(self.topoConfig['dem'], svfile))
            ts.start()

            tg = Process(target=self._gradient,
                         args=(self.topoConfig['dem'], gfile))
            tg.start()

            # wait for the processes to stop
            tg.join()
            ts.join()

        else:
            self._viewf(self.topoConfig['dem'], svfile)
            self._gradient(self.topoConfig['dem'], gfile)

        # combine into a value
        sfile = os.path.abspath(
            os.path.expanduser(os.path.join(self.tempDir, 'stoporad_in.ipw')))
        self._logger.debug('stoporad in file - %s' % sfile)

        cmd = 'mux %s %s %s > %s' % (self.topoConfig['dem'], gfile, svfile,
                                     sfile)
        proc = sp.Popen(cmd, shell=True).wait()

        if proc != 0:
            raise OSError('mux for stoporad_in.ipw failed')

        # read in the stoporad file to store in memory
        self.stoporad_in = ipw.IPW(sfile)
        self.stoporad_in_file = sfile
        self.slope = self.stoporad_in.bands[1].data.astype(np.float64)
        self.aspect = self.stoporad_in.bands[2].data.astype(np.float64)
        self.sky_view = self.stoporad_in.bands[3].data.astype(np.float64)

        # clean up the WORKDIR
        os.remove(gfile)
        os.remove(svfile)

        os.remove(self.topoConfig['dem'])
        self.topoConfig.pop('dem', None)
Example #10
0
    def do_update_isnobal(self, myawsm, update_info, update_snow, x, y):
        """
        Function to read in an output file and update it with the lidar depth field

        Argument:
            myawsm: instantiated awsm class
            update_info: update info pandas at correct index
            update_snow: file pointer to snow update image
            x: x vector
            y: y vector
        Returns:
            init_file: file pointer to init image
        """
        # get some info
        update_number = update_info['number']
        date = update_info['date_time']
        wyhr = update_info['wyhr']

        last_snow_image = ipw.IPW(update_snow)
        z_s = last_snow_image.bands[0].data  # Get modeled depth image.
        # z_s(mask==0) = NaN;

        ##  Continue as before:
        density = last_snow_image.bands[1].data.copy()  # Get density image.
        ## ## ## ## ## ## ## ## ## %
        # SPECIAL CASE... insert adjusted densities here:
        # density = arcgridread_v2(['/Volumes/data/blizzard/Tuolumne/lidar/snowon/2017' ...
        #                 '/adjusted_rho/TB2017' date_mmdd '_operational_rho_ARSgrid_50m.asc']);
        ## ## ## ## ## ## ## ## ## %
        m_s = last_snow_image.bands[2].data.copy()  # Get SWE image.
        T_s_0 = last_snow_image.bands[4].data.copy(
        )  # Get active snow layer temperature image
        T_s_l = last_snow_image.bands[5].data.copy(
        )  # Get lower snow layer temperature image
        T_s = last_snow_image.bands[6].data.copy(
        )  # Get average snowpack temperature image
        h2o_sat = last_snow_image.bands[8].data.copy(
        )  # Get liquid water saturation image

        updated_fields = self.hedrick_updating_procedure(
            m_s, T_s_0, T_s_l, T_s, h2o_sat, density, z_s, x, y, update_info)

        # write init file
        out_file = 'init_update_{}_wyhr{:04d}.ipw'.format(update_number, wyhr)
        init_file = os.path.join(self.pathinit, out_file)
        i_out = ipw.IPW()
        i_out.new_band(updated_fields['dem'])
        i_out.new_band(updated_fields['z0'])
        i_out.new_band(updated_fields['D'])
        i_out.new_band(updated_fields['rho'])
        i_out.new_band(updated_fields['T_s_0'])
        i_out.new_band(updated_fields['T_s_l'])
        i_out.new_band(updated_fields['T_s'])
        i_out.new_band(updated_fields['h2o_sat'])
        #i_out.add_geo_hdr([u, v], [du, dv], units, csys)
        i_out.add_geo_hdr([self.topo.u, self.topo.v],
                          [self.topo.du, self.topo.dv], self.topo.units,
                          self.csys)
        i_out.write(init_file, self.nbits)

        ##  Import newly-created init file and look at images to make sure they line up:
        self._logger.info('Wrote ipw image for update {}'.format(wyhr))

        return init_file
Example #11
0
def ipw2nc_mea(myawsm, runtype):
    '''
    Function to create netcdf files from iSnobal output. Reads the snow and em
    ouptuts in the 'output' folder and stores them in snow.nc and em.nc one
    directory up.

    Args:
        myawsm: AWSM instance
        runtype: either 'smrf' for standard run or 'forecast' for gridded data run
    '''
    myawsm._logger.info("making the NetCDF files from ipw"
                        " files for {}".format(runtype))

    if runtype != 'smrf' and runtype != 'forecast':
        myawsm._logger.error('Wrong run type given to ipw2nc. '
                             'not smrf or forecast')
        sys.exit()

    myawsm._logger.info("convert all .ipw output files to netcdf files")
    #######################################################################
    # Convert all .ipw output files to netcdf files #####################
    #######################################################################
    time_zone = myawsm.tmz
    # create the x,y vectors
    x = myawsm.topo.x
    y = myawsm.topo.y

    # ========================================================================
    # NetCDF EM image
    # ========================================================================
    m = {}
    m['name'] = [
        'net_rad', 'sensible_heat', 'latent_heat', 'snow_soil',
        'precip_advected', 'sum_EB', 'evaporation', 'snowmelt', 'SWI',
        'cold_content'
    ]
    m['units'] = [
        'W m-2', 'W m-2', 'W m-2', 'W m-2', 'W m-2', 'W m-2', 'kg m-2',
        'kg m-2', 'kg or mm m-2', 'J m-2'
    ]
    m['description'] = [
        'Average net all-wave radiation', 'Average sensible heat transfer',
        'Average latent heat exchange', 'Average snow/soil heat exchange',
        'Average advected heat from precipitation',
        'Average sum of EB terms for snowcover', 'Total evaporation',
        'Total snowmelt', 'Total runoff', 'Snowcover cold content'
    ]

    if runtype == 'smrf':
        netcdfFile = os.path.join(myawsm.pathrr, 'em.nc')
    elif runtype == 'forecast':
        netcdfFile = os.path.join(myawsm.pathrr, 'em_forecast.nc')

    dimensions = ('time', 'y', 'x')
    em = nc.Dataset(netcdfFile, 'w')

    # create the dimensions
    em.createDimension('time', None)
    em.createDimension('y', myawsm.topo.ny)
    em.createDimension('x', myawsm.topo.nx)

    # create some variables
    em.createVariable('time', 'f', dimensions[0])
    em.createVariable('y', 'f', dimensions[1])
    em.createVariable('x', 'f', dimensions[2])

    setattr(em.variables['time'], 'units', 'hours since %s' % myawsm.wy_start)
    setattr(em.variables['time'], 'calendar', 'standard')
    setattr(em.variables['time'], 'time_zone', time_zone)
    em.variables['x'][:] = x
    em.variables['y'][:] = y

    # em image
    for i, v in enumerate(m['name']):
        em.createVariable(v, 'f', dimensions[:3], chunksizes=(24, 10, 10))
        setattr(em.variables[v], 'units', m['units'][i])
        setattr(em.variables[v], 'description', m['description'][i])

    em.setncattr_string('source', 'AWSM {}'.format(myawsm.gitVersion))
    # ========================================================================
    # NetCDF SNOW image
    # ========================================================================

    s = {}
    s['name'] = [
        'thickness', 'snow_density', 'specific_mass', 'liquid_water',
        'temp_surf', 'temp_lower', 'temp_snowcover', 'thickness_lower',
        'water_saturation'
    ]
    s['units'] = [
        'm', 'kg m-3', 'kg m-2', 'kg m-2', 'C', 'C', 'C', 'm', 'percent'
    ]
    s['description'] = [
        'Predicted thickness of the snowcover',
        'Predicted average snow density',
        'Predicted specific mass of the snowcover',
        'Predicted mass of liquid water in the snowcover',
        'Predicted temperature of the surface layer',
        'Predicted temperature of the lower layer',
        'Predicted temperature of the snowcover',
        'Predicted thickness of the lower layer',
        'Predicted percentage of liquid water'
        ' saturation of the snowcover'
    ]

    if runtype == 'smrf':
        netcdfFile = os.path.join(myawsm.pathrr, 'snow.nc')
    elif runtype == 'forecast':
        netcdfFile = os.path.join(myawsm.pathrr, 'snow_forescast.nc')

    dimensions = ('time', 'y', 'x')
    snow = nc.Dataset(netcdfFile, 'w')

    # create the dimensions
    snow.createDimension('time', None)
    snow.createDimension('y', myawsm.topo.ny)
    snow.createDimension('x', myawsm.topo.nx)

    # create some variables
    snow.createVariable('time', 'f', dimensions[0])
    snow.createVariable('y', 'f', dimensions[1])
    snow.createVariable('x', 'f', dimensions[2])

    setattr(snow.variables['time'], 'units',
            'hours since %s' % myawsm.wy_start)
    setattr(snow.variables['time'], 'calendar', 'standard')
    setattr(snow.variables['time'], 'time_zone', time_zone)
    snow.variables['x'][:] = x
    snow.variables['y'][:] = y

    # snow image
    for i, v in enumerate(s['name']):

        snow.createVariable(v, 'f', dimensions[:3], chunksizes=(6, 10, 10))
        setattr(snow.variables[v], 'units', s['units'][i])
        setattr(snow.variables[v], 'description', s['description'][i])

    h = '[{}] Data added or updated'.format(datetime.now().strftime("%Y%m%d"))
    snow.setncattr_string('last modified', h)
    snow.setncattr_string('AWSM version', myawsm.gitVersion)
    if myawsm.do_smrf:
        snow.setncattr_string('SMRF version', myawsm.smrf_version)

    # =======================================================================
    # Get all files in the directory, open ipw file, and add to netCDF
    # =======================================================================

    # get all the files in the directory
    d = sorted(glob.glob("%s/snow*" % myawsm.pathro), key=os.path.getmtime)

    d.sort(key=lambda f: os.path.splitext(f))
    # find a drop any netcdfs in directory
    d = [ddp for ddp in d if '.nc' not in ddp]
    # pbar = progressbar.ProgressBar(max_value=len(d)).start()
    j = 0

    for idf, f in enumerate(d):
        # print out counter at certain percentages. pbar doesn't play nice
        # with logging
        if j == int(len(d) / 4):
            myawsm._logger.info("25 percent finished with "
                                "making NetCDF files!")
        if j == int(len(d) / 2):
            myawsm._logger.info("50 percent finished with "
                                "making NetCDF files!")
        if j == int(3 * len(d) / 4):
            myawsm._logger.info("75 percent finished with "
                                "making NetCDF files!")

        # get the hr
        nm = os.path.basename(f)
        head = os.path.dirname(f)
        hr = int(nm.split('.')[1])
        # hr = int(hr)
        snow.variables['time'][j] = hr  # +1
        em.variables['time'][j] = hr  # +1

        # Read the IPW file
        i = ipw.IPW(f)

        # output to the snow netcdf file
        for b, var in enumerate(s['name']):
            snow.variables[var][j, :] = i.bands[b].data

        # output to the em netcdf file
        # emFile = "%s/%s.%04i" % (head, 'em', hr)
        emFile = os.path.join(head, 'em.%04i' % (hr))
        i_em = ipw.IPW(emFile)
        for b, var in enumerate(m['name']):
            em.variables[var][j, :] = i_em.bands[b].data

        snow.setncattr_string('last modified', h)
        snow.setncattr_string('AWSM version', myawsm.gitVersion)
        if myawsm.do_smrf:
            snow.setncattr_string('SMRF version', myawsm.smrf_version)

        em.sync()
        snow.sync()
        j += 1

        # pbar.update(j)
    # pbar.finish()
    snow.close()
    em.close()

    myawsm._logger.info("Finished making the NetCDF "
                        "files from iSnobal output!")
Example #12
0
def nc2ipw_mea(myawsm, runtype):
    '''
    Function to create iSnobal forcing and precip images from smrf ouputs. Reads
    in all of the SMRF forcing outputs and converts them into input and ppt
    images in the 'input' and 'ppt_4b' directories. Also writes the  ppt_desc
    file.

    Args:
        myawsm: AWSM instance
        runtype: either 'smrf' for standard run or 'forecast' for gridded data run
    '''
    ################################################################
    # Make .ipw input files from netCDF files ####################
    ################################################################
    myawsm._logger.info("making the ipw files"
                        " from NetCDF files for {}".format(runtype))

    if runtype != 'smrf' and runtype != 'forecast':
        myawsm._logger.error('Wrong run type given to nc2ipw. '
                             'not smrf or forecast')
        sys.exit()

    tt = myawsm.start_date - myawsm.wy_start
    smrfpath = myawsm.paths
    datapath = myawsm.pathdd
    f = open(myawsm.ppt_desc, 'w')

    offset = tt.days * 24 + tt.seconds // 3600  # start index for the input file

    # File paths
    th = os.path.join(smrfpath, 'thermal.nc')
    th_var = 'thermal'
    ta = os.path.join(smrfpath, 'air_temp.nc')
    ta_var = 'air_temp'
    ea = os.path.join(smrfpath, 'vapor_pressure.nc')
    ea_var = 'vapor_pressure'
    wind = os.path.join(smrfpath, 'wind_speed.nc')
    wind_var = 'wind_speed'
    sn = os.path.join(smrfpath, 'net_solar.nc')
    sn_var = 'net_solar'

    in_path = os.path.join(datapath, 'input/')

    mp = os.path.join(smrfpath, 'precip.nc')
    mp_var = 'precip'
    ps = os.path.join(smrfpath, 'percent_snow.nc')
    ps_var = 'percent_snow'
    rho = os.path.join(smrfpath, 'snow_density.nc')
    rho_var = 'snow_density'
    tp = os.path.join(smrfpath, 'precip_temp.nc')
    tp_var = 'precip_temp'

    in_pathp = os.path.join(datapath, 'ppt_4b')

    th_file = nc.Dataset(th, 'r')
    ta_file = nc.Dataset(ta, 'r')
    ea_file = nc.Dataset(ea, 'r')
    wind_file = nc.Dataset(wind, 'r')
    sn_file = nc.Dataset(sn, 'r')
    mp_file = nc.Dataset(mp, 'r')
    ps_file = nc.Dataset(ps, 'r')
    rho_file = nc.Dataset(rho, 'r')
    tp_file = nc.Dataset(tp, 'r')

    N = th_file.variables[th_var].shape[0]
    # timeStep = np.arange(0,N)        # timesteps loop through
    timeStep = np.arange(offset, N + offset)  # timesteps loop through
    # pbar = progressbar.ProgressBar(max_value=len(timeStep)).start()
    j = 0
    for idxt, t in enumerate(timeStep):

        if j == int(len(timeStep) / 4):
            myawsm._logger.info("25 percent finished with "
                                "making IPW input files!")
        if j == int(len(timeStep) / 2):
            myawsm._logger.info("50 percent finished with "
                                "making IPW input files!")
        if j == int(3 * len(timeStep) / 4):
            myawsm._logger.info("75 percent finished with "
                                "making IPW input files!")

        # print('idxt: {} t: {}'.format(idxt, t))
        trad_step = th_file.variables[th_var][idxt, :]
        ta_step = ta_file.variables[ta_var][idxt, :]
        ea_step = ea_file.variables[ea_var][idxt, :]
        wind_step = wind_file.variables[wind_var][idxt, :]
        sn_step = sn_file.variables[sn_var][idxt, :]
        mp_step = mp_file.variables[mp_var][idxt, :]
        tg_step = np.ones_like(mp_step) * (myawsm.soil_temp)  # ground temp

        in_step = os.path.join(in_path, 'in.%04i' % (t))

        i = ipw.IPW()
        i.new_band(trad_step)
        i.new_band(ta_step)
        i.new_band(ea_step)
        i.new_band(wind_step)
        i.new_band(tg_step)

        # add solar if the sun is up
        if np.sum(sn_step) > 0:
            i.new_band(sn_step)

        i.add_geo_hdr([myawsm.topo.u, myawsm.topo.v],
                      [myawsm.topo.du, myawsm.topo.dv], myawsm.topo.units,
                      myawsm.csys)
        i.write(in_step, myawsm.nbits)

        # only output if precip
        if np.sum(mp_step) > 0:
            ps_step = ps_file.variables[ps_var][idxt, :]
            rho_step = rho_file.variables[rho_var][idxt, :]
            tp_step = tp_file.variables[tp_var][idxt, :]
            in_stepp = os.path.join(os.path.abspath(in_pathp),
                                    'ppt.4b_%04i' % (t))
            i = ipw.IPW()
            i.new_band(mp_step)
            i.new_band(ps_step)
            i.new_band(rho_step)
            i.new_band(tp_step)
            i.add_geo_hdr([myawsm.topo.u, myawsm.topo.v],
                          [myawsm.topo.du, myawsm.topo.dv], myawsm.topo.units,
                          myawsm.csys)
            i.write(in_stepp, myawsm.nbits)
            f.write('%i %s\n' % (t, in_stepp))

        j += 1
        # pbar.update(j)

    th_file.close()
    ta_file.close()
    ea_file.close()
    wind_file.close()
    sn_file.close()
    mp_file.close()
    ps_file.close()
    rho_file.close()
    tp_file.close()
    f.close()
    # pbar.finish()
    myawsm._logger.info("finished making the ipw "
                        "input and ppt files from NetCDF files")
Example #13
0
# double percision error for the input

# Load the netcdf dem and get all the geo coords for IPW
topo_path = './tests/Lakes/topo.nc'

d = nc.Dataset(topo_path)
dem = np.round(d.variables['dem'][:])
d.close()

ts = get_topo_stats(topo_path)

csys = 'UTM'
nbits = 16

# Create the IPW image
i = ipw.IPW()
i.new_band(dem)
i.add_geo_hdr(coordinates=[ts['u'], ts['v']],
              d=[ts['dv'], ts['du']],
              units=ts['units'],
              csys=csys)

i.write('./tests/Lakes/gold_ipw/gold_dem.ipw', nbits=nbits)

# Create a gradient ipw image (slope and aspect)
# in both 8 and 16 bit resolution
cmd = "gradient -i {0} ./tests/Lakes/gold_ipw/gold_dem.ipw >" \
    "./tests/Lakes/gold_ipw/gold_gradient{0}.ipw"

for nbit in [8, 16]:
    with subprocess.Popen(cmd.format(nbit),
Example #14
0
maxus_file = 'maxus690_%i.asc'
var = 'maxus'
nFile = 'maxus.nc'

# maxus_file = 'tbreak90_%i.asc'
# var = 'tbreak'
# nFile = 'tbreak.nc'

maxusInd = range(0, 360, 5)

#------------------------------------------------------------------------------
# include the geohdr
# execfile('/media/Drobo1/BRB/BRB-wy09/grid_info.py')
dem = '/media/Drobo1/BRB/BRB-wy09/spatial_WRF_OG/data/topo/dem30m.ipw'
dem = ipw.IPW(dem)

nx = dem.nsamps
ny = dem.nlines
v = dem.bands[0].bsamp
dv = dem.bands[0].dsamp
u = dem.bands[0].bline
du = dem.bands[0].dline

# files = glob.glob(maxus_dir)
nfiles = len(maxusInd)

#===============================================================================
# Create netCDF file
#===============================================================================