Beispiel #1
0
    def convert(self, img_file):
        """
        Convert a raw GFMS image into a GeoTIFF
        :param img_file: Name of raw image file from GFMS
        (assumed to be in temp directory)
        :return: Name of converted GeoTIFF file
        """
        basename = os.path.splitext(img_file)[0]
        aig_file = "{}.aig".format(basename)
        tif_file = "{}.tif".format(basename)

        outfile = open(os.path.join(self.tmp_dir, aig_file), "w")
        infile = open(os.path.join(self.tmp_dir, img_file), "rb")

        try:
            coords = struct.unpack('f'*self.rows*self.cols,
                                   infile.read(4*self.rows*self.cols))
            outfile.write(self.header)

            for idx, value in enumerate(coords):
                outfile.write(str(value) + " ")
                if (idx + 1) % 2458 == 0:
                    outfile.write("\n")
        finally:
            outfile.close()
            infile.close()

        gdal_translate(os.path.join(self.tmp_dir, aig_file),
                       os.path.join(self.tmp_dir, tif_file),
                       projection="EPSG:4326")
        return tif_file
Beispiel #2
0
    def convert(self, img_file):
        """
        Convert a raw GFMS image into a GeoTIFF
        :param img_file: Name of raw image file from GFMS
        (assumed to be in temp directory)
        :return: Name of converted GeoTIFF file
        """
        basename = os.path.splitext(img_file)[0]
        aig_file = "{}.aig".format(basename)
        tif_file = "{}.tif".format(basename)

        outfile = open(os.path.join(self.tmp_dir, aig_file), "w")
        infile = open(os.path.join(self.tmp_dir, img_file), "rb")

        try:
            coords = struct.unpack('f' * self.rows * self.cols,
                                   infile.read(4 * self.rows * self.cols))
            outfile.write(self.header)

            for idx, value in enumerate(coords):
                outfile.write(str(value) + " ")
                if (idx + 1) % 2458 == 0:
                    outfile.write("\n")
        finally:
            outfile.close()
            infile.close()

        gdal_translate(os.path.join(self.tmp_dir, aig_file),
                       os.path.join(self.tmp_dir, tif_file),
                       projection="EPSG:4326")
        return tif_file
Beispiel #3
0
 def extract_band(self, tif, band, outname):
     outfile = os.path.join(self.tmp_dir, outname)
     gdal_translate(tif,
                    outfile,
                    bands=[band],
                    projection='EPSG:4326',
                    options=['TILED=YES', 'COMPRESS=LZW'])
     return outfile
Beispiel #4
0
 def convert(self, nc_file):
     tif_file = "{}.tif".format(nc_file)
     nc_transform = nc_convert(os.path.join(self.tmp_dir, nc_file))
     cdo_transform = cdo_invert(os.path.join(self.tmp_dir, nc_transform))
     band = get_band_count(cdo_transform)
     gdal_translate(cdo_transform, os.path.join(self.tmp_dir, tif_file),
                    bands=[band], projection='EPSG:4326')
     return tif_file
Beispiel #5
0
 def convert(self, nc_file):
     tif_file = "{}.tif".format(nc_file)
     nc_transform = nc_convert(os.path.join(self.tmp_dir, nc_file))
     cdo_transform = cdo_invert(os.path.join(self.tmp_dir, nc_transform))
     band = get_band_count(cdo_transform)
     gdal_translate(cdo_transform,
                    os.path.join(self.tmp_dir, tif_file),
                    bands=[band],
                    projection='EPSG:4326')
     return tif_file
Beispiel #6
0
    def convert(self, tif_file):
        layer_title, imgtime = self.parse_name(tif_file)
        time_format = imgtime.strftime('%Y%m%dT%H0000000Z')
        tif_out = "{prefix}_{time}.tif".format(prefix=self.layer_name,
                                               time=time_format)
        # Use gdal_translate to embed projection info
        gdal_translate(os.path.join(self.tmp_dir, tif_file),
                       os.path.join(self.tmp_dir, tif_out),
                       nodata=0,
                       projection="EPSG:4326")

        return tif_out
Beispiel #7
0
    def convert(self, tif_file):
        layer_title, imgtime = self.parse_name(tif_file)
        time_format = imgtime.strftime('%Y%m%dT%H0000000Z')
        tif_out = "{prefix}_{time}.tif".format(
            prefix=self.layer_name,
            time=time_format)
        # Use gdal_translate to embed projection info
        gdal_translate(os.path.join(self.tmp_dir, tif_file),
                       os.path.join(self.tmp_dir, tif_out),
                       nodata=0, projection="EPSG:4326")

        return tif_out
Beispiel #8
0
 def process(self):
     for var, res, rcp, year, gcm in itertools.product(
             self.climate_vars,
             self.resolutions,
             self.rcps,
             self.years,
             self.gcms):
         outdir = os.path.join(
             self.tmp_dir, self.prefix, 'future', var[0], res)
         dl_name = "{}_{}_{}_{}_{}_{}.zip".format(
             self.prefix, gcm[0], rcp, var[0], year, res)
         print dl_name
         if not os.path.exists(os.path.join(self.tmp_dir, dl_name)):
             try:
                 self.download(self.base_url.format(
                     rcp=rcp, gcm=gcm[0], res=res, yr=str(year)[2:],
                     var=var[0]), dl_name)
             except HTTPError:
                 continue
         try:
             ZipFile(os.path.join(
                 self.tmp_dir, dl_name)).extractall(path=outdir)
             layer_name = "WorldClim {rcp} conditions w/{gcm} GCM for " + \
                          "{yr} at RCP {rcp}: {var}, {res} resolution"
             varcount = 20 if var[0] == 'bi' else 13
             var_iterator = range(1, varcount)
             for v in var_iterator:
                 name = '{}_{}_{}_{}{}_{}_{}'.format(
                     self.prefix, rcp, gcm[0], var[0],
                     v, year, res)
                 original_tif = os.path.join(
                     outdir, '{}{}{}{}{}.tif'.format(
                         gcm[0], rcp, var[0], str(year)[2:], v))
                 tif = original_tif.replace('.tif', '_4326.tif')
                 gdal_translate(original_tif,
                                tif,
                                projection='EPSG:4326',
                                options=['COMPRESS=DEFLATE'])
                 title = layer_name.format(
                     rcp=float(rcp/10),
                     gcm=gcm[1],
                     var=self.custom_title(var, v),
                     yr=year,
                     res=res.replace('-', '.'))
                 desc = (self.base_description + self.desc).format(
                     rcp=float(rcp/10), gcm=gcm[1], yr=year,
                     res=res.replace('-', '.')
                 )
                 self.publish(tif, name, title, desc)
         finally:
             self.cleanup(outdir)
Beispiel #9
0
 def process(self):
     for var, res, rcp, year, gcm in itertools.product(
             self.climate_vars, self.resolutions, self.rcps, self.years,
             self.gcms):
         outdir = os.path.join(self.tmp_dir, self.prefix, 'future', var[0],
                               res)
         dl_name = "{}_{}_{}_{}_{}_{}.zip".format(self.prefix, gcm[0], rcp,
                                                  var[0], year, res)
         print dl_name
         if not os.path.exists(os.path.join(self.tmp_dir, dl_name)):
             try:
                 self.download(
                     self.base_url.format(rcp=rcp,
                                          gcm=gcm[0],
                                          res=res,
                                          yr=str(year)[2:],
                                          var=var[0]), dl_name)
             except HTTPError:
                 continue
         try:
             ZipFile(os.path.join(self.tmp_dir,
                                  dl_name)).extractall(path=outdir)
             layer_name = "WorldClim {rcp} conditions w/{gcm} GCM for " + \
                          "{yr} at RCP {rcp}: {var}, {res} resolution"
             varcount = 20 if var[0] == 'bi' else 13
             var_iterator = range(1, varcount)
             for v in var_iterator:
                 name = '{}_{}_{}_{}{}_{}_{}'.format(
                     self.prefix, rcp, gcm[0], var[0], v, year, res)
                 original_tif = os.path.join(
                     outdir,
                     '{}{}{}{}{}.tif'.format(gcm[0], rcp, var[0],
                                             str(year)[2:], v))
                 tif = original_tif.replace('.tif', '_4326.tif')
                 gdal_translate(original_tif,
                                tif,
                                projection='EPSG:4326',
                                options=['COMPRESS=DEFLATE'])
                 title = layer_name.format(rcp=float(rcp / 10),
                                           gcm=gcm[1],
                                           var=self.custom_title(var, v),
                                           yr=year,
                                           res=res.replace('-', '.'))
                 desc = (self.base_description + self.desc).format(
                     rcp=float(rcp / 10),
                     gcm=gcm[1],
                     yr=year,
                     res=res.replace('-', '.'))
                 self.publish(tif, name, title, desc)
         finally:
             self.cleanup(outdir)
Beispiel #10
0
 def convert(self, dl_file, imgtime):
     """
     Set the correct projection on the image and save as a GeoTIFF.
     """
     tif_file = "{prefix}_{year}{month}{day}T{hour}0000000Z.tif".format(
         prefix=self.prefix,
         year=str(imgtime.year),
         month='{0:02d}'.format(imgtime.month),
         day='{0:02d}'.format(imgtime.day), hour='{0:02d}'.format(
             imgtime.hour))
     gdal_translate(os.path.join(self.tmp_dir, dl_file),
                    os.path.join(self.tmp_dir, tif_file),
                    projection='EPSG:4326',
                    options=['COMPRESS=DEFLATE'])
     return tif_file
Beispiel #11
0
    def process(self):

        for var in self.climate_vars:
            for res in self.resolutions:
                outdir = os.path.join(
                    self.tmp_dir, self.prefix, 'current', var[0], res)
                dl_zips = [[var][0][0]]
                if res == '30s' and [var][0][0] == 'bio':
                    dl_zips = ['bio1-9', 'bio10-19']
                for dl in dl_zips:
                    dl_name = "{}_{}_{}_{}.zip".format(self.prefix, "current",
                                                       dl, res)
                    if not os.path.exists(os.path.join(self.tmp_dir, dl_name)):
                        self.download(self.base_url.format(
                            self.version, dl, res), dl_name)
                try:
                    ZipFile(os.path.join(
                        self.tmp_dir, dl_name)).extractall(path=outdir)
                    layer_name = \
                        "WorldClim current conditions: {var}, {res} resolution"

                    varcount = 20 if var[0] == 'bio' else 13
                    var_iterator = range(1, varcount)

                    for v in var_iterator:
                        name = '{}_cur_{}{}_{}'.format(
                            self.prefix, var[0], v, res)
                        bil = os.path.join(
                            outdir, '{}{}.bil'.format(var[0], v))
                        tif = bil.replace('.bil', '.tif')
                        gdal_translate(bil,
                                       tif,
                                       projection='EPSG:4326',
                                       options=['COMPRESS=DEFLATE'])
                        title = layer_name.format(
                            var=self.custom_title(var, v),
                            res=res.replace('-', '.'))
                        desc = (self.base_description + self.desc).format(
                            self.custom_title(var, v)
                        )
                        self.publish(tif, name, title, desc)
                finally:
                    self.cleanup(outdir)
Beispiel #12
0
    def process(self):

        for var in self.climate_vars:
            for res in self.resolutions:
                outdir = os.path.join(self.tmp_dir, self.prefix, 'current',
                                      var[0], res)
                dl_zips = [[var][0][0]]
                if res == '30s' and [var][0][0] == 'bio':
                    dl_zips = ['bio1-9', 'bio10-19']
                for dl in dl_zips:
                    dl_name = "{}_{}_{}_{}.zip".format(self.prefix, "current",
                                                       dl, res)
                    if not os.path.exists(os.path.join(self.tmp_dir, dl_name)):
                        self.download(
                            self.base_url.format(self.version, dl, res),
                            dl_name)
                try:
                    ZipFile(os.path.join(self.tmp_dir,
                                         dl_name)).extractall(path=outdir)
                    layer_name = \
                        "WorldClim current conditions: {var}, {res} resolution"

                    varcount = 20 if var[0] == 'bio' else 13
                    var_iterator = range(1, varcount)

                    for v in var_iterator:
                        name = '{}_cur_{}{}_{}'.format(self.prefix, var[0], v,
                                                       res)
                        bil = os.path.join(outdir,
                                           '{}{}.bil'.format(var[0], v))
                        tif = bil.replace('.bil', '.tif')
                        gdal_translate(bil,
                                       tif,
                                       projection='EPSG:4326',
                                       options=['COMPRESS=DEFLATE'])
                        title = layer_name.format(var=self.custom_title(
                            var, v),
                                                  res=res.replace('-', '.'))
                        desc = (self.base_description + self.desc).format(
                            self.custom_title(var, v))
                        self.publish(tif, name, title, desc)
                finally:
                    self.cleanup(outdir)
Beispiel #13
0
 def extract_band(self, tif, band, outname):
     outfile = os.path.join(self.tmp_dir, outname)
     gdal_translate(tif, outfile, bands=[band],
                    projection='EPSG:4326',
                    options=['TILED=YES', 'COMPRESS=LZW'])
     return outfile