<s> '''<STR_LIT>''' <EOL> import sys <EOL> import netCDF4 <EOL> import json <EOL> from bottle import Bottle , request , route , response <EOL> import ocw . utils <EOL> lfme_app = Bottle ( ) <EOL> @ lfme_app . route ( '<STR_LIT>' ) <EOL> def list_latlon ( file_path ) : <EOL> '''<STR_LIT>''' <EOL> in_file = netCDF4 . Dataset ( file_path , mode = '<STR_LIT:r>' ) <EOL> var_names = set ( [ key . encode ( ) . lower ( ) for key in in_file . variables . keys ( ) ] ) <EOL> var_names_list = list ( var_names ) <EOL> lat_name_guesses = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> lon_name_guesses = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> found_lat_name = False <EOL> lat_guesses = list ( var_names & lat_name_guesses ) <EOL> if len ( lat_guesses ) >= <NUM_LIT:1> : <EOL> found_lat_name = True <EOL> lat_name = var_names_list [ var_names_list . index ( lat_guesses [ <NUM_LIT:0> ] ) ] <EOL> lats = in_file . variables [ lat_name ] [ : ] <EOL> lats [ lats > <NUM_LIT> ] = lats [ lats > <NUM_LIT> ] - <NUM_LIT> <EOL> lat_min = float ( lats . min ( ) ) <EOL> lat_max = float ( lats . max ( ) ) <EOL> found_lon_name = False <EOL> lon_guesses = list ( var_names & lon_name_guesses ) <EOL> if len ( lon_guesses ) >= <NUM_LIT:1> : <EOL> found_lon_name = True <EOL> lon_name = var_names_list [ var_names_list . index ( lon_guesses [ <NUM_LIT:0> ] ) ] <EOL> lons = in_file . variables [ lon_name ] [ : ] <EOL> lons [ lons > <NUM_LIT> ] = lons [ lons > <NUM_LIT> ] - <NUM_LIT> <EOL> lon_min = float ( lons . min ( ) ) <EOL> lon_max = float ( lons . max ( ) ) <EOL> in_file . close ( ) <EOL> success = found_lat_name and found_lon_name <EOL> if success : <EOL> values = [ success , lat_name , lon_name , lat_min , lat_max , lon_min , lon_max ] <EOL> value_names = [ '<STR_LIT:success>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> output = dict ( zip ( value_names , values ) ) <EOL> else : <EOL> output = { '<STR_LIT:success>' : success , '<STR_LIT>' : var_names_list } <EOL> if request . query . callback : <EOL> return '<STR_LIT>' % ( request . query . callback , json . dumps ( output ) ) <EOL> return output <EOL> @ lfme_app . route ( '<STR_LIT>' ) <EOL> def list_time ( file_path ) : <EOL> '''<STR_LIT>''' <EOL> in_file = netCDF4 . Dataset ( file_path , mode = '<STR_LIT:r>' ) <EOL> var_names = set ( [ key . encode ( ) . lower ( ) for key in in_file . variables . keys ( ) ] ) <EOL> var_names_list = list ( var_names ) <EOL> time_name_guesses = set ( [ '<STR_LIT:time>' , '<STR_LIT>' , '<STR_LIT:t>' , '<STR_LIT:date>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> time_guesses = list ( var_names & time_name_guesses ) <EOL> if len ( time_guesses ) >= <NUM_LIT:1> : <EOL> time_var_name = time_guesses [ <NUM_LIT:0> ] <EOL> times = ocw . utils . decode_time_values ( in_file , time_var_name ) <EOL> start_time = str ( min ( times ) ) <EOL> end_time = str ( max ( times ) ) <EOL> output = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT>' : time_var_name , <EOL> '<STR_LIT>' : start_time , <EOL> '<STR_LIT>' : end_time <EOL> } <EOL> else : <EOL> output = { '<STR_LIT:success>' : False , '<STR_LIT>' : var_names_list } <EOL> if request . query . callback : <EOL> return '<STR_LIT>' % ( request . query . callback , json . dumps ( output ) ) <EOL> return output <EOL> @ lfme_app . route ( '<STR_LIT>' ) <EOL> def list_vars ( file_path ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> in_file = netCDF4 . Dataset ( file_path , mode = '<STR_LIT:r>' ) <EOL> except RuntimeError : <EOL> output = { '<STR_LIT:success>' : False } <EOL> else : <EOL> output = { '<STR_LIT:success>' : True , '<STR_LIT>' : in_file . variables . keys ( ) } <EOL> in_file . close ( ) <EOL> finally : <EOL> if request . query . callback : <EOL> return "<STR_LIT>" % ( request . query . callback , json . dumps ( output ) ) <EOL> return output <EOL> @ lfme_app . hook ( '<STR_LIT>' ) <EOL> def enable_cors ( ) : <EOL> '''<STR_LIT>''' <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT:*>' </s>
