OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
cyan_alaska_tiles.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import argparse
4 from osgeo import gdal
5 from affine import Affine
6 import sys
7 
8 __version__ = '1.1'
9 # CONUS tile extents - Albers conic projection
10 # tile-index (in meters)
11 # h v ulx/lrx uly/lry
12 # 0 0 -851715 2474325
13 # 16 13 1698285 374325
14 #
15 # for the 4x4 grid, the lrx/lry are 1683285 and 404325
16 # ...unless I have x and y backward...
17 # the numbers passed to xy2rc work, so *meh*
18 parser = argparse.ArgumentParser(prog='cyan_conus_tiles',
19  description='This script generates 12 GeoTIFF tiles in a 4x3 grid from an input full\nAlaskan GeoTIFF\n' +
20  'The output tile names are based on the input file name with\n'+
21  '_row_col.tif appended')
22 parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
23 parser.add_argument('ifile',help='input Alaskan GeoTIFF file to slice and dice')
24 parser.add_argument('--verbose', '-v', action='store_true')
25 
26 args = parser.parse_args()
27 
28 nXtiles = 4
29 nYtiles = 3
30 
31 sourcefile = args.ifile
32 sourcefilebase = sourcefile.rstrip('.tif')
33 #Open source dataset
34 srcDS = gdal.Open(sourcefile)
35 #Find our starting point
36 T0 = Affine.from_gdal(*srcDS.GetGeoTransform())
37 xy2rc = lambda xm, ym: (ym, xm) * ~T0
38 
39 xoff,yoff = xy2rc(2474325,-851715)
40 xoff = int(xoff)
41 yoff = int(yoff)
42 endx,endy = xy2rc(404325,1683285)
43 endx = int(endx)
44 endy = int(endy)
45 
46 xstride = int((endx-xoff)/nXtiles)
47 ystride = int((endy-yoff)/nYtiles)
48 
49 for y in range(0,nYtiles):
50  ytile = y + 1
51  uly = y * ystride + yoff
52 
53  for x in range(0,nXtiles):
54  xtile = x +1
55  ulx = x * xstride + xoff
56  srcwin = [ulx,uly,xstride,ystride]
57  tileFile = sourcefilebase + '_' + str(xtile) + '_' + str(ytile) + '.tif'
58  if args.verbose:
59  print("generating %s" % tileFile)
60  transop = gdal.TranslateOptions(creationOptions=['COMPRESS=LZW'],srcWin=srcwin)
61  gdal.Translate(tileFile,srcDS, options=transop)
62 
63 sys.exit(0)
const char * str
Definition: l1c_msi.cpp:35