OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
cyan_conus_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 __version__ = '1.1'
8 
9 # CONUS tile extents - Albers conic projection
10 # tile-index (in meters)
11 # h v ulx/llx uly/lly
12 # 0 0 -2565585 3314805
13 # 32 21 2384415 14805
14 #
15 # for the 4x4 grid, the llx/lly are 2834415 and -285195
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 54 GeoTIFF tiles in a 9x6 grid from an input full\nCONUS 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 CONUS GeoTIFF file to slice and dice')
24 parser.add_argument('--verbose', '-v', action='store_true')
25 
26 args = parser.parse_args()
27 
28 nXtiles = 9
29 nYtiles = 6
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(3314805,-2565585)
40 xoff = int(xoff)
41 yoff = int(yoff)
42 endx,endy = xy2rc(-285195,2834415)
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