예제 #1
0
 def get_precip(self):
     poly_in = self.landsat.get_tile_geometry()
     poly_in = Polygon(poly_in[0]['coordinates'][0])
     project = partial(
         pytransform,
         Proj(self.profile['crs']),
         Proj(init='epsg:32612'))
     for_bounds = partial(
         pytransform,
         Proj(self.profile['crs']),
         Proj(init='epsg:4326'))
     dates = self.scenes['DATE_ACQUIRED'].values
     # Change the coordinate system
     # The issue: the CRSs for the bounding box and for the mask are different.
     # In _project, the incorrect CRS was making it throw an error.
     # the fix? Inputting bounds in a unprojected CRS and 
     # a projected shape for masking.
     poly = transform(project, poly_in)
     poly_bounds = transform(for_bounds, poly_in)
     poly = Polygon(poly.exterior.coords)
     geometry = [mapping(poly)]
     geometry[0]['crs'] = CRS({'init': 'epsg:32612'})
     bounds = poly_bounds.bounds
     for date in dates:
         outfile = os.path.join(self.root, 'precip_{}.tif'.format(date))
         if not os.path.isfile(outfile):
             print("Get {}".format(outfile))
             d = datetime.utcfromtimestamp(date.tolist() / 1e9)  # convert to a nicer format.
             bds = GeoBounds(wsen=bounds)
             gm = GridMet(variable='pr', clip_feature=geometry,
                          bbox=bds, target_profile=self.profile, date=d)
             out = gm.get_data_subset()
             gm.save_raster(out, self.landsat.rasterio_geometry, outfile)
예제 #2
0
    def test_elevation(self):

        l8 = Landsat8(self.dir_name_LC8)
        polygon = l8.get_tile_geometry()
        bounds = RasterBounds(affine_transform=l8.rasterio_geometry['transform'], profile=l8.rasterio_geometry)
        gridmet = GridMet('elev', date=self.date, bbox=bounds,
                          target_profile=l8.rasterio_geometry, clip_feature=polygon)
        gridmet.save_raster()
        gridmet.get_data_subset(os.path.join(self.grimet_raster_dir, 'elevation.tif'))
예제 #3
0
 def get_climate_timeseries(self):
     bounds, geometry = self._get_bounds()
     print(bounds, geometry)
     dates = self.scenes['DATE_ACQUIRED'].values
     all_dates = arange(datetime(self.year, 3, 1),
                        max(dates) + 1,
                        timedelta(days=1)).astype(datetime64)
     for target in self.climate_targets:
         out_arr = None
         first = True
         last = None
         check = [
             os.path.isfile(
                 os.path.join(self.root, 'climate_rasters',
                              '{}_{}.tif'.format(q, target))) for q in dates
         ]
         if False in check:
             for date in all_dates:
                 d = datetime.utcfromtimestamp(
                     date.tolist() / 1e9)  # convert to a nicer format.
                 bds = GeoBounds(wsen=bounds)
                 gm = GridMet(variable=target,
                              clip_feature=geometry,
                              bbox=bds,
                              target_profile=self.profile,
                              date=d)
                 out = gm.get_data_subset_nonconform()
                 if first:
                     out_arr = zeros(out.shape)
                     first = False
                 out_arr += out
                 if date in dates:
                     out_dir = 'climate_rasters'
                     out_dir = os.path.join(self.root, out_dir)
                     if not os.path.isdir(out_dir):
                         os.mkdir(out_dir)
                     outfile = os.path.join(
                         out_dir, '{}_{}.tif'.format(date, target))
                     print("Saving {}".format(outfile))
                     out_final = gm.conform(out_arr)
                     gm.save_raster(out_final,
                                    self.landsat.rasterio_geometry, outfile)
                     rmtree(gm.temp_dir)