#NEWLINE# def _downloadVariable( varname , dbname , dts , bbox ) : #NEWLINE# #INDENT# 'Download specific variable from the MERRA Reanalysis dataset.' #NEWLINE# res = 0.5 #NEWLINE# for ts in [ ( dts [ 0 ] + timedelta ( dti ) ) for dti in range ( ( ( dts [ 1 ] - dts [ 0 ] ) . days + 1 ) ) ] : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# runid = _merraRunid ( ts . year ) #NEWLINE# url = 'http://goldsmr4.sci.gsfc.nasa.gov:80/opendap/MERRA2/M2T1NXSLV.5.12.4/{1}/{2:02d}/MERRA2_{0}.tavg1_2d_slv_Nx.{1:04d}{2:02d}{3:02d}.nc4' . format ( runid , ts . year , ts . month , ts . day ) #NEWLINE# ds = netcdf . Dataset ( url ) #NEWLINE# lat = ds . variables [ 'lat' ] [ : ] #NEWLINE# lon = ds . variables [ 'lon' ] [ : ] #NEWLINE# lon [ ( lon > 180 ) ] -= 360.0 #NEWLINE# ( i1 , i2 , j1 , j2 ) = datasets . spatialSubset ( np . sort ( lat ) [ : : ( - 1 ) ] , np . sort ( lon ) , res , bbox ) #NEWLINE# data = np . zeros ( ( ( i2 - i1 ) , ( j2 - j1 ) ) ) #NEWLINE# lati = np . argsort ( lat ) [ : : ( - 1 ) ] [ i1 : i2 ] #NEWLINE# loni = np . argsort ( lon ) [ j1 : j2 ] #NEWLINE# if ( varname == 'tmax' ) : #NEWLINE# #INDENT# hdata = ds . variables [ 'T2M' ] [ : , lati , loni ] #NEWLINE# data = ( np . amax ( hdata , axis = 0 ) - 273.15 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( varname == 'tmin' ) : #NEWLINE# #INDENT# hdata = ds . variables [ 'T2M' ] [ : , lati , loni ] #NEWLINE# data = ( np . amin ( hdata , axis = 0 ) - 273.15 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( varname in [ 'wind' ] ) : #NEWLINE# #INDENT# hdata = np . sqrt ( ( ( ds . variables [ 'U10M' ] [ : , lati , loni ] ** 2 ) + ( ds . variables [ 'V10M' ] [ : , lati , loni ] ** 2 ) ) ) #NEWLINE# data = np . mean ( hdata , axis = 0 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# lat = np . sort ( lat ) [ : : ( - 1 ) ] [ i1 : i2 ] #NEWLINE# lon = np . sort ( lon ) [ j1 : j2 ] #NEWLINE# filename = dbio . writeGeotif ( lat , lon , res , data ) #NEWLINE# dbio . ingest ( varname , filename , ts , '{0}.merra' . format ( varname ) ) #NEWLINE# os . remove ( filename ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# print ( 'Cannot import MERRA dataset for {0}!' . format ( ts . strftime ( '%Y-%m-%d' ) ) )