Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selecting netcdf data files
#1
Hi there,

I'm trying to adapt some code I have been given for running climate simulations to work with Python3.7 on windows 10 using Anaconda (code was written in linux).
 #select netcdf data file
fname_data = 'C:\Users\Jodman\Documents\data\' + model + '\' + exp_id + '\' + variable + '\' + variable + '_day_' + model + '_' + exp_id + '_r1i1p1_20051201-20991230.nc'

#load data file as cube
cube = iris.load(fname_data)
The code is trying to select different climate models and various variables as shown below:
 #define model and experiment of choice
model = 'HadGEM2-CC' #HadGEM2-CC or HadGEM2-ES
exp_id = 'rcp45' #rcp45 or rcp85
variable = 'ua' # ua, tas or zg
if(variable == 'tas'): unit = 'K' 
if(variable == 'ua'): unit = 'm/s' 
if(variable == 'zg'): unit = 'm'
sel_lev = 5000 #Pa #5000 or 1000

I'm getting SyntaxError: unexpected character after line continuation character.

The code in linux looked like this below.
#select netcdf data file
fname_data = '/nfs/jack/earataj/Jody/data/' + model + '/' + exp_id + '/' + variable + '/' + variable + '_day_' + model + '_' + exp_id + '_r1i1p1_20051201-20991230.nc' 
I've tried to adapt the windows script by changing the / to \ but have come to a dead end.

would appreciate any help!
Reply
#2
could you please pose entire, unmodified error traceback
one thing, since you are using python 3.7, you can use f-string,so:
fname_data = 'C:\Users\Jodman\Documents\data\' + model + '\' + exp_id + '\' + variable + '\' + variable + '_day_' + model + '_' + exp_id + '_r1i1p1_20051201-20991230.nc'
can be better written:
fname_data = f'C:/Users/Jodman/Documents/data/{model}/{exp_id}/{variable}/{variable}{_day_}{model}_{exp_id}_r1i1p1_20051201-20991230.nc'
also use / in place of \ this will avoid collision with \t, \r, \n, etc.
Reply
#3
Thanks for the reply!

Used the adapted code and now get this

File "<ipython-input-78-a21ccddadee0>", line 2
    fname_data = f'C:/Users/Jodman/Documents/data/{model}/{exp_id}/{variable}/{variable}{_day_}{model}_{exp_id}_r1i1p1_20051201-20991230.nc
                                                                                                                                           ^
SyntaxError: EOL while scanning string literal

Sorry for the double post, can't find the edit option.

This is actually the new error, missed the mark at the end.

NameError                                 Traceback (most recent call last)
<ipython-input-99-812a32deeb5e> in <module>()
      1 #select netcdf data file
----> 2 fname_data = f'C:/Users/Jodman/Documents/data/{model}/{exp_id}/{variable}/{variable}{_day_}{model}_{exp_id}_r1i1p1_20051201-20991230.nc'
      3 
      4 #load data file as cube
      5 cube = iris.load(fname_data)

NameError: name '_day_' is not defined
Reply
#4
Managed to fix that error now appear to have a memory issue.

#these values can be changed to select a different Europe box
eurlon = iris.Constraint(longitude=lambda l: -10<l<40)  # the band of longitudes we want
eurlat = iris.Constraint(latitude=lambda l: 48<l<75)  # the band of longitudes we want

#average over longitude range selected above
dum = cube_djf[0].extract(eurlon).collapsed('longitude', iris.analysis.MEAN)

#compute weights for latitude area weighted mean
weights = cosine_latitude_weights(dum.extract(eurlat))

#average over latitude range selected above
cube_djf_eur = dum.extract(eurlat).collapsed('latitude', iris.analysis.MEAN, weights = weights)

cube_djf_zm = cube_djf.collapsed('longitude', iris.analysis.MEAN)
MemoryError: Failed to realise the lazy data as there was not enough memory available.
The data shape would have been (8580, 8, 20) with dtype('float32').
 Consider freeing up variables or indexing the data before trying again.
Any ideas, is 8gb of ram just too little for large datasets?
Reply
#5
I run with 32 gig, so not sure. I expect that some large databases could easily go beyond 8 gigabytes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problems with netcdf if I compile Android Blaubart 10 657 Yesterday, 07:05 PM
Last Post: Blaubart
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 3,939 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  script to calculate data in csv-files ledgreve 0 2,451 May-19-2023, 07:24 AM
Last Post: ledgreve
  SQL Alchemy help to extract sql data into csv files mg24 1 3,297 Sep-30-2022, 04:43 PM
Last Post: Larz60+
  Apply textual data cleaning to several CSV files ErcoleL99 0 1,422 Jul-09-2022, 03:01 PM
Last Post: ErcoleL99
  SQLALCHEMY - Not selecting data from table jamesaarr 4 3,301 Nov-02-2021, 03:02 PM
Last Post: Larz60+
  Including data files in a package ChrisOfBristol 4 4,774 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Plotting sum of data files using simple code Laplace12 3 4,469 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Selecting the first occurrence of a duplicate knight2000 8 7,791 May-25-2021, 01:37 AM
Last Post: knight2000
  combining netcdf data sebastiansieloff 1 2,828 Nov-19-2020, 03:59 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020