|
| 1 | +from __future__ import absolute_import |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import re |
| 5 | +import shutil |
| 6 | +from datetime import date |
| 7 | +from dateutil.relativedelta import relativedelta |
| 8 | +from dataqs.processor_base import GeoDataMosaicProcessor, GS_DATA_DIR |
| 9 | +from dataqs.helpers import get_band_count, gdal_translate, \ |
| 10 | + nc_convert, style_exists, cdo_fixlng, gunzip |
| 11 | + |
| 12 | +logger = logging.getLogger("dataqs.processors") |
| 13 | +script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 14 | + |
| 15 | + |
| 16 | +class GISTEMPProcessor(GeoDataMosaicProcessor): |
| 17 | + """ |
| 18 | + Processor for Land-Ocean Temperature Index, ERSSTv4, 1200km smoothing |
| 19 | + from the NASA Goddard Institute for Space Studies' Surface Temperature |
| 20 | + Analysis (GISTEMP). |
| 21 | + More info at http://data.giss.nasa.gov/gistemp/ |
| 22 | + """ |
| 23 | + prefix = "gistemp" |
| 24 | + base_url = "http://data.giss.nasa.gov/pub/gistemp/gistemp1200_ERSSTv4.nc.gz" |
| 25 | + layer_name = 'gistemp1200_ERSSTv4' |
| 26 | + title = 'Global Monthly Air Temperature Anomalies, 1880/01/01 - {}' |
| 27 | + abstract = """The GISTEMP analysis recalculates consistent temperature |
| 28 | +anomaly series from 1880 to the present for a regularly spaced array of virtual |
| 29 | +stations covering the whole globe. Those data are used to investigate regional |
| 30 | +and global patterns and trends. Graphs and tables are updated around the |
| 31 | +middle of every month using current data files from NOAA GHCN v3 (meteorological |
| 32 | + stations), ERSST v4 (ocean areas), and SCAR (Antarctic stations). |
| 33 | +
|
| 34 | + The displayed image is based on the most current month. |
| 35 | +
|
| 36 | +Citations: |
| 37 | + - GISTEMP Team, 2016: GISS Surface Temperature Analysis (GISTEMP). |
| 38 | + NASA Goddard Institute for Space Studies. Dataset accessed monthly |
| 39 | + since 8/2016 at http://data.giss.nasa.gov/gistemp/. |
| 40 | + - Hansen, J., R. Ruedy, M. Sato, and K. Lo, 2010: Global surface |
| 41 | + temperature change, Rev. Geophys., 48, RG4004, doi:10.1029/2010RG000345. |
| 42 | +
|
| 43 | + """ |
| 44 | + |
| 45 | + def convert(self, nc_file): |
| 46 | + nc_transform = nc_convert(nc_file) |
| 47 | + cdo_transform = cdo_fixlng(nc_transform) |
| 48 | + return cdo_transform |
| 49 | + |
| 50 | + def extract_band(self, tif, band, outname): |
| 51 | + outfile = os.path.join(self.tmp_dir, outname) |
| 52 | + gdal_translate(tif, outfile, bands=[band], |
| 53 | + projection='EPSG:4326', |
| 54 | + options=['TILED=YES', 'COMPRESS=LZW']) |
| 55 | + return outfile |
| 56 | + |
| 57 | + def get_date(self, months): |
| 58 | + start_month = date(1880, 1, 1) |
| 59 | + return start_month + relativedelta(months=months-1) |
| 60 | + |
| 61 | + def get_title(self, months): |
| 62 | + end_month = self.get_date(months) |
| 63 | + return self.title.format(end_month.strftime('%Y/%m/%d')) |
| 64 | + |
| 65 | + def run(self): |
| 66 | + """ |
| 67 | + Retrieve and process the latest NetCDF file. |
| 68 | + """ |
| 69 | + gzfile = self.download( |
| 70 | + self.base_url, '{}.nc.gz'.format(self.layer_name)) |
| 71 | + ncfile = gunzip(os.path.join(self.tmp_dir, gzfile)) |
| 72 | + cdf_file = self.convert(ncfile) |
| 73 | + bands = get_band_count(cdf_file) |
| 74 | + img_list = self.get_mosaic_filenames(self.layer_name) |
| 75 | + for band in range(1, bands+1): |
| 76 | + band_date = re.sub('[\-\.]+', '', self.get_date(band).isoformat()) |
| 77 | + img_name = '{}_{}T000000000Z.tif'.format(self.layer_name, band_date) |
| 78 | + if img_name not in img_list: |
| 79 | + band_tif = self.extract_band(cdf_file, band, img_name) |
| 80 | + dst_file = self.data_dir.format(gsd=GS_DATA_DIR, |
| 81 | + ws=self.workspace, |
| 82 | + layer=self.layer_name, |
| 83 | + file=img_name) |
| 84 | + dst_dir = os.path.dirname(dst_file) |
| 85 | + if not os.path.exists(dst_dir): |
| 86 | + os.makedirs(dst_dir) |
| 87 | + if dst_file.endswith('.tif'): |
| 88 | + shutil.move(os.path.join(self.tmp_dir, band_tif), dst_file) |
| 89 | + self.post_geoserver(dst_file, self.layer_name) |
| 90 | + |
| 91 | + if not style_exists(self.layer_name): |
| 92 | + with open(os.path.join(script_dir, |
| 93 | + 'resources/gistemp.sld')) as sld: |
| 94 | + self.set_default_style(self.layer_name, self.layer_name, |
| 95 | + sld.read().format(latest_band=bands)) |
| 96 | + self.update_geonode(self.layer_name, title=self.get_title(bands), |
| 97 | + description=self.abstract, |
| 98 | + store=self.layer_name, |
| 99 | + bounds=('-180.0', '180.0', '-90.0', '90.0', |
| 100 | + 'EPSG:4326')) |
| 101 | + self.truncate_gs_cache(self.layer_name) |
| 102 | + self.cleanup() |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + processor = GISTEMPProcessor() |
| 107 | + processor.run() |
0 commit comments