home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 460600500

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/2547#issuecomment-460600500 https://api.github.com/repos/pydata/xarray/issues/2547 460600500 MDEyOklzc3VlQ29tbWVudDQ2MDYwMDUwMA== 21049064 2019-02-05T11:15:01Z 2019-02-05T11:15:01Z NONE

Sorry for the silence! I got pulled away to another project. Unfortunately I wasn't able to finish completing the task in xarray but I found that the easiest way around the problem was to use a combination of two functions:

```python def change_missing_vals_to_9999f(ds, variable): """ Change the missing values from np.nan to -9999.0f""" arr = ds[variable].values

# set the values to -9999
arr[np.isnan(arr)] = -9999

# reassign the values back to the array
ds[variable] = (ds[variable].dims, arr)

return ds

def change_missing_data_values(filename): """ change the values INSIDE the .nc file to -9999.0f """ assert ( filename.split(".")[-1] == "nc" ), "This function only works with .nc files. Filename: {}".format(filename) print(" Processing {} ").format(filename)

# ONLY OPEN THE DATASET ONCE
ds = xr.open_dataset(filename)
variables = ds.data_vars.keys()

for variable in variables:
    print("** Working on variable {} **".format(variable))
    ds = change_missing_vals_to_9999f(ds, variable)

# ds.map(change_missing_vals_to_9999f, variable)

# rewrite to netcdf file
ds.to_netcdf(filename)
print("** Written variables {} to filename {} **").format(variables, filename)

return

```

and then another function using the NCO command:

def change_nc_FillValue(filename): """ use the NCO command to change the fillvalue metadata in the .nc files""" command = "ncatted -a _FillValue,,m,f,-9999.0 {}".format(filename) os.system(command) print("** _FillValue changed on {} file **".format(filename)) return

RUN HERE: ``` @click.command() @click.argument("filename", type=str) def main(filename): """ Run the two commands a) change the Values INSIDE the .nc file [python, numpy, xarray] b) change the associated METADATA for the .nc file headers [nco] """ change_missing_data_values(filename) change_nc_FillValue(filename)

print("**** PROCESS DONE FOR {} ****").format(filename)

return

```

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  377947810
Powered by Datasette · Queries took 6.495ms · About: xarray-datasette