def get_compressed(year,month,day,hour): prmsl=twcr.load('prmsl',datetime.datetime(year,month,day,hour), version='2c') prmsl=to_analysis_grid(prmsl.extract(iris.Constraint(member=1))) t2m=twcr.load('air.2m',datetime.datetime(year,month,day,hour), version='2c') t2m=to_analysis_grid(t2m.extract(iris.Constraint(member=1))) u10m=twcr.load('uwnd.10m',datetime.datetime(year,month,day,hour), version='2c') u10m=to_analysis_grid(u10m.extract(iris.Constraint(member=1))) v10m=twcr.load('vwnd.10m',datetime.datetime(year,month,day,hour), version='2c') v10m=to_analysis_grid(v10m.extract(iris.Constraint(member=1))) insol=to_analysis_grid(load_insolation(year,month,day,hour)) # Convert the validation data into tensor format t2m_t = tf.convert_to_tensor(normalise_t2m(t2m.data),numpy.float32) t2m_t = tf.reshape(t2m_t,[79,159,1]) prmsl_t = tf.convert_to_tensor(normalise_prmsl(prmsl.data),numpy.float32) prmsl_t = tf.reshape(prmsl_t,[79,159,1]) u10m_t = tf.convert_to_tensor(normalise_wind(u10m.data),numpy.float32) u10m_t = tf.reshape(u10m_t,[79,159,1]) v10m_t = tf.convert_to_tensor(normalise_wind(v10m.data),numpy.float32) v10m_t = tf.reshape(v10m_t,[79,159,1]) insol_t = tf.convert_to_tensor(normalise_insolation(insol.data),numpy.float32) insol_t = tf.reshape(insol_t,[79,159,1]) ict = tf.concat([t2m_t,prmsl_t,u10m_t,v10m_t,insol_t],2) # Now [79,159,5] ict = tf.reshape(ict,[1,79,159,5]) result = autoencoder.predict_on_batch(ict) result = tf.reshape(result,[79,159,5]) ls = encoder.predict_on_batch(ict) # Convert the encoded fields back to unnormalised cubes t2m_r=t2m.copy() t2m_r.data = tf.reshape(result.numpy()[:,:,0],[79,159]).numpy() t2m_r.data = unnormalise_t2m(t2m_r.data) prmsl_r=prmsl.copy() prmsl_r.data = tf.reshape(result.numpy()[:,:,1],[79,159]).numpy() prmsl_r.data = unnormalise_prmsl(prmsl_r.data) u10m_r=u10m.copy() u10m_r.data = tf.reshape(result.numpy()[:,:,2],[79,159]).numpy() u10m_r.data = unnormalise_wind(u10m_r.data) v10m_r=v10m.copy() v10m_r.data = tf.reshape(result.numpy()[:,:,3],[79,159]).numpy() v10m_r.data = unnormalise_wind(v10m_r.data) return {'t2m':t2m_r,'prmsl':prmsl_r,'u10m':u10m_r,'v10m':v10m_r,'ls':ls}
zorder=200) # Load the validation data prmsl = twcr.load('prmsl', datetime.datetime(2010, 3, 12, 18), version='2c') prmsl = to_analysis_grid(prmsl.extract(iris.Constraint(member=1))) t2m = twcr.load('air.2m', datetime.datetime(2010, 3, 12, 18), version='2c') t2m = to_analysis_grid(t2m.extract(iris.Constraint(member=1))) u10m = twcr.load('uwnd.10m', datetime.datetime(2010, 3, 12, 18), version='2c') u10m = to_analysis_grid(u10m.extract(iris.Constraint(member=1))) v10m = twcr.load('vwnd.10m', datetime.datetime(2010, 3, 12, 18), version='2c') v10m = to_analysis_grid(v10m.extract(iris.Constraint(member=1))) insol = to_analysis_grid(load_insolation(2010, 3, 12, 18)) # Convert the validation data into tensor format t2m_t = tf.convert_to_tensor(normalise_t2m(t2m.data), numpy.float32) t2m_t = tf.reshape(t2m_t, [79, 159, 1]) prmsl_t = tf.convert_to_tensor(normalise_prmsl(prmsl.data), numpy.float32) prmsl_t = tf.reshape(prmsl_t, [79, 159, 1]) u10m_t = tf.convert_to_tensor(normalise_wind(u10m.data), numpy.float32) u10m_t = tf.reshape(u10m_t, [79, 159, 1]) v10m_t = tf.convert_to_tensor(normalise_wind(v10m.data), numpy.float32) v10m_t = tf.reshape(v10m_t, [79, 159, 1]) insol_t = tf.convert_to_tensor(normalise_insolation(insol.data), numpy.float32) insol_t = tf.reshape(insol_t, [79, 159, 1]) # Get autoencoded versions of the validation data model_save_file = ("%s/ML_GCM/autoencoder.tst/" + "Epoch_%04d/autoencoder") % ( os.getenv('SCRATCH'), args.epoch) autoencoder = tf.keras.models.load_model(model_save_file, compile=False) ict = tf.concat([t2m_t, prmsl_t, u10m_t, v10m_t, insol_t], 2) # Now [79,159,5]
if not os.path.isdir(os.path.dirname(args.opfile)): os.makedirs(os.path.dirname(args.opfile)) # Load and standardise data if args.source == '20CR2c': ic = twcr.load( args.variable, (datetime.datetime(args.year, args.month, args.day, args.hour) + datetime.timedelta(hours=6)), version='2c') ic = ic.extract(iris.Constraint(member=args.member)) ic = to_analysis_grid(ic) if args.variable == 'uwnd.10m' or args.variable == 'vwnd.10m': ic.data = normalise_wind(ic.data) elif args.variable == 'air.2m': ic.data = normalise_t2m(ic.data) elif args.variable == 'prmsl': ic.data = normalise_prmsl(ic.data) else: raise ValueError('Variable %s is not supported' % args.variable) else: raise ValueError('Source %s is not supported' % args.source) # Convert to Tensor ict = tf.convert_to_tensor(ic.data, numpy.float32) # Write to file sict = tf.serialize_tensor(ict) tf.write_file(args.opfile, sict)