miniufo / xgrads

Parse and read ctl and associated binary file commonly used by GrADS into xarray
https://xgrads.readthedocs.io/
MIT License
71 stars 27 forks source link

xgrads.core #45

Closed FaeghPazhooh closed 1 year ago

FaeghPazhooh commented 1 year ago

Hello I received this error:"ImportError: cannot import name 'open_CtlDataset' from 'xgrads.core". I used your test files(test10). and I copied this code of https://stackoverflow.com/questions/57095286/reading-a-binary-file-in-python-using-a-control-file-with-multiple-variables.

1

miniufo commented 1 year ago

Maybe you did not install the package. That is also fine. You can:

import sys
sys.path.append('/absolute/path/to/your/xgrads/folder')
from xgrads import open_CtlDataset
FaeghPazhooh commented 1 year ago

Thanks a lot. I omitted .core, and it worked for test files. Now I have a binary file that is 85th percentile of 40 binary files by one variable. I want to open this file and I wrote this ctl file for it, but it does not work or plot it. ctl: dset IVT0-Copy.dat title IVT0 data undef -9.99e33 xdef 101 linear -20 1 ydef 61 linear -10 1 IVT0 0 99 endvars

from xgrads import open_CtlDataset dset = open_CtlDataset('E:/dissertation/Code/code2/ivt/Prog/output/IVT0out/IVT0-Copy.ctl')

print all the info in ctl file

print(dset)

for your ctl content, you can plot any variables (e.g.,

first time and level of T) immediately as

dset['IVT0'][-20,-10,...].plot() 13

miniufo commented 1 year ago

Try this:

dset ^IVT0-Copy.dat
title IVT0 data
undef -9.99e33
xdef 101 linear -20 1
ydef 61 linear -10 1
zdef   1 linear 0 1
tdef   1 linear 01Jan2000 1dy
vars 1
IVT0 0 99 comment
endvars

zdef, tdef, and vars 1 cannot be missing. The rule is that, the ctl and binary data can be opened by GrADS. Sometimes, people write their own (incorrect) ctl may not correctly parsed by GrADS, and hence xgrads.

FaeghPazhooh commented 1 year ago

Thanks a lot for your help! I need a 3d plot for lon, lat and the variable should display in Z. Is it available in xgrads? something like plot_surface on matplotlib. And for comparing I should put data in a variable. Is there any method to use lon, lat and IVT in diferent times(t) in a array on xgrads?

miniufo commented 1 year ago

xgrads is just for loading data into memory, not fully reproduce GrADS' plot functionalities. But you can use matplotlib for plotting, which is most popular in python.

FaeghPazhooh commented 1 year ago

Thanks a million !!!! How I can use data on memory in a variable? May I use open-ctldataset to load data in a 3d variable?

miniufo commented 1 year ago

Of course yes. If your ctl is associated with a 4D dataset (time, lev, lat, lon), you can open it and play with 4D variables.

FaeghPazhooh commented 1 year ago

Many thanks, I opened all of my files by xgrads, and now is there any way to compare these datasets?

I have a threshold in a dataset : dset ^IVT0-Copy.dat title IVT0 data undef -9.99e33 xdef 101 linear -20 1 ydef 61 linear -10 1 zdef 1 linear 0 1 tdef 1 linear 01Jan2000 1dy vars 1 IVT0 0 99 comment endvars

and 40 datasets as like as below: dset E:/dissertation/Code/code2/ivt/Prog/output/IVT0out/IVT_1980.dat title IVT data undef -9.99e33 xdef 101 linear -20 1 ydef 61 linear -10 1 zdef 4 linear 0 1 tdef 852 linear 01Nov1979 6hr vars 1 IVT 0 99 endvars,

I want to compare each tdef with the threshold dataset. Is there any command to compare datasets? Thank you so much in advance, if you can introduce a method in a library to compare these kinds of datasets?

miniufo commented 1 year ago

I don't know what you want to do. What do you mean by compare datasets? Compare tdef with a dataset? What does that mean? If you want to compare time steps with a reference time, you can just do

print(tdef[0] > ds_threshold.tdef[0])
FaeghPazhooh commented 1 year ago

I want compare every tdef with a threshold(it is a separate dataset) and change the variable after comparing. I am writting

From p in range(tdef) If dset1['Ivt'][p]>=dset[Ivt0][0]: dset1['Ivt'][p]==dset1['Ivt'][p] else: dset1['Ivt'][p]==0 p=p+1

but the dset1 dosenot change to new dset that I want after if.

On Fri, May 26, 2023, 12:02 AM Yu-Kun Qian @.***> wrote:

I don't know what you want to do. What do you mean by compare datasets? Compare tdef with a dataset? What does that mean? If you want to compare time steps with a reference time, you can just do

print(tdef[0] > ds_threshold.tdef[0])

— Reply to this email directly, view it on GitHub https://github.com/miniufo/xgrads/issues/45#issuecomment-1563471644, or unsubscribe https://github.com/notifications/unsubscribe-auth/A73ERQKBBGVKBOR7VRPR7KLXH66UJANCNFSM6AAAAAAYBIFY5I . You are receiving this because you authored the thread.Message ID: @.***>

miniufo commented 1 year ago

I don't think it is possible to modify all the times of a dataset as 0, because it is a coordinate that should be monotonic increasing or decreasing. But you can assign the whole tdef with a new series of times that you are happy with.

By the way, == is not a valid assignment, should be =.

FaeghPazhooh commented 1 year ago

"=" is not accepted. I have a dataset. It has just one tdef. I call it IVT0.

There is another dataset,IVT , It has 855 tdefs.

Now, in new step, I want to compare each tdef in IVT to IVT0. Grade by grade in the grid (lon and lot).

for p in range(0,855,1):

if dset1['IVT'][p,...,...].any() >= dset['IVT0'][0].any(): dset1['IVT'][p,...,...].any() == dset['IVT'][p].any();
else: dset1['IVT'][p,...].any()==0
print('dset==0',p) p=p+1

Overall, I want to make a new dataset that every grade value has changed after 'for', and use the new one.

miniufo commented 1 year ago

You can do if, and if true, assign new series of tdefs to the coordinates. You may want to search 'how to modify coordinates in xarray' in google.

FaeghPazhooh commented 1 year ago

At first step, I should load dataset to xarray in python. How I can open my dataset in xarray. I found "dataset to array" but it didnot work.

miniufo commented 1 year ago

After a quick look at previous posts, I guess you want to combine 40 datasets into a single one, right? So you have 40 ctls and want to load them all into memory?

FaeghPazhooh commented 1 year ago

I think I could not explain it well. I have a dataset that I want to use it as a threshold and I loaded it as "dset".

There are 40 datasets, that every one has 855 tdef. I want to compare the value of each dataset in each tdef with the value of dset and make new dataset to apply more condition. I think I should open my file in xarray to compare and change some arraies. How I can load my dataset in xarray?

miniufo commented 1 year ago

When you load your dataset (end with .ctl) as dset, it is already a xarray.Dataset.

FaeghPazhooh commented 1 year ago

So I should import xarray library and in the following I can use its commands for all of my datasets?

miniufo commented 1 year ago

Yes, you should import xarray as xr and call functions in it to help you. xarray is the data struct you play with, while xgrads is an input/output tool.

FaeghPazhooh commented 1 year ago

Thanks a million